-
Notifications
You must be signed in to change notification settings - Fork 0
/
clue.py
216 lines (191 loc) · 4.96 KB
/
clue.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
import socket
import logging
from threading import Thread
from binascii import b2a_hex
from collections import deque
from time import sleep
from libs.utils import *
from libs.knode import KNode
from bencodepy import encode, decode
UDP_IP = "0.0.0.0"
UDP_PORT = 6881
MAX_NODE_QSIZE = 50
DHT_NODES = (
("router.bittorrent.com", 6881),
("dht.transmissionbt.com", 6881),
("router.utorrent.com", 6881)
)
TID_LENGTH = 2
RE_JOIN_DHT_INTERVAL = 3
TOKEN_LENGTH = 2
class Clue(Thread):
def __init__(self, udp_ip, udp_port, max_node_qsize):
super().__init__()
self.daemon = True
self.logger = logging.getLogger('clue')
self.nid = generate_nid()
self.logger.debug( "nid: {}".format( b2a_hex(self.nid) ) )
self.nodes = deque(maxlen=max_node_qsize)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp_ip = udp_ip
self.udp_port = udp_port
self.max_node_qsize = max_node_qsize
self.process_request_actions = {
b"get_peers": self.on_get_peers_request,
b"announce_peer": self.on_announce_peer_request,
}
self.sock.bind((self.udp_ip, self.udp_port))
def run(self):
self.re_join_DHT()
while True:
try:
data, address = self.sock.recvfrom(65536)
message = decode(data)
self.on_message(message, address)
except Exception as error:
pass
def on_message(self, message, address):
try:
if message[b"y"] == b"r":
if b"nodes" in message[b"r"]:
self.find_node_response(message, address)
elif message[b"y"] == b"q":
try:
self.process_request_actions[message[b"q"]](message, address)
except KeyError:
self.play_dead(message, address)
except Exception as error:
pass
def send_krpc(self, message, address):
try:
self.sock.sendto(encode(message), address)
except Exception as error:
self.logger.error("send_krpc: {}".format(error))
def find_node(self, address, nid=None):
nid = get_neighbor(nid, self.nid) if nid else self.nid
tid = generate_chars(TID_LENGTH)
message = {
"t": tid,
"y": "q",
"q": "find_node",
"a": {
"id": nid,
"target": generate_nid()
}
}
self.send_krpc(message, address)
self.logger.debug(
"find_node: \nid: {}\ntg: {}".format(
b2a_hex( message["a"]["id"] ),
b2a_hex( message["a"]["target"] )
)
)
def find_node_response(self, message, address):
nodes = decode_nodes( message[b"r"][b"nodes"] )
for node in nodes:
(nid, ip, port) = node
if len(nid) != 20: continue
if ip == self.udp_ip: continue
if port < 1 or port > 65535: continue
n = KNode(nid, ip, port)
self.nodes.append(n)
self.logger.debug(
"find_node_response: \nnid: {}\nip: {}\nport: {}".format(
b2a_hex(nid), ip, port
)
)
def join_DHT(self):
for address in DHT_NODES:
self.find_node(address)
def re_join_DHT(self):
if len(self.nodes) == 0:
self.join_DHT()
timer(RE_JOIN_DHT_INTERVAL, self.re_join_DHT)
def auto_find_node(self):
wait = 1.0 / self.max_node_qsize
while True:
try:
node = self.nodes.popleft()
self.find_node((node.ip, node.port), node.nid)
except IndexError:
pass
sleep(wait)
def play_dead(self, message, address):
try:
tid = message[b"t"]
message = {
"t": tid,
"y": "e",
"e": [202, "Server Error"]
}
self.send_krpc(message, address)
except KeyError:
pass
def on_get_peers_request(self, message, address):
try:
infohash = message[b"a"][b"info_hash"]
tid = message[b"t"]
nid = message[b"a"][b"id"]
token = infohash[:TOKEN_LENGTH]
message = {
"t": tid,
"y": "r",
"r": {
"id": get_neighbor(infohash, self.nid),
"nodes": "",
"token": token
}
}
self.send_krpc(message, address)
self.logger.info(
"on_get_peers_request:\nnid: {}\ninfohash: {}\ntoken {}".format(
b2a_hex(nid), b2a_hex(infohash), b2a_hex(token)
)
)
except KeyError:
pass
def on_announce_peer_request(self, message, address):
try:
infohash = message[b"a"][b"info_hash"]
token = message[b"a"][b"token"]
nid = message[b"a"][b"id"]
tid = message[b"t"]
if infohash[:TOKEN_LENGTH] == token:
if b"implied_port" in message[b"a"] and message[b"a"][b"implied_port"] != b"0":
port = address[1]
else:
port = int(message[b"a"][b"port"])
if port < 1 or port > 65535: return
self.logger.warning(b2a_hex(infohash))
except Exception:
pass
finally:
self.ok(message, address)
def ok(self, message, address):
try:
tid = message[b"t"]
nid = message[b"a"][b"id"]
message = {
"t": tid,
"y": "r",
"r": {
"id": get_neighbor(nid, self.nid)
}
}
self.send_krpc(message, address)
except KeyError:
pass
if __name__ == "__main__":
logging.basicConfig(filename='log.log', level=logging.WARNING)
clue = Clue(UDP_IP, UDP_PORT, MAX_NODE_QSIZE)
logging.debug("initialized.")
clue.start()
logging.debug("started.")
thread = Thread(target=clue.auto_find_node)
thread.daemon = True
thread.start()
logging.debug("auto_find_node_thread started.")
while True:
i = input()
if i == 'q':
quit()