Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling protocol encryption stalls downloads #2594

Open
ghost opened this issue Jun 17, 2023 · 14 comments
Open

Enabling protocol encryption stalls downloads #2594

ghost opened this issue Jun 17, 2023 · 14 comments

Comments

@ghost
Copy link

ghost commented Jun 17, 2023

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

@ThaUnknown
Copy link
Member

oops.

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

@ThaUnknown
Copy link
Member

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

@ghost
Copy link
Author

ghost commented Jun 17, 2023

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

@ThaUnknown
Copy link
Member

thanks

tests are actually really simple:
https://github.com/webtorrent/webtorrent/blob/master/test/node/conn-pool.js#L7-L52

@ghost
Copy link
Author

ghost commented Jun 18, 2023

@ThaUnknown
Copy link
Member

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?

@ghost
Copy link
Author

ghost commented Jun 18, 2023

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

@ThaUnknown
Copy link
Member

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

@ghost
Copy link
Author

ghost commented Jun 18, 2023

I'm happy to help :)

@ghost
Copy link
Author

ghost commented Jul 8, 2023

I opened a PR

@NumDeP
Copy link

NumDeP commented Sep 15, 2023

Can you kindly link the pull @ghost

@sukhman-sukh
Copy link

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 ?

@ThaUnknown
Copy link
Member

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

@sukhman-sukh
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants