forked from patterniha/SNI-Spoofing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
228 lines (201 loc) · 9.04 KB
/
Copy pathmain.py
File metadata and controls
228 lines (201 loc) · 9.04 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
import asyncio
import os
import socket
import sys
import traceback
import threading
import json
# 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
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
with open(config_path, 'r') as f:
config = json.load(f)
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"]
INTERFACE_IPV4 = get_default_interface_ipv4(CONNECT_IP)
DATA_MODE = "tls"
BYPASS_METHOD = "wrong_seq"
##################
fake_injective_connections: dict[tuple, FakeInjectiveConnection] = {}
async def relay_main_loop(sock_1: socket.socket, sock_2: socket.socket, peer_task: asyncio.Task,
first_prefix_data: bytes):
try:
loop = asyncio.get_running_loop()
while True:
try:
data = await loop.sock_recv(sock_1, 65575)
if not data:
raise ValueError("eof")
if first_prefix_data:
data = first_prefix_data + data
first_prefix_data = b""
sent_len = await loop.sock_sendall(sock_2, data)
if sent_len != len(data):
raise ValueError("incomplete send")
except Exception:
sock_1.close()
sock_2.close()
peer_task.cancel()
return
except Exception:
traceback.print_exc()
sys.exit("relay main loop error!")
async def handle(incoming_sock: socket.socket, incoming_remote_addr):
try:
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:
sys.exit("impossible mode!")
outgoing_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
outgoing_sock.setblocking(False)
outgoing_sock.bind((INTERFACE_IPV4, 0))
outgoing_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
outgoing_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 11)
outgoing_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 2)
outgoing_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3)
src_port = outgoing_sock.getsockname()[1]
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 loop.sock_connect(outgoing_sock, (CONNECT_IP, CONNECT_PORT))
except Exception:
fake_injective_conn.monitor = False
del fake_injective_connections[fake_injective_conn.id]
outgoing_sock.close()
incoming_sock.close()
return
# if bypass_method == "wrong_checksum":
# ...
if BYPASS_METHOD == "wrong_seq":
try:
await asyncio.wait_for(fake_injective_conn.t2a_event.wait(), 2)
if fake_injective_conn.t2a_msg == "unexpected_close":
raise ValueError("unexpected close")
if fake_injective_conn.t2a_msg == "fake_data_ack_recv":
pass
else:
sys.exit("impossible t2a msg!")
except Exception:
fake_injective_conn.monitor = False
del fake_injective_connections[fake_injective_conn.id]
outgoing_sock.close()
incoming_sock.close()
return
else:
sys.exit("unknown bypass method!")
fake_injective_conn.monitor = False
del fake_injective_connections[fake_injective_conn.id]
# 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"")) # bytes([version, 0])
await relay_main_loop(incoming_sock, outgoing_sock, oti_task, b"")
except Exception:
traceback.print_exc()
sys.exit("handle should not raise exception")
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))
mother_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
mother_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 11)
mother_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 2)
mother_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3)
mother_sock.listen()
loop = asyncio.get_running_loop()
while True:
incoming_sock, addr = await loop.sock_accept(mother_sock)
incoming_sock.setblocking(False)
incoming_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
incoming_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 11)
incoming_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 2)
incoming_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3)
asyncio.create_task(handle(incoming_sock, addr))
if __name__ == "__main__":
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()
print("هشن شومافر تیامح دینکیم هدافتسا دازآ تنرتنیا هب یسرتسد یارب همانرب نیا زا رگا")
print(
"دراد امش تیامح هب زاین هک مراد رظن رد دازآ تنرتنیا هب ناریا مدرم مامت یسرتسد یارب یدایز یاه همانرب و اه هژورپ")
print("\n")
print("USDT (BEP20): 0x76a768B53Ca77B43086946315f0BDF21156bF424\n")
print("@patterniha")
asyncio.run(main())