-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrbdht.rb
266 lines (212 loc) · 5.09 KB
/
rbdht.rb
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
require File.expand_path(File.dirname(__FILE__) + '/peer')
require File.expand_path(File.dirname(__FILE__) + '/bucketset')
require File.expand_path(File.dirname(__FILE__) + '/utils')
require "socket"
require 'ipaddr'
require 'thread'
class RBDht
public
def initialize(host = "0.0.0.0", port = 9002, id = nil)
BasicSocket.do_not_reverse_lookup = true
@@hook = Hash.new
@@lock = Mutex.new
@@sock = UDPSocket.open
@@sock.bind(host, port)
if id != nil then
@id = strToHex(id)
else
@id = randId
end
@@bucketset = BucketSet.new
end
def bootstrap(host, port, id = "bootstrap", target_id = nil )
if target_id == nil then
target_id = @id
else
target_id = strToHex(target_id)
end
peer = Peer.new(host, port)
if id != "bootstrap" then
@@bucketset.insert(id, peer)
end
peer.find_node(@@sock, target_id, @id, @@lock, "bootstrap")
end
def run
Thread.new {
recv
}.join
end
def setHook(msgType, func)
if @@hook[msgType] == nil then
@@hook[msgType] = []
end
@@hook[msgType].insert(-1, func)
end
private
def recv
loop do
data = nil
@@lock.synchronize do
ready = IO.select([@@sock], nil, nil, 1)
if not ready then next end
readable = ready[0]
readable.each do |sock|
#data = sock.recvfrom(2048)
begin
data = sock.recvfrom_nonblock(2048)
rescue
puts "error:#{$!} at:#{$@}"
ensure
next
end
end
end
#防止出现锁嵌套造成的死锁
if data != nil then
handle(data)
end
end
end
def find_nodeHandle(ret, session)
nodes = ret['r']['nodes']
if nodes == nil then return end
nodes = hexToStr(nodes)
nnodes = nodes.length/(26*2)
nodeset = []
nnodes.times do |i|
node = getNodes(nodes[i * 26*2 , 26*2])
if @@bucketset.getlength < 1000 then
bootstrap(node[1], node[2])
end
node[2] = node[2].to_s
nodeset.insert(-1, node)
end
hookFunc = @@hook["find_node"]
if hookFunc != nil then
hookFunc.each do
|func|
func(session[3], session[1].to_s, nodeset)
end
end
end
def get_peerHandle(peer, ret)
token = ret['r']['token']
peer.setToken(token)
values = ret['r']['values']
values.each{|v|
p v
}
end
def repsondHandle(ret, session)
puts ("repsond " + session[3] + ":" + session[1] .to_s)
id = ret['r']["id"]
id = hexToStr(id)
peer = nil
if @@bucketset.hasKey(id) then
peer = @@bucketset.getPeer(id)
peer.setLastTime
else
peer = Peer.new(session[3], session[1])
@@bucketset.insert(id, peer)
end
type = nil
if ret['t'] == "bootstrap" then
type = {}
type["name"] = "find_node"
else
type = peer.get_trans(ret['t'])
end
if type != nil then
if type["name"] == "ping" then
#do setLasetTime
hookFunc = @@hook["ping"]
if hookFunc != nil then
hookFunc.each do
|func|
func(session[3], session[1] .to_s)
end
end
end
if type["name"] == "find_node" then
p "find_node respond"
find_nodeHandle(ret, session)
end
if type["name"] == "get_peer" then
p "get_peer respond"
get_peerHandle(peer, ret)
end
end
end
def erroHandle(ret, session)
puts ("erro")
end
def requestHanel(ret, session)
puts ("request")
type = ret["q"]
id = ret["a"]['id']
id = hexToStr(id)
trans_id = ret["t"]
if trans_id == nil or id == nil then return end
peer = nil
if @@bucketset.hasKey(id) then
peer = @@bucketset.getPeer(id)
peer.setLastTime
else
peer = Peer.new(session[3], session[1])
@@bucketset.insert(id, peer)
end
if type == "ping" then
peer.pong(@@sock, trans_id, @id, @@lock)
p "ping request"
elsif type == "find_node" then
nodes = @@bucketset.getnodes
peer.fond_node(@@sock, nodes, trans_id, @id, @@lock)
# peer.pong(sock, trans_id, @id, @@lock)
p "find_node request"
elsif type == "get_peers" then
infoHash = ret["a"]['info_hash']
#p "get_peers request"
nodes = @@bucketset.getnodes
token = get_token
peer.got_peers(@@sock, trans_id, @id , token, nil, nodes, @@lock)
if infoHash != nil then
hookFunc = @@hook["got_peers"]
if hookFunc != nil then
hookFunc.each do
|func|
func(session[3], session[1].to_s, hexToStr(infoHash) )
end
end
end
elsif type == "announce_peer" then
p "announce_peer request"
else puts "unkown info type"
end
end
def handle(data)
ret = Bdecode.new.decode(data[0])
if ret == nil then return end
if ret['v'] != nil then p "the clients version is " + ret['v'] end
type = ret["y"]
if type == "r" then repsondHandle(ret, data[1])
elsif type == "e" then erroHandle(ret, data[1])
elsif type == "q" then requestHanel(ret, data[1])
else puts "unkown info type"
end
end
def getNodes(data)
peer = data[0, 20*2]
host = data[20*2, 4*2].hex
port = data[20*2 + 4*2 , 2*2].hex
host = IPAddr.new(host , Socket::AF_INET)
return [peer, host.to_s, port]
end
def randId
arr = []
20.times do
|i|
arr[i] = rand(0xFF)
end
return arr.pack("C*")
end
end