我正在尝试创建一个种子搜索引擎。但我找到了一种从 DHT 网络收集 torrent infohashes 的方法。我的问题是如何从中提取 .torrent
文件?我有一个 MongoDB
集合,其中包含 1M-1.5M(增长非常快)
torrent 信息哈希。
我自己解决了这个问题。我使用了 webtorrent npm 包,还创建了一个算法来循环整个数据库,并在下载任务中添加了磁力链接。一旦客户端获得元数据,我就将其保存到 torrent 文件并取消下载。
嗯,该代码尚未完全准备好投入生产。我稍后会在这里发布代码片段。谢谢!
更新:我正在使用此类从磁铁下载 Torrent 文件
const Discovery = require('torrent-discovery');
const Protocol = require('bittorrent-protocol');
const ut_metadata = require('ut_metadata');
const addrToIPPort = require('addr-to-ip-port');
const net = require('net');
class TorrentDownloader {
constructor(port, trackers, timeout) {
this.SELF_HASH = '4290a5ff50130a90f1de64b1d9cc7822799affd5';
this.port = port | 6881;
this.trackers = trackers;
this.timeout = timeout | 80000;
}
downloadTorrent(infoHash) {
let self = this;
return new Promise((resolve, reject) => {
let dis = new Discovery({infoHash: infoHash, peerId: this.SELF_HASH, port: this.port, dht: true, announce: this.trackers})
.on('peer', function (peer) {
const peerAddress = {address: addrToIPPort(peer)[0], port: addrToIPPort(peer)[1]};
// console.log(`download metadata from peer ${peerAddress.address}:${peerAddress.port}`);
self.getMetadata(peerAddress, infoHash, resolve);
});
setTimeout(() => {
dis.destroy();
reject(new Error("Torrent Timeout"))
}, this.timeout)
})
}
getMetadata(peerAddress, infoHash, resolve) {
const socket = new net.Socket();
socket.setTimeout(this.timeout);
socket.connect(peerAddress.port, peerAddress.address, () => {
const wire = new Protocol();
socket.pipe(wire).pipe(socket);
wire.use(ut_metadata());
wire.handshake(infoHash, this.SELF_HASH, {dht: true});
wire.on('handshake', function (infoHash, peerId) {
wire.ut_metadata.fetch();
});
wire.ut_metadata.on('metadata', function (rawMetadata) {
resolve(rawMetadata);
wire.destroy();
socket.destroy()
})
});
socket.on('error', err => {
socket.destroy();
});
}
}
module.exports = TorrentDownloader;
我是一名优秀的程序员,十分优秀!