webtorrent/webtorrent

Enabling protocol encryption stalls downloads

anma-dev opened this issue · 14 comments

What version of this package are you using?
webtorrent version 2.1.9

What operating system, Node.js, and npm version?
The problem occurs on macOS and Linux as far as I know.
Node 19.4.0
Npm 9.7.1

What happened?
This issue is related to bittorrent protocol encryption. When enabling protocol encryption the torrent never starts downloading and it hangs indefinitely.

Steps to installation

  • npm init
  • npm install webtorrent network-address

the code:

#!/usr/bin/env node
import WebTorrent from "webtorrent";
import networkAddress from "network-address";

const torrentID = "...";
const client = new WebTorrent({
  secure: true,
});

client.on("error", (err) => {
  console.log(err);
});

function startServer(torrent) {
  if (torrent.ready) startTorrent(torrent);
  else torrent.once("ready", () => startTorrent(torrent));
}

function startTorrent(torrent) {
  // start the streaming torrent-to-http server
  let server = client.createServer();
  server.listen(0, () => {
    console.log("server listen");
    const port = server.address().port;
    const urlSuffix = ":" + port;
    const info = {
      torrentKey: torrent.key,
      localURL: "http://localhost" + urlSuffix,
      networkURL: "http://" + networkAddress() + urlSuffix,
      networkAddress: networkAddress(),
    };
    console.log(info.networkAddress);
  });
}

const torrent = client.add(torrentID);

startServer(torrent);

I think the problem is with this while loop that never ends and is always waiting for setGenerators to be set to true, which never happens: https://github.com/webtorrent/bittorrent-protocol/blob/v4.1.7/index.js#L1157

A potential solution is to change this line at https://github.com/webtorrent/webtorrent/blob/v2.1.5/lib/peer.js#L130 for:

        if (secure && this.retries === 0 && !this.sentPe1) {
          this.sendPe1();
          this.wire._determineHandshakeType() // added
      }

I'm not sure why this works but the script no longer stalls. I'm not sure how to check that encryption is actually being used after this change.

What did you expect to happen?
Specifying the secure option should enable protocol encryption without interrupting the download of torrented content.

Are you willing to submit a pull request to fix this bug?
Yes

oops.

might be my fault, i dont think we have tests for this which is an issue too

lets try reproducing this on 1.9.6, could you maybe write a test case on webtorrent 1.9.6 where this works?
specifically a test case, so 2 webtorrent instances connecting to eachother on the same script with secure, as minimal as possible, i'll try fixing it from there

Ok, I can try tomorrow. I'm not familiar with how webtorrent does testing.

here's the test https://github.com/anma-dev/webtorrent/blob/master/test/node/protocol-encryption.js

does it pass on v1.9.x and fail on v2.x.x?

this is the test for the 1.9.x version and it fails with an error: https://github.com/anma-dev/webtorrent/blob/add-pe-tests-1.9x/test/node/protocol-encryption.js

this is the test for the 2.x version and it doesn't show and error but it stalls indefinitely: https://github.com/anma-dev/webtorrent/blob/master/test/node/protocol-encryption.js

oke thanks for the tests, this is helpful af, im gonna look when i have the time

I'm happy to help :)

I opened a PR

Can you kindly link the pull @ghost

I am thinking to work on this issue in this year's hacktober fest.

Any suggestions as I am new to open source contributions.
And also is this issue taken or no one is currently working on this ?

it was worked on, but we didn't have the time to finish it, should be a simple one, see the changes and conversation in: https://github.com/webtorrent/bittorrent-protocol/pull/120/files

Okay, I have started to work on it.
Thanks.