This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 610
/
sub2conf.py
executable file
·228 lines (185 loc) · 7.13 KB
/
sub2conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding:utf-8 -*-
# author: Suummmmer
import base64
import json
import pickle
import requests
from config import conf_template as conf
class Sub2Conf(object):
def __init__(self, subs_url=None, conf_url=None):
# 订阅原始数据
self.origin = []
self.subs_url = subs_url
self.conf_url = conf_url
# 解析后配置
try:
with open("/etc/v2rayL/data", "rb") as f:
self.saved_conf = pickle.load(f)
except:
self.saved_conf = {
"local": {},
"subs": {}
}
self.conf = dict(self.saved_conf['local'], **self.saved_conf['subs'])
'''
self.conf结构
{
"地区": "配置"
}
配置为解析得到的内容 + 协议
'''
# if subs_url:
# try:
# ret = requests.get(subs_url)
# if ret.status_code != 200:
# return
# all_subs = base64.b64decode(ret.text + "==").decode().strip().split("\n")
# except Exception as e:
# pass
# for sub in all_subs:
# self.origin.append(sub.split("://"))
# for ori in self.origin:
# if ori[0] == "vmess":
# self.b642conf("vmess", ori[1])
# elif ori[0] == "ss":
# self.b642conf("ss", ori[1])
# if conf_url:
# try:
# op = conf_url.split("://")
# if op[0] == "vmess":
# self.b642conf("vmess", op[1])
# elif op[0] == "ss":
# self.b642conf("ss", op[1])
# except:
# pass
# with open("/etc/v2rayL/data", "wb") as jf:
# pickle.dump(self.conf, jf)
def b642conf(self, prot, tp, b64str):
if prot == "vmess":
ret = eval(base64.b64decode(b64str + "==").decode())
region = ret['ps']
elif prot == "ss":
string = b64str.split("#")
cf = string[0].split("@")
if len(cf) == 1:
tmp = base64.b64decode(cf[0] + "==").decode() # aes-256-cfb:[email protected]:9898
else:
tmp = base64.b64decode(cf[0] + "==").decode()+"@"+cf[1]
ret = {
"method": tmp.split(":")[0],
"port": tmp.split(":")[2],
"password": tmp.split(":")[1].split("@")[0],
"add": tmp.split(":")[1].split("@")[1],
}
region = string[1]
ret["prot"] = prot
self.saved_conf[["local", "subs"][tp]][region] = ret
def setconf(self, region):
use_conf = self.conf[region]
if use_conf['prot'] == "vmess":
conf['outbounds'][0]["protocol"] = "vmess"
conf['outbounds'][0]["settings"]["vnext"] = list()
conf['outbounds'][0]["settings"]["vnext"].append({
"address": use_conf["add"],
"port": int(use_conf["port"]),
"users": [
{
"id": use_conf["id"],
"alterId": use_conf["aid"]
}
]
})
conf['outbounds'][0]["streamSettings"] = {
"network": use_conf["net"],
"wsSettings":{
"path": use_conf["path"],
"headers": {
"Host": use_conf['host']
}
}
}
elif use_conf['prot'] == "ss":
conf['outbounds'][0]["protocol"] = "shadowsocks"
conf['outbounds'][0]["settings"]["servers"] = list()
conf['outbounds'][0]["settings"]["servers"].append({
"address": use_conf["add"],
"port": int(use_conf["port"]),
"password": use_conf["password"],
"ota": False,
"method": use_conf["method"]
})
conf['outbounds'][0]["streamSettings"] = {
"network": "tcp"
}
with open("/etc/v2rayL/config.json", "w") as f:
f.write(json.dumps(conf, indent=4))
def delconf(self, region):
self.conf.pop(region)
try:
self.saved_conf['local'].pop(region)
except:
self.saved_conf['subs'].pop(region)
with open("/etc/v2rayL/data", "wb") as jf:
pickle.dump(self.saved_conf, jf)
def update(self):
"""
更新订阅
"""
try:
ret = requests.get(self.subs_url, headers={'user-agent': 'v2rayL/1.0'})
if ret.status_code != 200:
return
all_subs = base64.b64decode(ret.text + "==").decode().strip().split("\n")
except Exception as e:
raise "wrong"
for sub in all_subs:
self.origin.append(sub.split("://"))
self.saved_conf["subs"] = {}
for ori in self.origin:
if ori[0] == "vmess":
self.b642conf("vmess", 1, ori[1])
elif ori[0] == "ss":
self.b642conf("ss", 1, ori[1])
self.conf = dict(self.saved_conf['local'], **self.saved_conf['subs'])
with open("/etc/v2rayL/data", "wb") as jf:
pickle.dump(self.saved_conf, jf)
def add_conf_by_uri(self):
"""
通过分享的连接添加配置
"""
try:
op = self.conf_url.split("://")
if op[0] == "vmess":
self.b642conf("vmess", 0, op[1])
elif op[0] == "ss":
self.b642conf("ss", 0, op[1])
except:
raise "wrong"
self.conf = dict(self.saved_conf['local'], **self.saved_conf['subs'])
with open("/etc/v2rayL/data", "wb") as jf:
pickle.dump(self.saved_conf, jf)
# def b642conf(prot, b64str):
# if prot == "vmess":
# ret = eval(base64.b64decode(b64str).decode())
# region = ret['ps']
# elif prot == "ss":
# string = b64str.split("#")
# tmp = base64.b64decode(string[0]).decode() # aes-256-cfb:[email protected]:9898
# ret = {
# "method": tmp.split(":")[0],
# "port": tmp.split(":")[2],
# "password": tmp.split(":")[1].split("@")[0],
# "address": tmp.split(":")[1].split("@")[1],
# }
# region = string[1]
# ret["prot"] = prot
# print(ret)
# exit()
# self.conf[region] = ret
if __name__ == '__main__':
# s = Sub2Conf("https://sub.qianglie.xyz/subscribe.php?sid=4594&token=TCDWnwMD0rGg")
# print(s.conf)
# s.setconf("1.0x TW-BGP-A 台湾")
# t = base64.b64decode("ewoidiI6ICIyIiwKInBzIjogIjIzM3YyLmNvbV8xNDIuOTMuNTAuNzgiLAoiYWRkIjogIjE0Mi45My41MC43OCIsCiJwb3J0IjogIjM5Mzk4IiwKImlkIjogIjc1Y2JmYzI0LTZhNjAtNDBmMC05Yjc2LTUyMTlmNTIwYTJlMCIsCiJhaWQiOiAiMjMzIiwKIm5ldCI6ICJrY3AiLAoidHlwZSI6ICJ1dHAiLAoiaG9zdCI6ICIiLAoicGF0aCI6ICIiLAoidGxzIjogIiIKfQo=").decode().strip()
# print(t)
b642conf("ss","YWVzLTI1Ni1jZmI6NTQxNjAzNDY2QDE0Mi45My41MC43ODo5ODk4#ss")