From 7df00bc45f168c3f34a8c92b78193301ef1bfd59 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 3 Jan 2023 16:17:06 +0800 Subject: [PATCH] feat: v2rayN encode framework --- Encoder/VMess.py | 43 +++++++++++++++++++++++++++++++++++++++++++ Encoder/__init__.py | 1 + encoder_test.py | 29 ++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 Encoder/VMess.py diff --git a/Encoder/VMess.py b/Encoder/VMess.py new file mode 100644 index 0000000..de4e858 --- /dev/null +++ b/Encoder/VMess.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from Utils.Common import b64Encode +from Utils.Common import urlEncode + + +def v2rayN(info: dict, name: str) -> str: + """ + https://github.com/2dust/v2rayN/wiki/%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E6%A0%BC%E5%BC%8F%E8%AF%B4%E6%98%8E(ver-2) + + FORMAT: vmess://BASE64-ENCODED-JSON-STRING + fields => v(=2) / ps / add / port / id / aid / scy / net / type / host / path / tls / sni / alpn + """ + # TODO: base64 with `+` and `/` + # TODO: => not urlSafe and with padding + config = { + 'v': '2', + 'ps': urlEncode(name), + 'add': info['server'], + 'port': str(info['port']), + 'id': info['id'], + 'aid': str(info['aid']), + 'scy': info['method'], + } + + stream = info['stream'] + if stream['type'] == 'tcp': + + ... + elif stream['type'] == 'kcp': + ... + elif stream['type'] == 'ws': + ... + elif stream['type'] == 'h2': + ... + elif stream['type'] == 'quic': + ... + elif stream['type'] == 'grpc': + ... + + print(config) + print(info) diff --git a/Encoder/__init__.py b/Encoder/__init__.py index 92950cc..d44b4d4 100644 --- a/Encoder/__init__.py +++ b/Encoder/__init__.py @@ -2,3 +2,4 @@ # -*- coding: utf-8 -*- from Encoder.VLESS import vless +from Encoder.VMess import v2rayN diff --git a/encoder_test.py b/encoder_test.py index 96deacc..8a22d4f 100755 --- a/encoder_test.py +++ b/encoder_test.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import Encoder from Filter import Filter vless_demo_1 = { @@ -26,6 +27,32 @@ vless_demo_1 = { } } +vmess_demo_1 = { + 'type': 'vmess', + 'name': '🇭🇰 DataGrids DMIT VMess 1Gbps', + 'info': { + 'server': '54.142.232.16', + 'port': 52227, + 'method': 'auto', + 'id': '75e6edee-2c30-41b0-9d88-676c4fb19ee8', + 'aid': 0, + 'stream': { + 'type': 'ws', + 'host': 'proxy.demo.com', + 'path': '/vmessws', + 'ed': 2048, + 'secure': { + 'sni': 'proxy.demo.com', + 'alpn': None, + 'verify': True + } + } + } +} + vless_demo_1['info'] = Filter('vless', vless_demo_1['info']) +vmess_demo_1['info'] = Filter('vmess', vmess_demo_1['info']) +# print(vless_demo_1) +# print(vmess_demo_1) -print(vless_demo_1) +Encoder.v2rayN(vmess_demo_1['info'], 'demo')