-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·34 lines (31 loc) · 953 Bytes
/
server.js
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
// 引入文件
var bencode = require("./bencode.js").bencode,
url = require('url');
// 种子列表结构体
peers = [];
require("http").createServer(function (req, res) {
var _GET = url.parse(req.url,true);
var GET = _GET.query;
res.response = function (body) {
body = bencode(body);
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8", "Content-Length": body.length});
res.end(body);
};
if(_GET.pathname == '/announce'){
var info_hash = GET.info_hash;
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if(peers[info_hash] == undefined)peers[info_hash] = [];
peers[info_hash][GET.peer_id] = {'ip' : ip, 'port' : parseInt(GET.port)};
// announce 结构体
var announce =
{
'interval' : 10,/*
'min interval' : 5,
'complete' : 1,
'incomplete' : 100,*/
'peers' : peers[info_hash]
};
res.response(announce);
}
res.end();
}).listen(80);