-
Notifications
You must be signed in to change notification settings - Fork 0
/
kademliadiscovery.py
354 lines (295 loc) · 14.7 KB
/
kademliadiscovery.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import asyncio
from collections import OrderedDict
import os
from typing import Callable, Union
from deccom.cryptofuncs.hash import SHA256
from deccom.peers.peer import Peer
from deccom.protocols.peerdiscovery.abstractpeerdiscovery import AbstractPeerDiscovery
from deccom.protocols.peerdiscovery._kademlia_routing import BucketManager
from deccom.protocols.wrappers import *
from deccom.protocols.peerdiscovery._finder import Finder
class KademliaDiscovery(AbstractPeerDiscovery):
INTRODUCTION = int.from_bytes(b'\xe1', byteorder="big") # english opening king's variation
RESPOND_FIND = int.from_bytes(b'\xc4', byteorder="big")
# TODO: Fix finding
FIND = int.from_bytes(b'\xf6', byteorder="big")
ASK_FOR_ID = int.from_bytes(b'\xf3',byteorder="big")
offers = dict(AbstractPeerDiscovery.offers, **{
"send_ping": "send_ping"
})
bindings = dict(AbstractPeerDiscovery.bindings, **{
"_lower_ping": "send_ping"
})
required_lower = AbstractPeerDiscovery.required_lower + ["send_ping"]
def __init__(self, bootstrap_peers: list[Peer] = [], interval: int = 60, k: int = 20, always_split = False, submodule=None, callback: Callable[[tuple[str, int], bytes], None] = None, disconnected_callback=lambda *args:..., connected_callback: Callable[[Peer], None] =lambda *args:...):
super().__init__(bootstrap_peers, interval, submodule, callback, disconnected_callback, connected_callback)
self.k = k
self.peer_crawls = dict()
self.sent_finds = dict()
self.warmup = 0
self.max_warmup = 30 * (k//10)
self.refresh_loop = None
self.always_split = always_split
self.searches: dict[bytes,bytes] = dict()
self.finders: dict[bytes, Finder] = dict()
self.bucket_manager = None
async def start(self, p: Peer):
await super().start(p)
self.bucket_manager = BucketManager(self.peer.id_node,self.k,self._add,always_split = self.always_split)
for p in self.bootstrap_peers:
await self.introduce_to_peer(p)
msg = bytearray([KademliaDiscovery.ASK_FOR_ID])
await self.send_datagram(msg,p.addr)
loop = asyncio.get_event_loop()
self.refresh_loop = loop.call_later(2, self.refresh_table)
async def stop(self):
self.searches.clear()
for k,v in self.finders.items():
v.stop()
self.finders.clear()
self.peer_crawls.clear()
self.sent_finds.clear()
if self.bucket_manager != None:
self.bucket_manager.clear()
if self.refresh_loop != None:
self.refresh_loop.cancel()
return await super().stop()
def refresh_table(self):
loop = asyncio.get_event_loop()
loop.create_task(self._refresh_table())
async def _refresh_table(self):
# print("refreshing")
loop = asyncio.get_running_loop()
if len(self.bucket_manager.buckets) == 1 and len(self.bucket_manager.buckets[0].peers) == 0:
print("i dont know anyone still")
for p in self.bootstrap_peers:
await self.introduce_to_peer(p)
await asyncio.sleep(1)
msg = bytearray([KademliaDiscovery.ASK_FOR_ID])
await self.send_datagram(msg,p.addr)
self.refresh_loop = loop.call_later(2, self.refresh_table)
return
rand_ids = []
other = self.bucket_manager.get_smallest_bucket().to_bytes(32, byteorder = "big")
rand_ids.append(other)
unique_id = os.urandom(8)
while self.searches.get(unique_id) != None:
unique_id = os.urandom(8)
if self.warmup == 0:
# print("looking for myself", self.peer.id_node)
self.finders[unique_id] = Finder(self.peer.id_node, self.bucket_manager.get_closest(self.peer.id_node,3), 3)
l = self.finders[unique_id].find_peer()
self.warmup+=1
for p in l:
msg = bytearray([KademliaDiscovery.FIND ^ 1])
msg += unique_id
msg += self.peer.id_node
await self.send_datagram(msg,p.addr)
self.refresh_loop = loop.call_later(self.interval, self.refresh_table)
return
ret = self.bucket_manager.get_buckets_not_updated(self.interval)
for r in ret:
rand_ids.append(r.to_bytes(32, byteorder="big"))
for ids in rand_ids:
l = self.bucket_manager.get_closest(ids,3)
if len(l) == 0:
continue
for p in l:
msg = bytearray([KademliaDiscovery.FIND ^ 1])
msg += unique_id
msg += ids
await self.send_datagram(msg,p.addr)
self.refresh_loop = loop.call_later(self.interval, self.refresh_table)
def remove_peer(self, addr: tuple[str, int], node_id: bytes):
if self.bucket_manager.get_peer(node_id) == None:
return super().remove_peer(addr, node_id)
del self.peers[node_id]
print(self.peer.pub_key, "removing peer.", self.bucket_manager.get_peer(node_id).pub_key)
self.bucket_manager.remove_peer(node_id)
return super().remove_peer(addr, node_id)
async def introduce_to_peer(self, peer: Peer):
# print("introducing to", peer.id_node)
msg = bytearray([KademliaDiscovery.INTRODUCTION])
msg = msg + bytes(self.peer)
await self.send_datagram(msg, peer.addr)
def process_datagram(self, addr: tuple[str, int], data: bytes):
if data[0] == KademliaDiscovery.INTRODUCTION:
other, i = Peer.from_bytes(data[1:])
#print(self.peer.pub_key,": introduction form", other.pub_key)
other.addr = addr
if self.bucket_manager.get_peer(other.id_node) != None:
self.bucket_manager.update_peer(other.id_node, other)
else:
self.connection_approval(addr,other,self.add_peer,self.ban_peer)
elif data[0] == KademliaDiscovery.FIND or data[0] ^ KademliaDiscovery.FIND == 1:
# print("peer looking")
i = 1
unique_id = data[i:i+8]
id = data[i+8:]
self.sent_finds[data] = i
if id == self.peer.id_node and data[0] == KademliaDiscovery.FIND:
#print("THATS ME ",self.peer.addr, addr)
loop = asyncio.get_running_loop()
msg = bytearray([KademliaDiscovery.INTRODUCTION])
msg = msg + bytes(self.peer)
loop = asyncio.get_running_loop()
loop.create_task(self.send_datagram(msg, addr))
elif self.get_peer(id) != None and data[0] == KademliaDiscovery.FIND:
# print(self.peer.pub_key,"I KNOW THAT GUY!",self.get_peer(id).pub_key)
self.send_find_response(addr,[self.get_peer(id)],unique_id)
else:
closest_peers = self.bucket_manager.get_closest(id)
if len(closest_peers) == 0:
# print("oops dont know anyone :/")
return
self.send_find_response(addr,closest_peers,unique_id)
elif data[0] == KademliaDiscovery.RESPOND_FIND:
#print(self.peer.pub_key,"got a response",addr)
i = 1
unique_id = data[i:i+8]
i+=8
if self.searches.get(unique_id) == None and self.warmup >= self.max_warmup:
#print(self.peer.pub_key,"NOT A VALID SEARCH")
return
else:
self.warmup += 1
peers: list[Peer] = []
while i < len(data):
peer_new, offs = Peer.from_bytes(data[i:])
i+=offs
if peer_new.id_node == self.peer.id_node:
continue
peers.append(peer_new)
# print("got ",len(peers), "to look up",self.searches.get(unique_id))
if self.searches.get(unique_id) != None:
for p in peers:
if p.id_node == self.searches.get(unique_id) and p.id_node != self.peer.id_node and self.finders.get(unique_id) != None:
self._lower_heard_from(addr,p.addr)
loop = asyncio.get_running_loop()
loop.create_task(self.introduce_to_peer(p))
msg = bytearray([KademliaDiscovery.FIND])
msg += unique_id
msg += self.finders[unique_id].look_for
del self.finders[unique_id]
loop.create_task(self.send_datagram(msg, p.addr))
return
for p in peers:
self._lower_heard_from(addr, p.addr)
if self.bucket_manager.get_peer(p.id_node) == None and p.id_node != self.peer.id_node:
loop = asyncio.get_running_loop()
loop.create_task(self.introduce_to_peer(p))
msg = bytearray([KademliaDiscovery.ASK_FOR_ID])
loop.create_task(self.send_datagram(msg,p.addr))
if self.finders.get(unique_id) != None:
self.finders[unique_id].add_peer(peers)
msg = bytearray([KademliaDiscovery.FIND if self.finders[unique_id].look_for != self.peer.id_node else KademliaDiscovery.FIND ^ 1])
msg += unique_id
msg += self.finders[unique_id].look_for
l = self.finders[unique_id].find_peer()
loop = asyncio.get_running_loop()
for p in l:
# print("sending to ", p.pub_key)
loop.create_task(self.send_datagram(msg, p.addr))
elif data[0] == KademliaDiscovery.ASK_FOR_ID:
#print("ASKING FOR ID", self.peer.addr, addr)
msg = bytearray([KademliaDiscovery.INTRODUCTION])
# print("PUBKEY", self.peer)
# pprint(vars(self.peer))
msg = msg + bytes(self.peer)
loop = asyncio.get_running_loop()
loop.create_task(self.send_datagram(msg, addr))
else:
return self.callback(addr, data[1:])
def send_find_response(self, addr, best_guess: list[Peer], uniq_id):
i = 0
while i < self.k:
msg = bytearray([KademliaDiscovery.RESPOND_FIND])
msg += uniq_id
for p in best_guess[i:i+10]:
msg += bytes(p)
loop = asyncio.get_running_loop()
loop.create_task(self.send_datagram(msg, addr))
i += 10
async def send_ping(self, addr, success, fail, timeout):
await self._lower_ping(addr, success, fail, timeout)
async def send_find(self, unique_id, p: Peer, bypass = False, for_peer: bytes = None):
if self.searches.get(unique_id) == None:
if bypass:
# print("bypassing")
msg = bytearray([KademliaDiscovery.FIND ^ 1])
msg += unique_id
msg += self.peer.id_node if for_peer == None else for_peer
await self.send_datagram(msg,p.addr)
elif self.warmup < self.max_warmup:
msg = bytearray([KademliaDiscovery.FIND])
msg += unique_id
msg += self.peer.id_node if for_peer == None else for_peer
await self.send_datagram(msg,p.addr)
return
msg = bytearray([KademliaDiscovery.FIND])
msg += unique_id
msg += self.searches[unique_id]
await self.send_datagram(msg,p.addr)
def successful_add(self, addr: tuple[str,int], p: Peer):
#print(self.peer.pub_key," : adding peer", p.pub_key)
if self.peer_crawls.get(p.id_node) != None:
self.peer_crawls[p.id_node][0].set_result("success")
del self.searches[self.peer_crawls[p.id_node][1]]
del self.peer_crawls[p.id_node]
self.bucket_manager.update_peer(p.id_node,p)
self.peers[p.id_node] = p
super().add_peer(addr, p)
async def _async_add(self,addr,p):
return self.successful_add(addr,p)
def _add(self, dist, p: Peer):
loop = asyncio.get_event_loop()
loop.create_task(self._async_add(p.addr,p))
def update_peer(self, p: Peer):
# print(self.peer.pub_key, "peer responded", p.pub_key)
self.bucket_manager.update_peer(p.id_node, p)
def add_peer(self, addr: tuple[str,int], p: Peer):
# print(p)
ret = self.bucket_manager.add_peer(p.id_node,p)
if ret != None:
#print(self.peer.pub_key,"oops, kinda big for", p.pub_key)
loop = asyncio.get_event_loop()
loop.create_task(self._lower_ping(ret[1].addr, lambda addr, peer=ret[1], self=self: self.update_peer(peer), lambda addr, oldp=ret[1], self=self: self.remove_peer(addr, oldp.id_node), 8))
else:
self.successful_add(addr,p)
async def _find_peer(self, fut, id):
unique_id = os.urandom(8)
while self.searches.get(unique_id) != None:
unique_id = os.urandom(8)
self.peer_crawls[id] = (fut, unique_id)
msg = bytearray([KademliaDiscovery.FIND])
self.searches[unique_id] = id
msg += unique_id
if not isinstance(id, bytes):
id = SHA256(id)
msg += id
self.finders[unique_id] = Finder(id, self.bucket_manager.get_closest(id,5), 5)
l = self.finders[unique_id].find_peer()
for p in l:
# print("sending to ", p.pub_key)
await self.send_datagram(msg, p.addr)
async def find_peer(self, id) -> Peer:
if id == self.peer.id_node:
return self.peer
if self.get_peer(id) == None:
if self.peer_crawls.get(id) == None:
loop = asyncio.get_running_loop()
fut = loop.create_future()
await self._find_peer(fut, id)
if self.get_peer(id) != None:
fut.set_result("success")
del self.peer_crawls[id]
return self.get_peer(id)
await fut
else:
await self.peer_crawls.get(id)[0]
return self.get_peer(id)
def get_peer(self, id) -> Union[Peer,None]:
return self.bucket_manager.get_peer(id)
@bindto("send_ping")
async def _lower_ping(self, addr, success, failure, timeout):
return