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
|
#!/usr/bin/python
import time
import select
import socket
class Proxy:
buffer_size = 4096 * 2
delay = 0.0001
def __init__(self, host_port, to_host_port):
self.forward_to = to_host_port
self.input_list = []
self.channel = {}
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 time.sleep(self.delay) or True:
for self.s in select.select(self.input_list, [], [])[0]:
if self.s == self.server:
self.on_accept()
break
self.data = self.s.recv(self.buffer_size)
if len(self.data) == 0:
self.on_close()
break
else:
self.on_recv()
def on_accept(self):
forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsock, clientaddr = self.server.accept()
try:
forward.connect(self.forward_to)
self.input_list.append(clientsock)
self.input_list.append(forward)
self.channel[clientsock] = forward
self.channel[forward] = clientsock
except Exception:
clientsock.close()
def on_close(self):
out = self.channel[self.s]
self.input_list.remove(self.s)
self.input_list.remove(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):
if self.allow_data():
print repr(self.data)
self.channel[self.s].send(self.data)
def allow_data(self):
return not ("admin.$cmd" in self.data and "shutdown" in self.data)
if __name__ == '__main__':
import os, sys
if len(sys.argv) < 3 or ":" not in sys.argv[2]:
program = os.path.basename(sys.argv[0])
print "usage: {} <port> <to_host>:<to_port>".format(program)
sys.exit(1)
try:
to_host, to_port = sys.argv[2].split(":")
Proxy(('', int(sys.argv[1])), (to_host, int(to_port))).main_loop()
except KeyboardInterrupt:
sys.exit(1)
|