-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
105 lines (88 loc) · 3.34 KB
/
Copy pathserver.py
File metadata and controls
105 lines (88 loc) · 3.34 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
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
#!/usr/bin/env python
# -*- coding: utf8 -*-
import socket
import SocketServer
import logbook
import icmp
from ThreadedICMPServer import ThreadedICMPServer
# global socket dict: identifier and tcp-stream-socket
demultiplexer = {}
# global shards: identifier and piece list
shards = {}
class ICMPRequestHandler(SocketServer.BaseRequestHandler):
'''
ICMP
'''
def handle(self):
global demultiplexer
global shards
raw_data, local = self.request
identifier, sequence, content = icmp.unpack_reply(raw_data)
logbook.info("identifier: {} sequence: {}"
.format(identifier, sequence))
if sequence == 6666:
remote_addr = eval(content)
remote = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
remote.connect(remote_addr)
remote.settimeout(0.5)
logbook.info(
"connect the remote server: {}".format(remote_addr))
demultiplexer[identifier] = remote
icmp_body = 'ok'
elif sequence == 8888:
if identifier not in demultiplexer:
packet = icmp.pack_reply(
identifier, sequence, content)
local.sendto(packet, self.client_address)
remote = demultiplexer[identifier]
if len(content) == 0:
remote.close()
demultiplexer.pop(identifier, 0)
logbook.info("send to remote:\n{}".format(content))
remote.send(content)
remote_recv = ''
while True:
try:
buf = remote.recv(8192)
except:
logbook.info("empty buf")
break
else:
remote_recv += buf
if len(remote_recv) <= 4096:
icmp_body = remote_recv
logbook.info(
"the length of icmp_body is {}"
.format(len(icmp_body)))
logbook.info("return direct")
else:
shards[identifier] = remote_recv
icmp_body = "shards"
logbook.info("shards")
elif sequence == 9999:
if not shards.get(identifier, '') \
or not len(shards.get(identifier, '')):
icmp_body = "over"
logbook.info("over")
else:
icmp_body = shards[identifier][:4096]
shards[identifier] = shards[identifier][4096:]
else:
icmp_body = shards[identifier][sequence]
logbook.info("shard content:\n{}".format(repr(icmp_body)))
if sequence == len(shards[identifier]) - 1:
shards.pop(identifier, 0)
logbook.info("send back the content")
packet = icmp.pack_reply(identifier, sequence, icmp_body)
local.sendto(packet, self.client_address)
if __name__ == '__main__':
local_log = logbook.FileHandler("/home/nightwish/pangolin/ping.log")
local_log.format_string = (
u'[{record.time:%H:%M:%S}] '
u'lineno:{record.lineno} '
u'{record.level_name}:{record.message}')
local_log.push_application()
server = ThreadedICMPServer(('0.0.0.0', 1), ICMPRequestHandler)
logbook.info("start ICMP server")
server.serve_forever()