forked from xkvyor/btcrawler
/
test.py
276 lines (251 loc) · 8.29 KB
/
test.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/env python
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
import hashlib
import time
import re
from struct import *
import random
from datetime import datetime
from util import *
from hashdb import *
from getinfo import *
import Queue
class BittorrentProtocol(DatagramProtocol):
max_tasks = 10
min_port = 6882
max_port = 6891
def __init__(self, bootnodes=()):
self.id = gen_id()
self.sessions = {}
self.nodes = {}
self.bootnodes = bootnodes
self.unvisitednodes = []
for host, port in bootnodes:
self.unvisitednodes.append((host, port))
self.hashdb = HashDB()
self.hashq = Queue.Queue()
self.tasks = {}
self.portmap = {}
for i in xrange(BittorrentProtocol.min_port, BittorrentProtocol.max_port+1):
self.portmap[i] = None
def startProtocol(self):
self.lc = LoopingCall(self.loop)
self.lc.start(3)
def stopProtocol(self):
self.lc.stop()
self.hashdb.release()
def write(self, ip, port, data):
self.transport.write(data, (ip, port))
def datagramReceived(self, data, (host, port)):
data = bytes(data)
bd = bdecode(data)
if bd == None:
# self.taskmgr.receive(data, (host, port))
return
rmsg, rm = bd
tid = rmsg['t']
if rmsg['y'] == 'r':
if (tid in self.sessions) == False:
return
mtype = self.sessions[tid]
del self.sessions[tid]
if mtype == 'ping':
self.nodes[rmsg['r']['id']] = (host, port)
elif mtype == 'find_node':
self.handle_rfindnode(rmsg)
# reactor.callLater(int(random.random()*10), self.handle_rfindnode, rmsg)
elif mtype == 'get_peers':
pass
elif mtype == 'announce_peer':
pass
elif rmsg['y'] == 'q':
if rmsg['q'] == 'ping':
self.nodes[rmsg['a']['id']] = (host, port)
self.rping(tid, (host, port))
elif rmsg['q'] == 'find_node':
self.rfind_node(tid, rmsg['a']['target'], (host, port))
elif rmsg['q'] == 'get_peers':
self.rget_peers(tid, rmsg['a']['info_hash'], (host, port))
self.found_hash(rmsg['a']['info_hash'])
elif rmsg['q'] == 'announce_peer':
self.rannounce_peer(tid, (host, port))
self.found_hash(rmsg['a']['info_hash'])
def find_node(self, target, (host, port)):
tid = gentid()
self.sessions[tid] = 'find_node'
msg = {
"t": tid,
"y": "q",
"q": "find_node",
"a": {
"id": self.id,
"target": target,
}
}
bmsg = bencode(msg)
reactor.resolve(host).addCallback(self.write, port, bmsg)
def rfind_node(self, tid, target, (host, port)):
nodes = ''
k = 8
for i in self.nodes:
if k == 0:
break
k -= 1
h, p = self.nodes[i]
nodes += i
bytes = map(int, h.split('.'))
for b in bytes:
nodes += pack('B', b)
nodes += pack('>H', p)
msg = {
"t": tid,
"y": "r",
"r": {
"id": self.id,
"nodes": nodes,
}
}
bmsg = bencode(msg)
reactor.resolve(host).addCallback(self.write, port, bmsg)
def ping(self, (host, port)):
tid = gentid()
self.sessions[tid] = 'ping'
msg = {
"t": tid,
"y": "q",
"q": "ping",
"a": {
"id": self.id
}
}
bmsg = bencode(msg)
reactor.resolve(host).addCallback(self.write, port, bmsg)
def rping(self, tid, (host, port)):
msg = {
"t": tid,
"y": "r",
"r": {
"id": self.id
}
}
bmsg = bencode(msg)
reactor.resolve(host).addCallback(self.write, port, bmsg)
def get_peers(self, info_hash, (host, port)):
tid = gentid()
self.sessions[tid] = 'get_peers'
msg = {
"t": tid,
"y": "q",
"q": "get_peers",
"a": {
"id": self.id,
"info_hash": info_hash
}
}
bmsg = bencode(msg)
reactor.resolve(host).addCallback(self.write, port, bmsg)
def rget_peers(self, tid, info_hash, (host, port)):
nodes = ''
k = 8
for i in self.nodes:
if k == 0:
break
k -= 1
h, p = self.nodes[i]
nodes += i
bytes = map(int, h.split('.'))
for b in bytes:
nodes += pack('B', b)
nodes += pack('>H', p)
msg = {
"t": tid,
"y": "r",
"r": {
"id": self.id,
"token": gen_id(),
"nodes": nodes,
}
}
bmsg = bencode(msg)
reactor.resolve(host).addCallback(self.write, port, bmsg)
def handle_rgetpeers(self, info_hash, rmsg):
if 'nodes' in rmsg['r']:
for i in xrange(0, len(rmsg['r']['nodes']), 26):
nid, compact = unpack('>20s6s', rmsg['r']['nodes'][i:i+26])
ip, port = decompact(compact)
if 'values' in rmsg['r']:
for compact in rmsg['r']['values']:
if len(compact) != 6:
continue
ip, port = decompact(compact)
# self.taskmgr.new_task(info_hash, ip, port)
print hexstr(info_hash), ip, port
def announce_peer(self, info_hash, (host, port)):
# TODO
pass
def rannounce_peer(self, tid, (host, port)):
msg = {
"t": tid,
"y": "r",
"r": {
"id": self.id
}
}
bmsg = bencode(msg)
reactor.resolve(host).addCallback(self.write, port, bmsg)
def handle_rfindnode(self, rmsg):
if ('nodes' in rmsg['r']) == False:
return
nodes = rmsg['r']['nodes']
for i in xrange(0, len(nodes), 26):
info = nodes[i:i+26]
nid, compact = unpack('>20s6s', info)
ip, port = decompact(compact)
if (nid in self.nodes) == False:
# self.find_node(gen_id(), (ip, port))
# reactor.callLater(random.random()*60, self.find_node, gen_id(), (ip, port))
self.unvisitednodes.append((ip, port))
self.nodes[nid] = (ip, port)
def found_hash(self, info_hash):
if self.hashdb.exist(info_hash):
return
self.hashq.put(info_hash)
def loop(self):
t = 16
while t > 0:
t -= 1
if len(self.unvisitednodes) == 0:
break
host, port = self.unvisitednodes.pop()
self.find_node(gen_id(), (host, port))
for info_hash in self.tasks.keys():
port, task = self.tasks[info_hash]
if task.finish():
self.hashdb.insert_hash(hexstr(info_hash), task.get_result())
if task.timeout() or task.finish():
self.portmap[port].stopListening()
self.portmap[port] = None
del self.tasks[info_hash]
while len(self.tasks) < BittorrentProtocol.max_tasks and not self.hashq.empty():
info_hash = self.hashq.get()
if info_hash in self.tasks:
continue
task = GetinfoProtocol(info_hash, self.bootnodes)
for p in self.portmap.keys():
if self.portmap[p] == None:
self.portmap[p] = reactor.listenUDP(p, task)
self.tasks[info_hash] = (p, task)
break
def monitor(p):
print "[%s] got %d nodes" % (datetime.now(), len(p.nodes))
def main():
boots = (('router.bittorrent.com', 6881),)
p = BittorrentProtocol(boots)
# lc = LoopingCall(monitor, p)
# lc.start(5)
reactor.listenUDP(6881, p)
reactor.run()
if __name__ == '__main__':
main()