mafintosh / discovery-swarm

A network swarm that uses discovery-channel to find peers
MIT License
374 stars 57 forks source link

Cannot connect peers behind NAT #16

Open gerhardberger opened 7 years ago

gerhardberger commented 7 years ago

I'm trying to use discovery-swarm with hyperdrive. On a local network the peers successfully find each other, but if I try to connect to a peer and we both behind NAT, they won't find each other.

I use the servers used in datland-swarm-defaults.

How you supposed to connect in this case? I'm quite new to these things so maybe I'm missing something.

okdistribute commented 7 years ago

Take a look at the implementation in the 'hyperdiscovery' module, do you still have issues connecting using that?

Curious also if you can run the 'p2p-test' cli tool on your machine?

gerhardberger commented 7 years ago

As I can see hyperdiscovery is a simplification of discovery-swarm with datland-swarm-defaults. The p2p-test gave this back:

{
  time: '2017-01-07T23:52:17.908Z',
  description: '',
  holePunchable: true,
  pings: 
   [ { port: 53271, host: '79.122.53.42' },
     { port: 53271, host: '79.122.53.42' } ]
}

I found though that the issue might be that I need some sort of hole punching mechanism in order to connect, but as far as I see discovery-channel's dht and dns methods cannot do that. I need turn or stun servers for that. I see that bittorrent-tracker can do that so I was thinking about adding it to discover-channel as a third peer discovery mechanism. What are your thoughts on that?

samuelmaddock commented 6 years ago

I'm experiencing similar issues with this test script I wrote. The peer address is found through the DHT, but no connection is established.

I've been testing communication between a Windows 10 and macOS High Sierra machine.

samuelmaddock commented 6 years ago

After some more debugging, I found that remote peers only seem to connect to each other over uTP when opts.announce is passed into sw.join on both ends.

skerit commented 6 years ago

I'm still having this issue. I use both DHT & DNS (also using the datland-swarm-defaults), but clients behind NAT can't connect.

In my non-nat machine I see incoming connections that have a host like '192.168.1.60', from an outside network. So something's going wrong there.

DennisSchwartz commented 6 years ago

I have the same problem when using DAT/Hyperdrive in Docker. I have exposed the ports and peers get discovered but dropped after 4 failed connection attempts.

I have tried creating the connection manually on both ends using opts.announce but I get the same issue.

@samuelmaddock can you elaborate a little about how you solved the issue if you did?

Thanks!

samuelmaddock commented 6 years ago

@skerit @DennisSchwartz I wrote a module using discovery-swarm to connect peers to a host, swarm-peer-server. It has an echo server example you could try using to test if peers connect: https://github.com/samuelmaddock/swarm-peer-server/blob/master/examples/echo.js

Also, you should use dat-swarm-defaults, not datland-swarm-defaults.

DennisSchwartz commented 6 years ago

Awesome, thanks! I'll have a look at that.

mjp0 commented 6 years ago

I'm having same problem as @DennisSchwartz with Docker and 4 retries. I have tried few different network configurations with Docker including host option (which should in theory not have NAT problems at all but no help).

Using dat-swarm-defaults.

Has anybody got swarm working inside a Docker container?

DennisSchwartz commented 6 years ago

@0fork Sorry for committing the internet crime of not posting my solution after finding one - kinda.

First of all, I couldn't run it in a docker container locally. HOWEVER: When running the container in Kubernetes, it magically works. I didn't have time yet to properly investigate why.

So my current workaround is to just run it in our cluster, and use Minikube for local development (which is a bit of a pain tbh).

Hope this helps.

mjp0 commented 6 years ago

I wanted to link my issue here from https://github.com/mafintosh/hypercore-multicast-swarm/issues/1 multicast-swarm because it solved my NAT issue. The issue is that it supports only hypercore at the moment but I'm lacking skills to make it support hyperdb.

All help appreciated!