-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
358 lines (311 loc) · 7.93 KB
/
settings.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import json
import os
import sys
import yaml
PLATFORM = sys.platform
def get_home_path() -> str:
env_dict = os.environ
if PLATFORM == "win32":
return env_dict['HOMEDRIVE']+(env_dict['HOMEPATH'].replace('\\', '/'))
else:
return env_dict['HOME']
CONFIG_DIR = get_home_path()+'/.config/v_cli/'
CONNECTIONS_DIR = CONFIG_DIR+"connections/"
LAST_CONNECT = CONFIG_DIR+"lastconnect.json"
RULES_YAML = CONFIG_DIR+'rules.yaml'
HISTORY_PATH = CONFIG_DIR+'.history'
TPL = {}
TPL["outbounds"] = """
[
{
"mux": {
"enabled": true
},
"protocol": "",
"sendThrough": "0.0.0.0",
"settings": {
"vnext": [
{
"address": "host.host",
"port": 1234,
"users": [
{
"alterId": 1,
"id": "",
"level": 0,
"security": "auto",
"testsEnabled": "none"
}
]
}
]
},
"streamSettings": {
"network": "ws"
},
"tag": "outBound_PROXY"
},
{
"protocol": "freedom",
"tag": "direct",
"settings": {
"domainStrategy": "UseIP"
}
}
]
"""
# tcpSettings
TPL["http"] = """
{
"header": {
"type": "http",
"request": {
"version": "1.1",
"method": "GET",
"path": [
"/"
],
"headers": {
"Host": [
"www.cloudflare.com",
"www.amazon.com"
],
"User-Agent": [
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"
],
"Accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
],
"Accept-language": [
"zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4"
],
"Accept-Encoding": [
"gzip, deflate, br"
],
"Cache-Control": [
"no-cache"
],
"Pragma": "no-cache"
}
}
}
}
"""
# kcpSettings
TPL["kcp"] = """
{
"mtu": 1350,
"tti": 50,
"uplinkCapacity": 12,
"downlinkCapacity": 100,
"congestion": false,
"readBufferSize": 2,
"writeBufferSize": 2,
"header": {
"type": "wechat-video"
}
}
"""
# wsSettings
TPL["ws"] = """
{
"connectionReuse": true,
"path": "/path",
"headers": {
"Host": "host.host.host"
}
}
"""
# httpSettings
TPL["h2"] = """
{
"host": [
"host.com"
],
"path": "/host"
}
"""
TPL["quic"] = """
{
"security": "none",
"key": "",
"header": {
"type": "none"
}
}
"""
TPL['config'] = """
{
"dns": {
"servers": [
"1.1.1.1",
"8.8.8.8",
"8.8.4.4"
]
},
"inbounds": [],
"outbounds": [],
"log": {
"loglevel": "warning"
},
"routing": {
"domainStrategy": "AsIs",
"rules": [{
"ip": [
"geoip:private"
],
"outboundTag": "outBound_DIRECT",
"type": "field"
},
{
"ip": [
"geoip:cn"
],
"outboundTag": "outBound_DIRECT",
"type": "field"
},
{
"domain": [
"geosite:cn"
],
"outboundTag": "outBound_DIRECT",
"type": "field"
}
]
}
}
"""
TPL['http_in'] = """
{
"listen": "127.0.0.1",
"port": 0,
"protocol": "http",
"settings": {},
"sniffing": {
"enabled": false
},
"tag": "http_IN"
}
"""
TPL['socks_in'] = """
{
"listen": "127.0.0.1",
"port": 1089,
"protocol": "socks",
"settings": {
"auth": "noauth",
"ip": "127.0.0.1",
"udp": true,
"userLevel": 0
},
"sniffing": {
"enabled": false
},
"tag": "socks_IN"
}
"""
nested_dict_base = {
'all': None,
'connect': {},
'current': None,
'delete': {},
'disconnect': None,
'exit': None,
'info': {},
'path': {
'set': None,
'show': None
},
'port': {
'set': None,
'show': None
},
'update': {},
}
def load_TPL(stype):
s = TPL[stype]
return json.loads(s)
def default_config():
if not os.path.exists(CONFIG_DIR):
os.makedirs(CONFIG_DIR)
if os.path.exists(LAST_CONNECT):
return
with open(LAST_CONNECT, 'w', encoding='utf-8') as f:
f.write(json.dumps({'node': '', 'http_port': '8889', 'socks_port': '11223',
'path': '/usr/bin/v2ray'}, ensure_ascii=False, indent=4))
def get_node_str(node_name):
with open(CONFIG_DIR+'connections.json', 'r', encoding='utf-8') as f:
conn = json.load(f)
for item in conn:
if conn[item]['displayName'] == node_name.replace('%20', ' '):
return item
def load_default_config():
if not os.path.exists(LAST_CONNECT):
return
with open(LAST_CONNECT, 'r', encoding='utf-8') as f:
return json.load(f)
def clean_blocks(blocks: list):
for item in blocks.copy():
if item == '':
blocks.remove(item)
return blocks
def trim(s):
import re
if s.startswith(' ') or s.endswith(' '):
return re.sub(r"^(\s+)|(\s+)$", "", s)
return s
def dict_copy(dict_input: dict, dict_output: dict):
for i in dict_input:
dict_output[i] = dict_input[i]
def load_rules():
if not os.path.exists(RULES_YAML):
return
else:
with open(RULES_YAML, 'r', encoding='utf-8') as f:
from update_sub import group_current
rules = yaml.unsafe_load(f)
rules = rules[group_current] if group_current in rules else None
return rules
def check_include(rules_include: dict, vmess: json):
if not rules_include:
return True
relation = rules_include['relation']
name = rules_include['name'] if 'name' in rules_include else []
protocol = rules_include['protocol'] if 'protocol' in rules_include else []
name_flag = True
protocol_flag = True
if protocol != vmess['net']:
protocol_flag = False
# or
if relation == 'or':
name_flag = any([i in vmess['ps'] for i in name])
# and
else:
name_flag = all([i in vmess['ps'] for i in name])
return (name_flag or protocol_flag)
def check_exclude(rules_exclude, vmess):
if not rules_exclude:
return True
relation = rules_exclude['relation']
name = rules_exclude['name'] if 'name' in rules_exclude else []
protocol = rules_exclude['protocol'] if 'protocol' in rules_exclude else []
name_flag = True
protocol_flag = True
if protocol == vmess['net']:
protocol_flag = False
if relation == 'or':
name_flag = not any([i in vmess['ps'] for i in name])
else:
name_flag = not all([i in vmess['ps'] for i in name])
return (name_flag and protocol_flag)
def check_link(vmess: json):
rules = load_rules()
if rules is None:
return True
if rules['mode'] == 'include':
return check_include(rules, vmess)
else:
return check_exclude(rules, vmess)