-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathproxy-bridge.py
More file actions
56 lines (51 loc) · 1.56 KB
/
Copy pathproxy-bridge.py
File metadata and controls
56 lines (51 loc) · 1.56 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
"""
Proxy bridge: binds 0.0.0.0:12080 and forwards to 127.0.0.1:2080 (Clash).
Run on Windows so WSL can reach Clash proxy via {Windows_IP}:12080.
"""
import socket, threading, sys
LOCAL_PORT = 12080
TARGET_HOST = "127.0.0.1"
TARGET_PORT = 2080
def forward(src, dst):
try:
while True:
data = src.recv(65536)
if not data:
break
dst.sendall(data)
except:
pass
finally:
try: src.shutdown(socket.SHUT_RD)
except: pass
try: dst.shutdown(socket.SHUT_WR)
except: pass
def handle(client):
try:
target = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
target.settimeout(30)
target.connect((TARGET_HOST, TARGET_PORT))
t1 = threading.Thread(target=forward, args=(client, target), daemon=True)
t2 = threading.Thread(target=forward, args=(target, client), daemon=True)
t1.start()
t2.start()
t1.join()
t2.join()
except Exception as e:
pass
finally:
try: client.close()
except: pass
try: target.close()
except: pass
def main():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("0.0.0.0", LOCAL_PORT))
server.listen(32)
print(f"[proxy-bridge] 0.0.0.0:{LOCAL_PORT} -> {TARGET_HOST}:{TARGET_PORT}")
while True:
client, _ = server.accept()
threading.Thread(target=handle, args=(client,), daemon=True).start()
if __name__ == "__main__":
main()