-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
239 lines (211 loc) · 7.08 KB
/
agent.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
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
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/python
import socket
import select
import time
import sys
import psutil
import array
from amqp.factory import get_protocol
from amqp.stream import AMQPStreamReader
class AMQPStateMachine:
def __init__(self):
self.protocol = None
self.wait_protocol = True
def handle_client_send(self, data):
if self.wait_protocol:
try:
self.protocol = get_protocol(''.join(data))
except Exception, e:
print e
pass
self.wait_protocol = False
return
self.handle_frame(data)
def handle_client_recv(self, data):
if not self.protocol:
return
self.handle_frame(data)
def handle_frame(self, data):
reader = AMQPStreamReader(data, self.protocol.SIG_TO_DATA_TYPE)
while not reader.eof():
frame = self.protocol.read_frame(reader)
print frame
class ClientSocketInfo:
def __init__(self, sock, addr):
self.sock = sock
self.addr = addr
self.pid = self.find_pid_by_laddr(self.addr)
self.cmdline = self.find_cmdline(self.pid) if self.pid else None
@staticmethod
def find_pid_by_laddr(laddr):
nc = psutil.net_connections()
for c in nc:
if c.laddr == laddr:
return c.pid
return None
@staticmethod
def find_cmdline(pid):
p = psutil.Process(pid)
return p.cmdline()
def __str__(self):
pid = self.pid if self.pid else -1
cmdline = ' '.join(self.cmdline) if self.cmdline else str(self.addr[0])
port = self.addr[1] if len(self.addr) == 2 else -1
return "%.8f\n%6d: %s [%d]" % (time.time(), pid, cmdline, port)
class ClientConnection:
def __init__(self, csockinfo, fwdsock):
self.csockinfo = csockinfo
self.fwdsock = fwdsock
self.strc = str(csockinfo)
self.strf = str(fwdsock.getpeername())
self.amqp = AMQPStateMachine()
def close(self):
self.csockinfo.sock.close()
self.fwdsock.close()
self.csockinfo.sock = None
self.csockinfo = None
self.fwdsock = None
@staticmethod
def print_data(data):
non_ascii = range(0, 31) + [127, 255]
l = len(data)
i = 0
while i < l:
sl = [data[i:i+8], data[i+8:i+16]]
ln = [
' '.join('%02X' % ord(x) for x in sl[0]).ljust(24),
' '.join('%02X' % ord(x) for x in sl[1]).ljust(24),
''.join('.' if ord(x) in non_ascii else x for x in sl[0]),
''.join('.' if ord(x) in non_ascii else x for x in sl[1])
]
print '%7d' % i, ' '.join(ln)
i += 16
print '-' * 75
def handle_data(self, sock, data):
if sock == self.fwdsock:
print self.strc, "<<", self.strf
self.amqp.handle_client_recv(data)
self.csockinfo.sock.send(data)
else:
print self.strc, ">>", self.strf
self.amqp.handle_client_send(data)
self.fwdsock.send(data)
self.print_data(data)
class ServerProxy:
def __init__(self, listen_addr, fwd_addr):
self.buffer_size = 1024 * 512 # 512KB
self.buffer = array.array('c', b'\x00' * self.buffer_size)
self.conns = {}
self.socks = []
self.fwd_addr = fwd_addr
self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.ssock.bind(listen_addr)
self.ssock.listen(200)
self.socks.append(self.ssock)
def run(self):
while 1:
time.sleep(0.0001)
iready, oready, exready = select.select(self.socks, [], [])
# todo: use threads
for sock in iready:
if sock == self.ssock:
self.handle_connect()
break
conn = self.conns[sock]
try:
read_size = sock.recv_into(self.buffer, self.buffer_size)
except socket.error, e:
print e
read_size = 0
if not read_size:
self.handle_close(conn)
break
conn.handle_data(sock, self.buffer[:read_size])
@staticmethod
def make_fwdsock(addr):
fwdsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
fwdsock.connect(addr)
except Exception, e:
print e
fwdsock = False
return fwdsock
def handle_connect(self):
fwdsock = self.make_fwdsock(self.fwd_addr)
csock, caddr = self.ssock.accept()
csockinfo = ClientSocketInfo(csock, caddr)
if fwdsock:
conn = ClientConnection(csockinfo, fwdsock)
self.conns[conn.csockinfo.sock] = conn
self.conns[conn.fwdsock] = conn
self.socks.append(conn.csockinfo.sock)
self.socks.append(conn.fwdsock)
print csockinfo, "connected client"
else:
print "Unable to connect to server." % self.ssock.getpeername(),
print "Disconnecting client.", csockinfo
csock.close()
def handle_close(self, conn):
print conn.csockinfo, "disconnected client"
del self.conns[conn.csockinfo.sock]
del self.conns[conn.fwdsock]
self.socks.remove(conn.csockinfo.sock)
self.socks.remove(conn.fwdsock)
conn.close()
def show_usage():
print "\n".join([
'Usage:',
' %s --listen=[host]:port --forward=host:[port]' % sys.argv[0],
'Example:',
' %s --listen=:9090 --forward=localhost:5672' % sys.argv[0],
''
])
exit(1)
def parse_args():
if len(sys.argv) != 3:
show_usage()
listen_host = ''
listen_port = None
fwd_host = None
fwd_port = 5672
for argv in sys.argv:
nv = argv.split('=')
if len(nv) != 2:
continue
if nv[0] == '--listen':
hp = nv[1].split(':')
if len(hp) != 2:
continue
listen_host = hp[0]
try:
listen_port = int(hp[1])
except ValueError:
print 'Invalid listen port.'
show_usage()
elif nv[0] == '--forward':
hp = nv[1].split(':')
if len(hp) != 2:
continue
fwd_host = hp[0]
if len(hp[1]):
try:
fwd_port = int(hp[1])
except ValueError:
print 'Invalid forward port.'
show_usage()
if not listen_port:
print 'Listen port not specified.'
show_usage()
if not fwd_host:
print 'Forward host not specified.'
show_usage()
return (listen_host, listen_port), (fwd_host, fwd_port)
if __name__ == '__main__':
listen_addr, fwd_addr = parse_args()
server = ServerProxy(listen_addr, fwd_addr)
try:
server.run()
except KeyboardInterrupt:
print " INTERRUPTED"
sys.exit(1)