From 0ec235cdfffac8ac31862550d84cca5a7159055c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 3 Jan 2023 17:09:23 +0800 Subject: [PATCH] update: v2rayN encoder --- Encoder/VMess.py | 68 +++++++++++++++++++++++++++++++++++++++++------- encoder_test.py | 2 +- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Encoder/VMess.py b/Encoder/VMess.py index de4e858..940249a 100644 --- a/Encoder/VMess.py +++ b/Encoder/VMess.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import json from Utils.Common import b64Encode from Utils.Common import urlEncode @@ -25,19 +26,68 @@ def v2rayN(info: dict, name: str) -> str: } stream = info['stream'] - if stream['type'] == 'tcp': + config['net'] = stream['type'] - ... + if stream['type'] == 'tcp': + if stream['obfs'] is None: + config.update({ + 'type': 'none', + 'host': '', + 'path': '', + }) + else: + config.update({ + 'type': 'http', + 'host': stream['obfs']['host'], + 'path': stream['obfs']['path'], + }) elif stream['type'] == 'kcp': - ... + config.update({ + 'type': stream['obfs'], + 'host': '', + 'path': '' if stream['seed'] is None else stream['seed'], + }) elif stream['type'] == 'ws': - ... + config.update({ + 'type': 'none', + 'host': stream['host'], + 'path': stream['path'], # TODO: add `ed` field + }) elif stream['type'] == 'h2': - ... + config.update({ + 'type': 'none', + 'host': stream['host'], + 'path': stream['path'], + }) elif stream['type'] == 'quic': - ... + config.update({ + 'type': stream['obfs'], + 'host': stream['method'], + 'path': stream['passwd'], + }) elif stream['type'] == 'grpc': - ... + config.update({ + 'type': stream['mode'], + 'host': '', + 'path': stream['service'], + }) + + secure = stream['secure'] + if secure is None: # without TLS secure layer + config.update({ + 'tls': '', + 'sni': '', + 'alpn': '', + }) + else: + config.update({ + 'tls': 'tls', + 'sni': secure['sni'], + 'alpn': '' if secure['alpn'] is None else secure['alpn'], + }) - print(config) - print(info) + return 'vmess://%s' % b64Encode( + json.dumps(config, indent = 2).replace('\n', '\r\n'), + urlSafe = False, + padding = True, + ) diff --git a/encoder_test.py b/encoder_test.py index 8a22d4f..adb0150 100755 --- a/encoder_test.py +++ b/encoder_test.py @@ -55,4 +55,4 @@ vmess_demo_1['info'] = Filter('vmess', vmess_demo_1['info']) # print(vless_demo_1) # print(vmess_demo_1) -Encoder.v2rayN(vmess_demo_1['info'], 'demo') +print(Encoder.v2rayN(vmess_demo_1['info'], 'demo'))