theobarrague / fake-bittorrent-client

A Node.js BitTorrent API implementation for testing or cheating on trackers
MIT License
1 stars 0 forks source link

Timeout fail sometimes #2

Open theobarrague opened 3 years ago

theobarrague commented 3 years ago

Orignal message from draeder in this issue

It looks like it's working for the most part. What I'm doing is building a package that uses fake-bittorrent-tracker to test the list of trackers from trackerslist. First I simply ping them and remove dead hosts. Next I use fake-bittorrent-tracker as designed to upload/download a fake torrent to the remaining hosts. I can see that the timeout is working for the majority of remaining trackers in my list.

But for some of the trackers, the timeout doesn't seem to work. This might be due to lower level networking failures, like SSL failures when trying to open the socket, or general socket failures. Here is one example where fake-bittorrent-client took well over 30 seconds to finally timeout, even with the updated code and the timeout set to 1500ms:

http://tracker4.itzmx.com:2710/announce Upload Failed Error: connect ETIMEDOUT 212.1.226.176:2710
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1137:16) {
  errno: -60,
  code: 'ETIMEDOUT',
  syscall: 'connect',
  address: '212.1.226.176',
  port: 2710
}
Error : Error: connect ETIMEDOUT 212.1.226.176:2710
http://tracker4.itzmx.com:2710/announce Download Failed Error: connect ETIMEDOUT 212.1.226.176:2710
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1137:16) {
  errno: -60,
  code: 'ETIMEDOUT',
  syscall: 'connect',
  address: '212.1.226.176',
  port: 2710
}
Error : Error: connect ETIMEDOUT 212.1.226.176:271

That particular tracker URL seems to have a variety of problems, sometimes it 404s, sometimes it has a socket reset, and other times the timeout above; so it's a good URL to test with.

draeder commented 3 years ago

Adding the following seems to speed up failed/timedout socket connection responses, per https://stackoverflow.com/a/66215639/1551027, along with keepAliveMsecs: timeout from the node documentation:

handler.Agent({keepAlive:true, keepAliveMsecs: timeout, timeout: timeout})

And also this per https://stackoverflow.com/a/53362571/1551027

maxSockets: 20

Finally putting it all together:

var options = urlToHttpOptions(new URL(url), handler.Agent({keepAlive:true, keepAliveMsecs: timeout, timeout: timeout, maxSockets:20}));

I tested it on this line:

https://github.com/theobarrague/fake-bittorrent-client/blob/572e9b8b215cc98156eac1967fb3e5d9edcba04b/src/FakeBitTorrentClient.ts#L70

Which seemed to resolve almost all of the issues I was having with ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete], but I'm stuck on one or two straggling problematic tracker servers that still cause this timeout exception and aren't honoring the timeout settings in the code.

theobarrague commented 3 years ago

Hi,

I updated my code with your suggestions and I tested with these lists.

I tested with :

$ cat ~/data/trackerslist/trackers_all_http.txt | grep -v "^$" | xargs -i node dist/cli.js --tracker {} --hash 0156fa3faf5ed9e6223a3544b3486dc5344ab9db --upload 1 --timeout 150
Uploaded 1 bytes
Error : Error: socket hang up
Error : Error: socket hang up
Uploaded 1 bytes
Error : 307
Uploaded 1 bytes
Uploaded 1 bytes
Error : 301
Error : Error: socket hang up
Uploaded 1 bytes
Error : Error: socket hang up
Uploaded 1 bytes
Uploaded 1 bytes

$ cat ~/data/trackerslist/trackers_all_https.txt | grep -v "^$" | xargs -i node dist/cli.js --tracker {} --hash 0156fa3faf5ed9e6223a3544b3486dc5344ab9db --upload 1 --timeout 150
Uploaded 1 bytes
Uploaded 1 bytes
Error : Error: socket hang up
Uploaded 1 bytes
Uploaded 1 bytes
Uploaded 1 bytes
Error : Error: socket hang up
Uploaded 1 bytes
Error : Error: socket hang up
Uploaded 1 bytes
Uploaded 1 bytes
Error : 301
Error : Error: socket hang up

I tried to simulate a non responding tracker with nc -l 1234 and I got this error after 2.5 sec :

$ node dist/cli.js --tracker http://127.0.0.1:1234 --hash 0156fa3faf5ed9e6223a3544b3486dc5344ab9db --upload 1 --timeout 2500
Error : Error: socket hang up

It seems to works. Try to reproduce your problems and if they are still present send me the link of your tracker that I will try at home.

draeder commented 3 years ago

I haven't been running into any other issues. I think the ones I was having problems with were because I was converting anything from ws/wss to http/https and trying to test those. I am now using a websockets client in node that can test basic connectivity. If you have any thoughts about attempting to upload/download torrents using this websockets client module, that would be appreciated. I'm not sure how to go about constructing the torrent / requests using websockets.

theobarrague commented 3 years ago

Can't look for websockets this week but I will have free time next week :)