diff --git a/daemon.go b/daemon.go index 37cc035..e4f38ce 100644 --- a/daemon.go +++ b/daemon.go @@ -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() } diff --git a/p2pd/main.go b/p2pd/main.go index e2fea02..cfef8c6 100644 --- a/p2pd/main.go +++ b/p2pd/main.go @@ -44,6 +44,7 @@ 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") flag.Parse() var opts []libp2p.Option @@ -112,6 +113,20 @@ func main() { log.Fatal(err) } + if *autonat { + var opts []libp2p.Option + 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 diff --git a/package.json b/package.json index 90686e9..4c13e25 100644 --- a/package.json +++ b/package.json @@ -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",