我正在尝试为学校项目制作一个简单的 BitTorrent 跟踪器。它现在完全被破解了,但我找不到哪里出错了。我想知道我是否对服务器响应应该是什么有误解。我正在使用 node.js 和 express。
服务器接收带有 ?info_hash 数据的 /GET 请求没有问题。而且我能够将该信息保存到 JSON 文件中。服务器还能够使用编码响应客户端。响应是一个字典,它有一个间隔和一个对等列表。列表里面有几个字典,每个字典都包含一个对等点的 ip 和端口。
目前,虽然没有一个对等方将相互连接。我将把我的笔记本电脑放在与我的桌面不同的网络上,它会将桌面视为潜在的对等点、正确的 ip 和端口(据我所知),但过了一会儿它就会从对等点列表中删除。我在每个客户端上都使用 deluge 和 qBitTorrent。
这是应用程序的代码:
var express = require('express');
var app = express();
var fs = require("fs");
var contents = fs.readFileSync("data.json");
var data = JSON.parse(contents);
var findTorrent = function(data, hash) {
for(var i = 0; i < data.length; i++) {
if(data[i].info_hash === hash) {
return data[i];
}
}
return false;
}
var findID = function(data, qPort, qip) {
for(var i = 0; i < data.length; i++) {
//console.log(data[i].peer_id);
if(data[i].port == qPort && data[i].ip === qip) {
return true;
}
}
return false;
}
var findHash = function(data, id) {
for(var i = 0; i < data.length; i++) {
if(data[i].peer_id === id) {
return data[i];
}
}
return false;
}
function hashy (str) {
var url = str;
var hexval = '';
for(var i = 0; i < url.length; i++) {
if(url[i] !== '%') {
var code = url.charCodeAt(i);
var hex = code.toString(16);
hexval += hex;
} else {
hexval += url[i+1] + url[i+2];
i += 2;
}
}
return hexval;
}
app.get('/', function(req, res) {
console.log(req.query);
var info_hash = hashy(req.query.info_hash);
console.log(info_hash);
var peer_id = decodeURIComponent(req.query.peer_id);
var escaped = escape(req.query.peer_id);
console.log('escaped ' + escaped);
console.log('decoded ' + peer_id);
console.log('normal ' + req.query.peer_id);
var ip = req.connection.remoteAddress;
if(ip.substring(0,7) == '::ffff:') {
ip = ip.substring(7);
}
//var port = req.connection.remotePort;
var port = req.query.port;
console.log(ip);
var torrent = findTorrent(data, info_hash);
var completed;
if (torrent === false){
if(req.query.left === '0') {
completed = true;
} else {
completed = false;
}
var obj = { "info_hash" : info_hash, "peers" : [{ "peer_id" : peer_id, "ip" : ip, "port" : port, "completed" : completed }]};
data.push(obj);
torrent = obj;
//console.log(obj.peers);
}
else {
//figure out if completed
if(req.query.left == '0') {
completed = true;
} else {
completed = false;
}
var peer = findHash(torrent.peers, peer_id);
if(peer === false){
var obj = { "peer_id" : peer_id, "ip" : ip, "port" : port, "completed" : completed };
torrent.peers.push(obj);
}
else {
peer.ip = ip;
peer.port = port;
peer.completed = completed;
}
}
if(torrent) {
var response = bencode(torrent);
}
else {
response = 'error';
}
//console.log(data);
fs.writeFileSync("data.json", JSON.stringify(data, null, 2), 'utf-8');
res.send(response);
});
var bencode = function(torrent) {
var response = 'd8:intervali600e12:min intervali30e'
var complete = 0;
var incomplete = 0;
for(var i = 0; i < torrent.peers.length; i++) {
if(torrent.peers[i].completed === true) {
complete++;
} else {
incomplete++;
}
}
var response = response.concat('8:completei' + complete + 'e');
var response = response.concat('10:incompletei' + incomplete + 'e5:peersl');
for(var i = 0; i < torrent.peers.length; i++) {
response = response.concat('d');
response = response.concat('2:ip');
response = response.concat(torrent.peers[i].ip.length + ':');
response = response.concat(torrent.peers[i].ip);
//response = response.concat('7:peer id');
//response = response.concat(torrent.peers[i].peer_id.length + ':');
//response = response.concat(torrent.peers[i].peer_id);
response = response.concat('4:port');
response = response.concat('i' + torrent.peers[i].port + 'e');
response = response.concat('e');
}
response = response.concat('ee');
console.log(response);
return response;
}
app.listen(4000, function() {
console.log('Example app listening on port 4000!');
});
我能够连接到托管在 Amazon AWS 上的跟踪器,并且 qBitTorrent 将其报告为“工作”。我还可以看到 GET 请求发出,服务器响应通过 wireshark 进入。该请求具有以下经过编码的字符串,我认为这是必要的:
d8:intervali600e12:min intervali30e8:completei2e10:incompletei3e5:peersld2:ip13:73.66.138.2174:porti8999eed2:ip13:73.66.138.2174:porti63014eed2:ip13:73.66.138.2174:porti8999eed2:ip13:73.25.106.1804:porti6881eed2:ip13:73.66.249.1414:porti8999eeee
根据 www.bittorrent.org 的说法,响应中所需的只是一个间隔和一个映射到对等点列表的对等点列表。每个对等点都需要 id、ip 和端口。
我已将端口切换到客户端在请求中报告的端口,并确保我的 torrent 客户端已转发它的端口并且它现在似乎正在工作。虽然我仍然会继续努力。目前,当他们停止播种/吸血时,我没有办法删除同行。