-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
80 lines (80 loc) · 2.79 KB
/
proxy.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
import socket
import select
import time
import sys
import re
def main():
try:
buffer_size = 4096
delay = 0.005
forward_to = ('127.0.0.1', 9387)
class Forward:
def __init__(self):
self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def start(self, host, port):
try:
self.forward.connect((host, port))
return self.forward
except:
return False
class sock:
input_list = []
channel = {}
def __init__(self, host, port):
self.s = None
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind((host, port))
self.server.listen(200)
def main_loop(self):
self.input_list.append(self.server)
while 1:
time.sleep(delay)
ss = select.select
inputready, outputready, exceptready = ss(self.input_list, [], [])
for self.s in inputready:
if self.s == self.server:
self.on_accept()
break
self.data = self.s.recv(buffer_size)
if len(self.data) == 0:
self.on_close()
break
else:
self.on_recv()
def on_accept(self):
forward = Forward().start(forward_to[0], forward_to[1])
clientsock, clientaddr = self.server.accept()
if forward:
self.input_list.append(clientsock)
self.input_list.append(forward)
self.channel[clientsock] = forward
self.channel[forward] = clientsock
else:
clientsock.close()
def on_close(self):
self.input_list.remove(self.s)
self.input_list.remove(self.channel[self.s])
out = self.channel[self.s]
self.channel[out].close()
self.channel[self.s].close()
del self.channel[out]
del self.channel[self.s]
def on_recv(self):
data = self.data
self.channel[self.s].send(data)
try:
try:
server = sock('0.0.0.0', 5000)
server.main_loop()
except:
pass
except:
pass
except:
pass
if __name__ == '__main__':
try:
main()
except:
sys.exit()