/
run.py
executable file
·161 lines (136 loc) · 4.57 KB
/
run.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
#!/usr/bin/python3.9
import socketserver, socket
import sys
import time
import _thread, threading
from sdt import DEBUG_PRINT, INFO_PRINT
listen_address, listen_port, remote_address, remote_port = sys.argv[1], \
int(sys.argv[2]), sys.argv[3], int(sys.argv[4])
threading.stack_size(4096 * 8)
EMPTY_BYTES = bytes()
BUFFER_SIZE = 4096 * 8
try:
max_active_connections = int(sys.argv[5])
except IndexError:
max_active_connections = 25
try:
activity_timeout_seconds = int(sys.argv[6])
except IndexError:
activity_timeout_seconds = 5
active_connections = 0
class SimpleQueue:
def __init__(self):
self.queue = []
def add(self, thread):
self.queue.append(thread)
def pop(self):
return self.queue.pop(0)
def __len__(self):
return len(self.queue)
connection_queue = SimpleQueue()
def queue_manager(queue):
global active_connections, max_active_connections, connection_queue
global remote_address, remote_port
count = 0
while True:
if active_connections <= max_active_connections:
try:
connection = connection_queue.pop()
new_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Loop that can wait for proxied server to come up
while True:
try:
new_socket.connect((remote_address, remote_port))
break
except ConnectionRefusedError:
time.sleep(0.1)
connection._proxy_socket = new_socket
connection._activated = True
except IndexError:
pass
count += 1
if not count % 20:
DEBUG_PRINT("Active connections, waiting connections",
active_connections, len(connection_queue))
time.sleep(0.1)
_thread.start_new_thread(queue_manager, (connection_queue,))
class SimpleHandler(socketserver.StreamRequestHandler):
def setup(self):
super().setup()
self._activated = False
def handle(self):
DEBUG_PRINT("Start of handle")
global EMPTY_BYTES, BUFFER_SIZE
if not self._activated:
global connection_queue
connection_queue.add(self)
while True:
if not self._activated:
time.sleep(0.1)
continue
else:
break
global active_connections
active_connections += 1
count = 0
quit = 0
user_s = self.request
proxy_s = self._proxy_socket
to_user = to_proxy = EMPTY_BYTES
last_activity = time.time()
while True:
fall_through = False
try:
to_proxy = user_s.recv(BUFFER_SIZE, socket.MSG_DONTWAIT)
except ConnectionResetError:
quit = 1
except BlockingIOError:
pass
if to_proxy:
fall_through = True
last_activity = time.time()
DEBUG_PRINT("Sending to proxy")
INFO_PRINT(to_proxy)
proxy_s.send(to_proxy)
to_proxy = EMPTY_BYTES
try:
to_user = proxy_s.recv(BUFFER_SIZE, socket.MSG_DONTWAIT)
except ConnectionResetError:
quit = 1
except BlockingIOError:
pass
if to_user:
fall_through = True
last_activity = time.time()
DEBUG_PRINT("Sending to user")
INFO_PRINT(to_user)
user_s.send(to_user)
to_user = EMPTY_BYTES
if fall_through:
continue
if last_activity + activity_timeout_seconds < time.time():
quit = 1
if quit: break
time.sleep(0.01)
DEBUG_PRINT("End of handle")
def finish(self):
global active_connections
active_connections -= 1
super().finish()
self.request.close()
self._proxy_socket.close()
class SimpleTCPProxy(socketserver.ThreadingMixIn, socketserver.TCPServer):
daemon_threads = True
def __init__(self):
global listen_address, listen_port
socketserver.TCPServer.__init__(self, (listen_address, listen_port),
SimpleHandler)
def serve_forever(self, *arguments, **keywords):
try:
super().serve_forever(*arguments, **keywords)
finally:
self.socket.close()
#self.socket.shutdown(socket.SHUT_RDWR)
server = SimpleTCPProxy()
server.server_activate()
server.serve_forever()