-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdht.php
More file actions
395 lines (346 loc) · 9.46 KB
/
dht.php
File metadata and controls
395 lines (346 loc) · 9.46 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
// 文件路径
define('ABSPATH', dirname(__FILE__));
// 主进程数, 一般为CPU的1至4倍
define('WORKER_NUM', 2);
// 允许最大连接数, 不可大于系统ulimit -n的值
define('MAX_REQUEST', 1000);
// 线程数
define('MAX_PROCESS', 10);
// 自动查找间隔, 单位为毫秒
define('AUTO_FIND_TIME', 10000);
// 发送find_node间隔, 单位秒
define('NEXT_FIND_NODE_TIME', 0.5);
// 载入类文件
require_once ABSPATH . '/inc/Node.class.php';
require_once ABSPATH . '/inc/Bencode.class.php';
require_once ABSPATH .'/inc/Base.class.php';
// 保存swoole_server对象
$serv = NULL;
// 设置自身node id
$nid = Base::get_node_id();
// 初始化路由器
$table = array();
// 最后请求时间
$last_find = time();
// 保存线程列表
$threads = [];
// 长期在线node
$bootstrap_nodes = array(
array('router.bittorrent.com', 6881),
array('dht.transmissionbt.com', 6881),
array('router.utorrent.com', 6881)
);
write(date('Y-m-d H:i:s', time()) . " - 服务启动...\n");
$serv = new swoole_server('0.0.0.0', 6882, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
$serv->set(array(
'worker_num' => WORKER_NUM,
'daemonize' => true,
'max_request' => MAX_REQUEST,
'dispatch_mode' => 2,
'log_file' => ABSPATH . 'error.log'
));
$serv->on('WorkerStart', function($serv, $worker_id){
// 添加一个定时器, 使服务器定时寻找节点
$serv->addtimer(AUTO_FIND_TIME);
auto_find_node();
});
$serv->on('Receive', function($serv, $fd, $from_id, $data){
// 检查数据长度
if(strlen($data) == 0)
return false;
// 对数据进行解码
$msg = Base::decode($data);
// 获取对端链接信息, udp链接需要加上$from_id参数
$fdinfo = $serv->connection_info($fd, $from_id);
// 对接收到的数据进行类型判断
if($msg['y'] == 'r'){
// 如果是回复, 且包含nodes信息
if(array_key_exists('nodes', $msg['r']))
// 对nodes进行操作
response_action($msg, array($fdinfo['remote_ip'], $fdinfo['remote_port']));
}elseif($msg['y'] == 'q'){
// 如果是请求, 则执行请求判断
request_action($msg, array($fdinfo['remote_ip'], $fdinfo['remote_port']));
}else{
return false;
}
});
$serv->on('Timer', function($interval){
for($i=0; $i<MAX_PROCESS; $i++){
$process = new swoole_process(function(){
auto_find_node();
});
$pid = $process->start();
$threads[$pid] = $process;
swoole_process::wait();
}
});
$serv->start();
/**
* 自动查找节点方法, 将在DHT网络中自动搜寻节点信息
* @return void
*/
function auto_find_node(){
global $table;
// 如果路由表中没有数据则先加入DHT网络
if(count($table) == 0)
return join_dht();
// 循环处理路由表
while(count($table)){
// 从路由表中删除第一个node并返回被删除的node
$node = array_shift($table);
// 发送查找find_node到node中
find_node(array($node->ip, $node->port), $node->nid);
sleep(0.005);
}
}
/**
* 加入dht网络
* @return void
*/
function join_dht(){
global $table, $bootstrap_nodes;
// 循环操作
foreach($bootstrap_nodes as $node){
// 将node域名解析为IP地址, 并发送find_node请求
find_node(array(gethostbyname($node[0]), $node[1]));
}
}
/**
* 发送find_node请求
* @param array $address 对端链接信息
* @param string $id node id
* @return void
*/
function find_node($address, $id = null){
global $nid, $table;
// 若未指定id则使用自身node id
if(is_null($id))
$mid = $nid;
else
// 否则伪造一个相邻id
$mid = Base::get_neighbor($id, $nid);
// 定义发送数据
$msg = array(
't' => Base::entropy(2),
'y' => 'q',
'q' => 'find_node',
'a' => array(
'id' => $nid,
'target' => $mid
)
);
// 发送请求数据到对端
send_response($msg, $address);
}
/**
* 处理对端发来的请求
* @param array $msg 接收到的请求数据
* @param array $address 对端链接信息
* @return void
*/
function request_action($msg, $address){
switch($msg['q']){
case 'ping':
on_ping($msg, $address);
break;
case 'find_node':
on_find_node($msg, $address);
break;
case 'get_peers':
// 处理get_peers请求
on_get_peers($msg, $address);
break;
case 'announce_peer':
// 处理announce_peer请求
on_announce_peer($msg, $address);
break;
default:
return false;
}
}
/**
* 处理接收到的find_node回复
* @param array $msg 接收到的数据
* @param array $address 对端链接信息
* @return void
*/
function response_action($msg, $address){
// 先检查接收到的信息是否正确
if(!isset($msg['r']['nodes']) || !isset($msg['r']['nodes'][1]))
return false;
// 对nodes数据进行解码
$nodes = Base::decode_nodes($msg['r']['nodes']);
// 对nodes循环处理
foreach($nodes as $node){
// 将node加入到路由表中
append($node);
}
}
/**
* 处理ping请求
* @param array $msg 接收到的ping请求数据
* @param array $address 对端链接信息
* @return void
*/
function on_ping($msg, $address){
global $nid;
// 获取对端node id
$id = $msg['a']['id'];
// 生成回复数据
$msg = array(
't' => $msg['t'],
'y' => 'r',
'r' => array(
'id' => $nid
)
);
// 将node加入路由表
append(new Node($id, $address[0], $address[1]));
// 发送回复数据
send_response($msg, $address);
}
/**
* 处理find_node请求
* @param array $msg 接收到的find_node请求数据
* @param array $address 对端链接信息
* @return void
*/
function on_find_node($msg, $address){
global $nid;
// 获取node列表
$nodes = get_nodes(16);
// 获取对端node id
$id = $msg['a']['id'];
// 生成回复数据
$msg = array(
't' => $msg['t'],
'y' => 'r',
'r' => array(
'id' => $nid,
'nodes' => Base::encode_nodes($nodes)
)
);
// 将node加入路由表
append(new Node($id, $address[0], $address[1]));
// 发送回复数据
send_response($msg, $address);
}
/**
* 处理get_peers请求
* @param array $msg 接收到的get_peers请求数据
* @param array $address 对端链接信息
* @return void
*/
function on_get_peers($msg, $address){
global $nid;
// 获取info_hash信息
$infohash = $msg['a']['info_hash'];
// 获取node id
$id = $msg['a']['id'];
// 生成回复数据
$msg = array(
't' => $msg['t'],
'y' => 'r',
'r' => array(
'id' => $nid,
'nodes' => Base::encode_nodes(get_nodes()),
'token' => substr($infohash, 0, 2)
)
);
// 将node加入路由表
append(new Node($id, $address[0], $address[1]));
// 向对端发送回复数据
send_response($msg, $address);
}
/**
* 处理announce_peer请求
* @param array $msg 接收到的announce_peer请求数据
* @param array $address 对端链接信息
* @return void
*/
function on_announce_peer($msg, $address){
global $nid;
// 获取infohash
$infohash = $msg['a']['info_hash'];
// 获取token
$token = $msg['a']['token'];
// 获取node id
$id = $msg['a']['id'];
// 验证token是否正确
if(substr($infohash, 0, 2) == $token){
/*$txt = array(
'action' => 'announce_peer',
'msg' => array(
'ip' => $address[0],
'port1' => $address[1],
'port2' => $msg['a']['port'],
'infohash' => $infohash
)
);
var_dump($txt);*/
write(date('Y-m-d H:i:s', time()) . " 获取到info_hash: " . strtoupper(bin2hex($infohash)) . "\n");
}
// 生成回复数据
$msg = array(
't' => $msg['t'],
'y' => 'r',
'r' => array(
'id' => $nid
)
);
// 发送请求回复
send_response($msg, $address);
}
/**
* 向对端发送数据
* @param array $msg 要发送的数据
* @param array $address 对端链接信息
* @return void
*/
function send_response($msg, $address){
global $serv;
$serv->sendto($address[0], $address[1], Base::encode($msg));
}
/**
* 添加node到路由表
* @param Node $node node模型
* @return boolean 是否添加成功
*/
function append($node){
global $nid, $table;
// 检查node id是否正确
if(!isset($node->nid[19]))
return false;
// 检查是否为自身node id
if($node->nid == $nid)
return false;
// 检查node是否已存在
if(in_array($node, $table))
return false;
// 如果路由表中的项达到200时, 删除第一项
if(count($table) >= 200)
array_shift($table);
return array_push($table, $node);
}
function get_nodes($len = 8){
global $table;
if(count($table) <= $len)
return $table;
$nodes = array();
for($i=0; $i<$len; $i++){
$nodes[] = $table[mt_rand(0, count($table) - 1)];
}
return $nodes;
}
/**
* 将数据写入文件
* @param string $msg 要写入文件的数据
* @return void
*/
function write($msg){
$fp = fopen('./infohash.log', 'ab');
fwrite($fp, $msg);
fclose($fp);
}