-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfilter_ru_sni.py
More file actions
482 lines (405 loc) · 13 KB
/
filter_ru_sni.py
File metadata and controls
482 lines (405 loc) · 13 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/env python3
"""
filter_ru_sni.py — фильтр конфигов по реальному SNI/host.
Парсит URI каждого протокола, извлекает именно SNI/host параметр,
а не ищет подстроку по всей строке.
Поддерживает: vless://, vmess://, trojan://, ss://, hysteria2://, hy2://
"""
import os
import sys
import json
import base64
import re
from urllib.parse import urlparse, parse_qs, unquote
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
CLEAN_DIR = os.path.join(BASE_PATH, "githubmirror", "clean")
OUT_DIR = os.path.join(BASE_PATH, "githubmirror", "ru-sni")
# ─── Белый список доменов (SNI), которые считаем подходящими ───
# Включает RU-домены + популярные глобальные, используемые в Reality/TLS
GOOD_SNI_DOMAINS = [
# --- RU соцсети ---
"vk.com",
"ok.ru",
"vk.ru",
"mail.ru",
# --- RU поисковики/почта ---
"yandex.ru",
"ya.ru",
"yastatic.net",
"bk.ru",
"inbox.ru",
"list.ru",
# --- RU банки ---
"sberbank.ru",
"online.sberbank.ru",
"vtb.ru",
"tinkoff.ru",
"gazprombank.ru",
"alfabank.ru",
# --- RU маркетплейсы/сервисы ---
"ozon.ru",
"wildberries.ru",
"avito.ru",
"hh.ru",
"gosuslugi.ru",
"mos.ru",
"rt.ru",
"megafon.ru",
"mts.ru",
"beeline.ru",
"tele2.ru",
"rostelecom.ru",
"dns-shop.ru",
"citilink.ru",
"lamoda.ru",
"drom.ru",
"ivi.ru",
"kinopoisk.ru",
# --- Глобальные домены (часто в Reality/TLS) ---
"www.google.com",
"google.com",
"www.googleapis.com",
"dl.google.com",
"play.google.com",
"fonts.googleapis.com",
"www.gstatic.com",
"www.microsoft.com",
"microsoft.com",
"update.microsoft.com",
"www.apple.com",
"apple.com",
"cdn.apple.com",
"www.cloudflare.com",
"cloudflare.com",
"one.one.one.one",
"cdn.cloudflare.com",
"www.amazon.com",
"amazon.com",
"aws.amazon.com",
"www.mozilla.org",
"mozilla.org",
"addons.mozilla.org",
"www.yahoo.com",
"yahoo.com",
"www.wikipedia.org",
"wikipedia.org",
"www.reddit.com",
"reddit.com",
"www.github.com",
"github.com",
"www.docker.com",
"docker.com",
"hub.docker.com",
"registry-1.docker.io",
"www.samsung.com",
"samsung.com",
"www.sony.com",
"www.nvidia.com",
"nvidia.com",
"www.speedtest.net",
"speedtest.net",
"www.whatsapp.com",
"whatsapp.com",
"web.whatsapp.com",
"www.instagram.com",
"instagram.com",
"www.facebook.com",
"facebook.com",
"www.twitter.com",
"twitter.com",
"x.com",
"www.linkedin.com",
"linkedin.com",
"www.spotify.com",
"spotify.com",
"www.discord.com",
"discord.com",
"gateway.discord.gg",
"cdn.discordapp.com",
"www.twitch.tv",
"twitch.tv",
"www.tiktok.com",
"tiktok.com",
"www.steam.com",
"store.steampowered.com",
"steamcommunity.com",
"www.epic.com",
"www.ea.com",
"www.paypal.com",
"paypal.com",
"www.visa.com",
"www.mastercard.com",
"www.netflix.com",
"netflix.com",
"www.youtube.com",
"youtube.com",
"yt3.ggpht.com",
"www.bing.com",
"bing.com",
"www.office.com",
"outlook.office365.com",
"login.microsoftonline.com",
"www.icloud.com",
"icloud.com",
"swdist.apple.com",
"xp.apple.com",
"www.tesla.com",
"tesla.com",
"www.oracle.com",
"www.ibm.com",
"www.intel.com",
"www.amd.com",
"www.hp.com",
"www.dell.com",
"www.lenovo.com",
"www.asus.com",
"www.cisco.com",
"www.vmware.com",
"www.adobe.com",
"adobe.com",
"creativecloud.adobe.com",
"www.zoom.us",
"zoom.us",
"www.slack.com",
"slack.com",
]
# Чёрный список — домены, которые точно НЕ подходят как SNI
BLACKLIST_DOMAINS = [
"localhost",
"example.com",
"example.org",
"test.com",
"invalid",
"local",
]
# ─── Построение множеств для быстрого поиска ───
_GOOD_SET = set(d.lower() for d in GOOD_SNI_DOMAINS)
_BLACK_SET = set(d.lower() for d in BLACKLIST_DOMAINS)
def _is_ip_address(s: str) -> bool:
"""Проверяет, является ли строка IP-адресом (v4 или v6)."""
# IPv4
if re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', s):
return True
# IPv6 (упрощённая проверка)
if ':' in s and re.match(r'^[0-9a-fA-F:]+$', s.replace('[', '').replace(']', '')):
return True
return False
def _domain_matches(sni: str) -> bool:
"""
Проверяет, совпадает ли SNI с одним из доменов белого списка.
Поддерживает точное совпадение и совпадение поддоменов.
Например: 'cdn.vk.com' совпадёт с 'vk.com'.
"""
sni = sni.lower().strip().rstrip('.')
if not sni:
return False
if sni in _BLACK_SET:
return False
if _is_ip_address(sni):
return False
# Точное совпадение
if sni in _GOOD_SET:
return True
# Совпадение поддомена: cdn.vk.com → vk.com
for good in _GOOD_SET:
if sni.endswith('.' + good):
return True
return False
def _strip_remark(uri: str) -> tuple[str, str]:
"""Отделяет #remark от URI. Возвращает (uri_без_remark, remark)."""
idx = uri.rfind('#')
if idx == -1:
return uri, ''
return uri[:idx], unquote(uri[idx + 1:])
def _extract_sni_vless_trojan(uri: str) -> str | None:
"""
Извлекает SNI из vless:// или trojan:// URI.
Приоритет: sni= > serverName= > host= > hostname из URI.
"""
try:
clean_uri, _ = _strip_remark(uri)
# Парсим query параметры
qmark = clean_uri.find('?')
if qmark != -1:
query_str = clean_uri[qmark + 1:]
params = parse_qs(query_str, keep_blank_values=True)
# Приоритет параметров
for key in ('sni', 'serverName', 'server_name', 'host'):
values = params.get(key, [])
if values and values[0]:
return values[0].strip()
# Fallback: hostname из URI
# vless://uuid@hostname:port?...
at_idx = clean_uri.find('@')
if at_idx != -1:
host_part = clean_uri[at_idx + 1:]
# Убираем query
qmark2 = host_part.find('?')
if qmark2 != -1:
host_part = host_part[:qmark2]
# Убираем порт
colon = host_part.rfind(':')
if colon != -1:
hostname = host_part[:colon]
else:
hostname = host_part
hostname = hostname.strip().strip('[]')
if hostname and not _is_ip_address(hostname):
return hostname
except Exception:
pass
return None
def _extract_sni_vmess(uri: str) -> str | None:
"""
Извлекает SNI из vmess:// URI.
VMess использует base64-encoded JSON.
Приоритет: sni > host > add.
"""
try:
clean_uri, _ = _strip_remark(uri)
# vmess://base64data
b64_part = clean_uri[len('vmess://'):]
# Добавляем padding если нужно
padding = 4 - len(b64_part) % 4
if padding != 4:
b64_part += '=' * padding
decoded = base64.b64decode(b64_part).decode('utf-8', errors='ignore')
data = json.loads(decoded)
# Приоритет полей
for key in ('sni', 'host', 'add'):
val = data.get(key, '')
if val and isinstance(val, str):
val = val.strip()
if val and not _is_ip_address(val):
return val
except Exception:
pass
return None
def _extract_sni_ss(uri: str) -> str | None:
"""
Извлекает SNI из ss:// URI.
SS может иметь plugin с obfs-host или SNI в параметрах.
"""
try:
clean_uri, _ = _strip_remark(uri)
qmark = clean_uri.find('?')
if qmark != -1:
query_str = clean_uri[qmark + 1:]
params = parse_qs(query_str, keep_blank_values=True)
# Проверяем plugin параметры
for key in ('sni', 'host', 'obfs-host', 'server_name'):
values = params.get(key, [])
if values and values[0]:
return values[0].strip()
# Проверяем внутри plugin=
plugin_vals = params.get('plugin', [])
if plugin_vals:
plugin_str = plugin_vals[0]
# plugin=obfs-local;obfs=http;obfs-host=example.com
for part in plugin_str.split(';'):
if '=' in part:
k, v = part.split('=', 1)
k = k.strip()
v = v.strip()
if k in ('obfs-host', 'host', 'sni', 'server_name') and v:
return v
except Exception:
pass
return None
def _extract_sni_hysteria(uri: str) -> str | None:
"""
Извлекает SNI из hysteria2:// или hy2:// URI.
"""
try:
clean_uri, _ = _strip_remark(uri)
qmark = clean_uri.find('?')
if qmark != -1:
query_str = clean_uri[qmark + 1:]
params = parse_qs(query_str, keep_blank_values=True)
for key in ('sni', 'server_name', 'host'):
values = params.get(key, [])
if values and values[0]:
return values[0].strip()
# Fallback: hostname
scheme_end = clean_uri.find('://')
if scheme_end != -1:
rest = clean_uri[scheme_end + 3:]
at_idx = rest.find('@')
if at_idx != -1:
host_part = rest[at_idx + 1:]
else:
host_part = rest
qmark2 = host_part.find('?')
if qmark2 != -1:
host_part = host_part[:qmark2]
colon = host_part.rfind(':')
if colon != -1:
hostname = host_part[:colon]
else:
hostname = host_part
hostname = hostname.strip().strip('[]')
if hostname and not _is_ip_address(hostname):
return hostname
except Exception:
pass
return None
def extract_sni(line: str) -> str | None:
"""
Главная функция: определяет протокол и извлекает реальный SNI/host.
Возвращает домен или None.
"""
stripped = line.strip()
lower = stripped.lower()
if lower.startswith('vless://') or lower.startswith('trojan://'):
return _extract_sni_vless_trojan(stripped)
elif lower.startswith('vmess://'):
return _extract_sni_vmess(stripped)
elif lower.startswith('ss://'):
return _extract_sni_ss(stripped)
elif lower.startswith('hysteria2://') or lower.startswith('hy2://'):
return _extract_sni_hysteria(stripped)
return None
def is_good_config(line: str) -> bool:
"""
Проверяет, подходит ли конфиг:
1. Извлекает реальный SNI
2. Проверяет по белому списку доменов
"""
sni = extract_sni(line)
if sni is None:
return False
return _domain_matches(sni)
def main():
os.makedirs(OUT_DIR, exist_ok=True)
total_in = 0
total_out = 0
for fname in sorted(os.listdir(CLEAN_DIR)):
if not fname.endswith(".txt"):
continue
src_path = os.path.join(CLEAN_DIR, fname)
dst_path = os.path.join(OUT_DIR, fname)
good_configs = []
file_total = 0
with open(src_path, "r", encoding="utf-8", errors="ignore") as f:
for line in f:
line = line.strip()
if not line:
continue
file_total += 1
if is_good_config(line):
good_configs.append(line)
with open(dst_path, "w", encoding="utf-8") as f:
f.write("\n".join(good_configs))
if good_configs:
f.write("\n")
total_in += file_total
total_out += len(good_configs)
print(f" {fname}: {file_total} → {len(good_configs)} (SNI-filtered)")
print(f"\n{'='*50}")
print(f" Всего входных: {total_in}")
print(f" Прошли фильтр: {total_out}")
print(f" Отброшено: {total_in - total_out}")
print(f"{'='*50}")
print(f"Готово → {OUT_DIR}")
if __name__ == "__main__":
main()