Browse Source

feat: support v2rayN share url

master
Dnomd343 3 years ago
parent
commit
f7cc7c860a
  1. 38
      ProxyDecoder/Shadowsocks.py
  2. 13
      ProxyDecoder/ShadowsocksR.py
  3. 142
      ProxyDecoder/VMess.py
  4. 5
      ProxyDecoder/decoder.py

38
ProxyDecoder/Shadowsocks.py

@ -4,7 +4,7 @@
import re
from ProxyDecoder import baseFunc
def __ssPlainDecode(url: str) -> dict or None:
def __ssPlainDecode(url: str) -> dict:
"""
Shadowsocks原始分享链接解码
@ -13,14 +13,12 @@ def __ssPlainDecode(url: str) -> dict or None:
EXAMPLE:
ss://bf-cfb:test@192.168.100.1:8888#EXAMPLE
"""
try:
content = re.search(r'^ss://([\s\S]+?)(#[\s\S]*)?$', url) # ...#REMARK
info = re.search(
r'^([\S]+?):([\S]+)@([a-zA-Z0-9.:_-]+):([0-9]+)$',
content.group(1) # method:password@server:port
)
remark = content.group(2)[1:] if content.group(2) is not None else ''
remark = remark.replace('+', ' ') # 向后兼容部分客户端
return {
'server': info[3],
'port': int(info[4]),
@ -28,10 +26,8 @@ def __ssPlainDecode(url: str) -> dict or None:
'method': info[1],
'remark': baseFunc.urlDecode(remark)
}
except:
return None
def __ssCommonDecode(url: str) -> dict or None:
def __ssCommonDecode(url: str) -> dict:
"""
Shadowsocks经典分享链接解码
@ -42,7 +38,6 @@ def __ssCommonDecode(url: str) -> dict or None:
-> YmYtY2ZiOnRlc3QvIUAjOkAxOTIuMTY4LjEwMC4xOjg4ODg
-> ss://YmYtY2ZiOnRlc3QvIUAjOkAxOTIuMTY4LjEwMC4xOjg4ODg#example-server
"""
try:
content = re.search(r'^ss://([a-zA-Z0-9_=+\\-]+)#?([\S]*)?$', url) # base64#REMARK
info = re.search(
r'^([\S]+?):([\S]+)@([a-zA-Z0-9.:_-]+):([0-9]+)$',
@ -55,10 +50,8 @@ def __ssCommonDecode(url: str) -> dict or None:
'method': info[1],
'remark': baseFunc.urlDecode(content.group(2))
}
except:
return None
def __sip002Decode(url: str) -> dict or None:
def __sip002Decode(url: str) -> dict:
"""
Shadowsocks SIP002分享链接解码
@ -73,7 +66,6 @@ def __sip002Decode(url: str) -> dict or None:
=> ss://cmM0LW1kNTpwYXNzd2Q@192.168.100.1:8888/?plugin=obfs-local%3Bobfs%3Dhttp#Example
"""
try:
content = re.search(
r'^ss://([a-zA-Z0-9_=+\\-]+)@([a-zA-Z0-9.:_-]+):([0-9]+)'
r'/?([\S]*)$', url # base64@server:port/... (/可选)
@ -121,15 +113,11 @@ def __sip002Decode(url: str) -> dict or None:
'type': plugin.group(1),
'param': plugin.group(2)
}
if pluginField['type'] == '': # 无插件情况
info['plugin'] = None
else:
if pluginField['type'] != '': # 带插件情况
info['plugin'] = pluginField
return info
except:
return None
def ssDecode(url: str) -> dict or None:
def ssDecode(url: str, compatible: bool = False) -> dict or None:
"""
Shadowsocks分享链接解码
@ -142,17 +130,19 @@ def ssDecode(url: str) -> dict or None:
链接不合法:
return None
"""
try:
if url[0:5] != 'ss://':
return None
try:
result = __ssCommonDecode(url) # try shadowsocks common decode
if result is None:
except:
try:
result = __sip002Decode(url) # try shadowsocks sip002 decode
if result is None:
except:
try:
result = __ssPlainDecode(url) # try shadowsocks plain decode
if result is not None: # 解析成功
result['type'] = 'ss'
return result
except:
pass
return None
if compatible and 'remark' in result: # 向后兼容部分客户端
result['remark'] = result['remark'].replace('+', ' ')
result['type'] = 'ss'
return result

13
ProxyDecoder/ShadowsocksR.py

@ -4,7 +4,7 @@
import re
from ProxyDecoder import baseFunc
def __ssrCommonDecode(url: str) -> dict or None:
def __ssrCommonDecode(url: str) -> dict:
"""
ShadowsocksR经典分享链接解码
@ -15,7 +15,6 @@ def __ssrCommonDecode(url: str) -> dict or None:
-> server:port:protocol:method:obfs:base64(passwd)/?...
-> obfsparam=...&protoparam=...&remarks=...&group=...
"""
try:
content = re.search(r'^ssr://([\S]+)$', url).group(1) # ssr://{base64}
content = re.search(
r'^([a-zA-Z0-9.:_-]*):([0-9]*):' # server:p/r
@ -44,8 +43,6 @@ def __ssrCommonDecode(url: str) -> dict or None:
elif field.group(1) == 'group':
info['group'] = baseFunc.base64Decode(field.group(2))
return info
except:
return None
def ssrDecode(url: str) -> dict or None:
"""
@ -60,13 +57,11 @@ def ssrDecode(url: str) -> dict or None:
链接不合法:
return None
"""
try:
if url[0:6] != 'ssr://':
return None
try:
result = __ssrCommonDecode(url) # try common decode
if result is not None: # 解析成功
result['type'] = 'ssr'
return result
except:
pass
return None
result['type'] = 'ssr'
return result

142
ProxyDecoder/VMess.py

@ -0,0 +1,142 @@
#!/usr/bin/python
# -*- coding:utf-8 -*-
import re
import json
from ProxyDecoder import baseFunc
def __splitEdParam(path: str) -> tuple[int or None, str]: # 分离early-data参数
if path.find('?') == -1:
return None, path
content = re.search(r'^([\s\S]*?)\?([\s\S]*)$', path)
ed = None
params = []
for field in content[2].split('&'): # ?param_a=...&param_b=...
if not field.startswith('ed='):
params.append(field)
continue
ed = int(field[3:]) # ed=...
if ed is None: # ed param not found
return None, path
if not params: # param -> []
return ed, content[1]
return ed, content[1] + '?' + '&'.join(params)
def __vmessV2raynDecode(url: str) -> dict:
"""
v2rayN / v2rayNG分享链接解码
FORMAT: vmess://BASE64-ENCODED-JSON-STRING
{
"v": "2",
"ps": "...",
"add": "...",
"port": "...",
"id": "...",
"aid": "...",
"scy": "...",
"net": "...",
"type": "...",
"host": "...",
"path": "...",
"tls": "...",
"sni": "...",
"alpn": "..."
}
"""
content = json.loads(
baseFunc.base64Decode(
re.search(r'^vmess://([\S]+)$', url).group(1) # vmess://{base64}
)
)
if int(content['v']) != 2: # version => 2
raise Exception('Unknown version field')
info = {
'server': content['add'],
'port': int(content['port']),
'id': content['id'],
'aid': int(content['aid']),
}
if 'ps' in content: # ps -> remark
info['remark'] = content['ps']
if 'scy' in content: # scy -> method
info['method'] = content['scy']
stream = {
'type': content['net'] # net -> stream.type
}
if content['net'] == 'tcp':
if 'http' in content and content['type'] == 'http': # type -> none / http
stream['obfs'] = {
'host': content['host'],
'path': content['path']
}
elif content['net'] == 'kcp':
if 'type' in content:
stream['obfs'] = content['type']
if 'path' in content:
stream['seed'] = content['path'] # path -> seed
elif content['net'] == 'ws':
if 'host' in content:
stream['host'] = content['host']
if 'path' in content:
try:
stream['ed'], stream['path'] = __splitEdParam(content['path'])
except:
stream['path'] = content['path']
elif content['net'] == 'h2':
if 'host' in content:
stream['host'] = content['host']
if 'path' in content:
stream['path'] = content['path']
elif content['net'] == 'quic':
if 'type' in content:
stream['obfs'] = content['type']
if 'host' in content:
stream['method'] = content['host']
if 'path' in content:
stream['passwd'] = content['path']
elif content['net'] == 'grpc':
if 'type' in content and content['type'] == 'multi':
stream['mode'] = 'multi'
if 'path' in content:
stream['service'] = content['path']
else:
raise Exception('Unknown network type')
secure = None
if 'tls' in content and content['tls'] == 'tls': # enable TLS
secure = {}
if 'sni' in content:
secure['sni'] = content['sni'] # sni option
if 'alpn' in content:
if content['alpn'] == '':
secure['alpn'] = None # ignore alpn option
else:
secure['alpn'] = content['alpn'] # h2 | http/1.1 | h2,http/1.1
stream['secure'] = secure
info['stream'] = stream
return info
def vmessDecode(url: str) -> dict or None:
"""
VMess分享链接解码
链接合法:
return {
'type': 'vmess',
...
}
链接不合法:
return None
"""
if url[0:8] != 'vmess://':
return None
try:
result = __vmessV2raynDecode(url) # try v2rayN decode
except:
return None
result['type'] = 'vmess'
return result

5
ProxyDecoder/decoder.py

@ -4,6 +4,7 @@
import re
from ProxyDecoder import Shadowsocks
from ProxyDecoder import ShadowsocksR
from ProxyDecoder import VMess
def decode(url: str) -> dict or None:
"""
@ -22,9 +23,11 @@ def decode(url: str) -> dict or None:
try:
scheme = re.search(r'^([\S]+?)://([\s\S]+)$', url).group(1)
if scheme == 'ss':
return Shadowsocks.ssDecode(url)
return Shadowsocks.ssDecode(url, compatible = True)
elif scheme == 'ssr':
return ShadowsocksR.ssrDecode(url)
elif scheme == 'vmess':
return VMess.vmessDecode(url)
except:
pass
return None

Loading…
Cancel
Save