-
Notifications
You must be signed in to change notification settings - Fork 24
/
lightdht.py
318 lines (262 loc) · 11.3 KB
/
lightdht.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
LightDHT - A lightweight python implementation of the Bittorrent distributed
hashtable.
The aim of LightDHT is to provide a simple, flexible implementation of the
Bittorrent DHT for use in research applications. If you want to trade files,
you have come to the wrong place. LightDHT does not implement the actual
file transfer parts of the bittorrent protocol. It only takes part in the
DHT.
Read README.md for more information.
"""
import socket
import os
import time
import hashlib
import hmac
import struct
import threading
import traceback
import logging
from krpcserver import KRPCServer, KRPCTimeout, KRPCError
from routingtable import PrefixRoutingTable
# See http://docs.python.org/library/logging.html
logger = logging.getLogger(__name__)
#
# Utility functions
def dottedQuadToNum(ip):
"""convert decimal dotted quad string to long integer"""
hexn = ''.join(["%02X" % long(i) for i in ip.split('.')])
return long(hexn, 16)
def numToDottedQuad(n):
"""convert long int to dotted quad string"""
d = 256 * 256 * 256
q = []
while d > 0:
m, n = divmod(n, d)
q.append(str(m))
d /= 256
return '.'.join(q)
def decode_nodes(nodes):
""" Decode node_info into a list of id, connect_info """
nrnodes = len(nodes) / 26
nodes = struct.unpack("!" + "20sIH" * nrnodes, nodes)
for i in xrange(nrnodes):
id_, ip, port = nodes[i * 3], numToDottedQuad(nodes[i * 3 + 1]), nodes[i * 3 + 2]
yield id_, (ip, port)
def encode_nodes(nodes):
""" Encode a list of (id, connect_info) pairs into a node_info """
n = []
for node in nodes:
n.extend([node[0], dottedQuadToNum(node[1].c[0]), node[1].c[1]])
return struct.pack("!" + "20sIH" * len(nodes), *n)
class Node(object):
def __init__(self, c):
self.c = c
self.treq = 0
self.trep = 0
self.t = set()
class NotFoundError(RuntimeError):
pass
class DHT(object):
def __init__(self, port, id_, version):
self._id = id_
self._version = version
self._server = KRPCServer(port, self._version)
self._rt = PrefixRoutingTable()
# Thread details
self._shutdown_flag = False
self._thread = None
# default handler
self.handler = self.default_handler
# Behaviour configuration
# Am I actively seeking out other nodes?
self.active_discovery = True
# After how many seconds should i do another self-lookup?
self.self_find_delay = 180.0
# How many active node discovery attempts between self-lookups?
self.active_discoveries = 10
# Session key
self._key = os.urandom(20) # 20 random bytes == 160 bits
def _get_id(self, target):
# Retrieve ID to use to communicate with target node
return self._id
def start(self):
"""
Start the DHT node
"""
self._server.start()
self._server.handler = self.handler
# Add the default nodes
DEFAULT_CONNECT_INFO = ('67.215.242.139', 6881) #(socket.gethostbyaddr("router.bittorrent.com")[2][0], 6881)
DEFAULT_NODE = Node(DEFAULT_CONNECT_INFO)
DEFAULT_ID = self._server.ping(os.urandom(20), DEFAULT_NODE)['id']
self._rt.update_entry(DEFAULT_ID, DEFAULT_NODE)
# Start our event thread
self._thread = threading.Thread(target=self._pump)
self._thread.daemon = True
self._thread.start()
def shutdown(self):
self._server.shutdown()
def __enter__(self):
self.start()
def __exit__(self, type_, value, traceback):
self.shutdown()
def _pump(self):
"""
Thread that maintains DHT connectivity and does
routing table housekeeping.
Started by self.start()
The very first thing this function does, is look up itself
in the DHT. This connects it to neighbouring nodes and enables
it to give reasonable answers to incoming queries.
Afterward we look up random nodes to increase our connectedness
and gather information about the DHT as a whole
"""
# Try to establish links to close nodes
logger.info("Establishing connections to DHT")
self.find_node(self._id)
delay = self.self_find_delay
if self.active_discovery:
delay /= (self.active_discoveries + 1)
iteration = 0
while True:
try:
time.sleep(delay)
iteration += 1
if self.active_discovery and iteration % (self.active_discoveries + 1) != 0:
target = hashlib.sha1("this is my salt 2348724" + str(iteration) + self._id).digest()
self.find_node(target)
logger.info("Tracing done, routing table contains %d nodes", self._rt.node_count())
else:
# Regular maintenance:
# Find N random nodes. Execute a find_node() on them.
# toss them if they come up empty.
n = self._rt.sample(self._id, 10, 1)
for node_id, c in n:
try:
r = self._server.find_node(self._id, c, self._id)
if "nodes" in r:
self._process_incoming_nodes(r["nodes"])
except KRPCTimeout:
# The node did not reply.
# Blacklist it.
self._rt.bad_node(node_id, c)
logger.info("Cleanup, routing table contains %d nodes", self._rt.node_count())
except:
# This loop should run forever. If we get into trouble, log
# the exception and carry on.
logger.critical("Exception in DHT maintenance thread:\n\n" + traceback.format_exc())
def _process_incoming_nodes(self, bnodes):
# Add them to the routing table
for node_id, node_c in decode_nodes(bnodes):
self._rt.update_entry(node_id, Node(node_c))
def _recurse(self, target, function, max_attempts=10, result_key=None):
"""
Recursively query the DHT, following "nodes" replies
until we hit the desired key
This is the workhorse function used by all recursive queries.
"""
logger.debug("Recursing to target %r" % target.encode("hex"))
attempts = 0
while attempts < max_attempts:
for id_, node in self._rt.get_close_nodes(target):
try:
r = function(self._get_id(id_), node, target)
logger.debug("Recursion results from %r ", node.c)
attempts += 1
if result_key and result_key in r:
return r[result_key]
if "nodes" in r:
self._process_incoming_nodes(r["nodes"])
except KRPCTimeout:
# The node did not reply.
# Blacklist it.
self._rt.bad_node(id_, node)
except KRPCError:
# Sometimes we just flake out due to UDP being unreliable
# Don't sweat it, just log and carry on.
logger.error("KRPC Error:\n\n" + traceback.format_exc())
if result_key:
# We were expecting a result, but we did not find it!
# Raise the NotFoundError exception instead of returning None
raise NotFoundError
def find_node(self, target, attempts=10):
"""
Recursively call the find_node function to get as
close as possible to the target node
"""
logger.debug("Tracing to %r" % target.encode("hex"))
self._recurse(target, self._server.find_node, max_attempts=attempts)
def get_peers(self, info_hash, attempts=10):
"""
Recursively call the get_peers function to fidn peers
for the given info_hash
"""
logger.debug("Finding peers for %r" % info_hash.encode("hex"))
return self._recurse(info_hash, self._server.get_peers, result_key="values", max_attempts=attempts)
def default_handler(self, rec, c):
"""
Process incoming requests
"""
logger.info("REQUEST: %r %r" % (c, rec))
# Use the request to update the routing table
peer_id = rec["a"]["id"]
#print peer_id.encode('base64'), self._get_id(peer_id).encode('base64')
if self._get_id(peer_id) == peer_id:
# don't talk to yourself.
return
self._rt.update_entry(peer_id, Node(c))
# Skeleton response
resp = {"y": "r", "t": rec["t"], "r": {"id": self._get_id(peer_id)}, "v": self._version}
if rec["q"] == "ping":
self._server.send_krpc_reply(resp, c)
elif rec["q"] == "find_node":
target = rec["a"]["target"]
resp["r"]["id"] = self._get_id(target)
resp["r"]["nodes"] = encode_nodes(self._rt.get_close_nodes(target))
self._server.send_krpc_reply(resp, c)
elif rec["q"] == "get_peers":
# Provide a token so we can receive announces
# The token is generated using HMAC and a secret
# session key, so we don't have to remember it.
# Token is based on nodes id, connection details
# torrent infohash to avoid clashes in NAT scenarios.
info_hash = rec["a"]["info_hash"]
resp["r"]["id"] = self._get_id(info_hash)
token = hmac.new(self._key, info_hash + peer_id + str(c), hashlib.sha1).digest()
resp["r"]["token"] = token
# We don't actually keep any peer administration, so we
# always send back the closest nodes
resp["r"]["nodes"] = encode_nodes(self._rt.get_close_nodes(info_hash))
self._server.send_krpc_reply(resp, c)
elif rec["q"] == "announce_peer":
# First things first, validate the token.
info_hash = rec["a"]["info_hash"]
resp["r"]["id"] = self._get_id(info_hash)
peer_id = rec["a"]["id"]
token = hmac.new(self._key, info_hash + peer_id + str(c), hashlib.sha1).digest()
if token != rec["a"]["token"]:
return # Ignore the request
else:
# We don't actually keep any peer administration, so we
# just acknowledge.
self._server.send_krpc_reply(resp, c)
else:
logger.error("Unknown request in query %r" % rec)
if __name__ == "__main__":
# Enable logging:
# Tell the module's logger to log at level DEBUG
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
logging.getLogger("krpcserver").setLevel(logging.DEBUG)
logging.getLogger("krpcserver").addHandler(logging.StreamHandler())
# Create a DHT node.
dht1 = DHT(port=54767, id_=hashlib.sha1(
"Change this to avoid getting ID clashes").digest(), version="XN\x00\x00")
# Start it!
with dht1:
# Look up peers that are sharing one of the Ubuntu 12.04 ISO torrents
print dht1.get_peers("8ac3731ad4b039c05393b5404afa6e7397810b41".decode("hex"))
# Go to sleep and let the DHT service requests.
while True:
time.sleep(1)