flag to enable the autonat service

This commit is contained in:
vyzo 2019-02-07 11:55:20 +02:00
parent 7b998143ea
commit 353f81ce6c
3 changed files with 31 additions and 2 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,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

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",