Merge pull request #64 from libp2p/feat/autonat

AutoNAT service and flag to specify host listen addrs
This commit is contained in:
vyzo 2019-02-07 14:08:59 +02:00 committed by GitHub
commit e4a1b9c90c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 8 deletions

View File

@ -7,6 +7,7 @@ import (
logging "github.com/ipfs/go-log"
libp2p "github.com/libp2p/go-libp2p"
autonat "github.com/libp2p/go-libp2p-autonat-svc"
host "github.com/libp2p/go-libp2p-host"
dht "github.com/libp2p/go-libp2p-kad-dht"
dhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
@ -25,8 +26,9 @@ type Daemon struct {
host host.Host
listener manet.Listener
dht *dht.IpfsDHT
pubsub *ps.PubSub
dht *dht.IpfsDHT
pubsub *ps.PubSub
autonat *autonat.AutoNATService
mx sync.Mutex
// stream handlers: map of protocol.ID to multi-address
@ -113,6 +115,12 @@ func (d *Daemon) EnablePubsub(router string, sign, strict bool) error {
}
func (d *Daemon) EnableAutoNAT(opts ...libp2p.Option) error {
svc, err := autonat.NewAutoNATService(d.ctx, d.host, opts...)
d.autonat = svc
return err
}
func (d *Daemon) ID() peer.ID {
return d.host.ID()
}

View File

@ -44,6 +44,8 @@ func main() {
relayHop := flag.Bool("relayHop", false, "Enables hop for relay")
relayDiscovery := flag.Bool("relayDiscovery", false, "Enables passive discovery for relay")
autoRelay := flag.Bool("autoRelay", false, "Enables autorelay")
autonat := flag.Bool("autonat", false, "Enables the AutoNAT service")
hostAddrs := flag.String("hostAddrs", "", "comma separated list of multiaddrs the host should listen on")
flag.Parse()
var opts []libp2p.Option
@ -62,6 +64,11 @@ func main() {
opts = append(opts, libp2p.Identity(key))
}
if *hostAddrs != "" {
addrs := strings.Split(*hostAddrs, ",")
opts = append(opts, libp2p.ListenAddrStrings(addrs...))
}
if *connMgr {
cm := connmgr.NewConnManager(*connMgrLo, *connMgrHi, *connMgrGrace)
opts = append(opts, libp2p.ConnectionManager(cm))
@ -71,12 +78,18 @@ func main() {
opts = append(opts,
libp2p.DefaultTransports,
libp2p.Transport(quic.NewTransport),
libp2p.ListenAddrStrings(
"/ip4/0.0.0.0/tcp/0",
"/ip4/0.0.0.0/udp/0/quic",
"/ip6/::1/tcp/0",
"/ip6/::1/udp/0/quic",
))
)
// if we explicitly specify a transport, we must also explicitly specify the listen addrs
if *hostAddrs == "" {
opts = append(opts,
libp2p.ListenAddrStrings(
"/ip4/0.0.0.0/tcp/0",
"/ip4/0.0.0.0/udp/0/quic",
"/ip6/::1/tcp/0",
"/ip6/::1/udp/0/quic",
))
}
}
if *natPortMap {
@ -112,6 +125,21 @@ func main() {
log.Fatal(err)
}
if *autonat {
var opts []libp2p.Option
// allow the AutoNAT service to dial back quic addrs.
if *QUIC {
opts = append(opts,
libp2p.DefaultTransports,
libp2p.Transport(quic.NewTransport),
)
}
err := d.EnableAutoNAT(opts...)
if err != nil {
log.Fatal(err)
}
}
if *pubsub {
if *gossipsubHeartbeatInterval > 0 {
ps.GossipSubHeartbeatInterval = *gossipsubHeartbeatInterval

View File

@ -38,6 +38,12 @@
"hash": "QmZcLBXKaFe8ND5YHPkJRAwmhJGrVsi1JqDZNyJ4nRK5Mj",
"name": "go-multiaddr-net",
"version": "1.7.1"
},
{
"author": "vyzo",
"hash": "QmRmMbeY5QC5iMsuW16wchtFt8wmYTv2suWb8t9MV8dsxm",
"name": "go-libp2p-autonat-svc",
"version": "1.0.5"
}
],
"gxVersion": "0.12.1",