Skip to content

Instantly share code, notes, and snippets.

@ety001
Last active April 1, 2023 03:54
Show Gist options
  • Save ety001/9bcd6945c0e3db5a5263890cbb29acef to your computer and use it in GitHub Desktop.
Save ety001/9bcd6945c0e3db5a5263890cbb29acef to your computer and use it in GitHub Desktop.
从机场订阅文件中解析标记为[推荐]的 vmess 节点并启动v2ray
#!/usr/bin/python
# coding: UTF-8
import urllib.request
import base64, json, os, time
import pycurl
sub_url = ''
'''
v: 配置文件版本号,主要用来识别当前配置
ps: 备注或别名
add: 地址IP或域名
port: 端口号
id: UUID
aid: alterId
scy: 加密方式(security),没有时值默认auto
net: 传输协议(tcp\\kcp\\ws\\h2\\quic)
type: 伪装类型(none\\http\\srtp\\utp\\wechat-video) *tcp or kcp or QUIC
host: 伪装的域名
path: path
tls: 底层传输安全(tls)
sni: serverName
'''
print('start download update')
c0 = urllib.request.urlopen(sub_url).read()
c1 = base64.b64decode(c0).decode('utf-8')
c2 = c1.split("\n")
v2 = {
'log': {
'access': '/dev/stdout',
'error': '/dev/stderr',
'loglevel': 'info'
},
'inbounds': [
{
'port': 44433,
'listen': '0.0.0.0',
'protocol': 'http',
'settings': {}
}
],
'dns': {
'servers': [
'https://dns.google/dns-query',
'223.5.5.5'
]
},
'routing': {
'domainStrategy': 'IPOnDemand',
'domainMatcher': 'mph',
'rules': [
{
'type': 'field',
'network': 'udp,tcp',
'outboundTag': 'tmp'
}
],
},
'outbounds': []
}
result = []
print('start process')
for v in c2:
# every config
v2['outbounds'] = []
proto = v[:5]
if proto != 'vmess':
continue
configStr = v[8:]
try:
tmpConfig = base64.b64decode(configStr).decode('utf-8')
tmpData = json.loads(tmpConfig)
except:
continue
ob = {}
ob['protocol'] = proto
ob['settings'] = {}
ob['settings']['vnext'] = []
#ob['streamSettings'] = {}
ob['tag'] = 'tmp'
if tmpData['add'] == '0.0.0.0':
continue
if tmpData['ps'].find('推荐') == -1:
continue
# vnext config params
vnext = {}
for k in tmpData.keys():
if k == 'add':
vnext['address'] = tmpData[k]
if k == 'port':
vnext['port'] = int(tmpData[k])
if k == 'id':
vnext['users'] = []
user = {}
user['id'] = tmpData[k]
user['level'] = 0
if 'aid' in tmpData.keys():
user['alterId'] = int(tmpData['aid'])
if 'scy' in tmpData.keys():
user['security'] = tmpData['scy']
vnext['users'].append(user)
#if k == 'net':
# ob['streamSettings']['network'] = tmpData[k]
# if 'tls' in tmpData.keys():
# ob['streamSettings']['security'] = tmpData['tls']
# else:
# ob['streamSettings']['security'] = 'none'
# #if 'type' in tmpData.keys():
# # if tmpData['type'] == 'none':
# # elif tmpData['type'] == 'http':
# # elif tmpData['type'] == 'utp':
ob['settings']['vnext'].append(vnext)
v2['outbounds'].append(ob)
testv2 = json.dumps(v2)
result.append(testv2)
#print(proto, testv2, tmpData['ps'])
#print()
if len(result) > 0:
with open('/tmp/testv2.json', 'w') as f:
f.write(result[0])
dockerStopCMD = '''docker stop testv2 && docker rm testv2'''
try:
os.system(dockerStopCMD)
except:
print('no container')
dockerStartCMD = '''docker run -itd\
--name testv2 \
-p 44433:44433 \
-v /tmp/testv2.json:/tmp/testv2.json \
v2fly/v2fly-core:v4.45.2 \
v2ray -c /tmp/testv2.json
'''
os.system(dockerStartCMD)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment