-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdhttornado.py
More file actions
206 lines (161 loc) · 5.36 KB
/
dhttornado.py
File metadata and controls
206 lines (161 loc) · 5.36 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
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
# coding:utf-8
import os
import ast
import dht
from dht import DHTPeer
import json
import tornado.ioloop
import tornado.options
from tornado.options import define, options
import tornado.web
import tornado.httpserver
class ComplexEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, DHTPeer):
return (obj.id.encode("hex"), obj.ip_port[0], obj.ip_port[1])
else:
return json.JSONEncoder.default(self, obj)
class AHandler(tornado.web.RequestHandler):
@classmethod
def register_dht(self, dht):
self.dht = dht
def get(self):
d = {}
# for id, n in self.dht.node_lists.iteritems():
# d["Node_List_%s" % id.encode("hex")] = n.get_debug_array()
self.set_header('Content-Type', 'text/plain')
self.write(json.dumps(d, indent=2, cls=ComplexEncoder))
class BHandler(tornado.web.RequestHandler):
@classmethod
def register_dht(self, dht):
self.dht = dht
def get(self):
d = {}
for id in self.dht.infohash_peers:
key = "Peers_for_%s" % id.encode("hex")
d[key] = self.dht.infohash_peers[id].keys()
self.set_header('Content-Type', 'text/plain')
self.write(json.dumps(d, indent=2, cls=ComplexEncoder))
class DumpPeersHandler(tornado.web.RequestHandler):
"""
备份所有的peers
"""
@classmethod
def register_dht(self, dht):
self.dht = dht
def get(self):
d = {}
for id in self.dht.infohash_peers:
d[id.encode("hex")] = self.dht.infohash_peers[id].keys()
f = open("/tmp/peers.txt", 'w')
f.write(repr(d))
f.close()
self.write("success!!")
class DumpHandler(tornado.web.RequestHandler):
"""
备份所有的ip:port
"""
@classmethod
def register_dht(self, dht):
self.dht = dht
def get(self):
def populate(tree, l):
if tree.left:
populate(tree.left, l)
if tree.right:
populate(tree.right, l)
if tree.value:
for i in tree.value:
l.append(tuple(i.ip_port))
return l
rt = []
populate(self.dht.routing_table._root, rt)
f = open("/tmp/ip_port.txt", 'w')
f.write(repr(rt))
f.close()
self.write("success!!")
class DumpMemHandler(tornado.web.RequestHandler):
"""
导出内存
"""
@classmethod
def register_dht(self, dht):
self.dht = dht
def get(self):
from meliae import scanner
import time
scanner.dump_all_objects('/tmp/dump%s.txt' % time.time())
self.write("success!!")
class IndexHandler(tornado.web.RequestHandler):
@classmethod
def register_dht(self, dht):
self.dht = dht
def get(self):
def populate(tree, dict):
if tree.left:
dict['left'] = {}
populate(tree.left, dict['left'])
if tree.right:
dict['right'] = {}
populate(tree.right, dict['right'])
if tree.value:
dict['value'] = tree.value
return dict
rt = {}
populate(self.dht.routing_table._root, rt)
d = {
# 'zid': str(self.dht.id),
# 'aid': str(self.dht.id),
'dht': str(self.dht),
'routing_table': str(self.dht.routing_table),
'rt': rt
}
self.set_header('Content-Type', 'text/plain')
self.write(json.dumps(d, indent=2, cls=ComplexEncoder))
if __name__ == "__main__":
ioloop = tornado.ioloop.IOLoop()
ioloop.install()
define('debug', default=True, type=bool) # save file causes autoreload
define('frontend_port', default=7070, type=int)
tornado.options.parse_command_line()
# settings = dict((k, v.value()) for k, v in options.items())
settings = {}
ip_ports = [('router.bittorrent.com', 6881),
# ('router.bitcomet.com', 6881),
('router.utorrent.com', 6881),
('dht.transmissionbt.com', 6881),
("service.ygrek.org.ua", 6881),
("router.transmission.com", 6881),
]
# 读dump
path = "/tmp/ip_port.txt"
if os.path.isfile(path):
f = open(path)
l = ast.literal_eval(f.read())
ip_ports = ip_ports + l
# print(ip_ports)
# 23649f6ace4b4062879066a6afe99b91c1880b8f
# node_id = 'd54408eb2a5d686bd3e587f7a96c2facebbeadfc'.decode('hex')
node_id = '23649f6ace4b4062879066a6afe99b91c1880b8f'.decode('hex')
dht = dht.DHT(51414, ip_ports, node_id=node_id)
frontend_routes = [
('/c', IndexHandler),
('/a', AHandler),
('/b', BHandler),
('/dump', DumpHandler),
('/dumppeers', DumpPeersHandler),
('/dumpmem', DumpMemHandler),
]
frontend_application = tornado.web.Application(frontend_routes, **settings)
frontend_server = tornado.httpserver.HTTPServer(frontend_application,
io_loop=ioloop)
frontend_server.bind(options.frontend_port, '')
frontend_server.start()
IndexHandler.register_dht(dht)
AHandler.register_dht(dht)
BHandler.register_dht(dht)
DumpHandler.register_dht(dht)
DumpMemHandler.register_dht(dht)
DumpPeersHandler.register_dht(dht)
dht.bootstrap()
dht.start()