UDP跟踪器在node.js中刮掉意外返回零

时间:2013-06-05 20:16:58

标签: javascript node.js udp bittorrent

我正在尝试使用BitTorrent UDP Tracker Protocol从公共UDP跟踪器(例如tracker.publicbt.com或tracker.openbittorrent.com)执行UDP搜索。我的应用程序向跟踪器发送一个请求以获取connection_id并使用该id执行刮擦。从跟踪器返回刮擦响应,没有错误指示数据包形成错误,但无论我使用什么info_hash,我都会返回“0”,用于播种器,leechers和已完成的数量。

我已经彻底检查了数据包是否正确,info_hash是否以正确的偏移量开始,并且数据都是正确的。据我所知,创建和发送数据包没有问题。这个问题已经开放了几天没有回复,所以我已经更新并编辑了下面的代码示例,希望有人可以提供帮助。

我已经将info_hash硬编码到以下示例中。在命令行上运行时,此代码应连接到跟踪器,获取connection_id,然后对Ubuntu torrent info_hash执行刮擦,将各种信息输出到控制台。

connection_id分为2部分,因为它是64位整数。

var dgram = require('dgram'),
    server = dgram.createSocket("udp4"),
    connectionIdHigh = 0x417,
    connectionIdLow = 0x27101980,
    transactionId,
    action,
    trackerHost = "tracker.publicbt.com",
    trackerPort = 80,
    infoHash = "",
    ACTION_CONNECT = 0,
    ACTION_ANNOUNCE = 1,
    ACTION_SCRAPE = 2,
    ACTION_ERROR = 3,
    sendPacket = function (buf, host, port) {
        "use strict";
        server.send(buf, 0, buf.length, port, host, function(err, bytes) {
            if (err) {
                console.log(err.message);
            }
        });
    },
    startConnection = function (host, port) {
        "use strict";
        var buf = new Buffer(16);

        transactionId = Math.floor((Math.random()*100000)+1);

        buf.fill(0);

        buf.writeUInt32BE(connectionIdHigh, 0);
        buf.writeUInt32BE(connectionIdLow, 4);
        buf.writeUInt32BE(ACTION_CONNECT, 8);
        buf.writeUInt32BE(transactionId, 12);

        sendPacket(buf, host, port);
    },
    scrapeTorrent = function (host, port, hash) {
        "use strict";
        var buf = new Buffer(56),
            tmp = '';

        infoHash = hash;

        if (!transactionId) {
            startConnection(host, port);
        } else {

            buf.fill(0);

            buf.writeUInt32BE(connectionIdHigh, 0);
            buf.writeUInt32BE(connectionIdLow, 4);
            buf.writeUInt32BE(ACTION_SCRAPE, 8);
            buf.writeUInt32BE(transactionId, 12);
            buf.write(infoHash, 16, buf.length);

            console.log(infoHash);
            console.log(buf.toString('utf8', 16, buf.length));

            // do scrape
            sendPacket(buf, host, port);

            transactionId = null;
            infoHash = null;
        }

    };

server.on("message", function (msg, rinfo) {
    "use strict";
    var buf = new Buffer(msg),
        seeders,
        completed,
        leechers;

    console.log(rinfo);

    action = buf.readUInt32BE(0, 4);
    transactionId = buf.readUInt32BE(4, 4);

    console.log("returned action: " + action);
    console.log("returned transactionId: " + transactionId);

    if (action === ACTION_CONNECT) {
        console.log("connect response");

        connectionIdHigh = buf.readUInt32BE(8, 4);
        connectionIdLow = buf.readUInt32BE(12, 4);

        scrapeTorrent(trackerHost, trackerPort, infoHash);

    } else if (action === ACTION_SCRAPE) {
        console.log("scrape response");

        seeders = buf.readUInt32BE(8, 4);
        completed = buf.readUInt32BE(12, 4);
        leechers = buf.readUInt32BE(16, 4);

        console.log(seeders);
        console.log(completed);
        console.log(leechers);

    } else if (action === ACTION_ERROR) {
        console.log("error response");
    }
});

server.on("listening", function () {
    "use strict";
    var address = server.address();
    console.log("server listening " + address.address + ":" + address.port);
});

server.bind();

scrapeTorrent(trackerHost, trackerPort, "335990D615594B9BE409CCFEB95864E24EC702C7");

1 个答案:

答案 0 :(得分:4)

我终于解决了这个问题,并因为没有早日实现而踢了我自己。

info_hash是十六进制编码的字符串,所以当它写入缓冲区时需要设置它的编码。例如:

buf.write(infoHash, 16, buf.length, 'hex');

UDP跟踪器协议未提及所需的编码,它只是将其描述为20字节的字符串。希望这个Q& A可以帮助遇到同样问题的其他人。