libp2p / go-libp2p

libp2p implementation in Go
MIT License
6.12k stars 1.08k forks source link

Chat Example #311

Closed agahEbrahimi closed 6 years ago

agahEbrahimi commented 6 years ago

Hello,

I was wondering how I would be able to give a non local IP to the chat example. When running the executable, a port is given to it, but how would I be able to set it so that it works with the public IP and not the local IP (127.0.0.1).

Thank you in advance, Agah Ebrahimi

Stebalien commented 6 years ago

The chat example actually listens on 0.0.0.0 (the global ip4 address) by default.

agahEbrahimi commented 6 years ago

but that's not the case for the sender, is it? wouldn't I have to set the public address for the sender address if I want to communicate over the internet?

Stebalien commented 6 years ago

@agahEbrahimi yes. You can do that by specifying ./chat -d /ip4/0.0.0.0/tcp/3001 (the all address).

agahEbrahimi commented 6 years ago

Hmm, it doesn't seem to be working, it gives protocol not found in multiaddr as an error.

upperwal commented 6 years ago

0.0.0.0 in the code means listen to all IPv4 addresses (ie. accept connects from all)

Try this if you want to run it on a public network.

Simple Case: Atleast one public IP

Run this on a system with public IP address:

./chat -sp 3000

It should output something like this Run './chat -d /ip4/127.0.0.1/tcp/3000/ipfs/<node_id>' on another console.

Now replace 127.0.0.1 with the public IP of the previous system and execute

./chat -d /ip4/<public_ip>/tcp/3000/ipfs/<node_id>

on any system (public or private ip)

Not so simple case: Different private networks This example does not support this. You need to enable discovery and port mapping for this.

agahEbrahimi commented 6 years ago

I'm not requesting anything but having a chat example with both discovery and port mapping would be awesome (just as a suggestion).

Stebalien commented 6 years ago

I'm currently working on a refactor that should make setting up port mapping easier. Once that gets merged, we can improve the examples.

agahEbrahimi commented 6 years ago

Awesome :)

upperwal commented 6 years ago

@Stebalien Would it be possible to share these changes? It would also be great if more NAT traversal techniques (NAT hole punching and STUN) are added to libp2p-nat. Let me know what do you think about it.

Stebalien commented 6 years ago

Actually, I forgot. @jvsteiner already added a nice option for configuring NAT port mapping. Pass the NATPortMap() option to libp2p.New(...).


We actually have three nat-traversal solutions at the moment:

  1. UPnP (NATPortMap): We try to ask the router to open a port for us.
  2. Custom hole-punching/STUN through a combination of peer-routing, external-address/port discovery, and reuseport (we try to use one local port for all inbound and outbound connections).
  3. Custom TURN (in-progress) through go-libp2p-circuit. Unfortunately, we don't currently advertise relayed addresses by default but we're working on that.
jvsteiner commented 6 years ago

FYI, I'm guessing one probably needs to use the routed host for some of those NAT techniques to work.

upperwal commented 6 years ago

@Stebalien Is NATPortMap not deprecated. https://github.com/libp2p/go-libp2p/blob/3b8f2275a2b24de5777aaa4fc77f1af57c5c4387/p2p/host/basic/basic_host.go#L50-L51

libp2p uses https://github.com/fd/go-nat repo for NAT and I could only find NAT UPnP and NAT-PMP, could you please point me to STUN implementation used in libp2p.

upperwal commented 6 years ago

@Stebalien I think https://github.com/libp2p/go-reuseport is the repo for STUN but its not being used in libp2p.

Stebalien commented 6 years ago

@upperwal

Is NATPortMap not deprecated.

That option is (technically) deprecated but NATPortMap isn't. That comment is just telling you to configure the nat manager using the HostOpts instead of passing that option to NewHost.

@Stebalien I think https://github.com/libp2p/go-reuseport is the repo for STUN but its not being used in libp2p.

It's used by go-tcp-transport which is used by go-libp2p-swarm for TCP connections.

upperwal commented 6 years ago

@Stebalien Ok. Got it. Thanks.