forked from patterniha/SNI-Spoofing
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
711 lines (623 loc) · 24.3 KB
/
Copy pathmain.py
File metadata and controls
711 lines (623 loc) · 24.3 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import asyncio
import ipaddress
import logging
import os
import socket
import subprocess
import sys
import threading
import json
import ctypes
import time
# from utils.proxy_protocols import parse_vless_protocol
from utils.network_tools import get_default_interface_ipv4
from utils.packet_templates import ClientHelloMaker
from fake_tcp import FakeInjectiveConnection, FakeTcpInjector
LOGGER = logging.getLogger("sni_spoofing")
def _is_windows_admin() -> bool:
if not sys.platform.startswith("win"):
return True
try:
return bool(ctypes.windll.shell32.IsUserAnAdmin())
except Exception:
return False
def _relaunch_as_admin():
params = subprocess.list2cmdline(sys.argv)
ret = ctypes.windll.shell32.ShellExecuteW(
None,
"runas",
sys.executable,
params,
None,
1,
)
if ret <= 32:
raise RuntimeError(f"failed to request admin privileges, code={ret}")
def _ensure_admin_or_exit():
if _is_windows_admin():
return
_relaunch_as_admin()
sys.exit(0)
def _is_valid_hostname(hostname: str) -> bool:
if not hostname or len(hostname) > 253:
return False
if hostname.endswith("."):
hostname = hostname[:-1]
labels = hostname.split(".")
if not labels:
return False
for label in labels:
if not label or len(label) > 63:
return False
if label.startswith("-") or label.endswith("-"):
return False
if not all(ch.isalnum() or ch == "-" for ch in label):
return False
return True
def _load_config(path: str) -> dict:
with open(path, "r", encoding="utf-8") as f:
cfg = json.load(f)
required_keys = [
"LISTEN_HOST",
"LISTEN_PORT",
"FAKE_SNI",
"CONNECT_IP",
"CONNECT_PORT",
]
for key in required_keys:
if key not in cfg:
raise ValueError(f"missing config key: {key}")
if not isinstance(cfg["LISTEN_HOST"], str):
raise ValueError("LISTEN_HOST must be a string")
if not isinstance(cfg["LISTEN_PORT"], int):
raise ValueError("LISTEN_PORT must be an integer")
if not isinstance(cfg["FAKE_SNI"], str):
raise ValueError("FAKE_SNI must be a string")
if not isinstance(cfg["CONNECT_IP"], str):
raise ValueError("CONNECT_IP must be a string")
if not isinstance(cfg["CONNECT_PORT"], int):
raise ValueError("CONNECT_PORT must be an integer")
cfg.setdefault("MAX_CONNECTIONS", 2048)
cfg.setdefault("MAX_CONNECTIONS_PER_IP", 128)
cfg.setdefault("HANDSHAKE_TIMEOUT_SEC", 2.0)
cfg.setdefault("RELAY_IDLE_TIMEOUT_SEC", 120.0)
cfg.setdefault("CONNECT_TIMEOUT_SEC", 4.0)
cfg.setdefault("CONNECT_RETRY_COUNT", 2)
cfg.setdefault("CONNECT_RETRY_DELAY_SEC", 0.25)
cfg.setdefault("RELAY_BUFFER_SIZE", 65536)
cfg.setdefault("SOCKET_SNDBUF", 262144)
cfg.setdefault("SOCKET_RCVBUF", 262144)
cfg.setdefault("ENABLE_TCP_NODELAY", True)
if not isinstance(cfg["MAX_CONNECTIONS"], int):
raise ValueError("MAX_CONNECTIONS must be an integer")
if not isinstance(cfg["MAX_CONNECTIONS_PER_IP"], int):
raise ValueError("MAX_CONNECTIONS_PER_IP must be an integer")
if not isinstance(cfg["HANDSHAKE_TIMEOUT_SEC"], (int, float)):
raise ValueError("HANDSHAKE_TIMEOUT_SEC must be a number")
if not isinstance(cfg["RELAY_IDLE_TIMEOUT_SEC"], (int, float)):
raise ValueError("RELAY_IDLE_TIMEOUT_SEC must be a number")
if not isinstance(cfg["CONNECT_TIMEOUT_SEC"], (int, float)):
raise ValueError("CONNECT_TIMEOUT_SEC must be a number")
if not isinstance(cfg["CONNECT_RETRY_COUNT"], int):
raise ValueError("CONNECT_RETRY_COUNT must be an integer")
if not isinstance(cfg["CONNECT_RETRY_DELAY_SEC"], (int, float)):
raise ValueError("CONNECT_RETRY_DELAY_SEC must be a number")
if not isinstance(cfg["RELAY_BUFFER_SIZE"], int):
raise ValueError("RELAY_BUFFER_SIZE must be an integer")
if not isinstance(cfg["SOCKET_SNDBUF"], int):
raise ValueError("SOCKET_SNDBUF must be an integer")
if not isinstance(cfg["SOCKET_RCVBUF"], int):
raise ValueError("SOCKET_RCVBUF must be an integer")
if not isinstance(cfg["ENABLE_TCP_NODELAY"], bool):
raise ValueError("ENABLE_TCP_NODELAY must be true/false")
if not (1 <= cfg["LISTEN_PORT"] <= 65535):
raise ValueError("LISTEN_PORT must be in range 1..65535")
if not (1 <= cfg["CONNECT_PORT"] <= 65535):
raise ValueError("CONNECT_PORT must be in range 1..65535")
if not cfg["FAKE_SNI"]:
raise ValueError("FAKE_SNI must not be empty")
if not _is_valid_hostname(cfg["FAKE_SNI"]):
raise ValueError("FAKE_SNI must be a valid hostname")
if cfg["MAX_CONNECTIONS"] < 1:
raise ValueError("MAX_CONNECTIONS must be >= 1")
if cfg["MAX_CONNECTIONS_PER_IP"] < 1:
raise ValueError("MAX_CONNECTIONS_PER_IP must be >= 1")
if not (0.5 <= float(cfg["HANDSHAKE_TIMEOUT_SEC"]) <= 60):
raise ValueError("HANDSHAKE_TIMEOUT_SEC must be in range 0.5..60")
if not (1 <= float(cfg["RELAY_IDLE_TIMEOUT_SEC"]) <= 3600):
raise ValueError("RELAY_IDLE_TIMEOUT_SEC must be in range 1..3600")
if not (0.2 <= float(cfg["CONNECT_TIMEOUT_SEC"]) <= 60):
raise ValueError("CONNECT_TIMEOUT_SEC must be in range 0.2..60")
if not (1 <= int(cfg["CONNECT_RETRY_COUNT"]) <= 10):
raise ValueError("CONNECT_RETRY_COUNT must be in range 1..10")
if not (0 <= float(cfg["CONNECT_RETRY_DELAY_SEC"]) <= 10):
raise ValueError("CONNECT_RETRY_DELAY_SEC must be in range 0..10")
if not (1024 <= int(cfg["RELAY_BUFFER_SIZE"]) <= 1048576):
raise ValueError("RELAY_BUFFER_SIZE must be in range 1024..1048576")
if not (8192 <= int(cfg["SOCKET_SNDBUF"]) <= 10485760):
raise ValueError("SOCKET_SNDBUF must be in range 8192..10485760")
if not (8192 <= int(cfg["SOCKET_RCVBUF"]) <= 10485760):
raise ValueError("SOCKET_RCVBUF must be in range 8192..10485760")
try:
ipaddress.ip_address(cfg["CONNECT_IP"])
except ValueError as exc:
raise ValueError("CONNECT_IP must be a valid IP address") from exc
listen_host = cfg["LISTEN_HOST"]
if listen_host not in ("0.0.0.0", "::"):
try:
ipaddress.ip_address(listen_host)
except ValueError:
if not _is_valid_hostname(listen_host):
raise ValueError("LISTEN_HOST must be an IP or hostname")
return cfg
def _apply_keepalive_opts(sock: socket.socket):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
if hasattr(socket, "TCP_KEEPIDLE"):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 11)
if hasattr(socket, "TCP_KEEPINTVL"):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 2)
if hasattr(socket, "TCP_KEEPCNT"):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3)
def _apply_performance_opts(sock: socket.socket):
if ENABLE_TCP_NODELAY:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, SOCKET_SNDBUF)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, SOCKET_RCVBUF)
def _close_quietly(*sockets: socket.socket):
for s in sockets:
try:
s.close()
except Exception:
pass
def _shutdown_write_quietly(sock: socket.socket):
try:
sock.shutdown(socket.SHUT_WR)
except Exception:
pass
def _remove_fake_connection(fake_conn: FakeInjectiveConnection):
fake_conn.monitor = False
fake_injective_connections.pop(fake_conn.id, None)
def _startup_connectivity_probe() -> bool:
probe_timeout = min(max(CONNECT_TIMEOUT_SEC, 0.2), 5.0)
try:
start = time.perf_counter()
with socket.create_connection(
(CONNECT_IP, CONNECT_PORT), timeout=probe_timeout
):
pass
elapsed_ms = (time.perf_counter() - start) * 1000.0
LOGGER.info(
"[startup][ok] outbound probe success target=%s:%s latency=%.1fms iface=%s",
CONNECT_IP,
CONNECT_PORT,
elapsed_ms,
INTERFACE_IPV4,
)
return True
except Exception as exc:
LOGGER.error(
"[startup][fail] outbound probe failed target=%s:%s timeout=%.1fs iface=%s err=%s",
CONNECT_IP,
CONNECT_PORT,
probe_timeout,
INTERFACE_IPV4,
exc,
)
return False
def get_exe_dir():
"""Returns the directory where the .exe (or script) is located."""
if getattr(sys, "frozen", False):
# Running as a PyInstaller EXE
return os.path.dirname(sys.executable)
else:
# Running as a normal Python script
return os.path.dirname(os.path.abspath(__file__))
# Build the path to config.json
config_path = os.path.join(get_exe_dir(), "config.json")
# Load the config
config = _load_config(config_path)
LISTEN_HOST = config["LISTEN_HOST"]
LISTEN_PORT = config["LISTEN_PORT"]
FAKE_SNI = config["FAKE_SNI"].encode()
CONNECT_IP = config["CONNECT_IP"]
CONNECT_PORT = config["CONNECT_PORT"]
MAX_CONNECTIONS = config["MAX_CONNECTIONS"]
MAX_CONNECTIONS_PER_IP = config["MAX_CONNECTIONS_PER_IP"]
HANDSHAKE_TIMEOUT_SEC = float(config["HANDSHAKE_TIMEOUT_SEC"])
RELAY_IDLE_TIMEOUT_SEC = float(config["RELAY_IDLE_TIMEOUT_SEC"])
CONNECT_TIMEOUT_SEC = float(config["CONNECT_TIMEOUT_SEC"])
CONNECT_RETRY_COUNT = int(config["CONNECT_RETRY_COUNT"])
CONNECT_RETRY_DELAY_SEC = float(config["CONNECT_RETRY_DELAY_SEC"])
RELAY_BUFFER_SIZE = int(config["RELAY_BUFFER_SIZE"])
SOCKET_SNDBUF = int(config["SOCKET_SNDBUF"])
SOCKET_RCVBUF = int(config["SOCKET_RCVBUF"])
ENABLE_TCP_NODELAY = bool(config["ENABLE_TCP_NODELAY"])
INTERFACE_IPV4 = get_default_interface_ipv4(CONNECT_IP)
DATA_MODE = "tls"
BYPASS_METHOD = "wrong_seq"
##################
fake_injective_connections: dict[tuple, FakeInjectiveConnection] = {}
active_connections_by_ip: dict[str, int] = {}
active_connections_total = 0
connection_guard_lock = asyncio.Lock()
total_upload_bytes = 0
total_download_bytes = 0
window_upload_bytes = 0
window_download_bytes = 0
def _record_transfer(direction: str, size: int):
global total_upload_bytes
global total_download_bytes
global window_upload_bytes
global window_download_bytes
if size <= 0:
return
if direction == "up":
total_upload_bytes += size
window_upload_bytes += size
else:
total_download_bytes += size
window_download_bytes += size
def _bytes_to_human(value: int) -> str:
if value < 1024:
return f"{value}B"
if value < 1024 * 1024:
return f"{value / 1024.0:.1f}KB"
if value < 1024 * 1024 * 1024:
return f"{value / (1024.0 * 1024.0):.2f}MB"
return f"{value / (1024.0 * 1024.0 * 1024.0):.2f}GB"
async def _speed_reporter():
global window_upload_bytes
global window_download_bytes
while True:
await asyncio.sleep(1.0)
up_window = window_upload_bytes
down_window = window_download_bytes
window_upload_bytes = 0
window_download_bytes = 0
LOGGER.info(
"[speed] up_bps=%d down_bps=%d total_up=%s total_down=%s active=%d",
up_window,
down_window,
_bytes_to_human(total_upload_bytes),
_bytes_to_human(total_download_bytes),
active_connections_total,
)
async def _register_connection(client_ip: str) -> bool:
global active_connections_total
async with connection_guard_lock:
if active_connections_total >= MAX_CONNECTIONS:
return False
current_for_ip = active_connections_by_ip.get(client_ip, 0)
if current_for_ip >= MAX_CONNECTIONS_PER_IP:
return False
active_connections_by_ip[client_ip] = current_for_ip + 1
active_connections_total += 1
return True
async def _unregister_connection(client_ip: str):
global active_connections_total
async with connection_guard_lock:
current_for_ip = active_connections_by_ip.get(client_ip, 0)
if current_for_ip <= 1:
active_connections_by_ip.pop(client_ip, None)
else:
active_connections_by_ip[client_ip] = current_for_ip - 1
if active_connections_total > 0:
active_connections_total -= 1
async def relay_main_loop(
sock_1: socket.socket,
sock_2: socket.socket,
peer_task: asyncio.Task | None,
first_prefix_data: bytes,
direction: str,
):
loop = asyncio.get_running_loop()
while True:
try:
data = await asyncio.wait_for(
loop.sock_recv(sock_1, RELAY_BUFFER_SIZE),
timeout=RELAY_IDLE_TIMEOUT_SEC,
)
if not data:
if peer_task is not None and not peer_task.done():
peer_task.cancel()
_shutdown_write_quietly(sock_2)
_close_quietly(sock_1)
return
if first_prefix_data:
data = first_prefix_data + data
first_prefix_data = b""
_record_transfer(direction, len(data))
await loop.sock_sendall(sock_2, data)
except asyncio.CancelledError:
if peer_task is not None and not peer_task.done():
peer_task.cancel()
_shutdown_write_quietly(sock_2)
_close_quietly(sock_1, sock_2)
raise
except Exception:
if peer_task is not None and not peer_task.done():
peer_task.cancel()
_shutdown_write_quietly(sock_2)
_close_quietly(sock_1, sock_2)
return
async def handle(incoming_sock: socket.socket, incoming_remote_addr):
outgoing_sock = None
fake_injective_conn = None
oti_task: asyncio.Task | None = None
client_ip = incoming_remote_addr[0] if incoming_remote_addr else "unknown"
registered = False
try:
registered = await _register_connection(client_ip)
if not registered:
LOGGER.warning("Connection rejected by limits for client_ip=%s", client_ip)
_close_quietly(incoming_sock)
return
loop = asyncio.get_running_loop()
# try:
# data = await loop.sock_recv(incoming_sock, 65575)
# if not data:
# raise ValueError("eof")
# except Exception:
# incoming_sock.close()
# return
# try:
# version, uuid_bytes, transport_protocol, remote_address_type, remote_address, remote_port, payload_index = parse_vless_protocol(
# data)
# except Exception as e:
# print("No Vless Request!, Connection Closed", repr(e), data)
# incoming_sock.close()
# return
# if transport_protocol != "tcp":
# print("Transport Protocol Error!, Connection Closed", transport_protocol, data)
# incoming_sock.close()
# return
# if remote_address_type == "hostname":
# print("hostname address not implemented yet!", data)
# incoming_sock.close()
# return
# if remote_address_type == "ipv4":
# if not INTERFACE_IPV4:
# print("no interface ipv4!", data)
# incoming_sock.close()
# return
# family = socket.AF_INET
# src_ip = INTERFACE_IPV4
#
# elif remote_address_type == "ipv6":
# if not INTERFACE_IPV6:
# print("no interface ipv6!", data)
# incoming_sock.close()
# return
# family = socket.AF_INET6
# src_ip = INTERFACE_IPV6
#
# else:
# print(data)
# sys.exit("impossible address type!")
# try:
# fake_sni_host, data_mode, bypass_method = UUID_FAKE_MAP[uuid_bytes]
# except KeyError:
# print("unmatched uuid", uuid_bytes)
# incoming_sock.close()
# return
# if data_mode == "http":
# ...
if DATA_MODE == "tls":
fake_data = ClientHelloMaker.get_client_hello_with(
os.urandom(32), os.urandom(32), FAKE_SNI, os.urandom(32)
)
else:
LOGGER.error("Unsupported data mode: %s", DATA_MODE)
_close_quietly(incoming_sock)
return
connected = False
for attempt in range(1, CONNECT_RETRY_COUNT + 1):
outgoing_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
outgoing_sock.setblocking(False)
outgoing_sock.bind((INTERFACE_IPV4, 0))
_apply_performance_opts(outgoing_sock)
_apply_keepalive_opts(outgoing_sock)
src_port = outgoing_sock.getsockname()[1]
if src_port == 0:
LOGGER.error("Failed to allocate local source port")
_close_quietly(outgoing_sock, incoming_sock)
return
fake_injective_conn = FakeInjectiveConnection(
outgoing_sock,
INTERFACE_IPV4,
CONNECT_IP,
src_port,
CONNECT_PORT,
fake_data,
BYPASS_METHOD,
incoming_sock,
)
fake_injective_connections[fake_injective_conn.id] = fake_injective_conn
try:
await asyncio.wait_for(
loop.sock_connect(outgoing_sock, (CONNECT_IP, CONNECT_PORT)),
timeout=CONNECT_TIMEOUT_SEC,
)
except Exception as exc:
LOGGER.warning(
"connect attempt %s/%s failed to %s:%s err=%s",
attempt,
CONNECT_RETRY_COUNT,
CONNECT_IP,
CONNECT_PORT,
exc,
)
_remove_fake_connection(fake_injective_conn)
_close_quietly(outgoing_sock)
outgoing_sock = None
fake_injective_conn = None
if attempt < CONNECT_RETRY_COUNT and CONNECT_RETRY_DELAY_SEC > 0:
await asyncio.sleep(CONNECT_RETRY_DELAY_SEC)
else:
connected = True
break
if not connected:
if fake_injective_conn is not None:
_remove_fake_connection(fake_injective_conn)
_close_quietly(outgoing_sock, incoming_sock)
return
# if bypass_method == "wrong_checksum":
# ...
if BYPASS_METHOD == "wrong_seq":
try:
await asyncio.wait_for(
fake_injective_conn.t2a_event.wait(), HANDSHAKE_TIMEOUT_SEC
)
if fake_injective_conn.t2a_msg == "unexpected_close":
raise ValueError("unexpected close")
if fake_injective_conn.t2a_msg == "fake_data_ack_recv":
pass
else:
raise ValueError(
f"unexpected t2a message: {fake_injective_conn.t2a_msg}"
)
except Exception:
_remove_fake_connection(fake_injective_conn)
_close_quietly(outgoing_sock, incoming_sock)
return
else:
LOGGER.error("Unknown bypass method: %s", BYPASS_METHOD)
_remove_fake_connection(fake_injective_conn)
_close_quietly(outgoing_sock, incoming_sock)
return
_remove_fake_connection(fake_injective_conn)
# early_data = data[payload_index:]
# if early_data:
# try:
# sent_len = await loop.sock_sendall(outgoing_sock, early_data)
# if sent_len != len(early_data):
# raise ValueError("incomplete send")
# except Exception:
# outgoing_sock.close()
# incoming_sock.close()
# return
oti_task = asyncio.create_task(
relay_main_loop(
outgoing_sock,
incoming_sock,
asyncio.current_task(),
b"",
"down",
)
) # bytes([version, 0])
await relay_main_loop(incoming_sock, outgoing_sock, oti_task, b"", "up")
await oti_task
except asyncio.CancelledError:
if oti_task is not None and not oti_task.done():
oti_task.cancel()
await asyncio.gather(oti_task, return_exceptions=True)
raise
except Exception:
LOGGER.exception("connection handler crashed")
if fake_injective_conn is not None:
_remove_fake_connection(fake_injective_conn)
_close_quietly(outgoing_sock, incoming_sock)
finally:
if oti_task is not None and not oti_task.done():
oti_task.cancel()
await asyncio.gather(oti_task, return_exceptions=True)
if registered:
await _unregister_connection(client_ip)
async def main():
mother_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mother_sock.setblocking(False)
mother_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mother_sock.bind((LISTEN_HOST, LISTEN_PORT))
_apply_keepalive_opts(mother_sock)
mother_sock.listen()
loop = asyncio.get_running_loop()
handler_tasks: set[asyncio.Task] = set()
speed_task = asyncio.create_task(_speed_reporter())
try:
while True:
try:
incoming_sock, addr = await loop.sock_accept(mother_sock)
except OSError as exc:
if getattr(exc, "winerror", None) == 10038:
LOGGER.warning("Suppressed Windows socket shutdown race: %s", exc)
break
raise
incoming_sock.setblocking(False)
_apply_performance_opts(incoming_sock)
_apply_keepalive_opts(incoming_sock)
task = asyncio.create_task(handle(incoming_sock, addr))
handler_tasks.add(task)
task.add_done_callback(handler_tasks.discard)
finally:
speed_task.cancel()
await asyncio.gather(speed_task, return_exceptions=True)
_close_quietly(mother_sock)
if handler_tasks:
for task in list(handler_tasks):
task.cancel()
await asyncio.gather(*list(handler_tasks), return_exceptions=True)
def run_core():
try:
_ensure_admin_or_exit()
except Exception as exc:
sys.exit(f"Failed to elevate privileges: {exc}")
if sys.platform.startswith("win"):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
logging.basicConfig(
level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s - %(message)s"
)
LOGGER.info(
"[startup] SMART FOX listen=%s:%s target=%s:%s fake_sni=%s",
LISTEN_HOST,
LISTEN_PORT,
CONNECT_IP,
CONNECT_PORT,
FAKE_SNI.decode(errors="ignore"),
)
if LISTEN_HOST == "0.0.0.0":
LOGGER.warning("LISTEN_HOST is 0.0.0.0; service is reachable from the network.")
if not INTERFACE_IPV4:
sys.exit("No IPv4 interface available for CONNECT_IP route")
w_filter = (
"tcp and "
+ "("
+ "(ip.SrcAddr == "
+ INTERFACE_IPV4
+ " and ip.DstAddr == "
+ CONNECT_IP
+ ")"
+ " or "
+ "(ip.SrcAddr == "
+ CONNECT_IP
+ " and ip.DstAddr == "
+ INTERFACE_IPV4
+ ")"
+ ")"
)
fake_tcp_injector = FakeTcpInjector(w_filter, fake_injective_connections)
threading.Thread(target=fake_tcp_injector.run, args=(), daemon=True).start()
_startup_connectivity_probe()
print("SMART FOX")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(main())
except OSError as exc:
if getattr(exc, "winerror", None) == 10038:
LOGGER.warning("Suppressed Windows socket shutdown race: %s", exc)
else:
raise
finally:
pending = asyncio.all_tasks(loop)
for task in pending:
task.cancel()
if pending:
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
loop.run_until_complete(loop.shutdown_asyncgens())
try:
loop.run_until_complete(loop.shutdown_default_executor())
except Exception:
pass
loop.close()
if __name__ == "__main__":
run_core()