Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
bittorrent-wire
Advanced tools
One of the fastest, lightest, and smartest bittorrent wires yet.
streams | protocol |
---|---|
Topic | Source |
---|---|
Extension | BEP_0010 |
UT_PEX | BEP_0011 |
UT_METADATA | BEP_0009 |
module | tests | version | description |
---|---|---|---|
ut-extensions | Extensions for The Bittorent Protocol |
npm install bittorrent-wire
Basic
import Wire from "bittorrent-wire"
let wire = new Wire("INFO_HASH_GOES_HERE", "PEER_ID_GOES_HERE");
wire.pipe(wire);
Online
import * as net from 'net';
import Wire from "bittorrent-wire"
let socket = net.connect(1337, 'localhost');
socket.once('connect', () => {
let wire = new Wire("e940a7a57294e4c98f62514b32611e38181b6cae", "-EM0022-PEANUTS4AITH");
socket.pipe(wire).pipe(socket);
wire.on("handshake", (infoHash, peerID) => {
// ...
});
});
wire.sendHandshake();
wire.on("handshake", function (infoHash: Buffer, peerId: Buffer) {
// infoHash -> 20 bit hex buffer
// peerId -> 20 bit utf-8 buffer
});
wire.sendHandshake();
wire.sendNotInterested();
wire.on("close", () => {
// wire.isActive -> false
});
wire.sendHandshake();
wire.sendInterested();
wire.on("interested", () => {
// wire.choked -> false
});
wire.sendHandshake();
wire.sendHave(1); //Index number
wire.on("have", (index: number) => {
// index -> 1
});
let buffer = Buffer.from("40", "hex");
wire.sendHandshake();
wire.sendBitfield(buffer); // Hex Buffer
wire.on("bitfield", (bits) => {
// bits.toString("hex") -> 40
});
const TPH = require("torrent-piece-handler");
const files = [ { path: "Downloads/lol1/1.png",
name: "1.png",
length: 255622,
offset: 0 },
{ path: "Downloads/lol2/2.png",
name: "2.png",
length: 1115627,
offset: 255622 } ];
// ...
wire.sendHandshake();
wire.sendInterested();
// Prepare a request for the pieces
let tph = new TPH.default(files, 962416635, 1048576, 918, 872443);
tph.prepareRequest(0, (buf, count) => {
// Send to wire.
wire.sendRequest(buf, count);
});
wire.on("request", () => {
// wire.inRequests[2].index -> 0
// wire.inRequests[2].begin -> 16384 * 2
// wire.inRequests[2].length -> 16384
});
wire.sendHandshake();
wire.sendInterested();
wire.sendCancel(0, 16384 * 2, 16384); //Index number
wire.on("cancel", (index, begin, length) => {
// index -> 0
// begin -> 16384 * 2
// length -> 16384
});
new Wire(infoHash: string | Buffer, myID: string | Buffer, options?: Options)
"e940a7a57294e4c98f62514b32611e38181b6cae"
"-EM0022-PEANUTS4AITH"
interface Options {
"metadata_handshake": MetadataHandshake;
}
interface MetadataHandshake {
"ipv4": Buffer;
"ipv6": Buffer;
"m": Extension;
"metadata_size": number;
"p": number;
"reqq": number;
"v": Buffer;
"yourip": Buffer;
}
interface Extension {
"ut_pex": number;
"ut_metadata": number;
}
<pstrlen><pstr><reserved><info_hash><peer_id>
The first step in starting a channel with another peer
<len=0001><id=2>
This will request unchoking the connection
<len=0001><id=3>
If the peer does not have data we need
<len=0005><id=4><piece index>
index
the block in question [e.g. 0, 1, 2, 3, ...]<len=0001+X><id=5><bitfield>
bitfield
string representing a hex bitfield [e.g. "f8"] [binary: 1111 1000]<len=0013><id=6><index><begin><length>
payload
wrap the index, begin, and length all into one buffercount
number of pieces you are requesting [e.g. 0, 1, 2, 3, ...]<len=0009+X><id=7><index><begin><block>
piece
wrap the index, begin, and length all into one buffer<len=0013><id=8><index><begin><length>
index
index of the block [e.g. 0, 1, 2, 3, ...]begin
number inside the block [e.g. 0 * 16384, 1 * 16384, 2 * 16384, ...]<len=0003><id=9><listen-port>
port
number of the UDP port the DHT is listening onut_metadata
2ut_pex
1this.createUTmetadata(metadataSize, torrentInfo)
this.createUTpex()
this.ext[UT_METADATA] = new UTmetadata(metadataSize, this.infoHash, torrentInfo)
this.ext[UT_PEX] = new UTpex()
addPeers
ipv4 peers that you know are goodaddPeers6
ipv6 peers that you know are gooddropPeers
ipv4 peers that you know are baddropPeers6
ipv6 peers that you know are badreturn this.choked
return this.busy
this.busy = true
this.busy = false
this.isActive = false
this.emit("close")
this.meta = false
delete this.ext[ UT_METADATA ]
delete this.ext[ UT_PEX ]
DEBUG="bittorrent-wire" ts-node bittorrent-wire.ts
ISC License (ISC) Copyright 2017 Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC") Copyright (c) 1995-2003 by Internet Software Consortium
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
FAQs
A stream ready wire for the Bittorrent Protocol
We found that bittorrent-wire demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.