From 843a30cb9afc681538be65ca2fb8cb276ad247cf Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Wed, 30 Sep 2015 18:42:55 -0400 Subject: [PATCH 01/35] move to p2p dir --- p2p/net/nat/nat.go | 439 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 439 insertions(+) create mode 100644 p2p/net/nat/nat.go diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go new file mode 100644 index 0000000000..a8bdb81be6 --- /dev/null +++ b/p2p/net/nat/nat.go @@ -0,0 +1,439 @@ +package nat + +import ( + "errors" + "fmt" + "strconv" + "strings" + "sync" + "time" + + ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" + manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net" + + nat "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/fd/go-nat" + goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess" + periodic "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/periodic" + notifier "github.com/ipfs/go-ipfs/thirdparty/notifier" + logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0" +) + +var ( + // ErrNoMapping signals no mapping exists for an address + ErrNoMapping = errors.New("mapping not established") +) + +var log = logging.Logger("nat") + +// MappingDuration is a default port mapping duration. +// Port mappings are renewed every (MappingDuration / 3) +const MappingDuration = time.Second * 60 + +// CacheTime is the time a mapping will cache an external address for +const CacheTime = time.Second * 15 + +// DiscoverNAT looks for a NAT device in the network and +// returns an object that can manage port mappings. +func DiscoverNAT() *NAT { + nat, err := nat.DiscoverGateway() + if err != nil { + log.Debug("DiscoverGateway error:", err) + return nil + } + addr, err := nat.GetDeviceAddress() + if err != nil { + log.Debug("DiscoverGateway address error:", err) + } else { + log.Debug("DiscoverGateway address:", addr) + } + return newNAT(nat) +} + +// NAT is an object that manages address port mappings in +// NATs (Network Address Translators). It is a long-running +// service that will periodically renew port mappings, +// and keep an up-to-date list of all the external addresses. +type NAT struct { + nat nat.NAT + proc goprocess.Process // manages nat mappings lifecycle + + mappingmu sync.RWMutex // guards mappings + mappings map[*mapping]struct{} + + Notifier +} + +func newNAT(realNAT nat.NAT) *NAT { + return &NAT{ + nat: realNAT, + proc: goprocess.WithParent(goprocess.Background()), + mappings: make(map[*mapping]struct{}), + } +} + +// Close shuts down all port mappings. NAT can no longer be used. +func (nat *NAT) Close() error { + return nat.proc.Close() +} + +// Process returns the nat's life-cycle manager, for making it listen +// to close signals. +func (nat *NAT) Process() goprocess.Process { + return nat.proc +} + +// Notifier is an object that assists NAT in notifying listeners. +// It is implemented using github.com/ipfs/go-ipfs/thirdparty/notifier +type Notifier struct { + n notifier.Notifier +} + +func (n *Notifier) notifyAll(notify func(n Notifiee)) { + n.n.NotifyAll(func(n notifier.Notifiee) { + notify(n.(Notifiee)) + }) +} + +// Notify signs up notifiee to listen to NAT events. +func (n *Notifier) Notify(notifiee Notifiee) { + n.n.Notify(n) +} + +// StopNotify stops signaling events to notifiee. +func (n *Notifier) StopNotify(notifiee Notifiee) { + n.n.StopNotify(notifiee) +} + +// Notifiee is an interface objects must implement to listen to NAT events. +type Notifiee interface { + + // Called every time a successful mapping happens + // Warning: the port mapping may have changed. If that is the + // case, both MappingSuccess and MappingChanged are called. + MappingSuccess(nat *NAT, m Mapping) + + // Called when mapping a port succeeds, but the mapping is + // with a different port than an earlier success. + MappingChanged(nat *NAT, m Mapping, oldport int, newport int) + + // Called when a port mapping fails. NAT will continue attempting after + // the next period. To stop trying, use: mapping.Close(). After this failure, + // mapping.ExternalPort() will be zero, and nat.ExternalAddrs() will not + // return the address for this mapping. With luck, the next attempt will + // succeed, without the client needing to do anything. + MappingFailed(nat *NAT, m Mapping, oldport int, err error) +} + +// Mapping represents a port mapping in a NAT. +type Mapping interface { + // NAT returns the NAT object this Mapping belongs to. + NAT() *NAT + + // Protocol returns the protocol of this port mapping. This is either + // "tcp" or "udp" as no other protocols are likely to be NAT-supported. + Protocol() string + + // InternalPort returns the internal device port. Mapping will continue to + // try to map InternalPort() to an external facing port. + InternalPort() int + + // ExternalPort returns the external facing port. If the mapping is not + // established, port will be 0 + ExternalPort() int + + // InternalAddr returns the internal address. + InternalAddr() ma.Multiaddr + + // ExternalAddr returns the external facing address. If the mapping is not + // established, addr will be nil, and and ErrNoMapping will be returned. + ExternalAddr() (addr ma.Multiaddr, err error) + + // Close closes the port mapping + Close() error +} + +// keeps republishing +type mapping struct { + sync.Mutex // guards all fields + + nat *NAT + proto string + intport int + extport int + intaddr ma.Multiaddr + proc goprocess.Process + + cached ma.Multiaddr + cacheTime time.Time +} + +func (m *mapping) NAT() *NAT { + m.Lock() + defer m.Unlock() + return m.nat +} + +func (m *mapping) Protocol() string { + m.Lock() + defer m.Unlock() + return m.proto +} + +func (m *mapping) InternalPort() int { + m.Lock() + defer m.Unlock() + return m.intport +} + +func (m *mapping) ExternalPort() int { + m.Lock() + defer m.Unlock() + return m.extport +} + +func (m *mapping) setExternalPort(p int) { + m.Lock() + defer m.Unlock() + m.extport = p +} + +func (m *mapping) InternalAddr() ma.Multiaddr { + m.Lock() + defer m.Unlock() + return m.intaddr +} + +func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { + if time.Now().Sub(m.cacheTime) < CacheTime { + return m.cached, nil + } + + if m.ExternalPort() == 0 { // dont even try right now. + return nil, ErrNoMapping + } + + ip, err := m.nat.nat.GetExternalAddress() + if err != nil { + return nil, err + } + + ipmaddr, err := manet.FromIP(ip) + if err != nil { + return nil, fmt.Errorf("error parsing ip") + } + + // call m.ExternalPort again, as mapping may have changed under our feet. (tocttou) + extport := m.ExternalPort() + if extport == 0 { + return nil, ErrNoMapping + } + + tcp, err := ma.NewMultiaddr(fmt.Sprintf("/%s/%d", m.Protocol(), extport)) + if err != nil { + return nil, err + } + + maddr2 := ipmaddr.Encapsulate(tcp) + + m.cached = maddr2 + m.cacheTime = time.Now() + return maddr2, nil +} + +func (m *mapping) Close() error { + return m.proc.Close() +} + +// Mappings returns a slice of all NAT mappings +func (nat *NAT) Mappings() []Mapping { + nat.mappingmu.Lock() + maps2 := make([]Mapping, 0, len(nat.mappings)) + for m := range nat.mappings { + maps2 = append(maps2, m) + } + nat.mappingmu.Unlock() + return maps2 +} + +func (nat *NAT) addMapping(m *mapping) { + // make mapping automatically close when nat is closed. + nat.proc.AddChild(m.proc) + + nat.mappingmu.Lock() + nat.mappings[m] = struct{}{} + nat.mappingmu.Unlock() +} + +func (nat *NAT) rmMapping(m *mapping) { + nat.mappingmu.Lock() + delete(nat.mappings, m) + nat.mappingmu.Unlock() +} + +// NewMapping attemps to construct a mapping on protocol and internal port +// It will also periodically renew the mapping until the returned Mapping +// -- or its parent NAT -- is Closed. +// +// May not succeed, and mappings may change over time; +// NAT devices may not respect our port requests, and even lie. +// Clients should not store the mapped results, but rather always +// poll our object for the latest mappings. +func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) { + if nat == nil { + return nil, fmt.Errorf("no nat available") + } + + network, addr, err := manet.DialArgs(maddr) + if err != nil { + return nil, fmt.Errorf("DialArgs failed on addr:", maddr.String()) + } + + switch network { + case "tcp", "tcp4", "tcp6": + network = "tcp" + case "udp", "udp4", "udp6": + network = "udp" + default: + return nil, fmt.Errorf("transport not supported by NAT: %s", network) + } + + intports := strings.Split(addr, ":")[1] + intport, err := strconv.Atoi(intports) + if err != nil { + return nil, err + } + + m := &mapping{ + nat: nat, + proto: network, + intport: intport, + intaddr: maddr, + } + m.proc = goprocess.WithTeardown(func() error { + nat.rmMapping(m) + return nil + }) + nat.addMapping(m) + + m.proc.AddChild(periodic.Every(MappingDuration/3, func(worker goprocess.Process) { + nat.establishMapping(m) + })) + + // do it once synchronously, so first mapping is done right away, and before exiting, + // allowing users -- in the optimistic case -- to use results right after. + nat.establishMapping(m) + return m, nil +} + +func (nat *NAT) establishMapping(m *mapping) { + oldport := m.ExternalPort() + log.Debugf("Attempting port map: %s/%d", m.Protocol(), m.InternalPort()) + newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "http", MappingDuration) + + failure := func() { + m.setExternalPort(0) // clear mapping + // TODO: log.Event + log.Debugf("failed to establish port mapping: %s", err) + nat.Notifier.notifyAll(func(n Notifiee) { + n.MappingFailed(nat, m, oldport, err) + }) + + // we do not close if the mapping failed, + // because it may work again next time. + } + + if err != nil || newport == 0 { + failure() + return + } + + m.setExternalPort(newport) + ext, err := m.ExternalAddr() + if err != nil { + log.Debugf("NAT Mapping addr error: %s %s", m.InternalAddr(), err) + failure() + return + } + + log.Debugf("NAT Mapping: %s --> %s", m.InternalAddr(), ext) + if oldport != 0 && newport != oldport { + log.Debugf("failed to renew same port mapping: ch %d -> %d", oldport, newport) + nat.Notifier.notifyAll(func(n Notifiee) { + n.MappingChanged(nat, m, oldport, newport) + }) + } + + nat.Notifier.notifyAll(func(n Notifiee) { + n.MappingSuccess(nat, m) + }) +} + +// PortMapAddrs attempts to open (and continue to keep open) +// port mappings for given addrs. This function blocks until +// all addresses have been tried. This allows clients to +// retrieve results immediately after: +// +// nat.PortMapAddrs(addrs) +// mapped := nat.ExternalAddrs() +// +// Some may not succeed, and mappings may change over time; +// NAT devices may not respect our port requests, and even lie. +// Clients should not store the mapped results, but rather always +// poll our object for the latest mappings. +func (nat *NAT) PortMapAddrs(addrs []ma.Multiaddr) { + // spin off addr mappings independently. + var wg sync.WaitGroup + for _, addr := range addrs { + // do all of them concurrently + wg.Add(1) + go func() { + defer wg.Done() + nat.NewMapping(addr) + }() + } + wg.Wait() +} + +// MappedAddrs returns address mappings NAT believes have been +// successfully established. Unsuccessful mappings are nil. This is: +// +// map[internalAddr]externalAddr +// +// This set of mappings _may not_ be correct, as NAT devices are finicky. +// Consider this with _best effort_ semantics. +func (nat *NAT) MappedAddrs() map[ma.Multiaddr]ma.Multiaddr { + + mappings := nat.Mappings() + addrmap := make(map[ma.Multiaddr]ma.Multiaddr, len(mappings)) + + for _, m := range mappings { + i := m.InternalAddr() + e, err := m.ExternalAddr() + if err != nil { + addrmap[i] = nil + } else { + addrmap[i] = e + } + } + return addrmap +} + +// ExternalAddrs returns a list of addresses that NAT believes have +// been successfully established. Unsuccessful mappings are omitted, +// so nat.ExternalAddrs() may return less addresses than nat.InternalAddrs(). +// To see which addresses are mapped, use nat.MappedAddrs(). +// +// This set of mappings _may not_ be correct, as NAT devices are finicky. +// Consider this with _best effort_ semantics. +func (nat *NAT) ExternalAddrs() []ma.Multiaddr { + mappings := nat.Mappings() + addrs := make([]ma.Multiaddr, 0, len(mappings)) + for _, m := range mappings { + a, err := m.ExternalAddr() + if err != nil { + continue // this mapping not currently successful. + } + addrs = append(addrs, a) + } + return addrs +} From 0be91aa3ba54cbdf84040dca47358f20f60f05de Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 11 Nov 2015 16:15:47 -0800 Subject: [PATCH 02/35] extract from 0.4.0 --- p2p/net/nat/nat.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index a8bdb81be6..3c29d4ee08 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,14 +8,14 @@ import ( "sync" "time" - ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" - manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net" - - nat "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/fd/go-nat" - goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess" - periodic "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/periodic" - notifier "github.com/ipfs/go-ipfs/thirdparty/notifier" - logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0" + ma "github.com/jbenet/go-multiaddr" + manet "github.com/jbenet/go-multiaddr-net" + + nat "github.com/fd/go-nat" + goprocess "github.com/jbenet/goprocess" + periodic "github.com/jbenet/goprocess/periodic" + notifier "thirdparty/notifier" + logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log" ) var ( @@ -83,7 +83,7 @@ func (nat *NAT) Process() goprocess.Process { } // Notifier is an object that assists NAT in notifying listeners. -// It is implemented using github.com/ipfs/go-ipfs/thirdparty/notifier +// It is implemented using thirdparty/notifier type Notifier struct { n notifier.Notifier } From fa375b9634bbc88356e5b6931765160416e14bdb Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sun, 15 Nov 2015 11:56:01 -0800 Subject: [PATCH 03/35] more vendoring --- p2p/net/nat/nat.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 3c29d4ee08..fe3a52114e 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,14 +8,14 @@ import ( "sync" "time" - ma "github.com/jbenet/go-multiaddr" - manet "github.com/jbenet/go-multiaddr-net" + ma "QmaA6aDzeHjZiuqBtgYRz8ZXb1qMCoyMHgyDjBEYQniUKF/go-multiaddr" + manet "QmanZCL6SXRfafiUEMCBLq2QR171uQSdXQ8YAdHXLd8Cwr/go-multiaddr-net" + logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log" nat "github.com/fd/go-nat" goprocess "github.com/jbenet/goprocess" periodic "github.com/jbenet/goprocess/periodic" notifier "thirdparty/notifier" - logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log" ) var ( From aa5c8a270dc7cb463cca5e035213568eec545b99 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sun, 15 Nov 2015 18:28:35 -0800 Subject: [PATCH 04/35] fixes for sha3 --- p2p/net/nat/nat.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index fe3a52114e..3258417220 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,8 +8,8 @@ import ( "sync" "time" - ma "QmaA6aDzeHjZiuqBtgYRz8ZXb1qMCoyMHgyDjBEYQniUKF/go-multiaddr" - manet "QmanZCL6SXRfafiUEMCBLq2QR171uQSdXQ8YAdHXLd8Cwr/go-multiaddr-net" + manet "QmU5s159q8cZuM1f9Vqti4LHu6y8zyVc5dxv2py81sdp6Q/go-multiaddr-net" + ma "QmbWxL1aXQhBjc1XGjGF1f2KGBMCBYSuT2ThA8YXnXJK83/go-multiaddr" logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log" nat "github.com/fd/go-nat" From 2c7dfee2f9e7696b29e36da1f99fd0c4ee934678 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sun, 15 Nov 2015 18:42:27 -0800 Subject: [PATCH 05/35] vendor in notifier --- p2p/net/nat/nat.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 3258417220..8e7fb95ea7 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -11,11 +11,11 @@ import ( manet "QmU5s159q8cZuM1f9Vqti4LHu6y8zyVc5dxv2py81sdp6Q/go-multiaddr-net" ma "QmbWxL1aXQhBjc1XGjGF1f2KGBMCBYSuT2ThA8YXnXJK83/go-multiaddr" + goprocess "QmSir6qPL1tjuxd8LkR8VZq6v625ExAUVs2eCLeqQuaPGU/goprocess" + periodic "QmSir6qPL1tjuxd8LkR8VZq6v625ExAUVs2eCLeqQuaPGU/goprocess/periodic" + notifier "QmUtEiB6DmXs7eLJiwS9YFyTAtptqzaWutxCsjHy7UKEgo/go-notifier" logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log" nat "github.com/fd/go-nat" - goprocess "github.com/jbenet/goprocess" - periodic "github.com/jbenet/goprocess/periodic" - notifier "thirdparty/notifier" ) var ( From ac1dafff64a8f2bd718be0b1fe9184fbda9df098 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sun, 15 Nov 2015 21:04:44 -0800 Subject: [PATCH 06/35] remove multiple multihash deps --- p2p/net/nat/nat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 8e7fb95ea7..3583a1d3f9 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,7 +8,7 @@ import ( "sync" "time" - manet "QmU5s159q8cZuM1f9Vqti4LHu6y8zyVc5dxv2py81sdp6Q/go-multiaddr-net" + manet "QmRCPT5WRph8aWXmaT2Rfn6ac98YRUUJnNURpD3hNAWp4f/go-multiaddr-net" ma "QmbWxL1aXQhBjc1XGjGF1f2KGBMCBYSuT2ThA8YXnXJK83/go-multiaddr" goprocess "QmSir6qPL1tjuxd8LkR8VZq6v625ExAUVs2eCLeqQuaPGU/goprocess" From f2b4ef28344579a2da7782d918e75723806b8c31 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 18 Nov 2015 11:47:51 -0800 Subject: [PATCH 07/35] migrate to gx namespace --- p2p/net/nat/nat.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 3583a1d3f9..c549527c57 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,14 +8,14 @@ import ( "sync" "time" - manet "QmRCPT5WRph8aWXmaT2Rfn6ac98YRUUJnNURpD3hNAWp4f/go-multiaddr-net" - ma "QmbWxL1aXQhBjc1XGjGF1f2KGBMCBYSuT2ThA8YXnXJK83/go-multiaddr" - - goprocess "QmSir6qPL1tjuxd8LkR8VZq6v625ExAUVs2eCLeqQuaPGU/goprocess" - periodic "QmSir6qPL1tjuxd8LkR8VZq6v625ExAUVs2eCLeqQuaPGU/goprocess/periodic" - notifier "QmUtEiB6DmXs7eLJiwS9YFyTAtptqzaWutxCsjHy7UKEgo/go-notifier" - logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log" - nat "github.com/fd/go-nat" + manet "gx/QmNT7d1e4Xcp3KcsvxyzUHVtqrR43uypoxLLzdKj6YZga2/go-multiaddr-net" + ma "gx/QmVUi2ncqnU48zsPgR1rQosDGwY3SSZ1Ndp33j33YjXdsj/go-multiaddr" + + notifier "gx/QmRV5aE4LZ4Kw9g2VAMpbEcaT7uz3Z3WxgdW8mzRPLL7g8/go-notifier" + nat "gx/QmWjjKcvWKtkwzbqF6t3Kz3wXA3Uj8DRAbPaTtcgFnvTQt/go-nat" + goprocess "gx/QmfDXyLfKNfja2XebomRZjZ2UZCa4BDyFoCymKtzNRVQ5b/goprocess" + periodic "gx/QmfDXyLfKNfja2XebomRZjZ2UZCa4BDyFoCymKtzNRVQ5b/goprocess/periodic" + logging "gx/QmfZZB1aVXWA4kaR5R4e9NifERT366TTCSagkfhmAbYLsu/go-log" ) var ( From 8863821e46a309d65f8b34b59f832a4d7d07dd2d Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sun, 6 Dec 2015 23:00:10 -0800 Subject: [PATCH 08/35] WIP --- p2p/net/nat/nat.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index c549527c57..6032ccd5a1 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,14 +8,14 @@ import ( "sync" "time" - manet "gx/QmNT7d1e4Xcp3KcsvxyzUHVtqrR43uypoxLLzdKj6YZga2/go-multiaddr-net" - ma "gx/QmVUi2ncqnU48zsPgR1rQosDGwY3SSZ1Ndp33j33YjXdsj/go-multiaddr" - - notifier "gx/QmRV5aE4LZ4Kw9g2VAMpbEcaT7uz3Z3WxgdW8mzRPLL7g8/go-notifier" - nat "gx/QmWjjKcvWKtkwzbqF6t3Kz3wXA3Uj8DRAbPaTtcgFnvTQt/go-nat" - goprocess "gx/QmfDXyLfKNfja2XebomRZjZ2UZCa4BDyFoCymKtzNRVQ5b/goprocess" - periodic "gx/QmfDXyLfKNfja2XebomRZjZ2UZCa4BDyFoCymKtzNRVQ5b/goprocess/periodic" - logging "gx/QmfZZB1aVXWA4kaR5R4e9NifERT366TTCSagkfhmAbYLsu/go-log" + ma "github.com/jbenet/go-multiaddr" + manet "github.com/jbenet/go-multiaddr-net" + + nat "github.com/fd/go-nat" + logging "github.com/ipfs/go-log" + goprocess "github.com/jbenet/goprocess" + periodic "github.com/jbenet/goprocess/periodic" + notifier "github.com/whyrusleeping/go-notifier" ) var ( From eff52d23d3cd2a68a7177a95d25569e9e27f8626 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 4 Jan 2016 05:45:58 -0800 Subject: [PATCH 09/35] path rewrites --- p2p/net/nat/nat.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 6032ccd5a1..c6f775989f 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,14 +8,14 @@ import ( "sync" "time" - ma "github.com/jbenet/go-multiaddr" - manet "github.com/jbenet/go-multiaddr-net" - - nat "github.com/fd/go-nat" - logging "github.com/ipfs/go-log" - goprocess "github.com/jbenet/goprocess" - periodic "github.com/jbenet/goprocess/periodic" - notifier "github.com/whyrusleeping/go-notifier" + ma "gx/ipfs/QmR3JkmZBKYXgNMNsNZawm914455Qof3PEopwuVSeXG7aV/go-multiaddr" + manet "gx/ipfs/QmYtzQmUwPFGxjCXctJ8e6GXS8sYfoXy2pdeMbS5SFWqRi/go-multiaddr-net" + + nat "gx/ipfs/QmNLvkCDV6ZjUJsEwGNporYBuZdhWT6q7TBVYQwwRv12HT/go-nat" + goprocess "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess" + periodic "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess/periodic" + logging "gx/ipfs/Qmazh5oNUVsDZTs2g59rq8aYQqwpss8tcUWQzor5sCCEuH/go-log" + notifier "gx/ipfs/QmbcS9XrwZkF1rZj8bBwwzoYhVuA2PCnPhFUL1pyWGgt2A/go-notifier" ) var ( From 122bc10af3b818a6bb6464494d72fe7517ec96d5 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Thu, 3 Mar 2016 13:03:24 -0800 Subject: [PATCH 10/35] update version of go-multiaddr --- p2p/net/nat/nat.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index c6f775989f..d840a669ad 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,14 +8,13 @@ import ( "sync" "time" - ma "gx/ipfs/QmR3JkmZBKYXgNMNsNZawm914455Qof3PEopwuVSeXG7aV/go-multiaddr" - manet "gx/ipfs/QmYtzQmUwPFGxjCXctJ8e6GXS8sYfoXy2pdeMbS5SFWqRi/go-multiaddr-net" - nat "gx/ipfs/QmNLvkCDV6ZjUJsEwGNporYBuZdhWT6q7TBVYQwwRv12HT/go-nat" + manet "gx/ipfs/QmQB7mNP3QE7b4zP2MQmsyJDqG5hzYE2CL8k1VyLWky2Ed/go-multiaddr-net" goprocess "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess" periodic "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess/periodic" logging "gx/ipfs/Qmazh5oNUVsDZTs2g59rq8aYQqwpss8tcUWQzor5sCCEuH/go-log" notifier "gx/ipfs/QmbcS9XrwZkF1rZj8bBwwzoYhVuA2PCnPhFUL1pyWGgt2A/go-notifier" + ma "gx/ipfs/QmcobAGsCjYt5DXoq9et9L8yR8er7o7Cu3DTvpaq12jYSz/go-multiaddr" ) var ( From a61a7ada39097a144fbf33feb2ac78cdfd963c23 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Tue, 29 Mar 2016 18:38:24 -0700 Subject: [PATCH 11/35] update utp lib --- p2p/net/nat/nat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index d840a669ad..0c7d86ca55 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -9,9 +9,9 @@ import ( "time" nat "gx/ipfs/QmNLvkCDV6ZjUJsEwGNporYBuZdhWT6q7TBVYQwwRv12HT/go-nat" - manet "gx/ipfs/QmQB7mNP3QE7b4zP2MQmsyJDqG5hzYE2CL8k1VyLWky2Ed/go-multiaddr-net" goprocess "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess" periodic "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess/periodic" + manet "gx/ipfs/QmYVqhVfbK4BKvbW88Lhm26b3ud14sTBvcm1H7uWUx1Fkp/go-multiaddr-net" logging "gx/ipfs/Qmazh5oNUVsDZTs2g59rq8aYQqwpss8tcUWQzor5sCCEuH/go-log" notifier "gx/ipfs/QmbcS9XrwZkF1rZj8bBwwzoYhVuA2PCnPhFUL1pyWGgt2A/go-notifier" ma "gx/ipfs/QmcobAGsCjYt5DXoq9et9L8yR8er7o7Cu3DTvpaq12jYSz/go-multiaddr" From b6ac01bf1fbe3f7bf402eb687d8d91235de0f7b1 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 27 Apr 2016 10:12:51 -0700 Subject: [PATCH 12/35] recursive dependency update of utp lib --- p2p/net/nat/nat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 0c7d86ca55..a3dd83c6d0 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -11,7 +11,7 @@ import ( nat "gx/ipfs/QmNLvkCDV6ZjUJsEwGNporYBuZdhWT6q7TBVYQwwRv12HT/go-nat" goprocess "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess" periodic "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess/periodic" - manet "gx/ipfs/QmYVqhVfbK4BKvbW88Lhm26b3ud14sTBvcm1H7uWUx1Fkp/go-multiaddr-net" + manet "gx/ipfs/QmTrxSBY8Wqd5aBB4MeizeSzS5xFbK8dQBrYaMsiGnCBhb/go-multiaddr-net" logging "gx/ipfs/Qmazh5oNUVsDZTs2g59rq8aYQqwpss8tcUWQzor5sCCEuH/go-log" notifier "gx/ipfs/QmbcS9XrwZkF1rZj8bBwwzoYhVuA2PCnPhFUL1pyWGgt2A/go-notifier" ma "gx/ipfs/QmcobAGsCjYt5DXoq9et9L8yR8er7o7Cu3DTvpaq12jYSz/go-multiaddr" From 7c74ee6d74c4c8bee43c2795d1cc67f0fc0c385e Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 27 Apr 2016 12:45:16 -0700 Subject: [PATCH 13/35] rewrite all package paths to dvcs --- p2p/net/nat/nat.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index a3dd83c6d0..237c1296ca 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,13 +8,13 @@ import ( "sync" "time" - nat "gx/ipfs/QmNLvkCDV6ZjUJsEwGNporYBuZdhWT6q7TBVYQwwRv12HT/go-nat" - goprocess "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess" - periodic "gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess/periodic" - manet "gx/ipfs/QmTrxSBY8Wqd5aBB4MeizeSzS5xFbK8dQBrYaMsiGnCBhb/go-multiaddr-net" - logging "gx/ipfs/Qmazh5oNUVsDZTs2g59rq8aYQqwpss8tcUWQzor5sCCEuH/go-log" - notifier "gx/ipfs/QmbcS9XrwZkF1rZj8bBwwzoYhVuA2PCnPhFUL1pyWGgt2A/go-notifier" - ma "gx/ipfs/QmcobAGsCjYt5DXoq9et9L8yR8er7o7Cu3DTvpaq12jYSz/go-multiaddr" + nat "github.com/fd/go-nat" + logging "github.com/ipfs/go-log" + ma "github.com/jbenet/go-multiaddr" + manet "github.com/jbenet/go-multiaddr-net" + goprocess "github.com/jbenet/goprocess" + periodic "github.com/jbenet/goprocess/periodic" + notifier "github.com/whyrusleeping/go-notifier" ) var ( From 47808266ff204de00bf33fdb78490741818cc175 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 15 Jun 2016 11:07:30 -0700 Subject: [PATCH 14/35] fix minor race condition in nat detection code --- p2p/net/nat/nat.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 237c1296ca..4dd6f0439e 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -164,6 +164,7 @@ type mapping struct { cached ma.Multiaddr cacheTime time.Time + cacheLk sync.Mutex } func (m *mapping) NAT() *NAT { @@ -203,8 +204,12 @@ func (m *mapping) InternalAddr() ma.Multiaddr { } func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { - if time.Now().Sub(m.cacheTime) < CacheTime { - return m.cached, nil + m.cacheLk.Lock() + ctime := m.cacheTime + cval := m.cached + m.cacheLk.Unlock() + if time.Now().Sub(ctime) < CacheTime { + return cval, nil } if m.ExternalPort() == 0 { // dont even try right now. @@ -234,8 +239,10 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { maddr2 := ipmaddr.Encapsulate(tcp) + m.cacheLk.Lock() m.cached = maddr2 m.cacheTime = time.Now() + m.cacheLk.Unlock() return maddr2, nil } From 977485df98ca806114b17312b2c3f6dd62979240 Mon Sep 17 00:00:00 2001 From: John Steidley Date: Sat, 23 Jul 2016 13:24:45 -0700 Subject: [PATCH 15/35] gosimple --- p2p/net/nat/nat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 4dd6f0439e..7dac06df1d 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -208,7 +208,7 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { ctime := m.cacheTime cval := m.cached m.cacheLk.Unlock() - if time.Now().Sub(ctime) < CacheTime { + if time.Since(ctime) < CacheTime { return cval, nil } From 3339abd03e403c076c1f1b201e4afc7f61393a46 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 26 Aug 2016 18:15:27 +0200 Subject: [PATCH 16/35] Retry NAT punching without duration on mapping failure Some hardware doesn't support UPnP with durations. --- p2p/net/nat/nat.go | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 7dac06df1d..6324715bf5 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -155,12 +155,13 @@ type Mapping interface { type mapping struct { sync.Mutex // guards all fields - nat *NAT - proto string - intport int - extport int - intaddr ma.Multiaddr - proc goprocess.Process + nat *NAT + proto string + intport int + extport int + permanent bool + intaddr ma.Multiaddr + proc goprocess.Process cached ma.Multiaddr cacheTime time.Time @@ -197,6 +198,18 @@ func (m *mapping) setExternalPort(p int) { m.extport = p } +func (m *mapping) setPermanent(p bool) { + m.Lock() + defer m.Unlock() + m.permanent = p +} + +func (m *mapping) isPermanent() bool { + m.Lock() + defer m.Unlock() + return m.permanent +} + func (m *mapping) InternalAddr() ma.Multiaddr { m.Lock() defer m.Unlock() @@ -333,13 +346,26 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) { func (nat *NAT) establishMapping(m *mapping) { oldport := m.ExternalPort() + if oldport != 0 && m.isPermanent() { + // mapping was already established and it is permanent + return + } + log.Debugf("Attempting port map: %s/%d", m.Protocol(), m.InternalPort()) - newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "http", MappingDuration) + permanent := false + + newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "libp2p", MappingDuration) + + if err != nil { + // Some hardware does not support mappings with timeout, so try that + newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "libp2p", 0) + permanent = (err == nil) + } failure := func() { m.setExternalPort(0) // clear mapping // TODO: log.Event - log.Debugf("failed to establish port mapping: %s", err) + log.Warningf("failed to establish port mapping: %s", err) nat.Notifier.notifyAll(func(n Notifiee) { n.MappingFailed(nat, m, oldport, err) }) @@ -353,6 +379,7 @@ func (nat *NAT) establishMapping(m *mapping) { return } + m.setPermanent(permanent) m.setExternalPort(newport) ext, err := m.ExternalAddr() if err != nil { From 59a1cee3e674f1a57aceedc3bf9f0dbb72c96069 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 26 Aug 2016 18:45:28 +0200 Subject: [PATCH 17/35] nat: do not shortcircuit permanent mappings If we use same NAT agent and call for the same permanent mapping again we get the same mapping, no harm done. If router dies, we will remap again. Just pros, no cons. --- p2p/net/nat/nat.go | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 6324715bf5..a7fc75b0d3 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -163,6 +163,8 @@ type mapping struct { intaddr ma.Multiaddr proc goprocess.Process + comment string + cached ma.Multiaddr cacheTime time.Time cacheLk sync.Mutex @@ -198,18 +200,6 @@ func (m *mapping) setExternalPort(p int) { m.extport = p } -func (m *mapping) setPermanent(p bool) { - m.Lock() - defer m.Unlock() - m.permanent = p -} - -func (m *mapping) isPermanent() bool { - m.Lock() - defer m.Unlock() - return m.permanent -} - func (m *mapping) InternalAddr() ma.Multiaddr { m.Lock() defer m.Unlock() @@ -346,20 +336,17 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) { func (nat *NAT) establishMapping(m *mapping) { oldport := m.ExternalPort() - if oldport != 0 && m.isPermanent() { - // mapping was already established and it is permanent - return - } log.Debugf("Attempting port map: %s/%d", m.Protocol(), m.InternalPort()) - permanent := false - - newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "libp2p", MappingDuration) + comment := "libp2p" + if m.comment != "" { + comment = "libp2p-" + m.comment + } + newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, MappingDuration) if err != nil { // Some hardware does not support mappings with timeout, so try that - newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "libp2p", 0) - permanent = (err == nil) + newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, 0) } failure := func() { @@ -379,7 +366,6 @@ func (nat *NAT) establishMapping(m *mapping) { return } - m.setPermanent(permanent) m.setExternalPort(newport) ext, err := m.ExternalAddr() if err != nil { From 72d32fc62f3109195b858c5b99689843f3fa985f Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 12 Sep 2016 15:59:01 +0200 Subject: [PATCH 18/35] nat: split into files --- p2p/net/nat/mapping.go | 141 ++++++++++++++++++++++++++++++++ p2p/net/nat/nat.go | 173 ---------------------------------------- p2p/net/nat/notifier.go | 47 +++++++++++ 3 files changed, 188 insertions(+), 173 deletions(-) create mode 100644 p2p/net/nat/mapping.go create mode 100644 p2p/net/nat/notifier.go diff --git a/p2p/net/nat/mapping.go b/p2p/net/nat/mapping.go new file mode 100644 index 0000000000..2f45726308 --- /dev/null +++ b/p2p/net/nat/mapping.go @@ -0,0 +1,141 @@ +package nat + +import ( + "fmt" + "sync" + "time" + + ma "github.com/jbenet/go-multiaddr" + manet "github.com/jbenet/go-multiaddr-net" + "github.com/jbenet/goprocess" +) + +// Mapping represents a port mapping in a NAT. +type Mapping interface { + // NAT returns the NAT object this Mapping belongs to. + NAT() *NAT + + // Protocol returns the protocol of this port mapping. This is either + // "tcp" or "udp" as no other protocols are likely to be NAT-supported. + Protocol() string + + // InternalPort returns the internal device port. Mapping will continue to + // try to map InternalPort() to an external facing port. + InternalPort() int + + // ExternalPort returns the external facing port. If the mapping is not + // established, port will be 0 + ExternalPort() int + + // InternalAddr returns the internal address. + InternalAddr() ma.Multiaddr + + // ExternalAddr returns the external facing address. If the mapping is not + // established, addr will be nil, and and ErrNoMapping will be returned. + ExternalAddr() (addr ma.Multiaddr, err error) + + // Close closes the port mapping + Close() error +} + +// keeps republishing +type mapping struct { + sync.Mutex // guards all fields + + nat *NAT + proto string + intport int + extport int + permanent bool + intaddr ma.Multiaddr + proc goprocess.Process + + comment string + + cached ma.Multiaddr + cacheTime time.Time + cacheLk sync.Mutex +} + +func (m *mapping) NAT() *NAT { + m.Lock() + defer m.Unlock() + return m.nat +} + +func (m *mapping) Protocol() string { + m.Lock() + defer m.Unlock() + return m.proto +} + +func (m *mapping) InternalPort() int { + m.Lock() + defer m.Unlock() + return m.intport +} + +func (m *mapping) ExternalPort() int { + m.Lock() + defer m.Unlock() + return m.extport +} + +func (m *mapping) setExternalPort(p int) { + m.Lock() + defer m.Unlock() + m.extport = p +} + +func (m *mapping) InternalAddr() ma.Multiaddr { + m.Lock() + defer m.Unlock() + return m.intaddr +} + +func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { + m.cacheLk.Lock() + ctime := m.cacheTime + cval := m.cached + m.cacheLk.Unlock() + if time.Since(ctime) < CacheTime { + return cval, nil + } + + if m.ExternalPort() == 0 { // dont even try right now. + return nil, ErrNoMapping + } + + ip, err := m.nat.nat.GetExternalAddress() + if err != nil { + return nil, err + } + + ipmaddr, err := manet.FromIP(ip) + if err != nil { + return nil, fmt.Errorf("error parsing ip") + } + + // call m.ExternalPort again, as mapping may have changed under our feet. (tocttou) + extport := m.ExternalPort() + if extport == 0 { + return nil, ErrNoMapping + } + + tcp, err := ma.NewMultiaddr(fmt.Sprintf("/%s/%d", m.Protocol(), extport)) + if err != nil { + return nil, err + } + + maddr2 := ipmaddr.Encapsulate(tcp) + + m.cacheLk.Lock() + m.cached = maddr2 + m.cacheTime = time.Now() + m.cacheLk.Unlock() + return maddr2, nil +} + +func (m *mapping) Close() error { + return m.proc.Close() +} diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index a7fc75b0d3..210512cd1a 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -14,7 +14,6 @@ import ( manet "github.com/jbenet/go-multiaddr-net" goprocess "github.com/jbenet/goprocess" periodic "github.com/jbenet/goprocess/periodic" - notifier "github.com/whyrusleeping/go-notifier" ) var ( @@ -81,178 +80,6 @@ func (nat *NAT) Process() goprocess.Process { return nat.proc } -// Notifier is an object that assists NAT in notifying listeners. -// It is implemented using thirdparty/notifier -type Notifier struct { - n notifier.Notifier -} - -func (n *Notifier) notifyAll(notify func(n Notifiee)) { - n.n.NotifyAll(func(n notifier.Notifiee) { - notify(n.(Notifiee)) - }) -} - -// Notify signs up notifiee to listen to NAT events. -func (n *Notifier) Notify(notifiee Notifiee) { - n.n.Notify(n) -} - -// StopNotify stops signaling events to notifiee. -func (n *Notifier) StopNotify(notifiee Notifiee) { - n.n.StopNotify(notifiee) -} - -// Notifiee is an interface objects must implement to listen to NAT events. -type Notifiee interface { - - // Called every time a successful mapping happens - // Warning: the port mapping may have changed. If that is the - // case, both MappingSuccess and MappingChanged are called. - MappingSuccess(nat *NAT, m Mapping) - - // Called when mapping a port succeeds, but the mapping is - // with a different port than an earlier success. - MappingChanged(nat *NAT, m Mapping, oldport int, newport int) - - // Called when a port mapping fails. NAT will continue attempting after - // the next period. To stop trying, use: mapping.Close(). After this failure, - // mapping.ExternalPort() will be zero, and nat.ExternalAddrs() will not - // return the address for this mapping. With luck, the next attempt will - // succeed, without the client needing to do anything. - MappingFailed(nat *NAT, m Mapping, oldport int, err error) -} - -// Mapping represents a port mapping in a NAT. -type Mapping interface { - // NAT returns the NAT object this Mapping belongs to. - NAT() *NAT - - // Protocol returns the protocol of this port mapping. This is either - // "tcp" or "udp" as no other protocols are likely to be NAT-supported. - Protocol() string - - // InternalPort returns the internal device port. Mapping will continue to - // try to map InternalPort() to an external facing port. - InternalPort() int - - // ExternalPort returns the external facing port. If the mapping is not - // established, port will be 0 - ExternalPort() int - - // InternalAddr returns the internal address. - InternalAddr() ma.Multiaddr - - // ExternalAddr returns the external facing address. If the mapping is not - // established, addr will be nil, and and ErrNoMapping will be returned. - ExternalAddr() (addr ma.Multiaddr, err error) - - // Close closes the port mapping - Close() error -} - -// keeps republishing -type mapping struct { - sync.Mutex // guards all fields - - nat *NAT - proto string - intport int - extport int - permanent bool - intaddr ma.Multiaddr - proc goprocess.Process - - comment string - - cached ma.Multiaddr - cacheTime time.Time - cacheLk sync.Mutex -} - -func (m *mapping) NAT() *NAT { - m.Lock() - defer m.Unlock() - return m.nat -} - -func (m *mapping) Protocol() string { - m.Lock() - defer m.Unlock() - return m.proto -} - -func (m *mapping) InternalPort() int { - m.Lock() - defer m.Unlock() - return m.intport -} - -func (m *mapping) ExternalPort() int { - m.Lock() - defer m.Unlock() - return m.extport -} - -func (m *mapping) setExternalPort(p int) { - m.Lock() - defer m.Unlock() - m.extport = p -} - -func (m *mapping) InternalAddr() ma.Multiaddr { - m.Lock() - defer m.Unlock() - return m.intaddr -} - -func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { - m.cacheLk.Lock() - ctime := m.cacheTime - cval := m.cached - m.cacheLk.Unlock() - if time.Since(ctime) < CacheTime { - return cval, nil - } - - if m.ExternalPort() == 0 { // dont even try right now. - return nil, ErrNoMapping - } - - ip, err := m.nat.nat.GetExternalAddress() - if err != nil { - return nil, err - } - - ipmaddr, err := manet.FromIP(ip) - if err != nil { - return nil, fmt.Errorf("error parsing ip") - } - - // call m.ExternalPort again, as mapping may have changed under our feet. (tocttou) - extport := m.ExternalPort() - if extport == 0 { - return nil, ErrNoMapping - } - - tcp, err := ma.NewMultiaddr(fmt.Sprintf("/%s/%d", m.Protocol(), extport)) - if err != nil { - return nil, err - } - - maddr2 := ipmaddr.Encapsulate(tcp) - - m.cacheLk.Lock() - m.cached = maddr2 - m.cacheTime = time.Now() - m.cacheLk.Unlock() - return maddr2, nil -} - -func (m *mapping) Close() error { - return m.proc.Close() -} - // Mappings returns a slice of all NAT mappings func (nat *NAT) Mappings() []Mapping { nat.mappingmu.Lock() diff --git a/p2p/net/nat/notifier.go b/p2p/net/nat/notifier.go new file mode 100644 index 0000000000..462a78b953 --- /dev/null +++ b/p2p/net/nat/notifier.go @@ -0,0 +1,47 @@ +package nat + +import ( + notifier "github.com/whyrusleeping/go-notifier" +) + +// Notifier is an object that assists NAT in notifying listeners. +// It is implemented using thirdparty/notifier +type Notifier struct { + n notifier.Notifier +} + +func (n *Notifier) notifyAll(notify func(n Notifiee)) { + n.n.NotifyAll(func(n notifier.Notifiee) { + notify(n.(Notifiee)) + }) +} + +// Notify signs up notifiee to listen to NAT events. +func (n *Notifier) Notify(notifiee Notifiee) { + n.n.Notify(n) +} + +// StopNotify stops signaling events to notifiee. +func (n *Notifier) StopNotify(notifiee Notifiee) { + n.n.StopNotify(notifiee) +} + +// Notifiee is an interface objects must implement to listen to NAT events. +type Notifiee interface { + + // Called every time a successful mapping happens + // Warning: the port mapping may have changed. If that is the + // case, both MappingSuccess and MappingChanged are called. + MappingSuccess(nat *NAT, m Mapping) + + // Called when mapping a port succeeds, but the mapping is + // with a different port than an earlier success. + MappingChanged(nat *NAT, m Mapping, oldport int, newport int) + + // Called when a port mapping fails. NAT will continue attempting after + // the next period. To stop trying, use: mapping.Close(). After this failure, + // mapping.ExternalPort() will be zero, and nat.ExternalAddrs() will not + // return the address for this mapping. With luck, the next attempt will + // succeed, without the client needing to do anything. + MappingFailed(nat *NAT, m Mapping, oldport int, err error) +} From d44566d5a54e275bcee0d24c0dfce4f1776271b7 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 12 Sep 2016 16:01:05 +0200 Subject: [PATCH 19/35] nat: add locks around nat --- p2p/net/nat/mapping.go | 2 ++ p2p/net/nat/nat.go | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/p2p/net/nat/mapping.go b/p2p/net/nat/mapping.go index 2f45726308..b2aeb9c4d7 100644 --- a/p2p/net/nat/mapping.go +++ b/p2p/net/nat/mapping.go @@ -106,7 +106,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { return nil, ErrNoMapping } + m.nat.natmu.Lock() ip, err := m.nat.nat.GetExternalAddress() + m.nat.natmu.Unlock() if err != nil { return nil, err } diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 210512cd1a..879b69c06d 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -52,8 +52,9 @@ func DiscoverNAT() *NAT { // service that will periodically renew port mappings, // and keep an up-to-date list of all the external addresses. type NAT struct { - nat nat.NAT - proc goprocess.Process // manages nat mappings lifecycle + natmu sync.Mutex + nat nat.NAT + proc goprocess.Process // manages nat mappings lifecycle mappingmu sync.RWMutex // guards mappings mappings map[*mapping]struct{} @@ -170,11 +171,13 @@ func (nat *NAT) establishMapping(m *mapping) { comment = "libp2p-" + m.comment } + nat.natmu.Lock() newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, MappingDuration) if err != nil { // Some hardware does not support mappings with timeout, so try that newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, 0) } + nat.natmu.Lock() failure := func() { m.setExternalPort(0) // clear mapping From c803102bdf8849be70985abb5f6025befabb8852 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 29 Sep 2016 15:32:27 +0200 Subject: [PATCH 20/35] fix locking issue introduced in 90eeff42fcd --- p2p/net/nat/nat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 879b69c06d..3a4ae60b4d 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -177,7 +177,7 @@ func (nat *NAT) establishMapping(m *mapping) { // Some hardware does not support mappings with timeout, so try that newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, 0) } - nat.natmu.Lock() + nat.natmu.Unlock() failure := func() { m.setExternalPort(0) // clear mapping From e7fa567007593345bb94c71c726e1381faed78ce Mon Sep 17 00:00:00 2001 From: Jeromy Date: Tue, 4 Oct 2016 21:18:07 -0700 Subject: [PATCH 21/35] update deps --- p2p/net/nat/mapping.go | 4 ++-- p2p/net/nat/nat.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/p2p/net/nat/mapping.go b/p2p/net/nat/mapping.go index b2aeb9c4d7..b03b002d00 100644 --- a/p2p/net/nat/mapping.go +++ b/p2p/net/nat/mapping.go @@ -5,9 +5,9 @@ import ( "sync" "time" - ma "github.com/jbenet/go-multiaddr" - manet "github.com/jbenet/go-multiaddr-net" "github.com/jbenet/goprocess" + ma "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr-net" ) // Mapping represents a port mapping in a NAT. diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 3a4ae60b4d..602667a3fc 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -10,10 +10,10 @@ import ( nat "github.com/fd/go-nat" logging "github.com/ipfs/go-log" - ma "github.com/jbenet/go-multiaddr" - manet "github.com/jbenet/go-multiaddr-net" goprocess "github.com/jbenet/goprocess" periodic "github.com/jbenet/goprocess/periodic" + ma "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr-net" ) var ( From 0ee4048d653a8ac65d6db2be406340e80e78f1f8 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Fri, 17 Nov 2017 03:57:35 -0500 Subject: [PATCH 22/35] Fix "go vet" errors. --- p2p/net/nat/nat.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 602667a3fc..d68aca0752 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -122,7 +122,7 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) { network, addr, err := manet.DialArgs(maddr) if err != nil { - return nil, fmt.Errorf("DialArgs failed on addr:", maddr.String()) + return nil, fmt.Errorf("DialArgs failed on addr: %s", maddr.String()) } switch network { @@ -235,10 +235,10 @@ func (nat *NAT) PortMapAddrs(addrs []ma.Multiaddr) { for _, addr := range addrs { // do all of them concurrently wg.Add(1) - go func() { + go func(addr ma.Multiaddr) { defer wg.Done() nat.NewMapping(addr) - }() + }(addr) } wg.Wait() } From 0ec0019d06cd4df0744d50770be21a4cdcb0de47 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 16 Aug 2018 15:56:03 -0700 Subject: [PATCH 23/35] only map *usable* addresses Otherwise, we'll try to port-map localhost. --- p2p/net/nat/nat.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index d68aca0752..0c47a4c2c2 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -3,6 +3,7 @@ package nat import ( "errors" "fmt" + "net" "strconv" "strings" "sync" @@ -125,15 +126,40 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) { return nil, fmt.Errorf("DialArgs failed on addr: %s", maddr.String()) } + var ip net.IP switch network { case "tcp", "tcp4", "tcp6": + addr, err := net.ResolveTCPAddr(network, addr) + if err != nil { + return nil, err + } + ip = addr.IP network = "tcp" case "udp", "udp4", "udp6": + addr, err := net.ResolveUDPAddr(network, addr) + if err != nil { + return nil, err + } + ip = addr.IP network = "udp" default: return nil, fmt.Errorf("transport not supported by NAT: %s", network) } + // XXX: Known limitation: doesn't handle multiple internal addresses. + // If this applies to you, you can figure it out yourself. Ideally, the + // NAT library would allow us to handle this case but the "go way" + // appears to be to just "shrug" at edge-cases. + if !ip.IsUnspecified() { + internalAddr, err := nat.nat.GetInternalAddress() + if err != nil { + return nil, fmt.Errorf("failed to discover address on nat: %s", err) + } + if !ip.Equal(internalAddr) { + return nil, fmt.Errorf("nat address is %s, refusing to map %s", internalAddr, ip) + } + } + intports := strings.Split(addr, ":")[1] intport, err := strconv.Atoi(intports) if err != nil { From d6a73bd9d7306f4539cfb8ee23ba7e9d23b42d14 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 5 Mar 2019 13:58:46 -0800 Subject: [PATCH 24/35] remove all uses of multiaddrs NATs only care about TCP/UDP and ports. Using multiaddrs here made this library really hard to work with correctly. Furthermore, this library doesn't _actually_ support specifying the internal IP address. However, we'd still _act_ like the internal IP address mattered. This caused all sorts of mismatches. --- p2p/net/nat/mapping.go | 81 +++++++----------- p2p/net/nat/nat.go | 184 +++++++++------------------------------- p2p/net/nat/notifier.go | 2 +- 3 files changed, 71 insertions(+), 196 deletions(-) diff --git a/p2p/net/nat/mapping.go b/p2p/net/nat/mapping.go index b03b002d00..33f25f615f 100644 --- a/p2p/net/nat/mapping.go +++ b/p2p/net/nat/mapping.go @@ -2,12 +2,11 @@ package nat import ( "fmt" + "net" "sync" "time" "github.com/jbenet/goprocess" - ma "github.com/multiformats/go-multiaddr" - manet "github.com/multiformats/go-multiaddr-net" ) // Mapping represents a port mapping in a NAT. @@ -27,12 +26,9 @@ type Mapping interface { // established, port will be 0 ExternalPort() int - // InternalAddr returns the internal address. - InternalAddr() ma.Multiaddr - // ExternalAddr returns the external facing address. If the mapping is not // established, addr will be nil, and and ErrNoMapping will be returned. - ExternalAddr() (addr ma.Multiaddr, err error) + ExternalAddr() (addr net.Addr, err error) // Close closes the port mapping Close() error @@ -47,12 +43,9 @@ type mapping struct { intport int extport int permanent bool - intaddr ma.Multiaddr proc goprocess.Process - comment string - - cached ma.Multiaddr + cached net.IP cacheTime time.Time cacheLk sync.Mutex } @@ -87,55 +80,41 @@ func (m *mapping) setExternalPort(p int) { m.extport = p } -func (m *mapping) InternalAddr() ma.Multiaddr { - m.Lock() - defer m.Unlock() - return m.intaddr -} - -func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { +func (m *mapping) ExternalAddr() (net.Addr, error) { m.cacheLk.Lock() - ctime := m.cacheTime - cval := m.cached - m.cacheLk.Unlock() - if time.Since(ctime) < CacheTime { - return cval, nil - } - - if m.ExternalPort() == 0 { // dont even try right now. + defer m.cacheLk.Unlock() + oport := m.ExternalPort() + if oport == 0 { + // dont even try right now. return nil, ErrNoMapping } - m.nat.natmu.Lock() - ip, err := m.nat.nat.GetExternalAddress() - m.nat.natmu.Unlock() - if err != nil { - return nil, err - } + if time.Since(m.cacheTime) >= CacheTime { + m.nat.natmu.Lock() + cval, err := m.nat.nat.GetExternalAddress() + m.nat.natmu.Unlock() - ipmaddr, err := manet.FromIP(ip) - if err != nil { - return nil, fmt.Errorf("error parsing ip") - } + if err != nil { + return nil, err + } - // call m.ExternalPort again, as mapping may have changed under our feet. (tocttou) - extport := m.ExternalPort() - if extport == 0 { - return nil, ErrNoMapping + m.cached = cval + m.cacheTime = time.Now() } - - tcp, err := ma.NewMultiaddr(fmt.Sprintf("/%s/%d", m.Protocol(), extport)) - if err != nil { - return nil, err + switch m.Protocol() { + case "tcp": + return &net.TCPAddr{ + IP: m.cached, + Port: oport, + }, nil + case "udp": + return &net.UDPAddr{ + IP: m.cached, + Port: oport, + }, nil + default: + panic(fmt.Sprintf("invalid protocol %q", m.Protocol())) } - - maddr2 := ipmaddr.Encapsulate(tcp) - - m.cacheLk.Lock() - m.cached = maddr2 - m.cacheTime = time.Now() - m.cacheLk.Unlock() - return maddr2, nil } func (m *mapping) Close() error { diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 0c47a4c2c2..938d1153b3 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -1,11 +1,9 @@ package nat import ( + "context" "errors" "fmt" - "net" - "strconv" - "strings" "sync" "time" @@ -13,8 +11,6 @@ import ( logging "github.com/ipfs/go-log" goprocess "github.com/jbenet/goprocess" periodic "github.com/jbenet/goprocess/periodic" - ma "github.com/multiformats/go-multiaddr" - manet "github.com/multiformats/go-multiaddr-net" ) var ( @@ -33,19 +29,38 @@ const CacheTime = time.Second * 15 // DiscoverNAT looks for a NAT device in the network and // returns an object that can manage port mappings. -func DiscoverNAT() *NAT { - nat, err := nat.DiscoverGateway() +func DiscoverNAT(ctx context.Context) (*NAT, error) { + var ( + natInstance nat.NAT + err error + ) + + done := make(chan struct{}) + go func() { + defer close(done) + // This will abort in 10 seconds anyways. + natInstance, err = nat.DiscoverGateway() + }() + + select { + case <-done: + case <-ctx.Done(): + return nil, ctx.Err() + } + if err != nil { - log.Debug("DiscoverGateway error:", err) - return nil + return nil, err } - addr, err := nat.GetDeviceAddress() + + // Log the device addr. + addr, err := natInstance.GetDeviceAddress() if err != nil { log.Debug("DiscoverGateway address error:", err) } else { log.Debug("DiscoverGateway address:", addr) } - return newNAT(nat) + + return newNAT(natInstance), nil } // NAT is an object that manages address port mappings in @@ -55,7 +70,7 @@ func DiscoverNAT() *NAT { type NAT struct { natmu sync.Mutex nat nat.NAT - proc goprocess.Process // manages nat mappings lifecycle + proc goprocess.Process mappingmu sync.RWMutex // guards mappings mappings map[*mapping]struct{} @@ -116,66 +131,31 @@ func (nat *NAT) rmMapping(m *mapping) { // NAT devices may not respect our port requests, and even lie. // Clients should not store the mapped results, but rather always // poll our object for the latest mappings. -func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) { +func (nat *NAT) NewMapping(protocol string, port int) (Mapping, error) { if nat == nil { return nil, fmt.Errorf("no nat available") } - network, addr, err := manet.DialArgs(maddr) - if err != nil { - return nil, fmt.Errorf("DialArgs failed on addr: %s", maddr.String()) - } - - var ip net.IP - switch network { - case "tcp", "tcp4", "tcp6": - addr, err := net.ResolveTCPAddr(network, addr) - if err != nil { - return nil, err - } - ip = addr.IP - network = "tcp" - case "udp", "udp4", "udp6": - addr, err := net.ResolveUDPAddr(network, addr) - if err != nil { - return nil, err - } - ip = addr.IP - network = "udp" + switch protocol { + case "tcp", "udp": default: - return nil, fmt.Errorf("transport not supported by NAT: %s", network) - } - - // XXX: Known limitation: doesn't handle multiple internal addresses. - // If this applies to you, you can figure it out yourself. Ideally, the - // NAT library would allow us to handle this case but the "go way" - // appears to be to just "shrug" at edge-cases. - if !ip.IsUnspecified() { - internalAddr, err := nat.nat.GetInternalAddress() - if err != nil { - return nil, fmt.Errorf("failed to discover address on nat: %s", err) - } - if !ip.Equal(internalAddr) { - return nil, fmt.Errorf("nat address is %s, refusing to map %s", internalAddr, ip) - } - } - - intports := strings.Split(addr, ":")[1] - intport, err := strconv.Atoi(intports) - if err != nil { - return nil, err + return nil, fmt.Errorf("invalid protocol: %s", protocol) } m := &mapping{ + intport: port, nat: nat, - proto: network, - intport: intport, - intaddr: maddr, + proto: protocol, } + m.proc = goprocess.WithTeardown(func() error { nat.rmMapping(m) + nat.natmu.Lock() + defer nat.natmu.Unlock() + nat.nat.DeletePortMapping(m.Protocol(), m.InternalPort()) return nil }) + nat.addMapping(m) m.proc.AddChild(periodic.Every(MappingDuration/3, func(worker goprocess.Process) { @@ -193,9 +173,6 @@ func (nat *NAT) establishMapping(m *mapping) { log.Debugf("Attempting port map: %s/%d", m.Protocol(), m.InternalPort()) comment := "libp2p" - if m.comment != "" { - comment = "libp2p-" + m.comment - } nat.natmu.Lock() newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, MappingDuration) @@ -205,7 +182,7 @@ func (nat *NAT) establishMapping(m *mapping) { } nat.natmu.Unlock() - failure := func() { + if err != nil || newport == 0 { m.setExternalPort(0) // clear mapping // TODO: log.Event log.Warningf("failed to establish port mapping: %s", err) @@ -215,22 +192,11 @@ func (nat *NAT) establishMapping(m *mapping) { // we do not close if the mapping failed, // because it may work again next time. - } - - if err != nil || newport == 0 { - failure() return } m.setExternalPort(newport) - ext, err := m.ExternalAddr() - if err != nil { - log.Debugf("NAT Mapping addr error: %s %s", m.InternalAddr(), err) - failure() - return - } - - log.Debugf("NAT Mapping: %s --> %s", m.InternalAddr(), ext) + log.Debugf("NAT Mapping: %s --> %s (%s)", m.ExternalPort(), m.InternalPort(), m.Protocol()) if oldport != 0 && newport != oldport { log.Debugf("failed to renew same port mapping: ch %d -> %d", oldport, newport) nat.Notifier.notifyAll(func(n Notifiee) { @@ -242,73 +208,3 @@ func (nat *NAT) establishMapping(m *mapping) { n.MappingSuccess(nat, m) }) } - -// PortMapAddrs attempts to open (and continue to keep open) -// port mappings for given addrs. This function blocks until -// all addresses have been tried. This allows clients to -// retrieve results immediately after: -// -// nat.PortMapAddrs(addrs) -// mapped := nat.ExternalAddrs() -// -// Some may not succeed, and mappings may change over time; -// NAT devices may not respect our port requests, and even lie. -// Clients should not store the mapped results, but rather always -// poll our object for the latest mappings. -func (nat *NAT) PortMapAddrs(addrs []ma.Multiaddr) { - // spin off addr mappings independently. - var wg sync.WaitGroup - for _, addr := range addrs { - // do all of them concurrently - wg.Add(1) - go func(addr ma.Multiaddr) { - defer wg.Done() - nat.NewMapping(addr) - }(addr) - } - wg.Wait() -} - -// MappedAddrs returns address mappings NAT believes have been -// successfully established. Unsuccessful mappings are nil. This is: -// -// map[internalAddr]externalAddr -// -// This set of mappings _may not_ be correct, as NAT devices are finicky. -// Consider this with _best effort_ semantics. -func (nat *NAT) MappedAddrs() map[ma.Multiaddr]ma.Multiaddr { - - mappings := nat.Mappings() - addrmap := make(map[ma.Multiaddr]ma.Multiaddr, len(mappings)) - - for _, m := range mappings { - i := m.InternalAddr() - e, err := m.ExternalAddr() - if err != nil { - addrmap[i] = nil - } else { - addrmap[i] = e - } - } - return addrmap -} - -// ExternalAddrs returns a list of addresses that NAT believes have -// been successfully established. Unsuccessful mappings are omitted, -// so nat.ExternalAddrs() may return less addresses than nat.InternalAddrs(). -// To see which addresses are mapped, use nat.MappedAddrs(). -// -// This set of mappings _may not_ be correct, as NAT devices are finicky. -// Consider this with _best effort_ semantics. -func (nat *NAT) ExternalAddrs() []ma.Multiaddr { - mappings := nat.Mappings() - addrs := make([]ma.Multiaddr, 0, len(mappings)) - for _, m := range mappings { - a, err := m.ExternalAddr() - if err != nil { - continue // this mapping not currently successful. - } - addrs = append(addrs, a) - } - return addrs -} diff --git a/p2p/net/nat/notifier.go b/p2p/net/nat/notifier.go index 462a78b953..10fb6ac6bf 100644 --- a/p2p/net/nat/notifier.go +++ b/p2p/net/nat/notifier.go @@ -36,7 +36,7 @@ type Notifiee interface { // Called when mapping a port succeeds, but the mapping is // with a different port than an earlier success. - MappingChanged(nat *NAT, m Mapping, oldport int, newport int) + MappingChanged(nat *NAT, m Mapping, oldport, newport int) // Called when a port mapping fails. NAT will continue attempting after // the next period. To stop trying, use: mapping.Close(). After this failure, From 8d26ea3591eae653289581ea1c0c84d036467a6e Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 12 Mar 2019 10:45:40 -0700 Subject: [PATCH 25/35] switch to libp2p's go-nat fork --- p2p/net/nat/nat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 938d1153b3..178f7d0140 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -7,10 +7,10 @@ import ( "sync" "time" - nat "github.com/fd/go-nat" logging "github.com/ipfs/go-log" goprocess "github.com/jbenet/goprocess" periodic "github.com/jbenet/goprocess/periodic" + nat "github.com/libp2p/go-nat" ) var ( From 0c48498807ae6d7926f4d00314af0c6ce1c3a888 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 1 May 2019 22:16:23 -0700 Subject: [PATCH 26/35] fix: remove notifier 1. It was unused. 2. It was broken. Attempting to register a notification listener would have registered the notifier itself. 3. We need to switch to some consistent event/notification system anyways so we're going to replace this code no matter what. (found by @pornin) --- p2p/net/nat/nat.go | 13 ------------ p2p/net/nat/notifier.go | 47 ----------------------------------------- 2 files changed, 60 deletions(-) delete mode 100644 p2p/net/nat/notifier.go diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 178f7d0140..046627289d 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -74,8 +74,6 @@ type NAT struct { mappingmu sync.RWMutex // guards mappings mappings map[*mapping]struct{} - - Notifier } func newNAT(realNAT nat.NAT) *NAT { @@ -186,10 +184,6 @@ func (nat *NAT) establishMapping(m *mapping) { m.setExternalPort(0) // clear mapping // TODO: log.Event log.Warningf("failed to establish port mapping: %s", err) - nat.Notifier.notifyAll(func(n Notifiee) { - n.MappingFailed(nat, m, oldport, err) - }) - // we do not close if the mapping failed, // because it may work again next time. return @@ -199,12 +193,5 @@ func (nat *NAT) establishMapping(m *mapping) { log.Debugf("NAT Mapping: %s --> %s (%s)", m.ExternalPort(), m.InternalPort(), m.Protocol()) if oldport != 0 && newport != oldport { log.Debugf("failed to renew same port mapping: ch %d -> %d", oldport, newport) - nat.Notifier.notifyAll(func(n Notifiee) { - n.MappingChanged(nat, m, oldport, newport) - }) } - - nat.Notifier.notifyAll(func(n Notifiee) { - n.MappingSuccess(nat, m) - }) } diff --git a/p2p/net/nat/notifier.go b/p2p/net/nat/notifier.go deleted file mode 100644 index 10fb6ac6bf..0000000000 --- a/p2p/net/nat/notifier.go +++ /dev/null @@ -1,47 +0,0 @@ -package nat - -import ( - notifier "github.com/whyrusleeping/go-notifier" -) - -// Notifier is an object that assists NAT in notifying listeners. -// It is implemented using thirdparty/notifier -type Notifier struct { - n notifier.Notifier -} - -func (n *Notifier) notifyAll(notify func(n Notifiee)) { - n.n.NotifyAll(func(n notifier.Notifiee) { - notify(n.(Notifiee)) - }) -} - -// Notify signs up notifiee to listen to NAT events. -func (n *Notifier) Notify(notifiee Notifiee) { - n.n.Notify(n) -} - -// StopNotify stops signaling events to notifiee. -func (n *Notifier) StopNotify(notifiee Notifiee) { - n.n.StopNotify(notifiee) -} - -// Notifiee is an interface objects must implement to listen to NAT events. -type Notifiee interface { - - // Called every time a successful mapping happens - // Warning: the port mapping may have changed. If that is the - // case, both MappingSuccess and MappingChanged are called. - MappingSuccess(nat *NAT, m Mapping) - - // Called when mapping a port succeeds, but the mapping is - // with a different port than an earlier success. - MappingChanged(nat *NAT, m Mapping, oldport, newport int) - - // Called when a port mapping fails. NAT will continue attempting after - // the next period. To stop trying, use: mapping.Close(). After this failure, - // mapping.ExternalPort() will be zero, and nat.ExternalAddrs() will not - // return the address for this mapping. With luck, the next attempt will - // succeed, without the client needing to do anything. - MappingFailed(nat *NAT, m Mapping, oldport int, err error) -} From 902962718cb4eabb6b07afa2c856a493d8652a37 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 31 Jul 2019 13:39:19 -0700 Subject: [PATCH 27/35] nit: fix log format --- p2p/net/nat/nat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 046627289d..cdddaeaec5 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -190,7 +190,7 @@ func (nat *NAT) establishMapping(m *mapping) { } m.setExternalPort(newport) - log.Debugf("NAT Mapping: %s --> %s (%s)", m.ExternalPort(), m.InternalPort(), m.Protocol()) + log.Debugf("NAT Mapping: %d --> %d (%s)", m.ExternalPort(), m.InternalPort(), m.Protocol()) if oldport != 0 && newport != oldport { log.Debugf("failed to renew same port mapping: ch %d -> %d", oldport, newport) } From 33f098383220c5579b8dfd836a1366bf06df2655 Mon Sep 17 00:00:00 2001 From: Arber Avdullahu Date: Sun, 12 Jan 2020 23:15:31 +0100 Subject: [PATCH 28/35] Update nat.go --- p2p/net/nat/nat.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index cdddaeaec5..9601bf2c88 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -121,7 +121,7 @@ func (nat *NAT) rmMapping(m *mapping) { nat.mappingmu.Unlock() } -// NewMapping attemps to construct a mapping on protocol and internal port +// NewMapping attempts to construct a mapping on protocol and internal port // It will also periodically renew the mapping until the returned Mapping // -- or its parent NAT -- is Closed. // @@ -183,7 +183,7 @@ func (nat *NAT) establishMapping(m *mapping) { if err != nil || newport == 0 { m.setExternalPort(0) // clear mapping // TODO: log.Event - log.Warningf("failed to establish port mapping: %s", err) + log.Warnf("failed to establish port mapping: %s", err) // we do not close if the mapping failed, // because it may work again next time. return From 8aecd4d9952d2b1afcd4894402967e2224619db3 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 5 May 2021 19:52:21 +0700 Subject: [PATCH 29/35] remove unused field permanent from mapping --- p2p/net/nat/mapping.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/p2p/net/nat/mapping.go b/p2p/net/nat/mapping.go index 33f25f615f..1835ed9a77 100644 --- a/p2p/net/nat/mapping.go +++ b/p2p/net/nat/mapping.go @@ -38,12 +38,11 @@ type Mapping interface { type mapping struct { sync.Mutex // guards all fields - nat *NAT - proto string - intport int - extport int - permanent bool - proc goprocess.Process + nat *NAT + proto string + intport int + extport int + proc goprocess.Process cached net.IP cacheTime time.Time From 97b8eb5fb508a63b564e387a2a42acadbbecebf5 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 15 Sep 2021 17:04:09 +0200 Subject: [PATCH 30/35] chore: update go-log --- p2p/net/nat/nat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 9601bf2c88..a29b5a9c1f 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -7,7 +7,7 @@ import ( "sync" "time" - logging "github.com/ipfs/go-log" + logging "github.com/ipfs/go-log/v2" goprocess "github.com/jbenet/goprocess" periodic "github.com/jbenet/goprocess/periodic" nat "github.com/libp2p/go-nat" From 67d483bf334838434d372b263b1dea1549019f26 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 19 Sep 2021 10:06:37 +0100 Subject: [PATCH 31/35] update go-nat to v0.1.0 --- p2p/net/nat/nat.go | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index a29b5a9c1f..a0e00b70b7 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,9 +8,10 @@ import ( "time" logging "github.com/ipfs/go-log/v2" - goprocess "github.com/jbenet/goprocess" + "github.com/libp2p/go-nat" + + "github.com/jbenet/goprocess" periodic "github.com/jbenet/goprocess/periodic" - nat "github.com/libp2p/go-nat" ) var ( @@ -30,24 +31,7 @@ const CacheTime = time.Second * 15 // DiscoverNAT looks for a NAT device in the network and // returns an object that can manage port mappings. func DiscoverNAT(ctx context.Context) (*NAT, error) { - var ( - natInstance nat.NAT - err error - ) - - done := make(chan struct{}) - go func() { - defer close(done) - // This will abort in 10 seconds anyways. - natInstance, err = nat.DiscoverGateway() - }() - - select { - case <-done: - case <-ctx.Done(): - return nil, ctx.Err() - } - + natInstance, err := nat.DiscoverGateway(ctx) if err != nil { return nil, err } From 1ce83dbab103f38e7a44d790e7893075739597cc Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 19 Sep 2021 10:44:38 +0100 Subject: [PATCH 32/35] stop using goprocess for shutdown --- p2p/net/nat/mapping.go | 15 +++--- p2p/net/nat/nat.go | 107 ++++++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 57 deletions(-) diff --git a/p2p/net/nat/mapping.go b/p2p/net/nat/mapping.go index 1835ed9a77..4ab636d40a 100644 --- a/p2p/net/nat/mapping.go +++ b/p2p/net/nat/mapping.go @@ -5,8 +5,6 @@ import ( "net" "sync" "time" - - "github.com/jbenet/goprocess" ) // Mapping represents a port mapping in a NAT. @@ -38,11 +36,11 @@ type Mapping interface { type mapping struct { sync.Mutex // guards all fields - nat *NAT - proto string - intport int - extport int - proc goprocess.Process + nat *NAT + proto string + intport int + extport int + teardown func(*mapping) cached net.IP cacheTime time.Time @@ -117,5 +115,6 @@ func (m *mapping) ExternalAddr() (net.Addr, error) { } func (m *mapping) Close() error { - return m.proc.Close() + m.teardown(m) + return nil } diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index a0e00b70b7..cad873cca7 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -8,16 +8,12 @@ import ( "time" logging "github.com/ipfs/go-log/v2" - "github.com/libp2p/go-nat" - "github.com/jbenet/goprocess" - periodic "github.com/jbenet/goprocess/periodic" + "github.com/libp2p/go-nat" ) -var ( - // ErrNoMapping signals no mapping exists for an address - ErrNoMapping = errors.New("mapping not established") -) +// ErrNoMapping signals no mapping exists for an address +var ErrNoMapping = errors.New("mapping not established") var log = logging.Logger("nat") @@ -54,29 +50,35 @@ func DiscoverNAT(ctx context.Context) (*NAT, error) { type NAT struct { natmu sync.Mutex nat nat.NAT - proc goprocess.Process + + refCount sync.WaitGroup + ctx context.Context + ctxCancel context.CancelFunc mappingmu sync.RWMutex // guards mappings + closed bool mappings map[*mapping]struct{} } func newNAT(realNAT nat.NAT) *NAT { + ctx, cancel := context.WithCancel(context.Background()) return &NAT{ - nat: realNAT, - proc: goprocess.WithParent(goprocess.Background()), - mappings: make(map[*mapping]struct{}), + nat: realNAT, + mappings: make(map[*mapping]struct{}), + ctx: ctx, + ctxCancel: cancel, } } // Close shuts down all port mappings. NAT can no longer be used. func (nat *NAT) Close() error { - return nat.proc.Close() -} + nat.mappingmu.Lock() + nat.closed = true + nat.mappingmu.Unlock() -// Process returns the nat's life-cycle manager, for making it listen -// to close signals. -func (nat *NAT) Process() goprocess.Process { - return nat.proc + nat.ctxCancel() + nat.refCount.Wait() + return nil } // Mappings returns a slice of all NAT mappings @@ -90,21 +92,6 @@ func (nat *NAT) Mappings() []Mapping { return maps2 } -func (nat *NAT) addMapping(m *mapping) { - // make mapping automatically close when nat is closed. - nat.proc.AddChild(m.proc) - - nat.mappingmu.Lock() - nat.mappings[m] = struct{}{} - nat.mappingmu.Unlock() -} - -func (nat *NAT) rmMapping(m *mapping) { - nat.mappingmu.Lock() - delete(nat.mappings, m) - nat.mappingmu.Unlock() -} - // NewMapping attempts to construct a mapping on protocol and internal port // It will also periodically renew the mapping until the returned Mapping // -- or its parent NAT -- is Closed. @@ -125,24 +112,21 @@ func (nat *NAT) NewMapping(protocol string, port int) (Mapping, error) { } m := &mapping{ - intport: port, - nat: nat, - proto: protocol, + intport: port, + nat: nat, + proto: protocol, + teardown: nat.removeMapping, } - m.proc = goprocess.WithTeardown(func() error { - nat.rmMapping(m) - nat.natmu.Lock() - defer nat.natmu.Unlock() - nat.nat.DeletePortMapping(m.Protocol(), m.InternalPort()) - return nil - }) - - nat.addMapping(m) - - m.proc.AddChild(periodic.Every(MappingDuration/3, func(worker goprocess.Process) { - nat.establishMapping(m) - })) + nat.mappingmu.Lock() + if nat.closed { + nat.mappingmu.Unlock() + return nil, errors.New("closed") + } + nat.mappings[m] = struct{}{} + nat.refCount.Add(1) + nat.mappingmu.Unlock() + go nat.refreshMappings(m) // do it once synchronously, so first mapping is done right away, and before exiting, // allowing users -- in the optimistic case -- to use results right after. @@ -150,11 +134,36 @@ func (nat *NAT) NewMapping(protocol string, port int) (Mapping, error) { return m, nil } +func (nat *NAT) removeMapping(m *mapping) { + nat.mappingmu.Lock() + delete(nat.mappings, m) + nat.mappingmu.Unlock() + nat.natmu.Lock() + nat.nat.DeletePortMapping(m.Protocol(), m.InternalPort()) + nat.natmu.Unlock() +} + +func (nat *NAT) refreshMappings(m *mapping) { + defer nat.refCount.Done() + t := time.NewTicker(MappingDuration / 3) + defer t.Stop() + + for { + select { + case <-t.C: + nat.establishMapping(m) + case <-nat.ctx.Done(): + m.Close() + return + } + } +} + func (nat *NAT) establishMapping(m *mapping) { oldport := m.ExternalPort() log.Debugf("Attempting port map: %s/%d", m.Protocol(), m.InternalPort()) - comment := "libp2p" + const comment = "libp2p" nat.natmu.Lock() newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, MappingDuration) From 5e8e3d8385f24e8a839dcf29efaebb98bef45daa Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 25 Sep 2021 14:50:47 +0100 Subject: [PATCH 33/35] remove mapping.teardown --- p2p/net/nat/mapping.go | 11 +++++------ p2p/net/nat/nat.go | 7 +++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/p2p/net/nat/mapping.go b/p2p/net/nat/mapping.go index 4ab636d40a..f9b508e4e2 100644 --- a/p2p/net/nat/mapping.go +++ b/p2p/net/nat/mapping.go @@ -36,11 +36,10 @@ type Mapping interface { type mapping struct { sync.Mutex // guards all fields - nat *NAT - proto string - intport int - extport int - teardown func(*mapping) + nat *NAT + proto string + intport int + extport int cached net.IP cacheTime time.Time @@ -115,6 +114,6 @@ func (m *mapping) ExternalAddr() (net.Addr, error) { } func (m *mapping) Close() error { - m.teardown(m) + m.nat.removeMapping(m) return nil } diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index cad873cca7..dad3226b46 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -112,10 +112,9 @@ func (nat *NAT) NewMapping(protocol string, port int) (Mapping, error) { } m := &mapping{ - intport: port, - nat: nat, - proto: protocol, - teardown: nat.removeMapping, + intport: port, + nat: nat, + proto: protocol, } nat.mappingmu.Lock() From 7201aee2f851f1a84e5187e5d0b1a1d44d00de5f Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Fri, 1 Oct 2021 18:42:56 -0400 Subject: [PATCH 34/35] Fix error log --- p2p/net/nat/nat.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index dad3226b46..e2656f8bcc 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -175,7 +175,11 @@ func (nat *NAT) establishMapping(m *mapping) { if err != nil || newport == 0 { m.setExternalPort(0) // clear mapping // TODO: log.Event - log.Warnf("failed to establish port mapping: %s", err) + if err != nil { + log.Warnf("failed to establish port mapping: %s", err) + } else { + log.Warnf("failed to establish port mapping: newport = 0") + } // we do not close if the mapping failed, // because it may work again next time. return From f656048800082a48fef7bba5457e67d131934bcb Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Fri, 20 May 2022 12:37:09 +0200 Subject: [PATCH 35/35] switch from github.com/libp2p/go-libp2p-nat to p2p/net/nat --- go.mod | 3 +-- go.sum | 4 ---- p2p/host/basic/basic_host.go | 2 +- p2p/host/basic/natmgr.go | 4 +++- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 157942b1c8..2ab8784891 100644 --- a/go.mod +++ b/go.mod @@ -22,12 +22,12 @@ require ( github.com/libp2p/go-libp2p-asn-util v0.1.0 github.com/libp2p/go-libp2p-circuit v0.6.0 github.com/libp2p/go-libp2p-core v0.15.1 - github.com/libp2p/go-libp2p-nat v0.1.0 github.com/libp2p/go-libp2p-peerstore v0.6.0 github.com/libp2p/go-libp2p-resource-manager v0.2.1 github.com/libp2p/go-libp2p-testing v0.9.2 github.com/libp2p/go-mplex v0.7.0 github.com/libp2p/go-msgio v0.2.0 + github.com/libp2p/go-nat v0.1.0 github.com/libp2p/go-netroute v0.2.0 github.com/libp2p/go-reuseport v0.1.0 github.com/libp2p/go-reuseport-transport v0.1.0 @@ -84,7 +84,6 @@ require ( github.com/libp2p/go-libp2p-tls v0.4.1 // indirect github.com/libp2p/go-libp2p-transport-upgrader v0.7.1 // indirect github.com/libp2p/go-libp2p-yamux v0.9.1 // indirect - github.com/libp2p/go-nat v0.1.0 // indirect github.com/libp2p/go-openssl v0.0.7 // indirect github.com/libp2p/go-tcp-transport v0.5.1 // indirect github.com/marten-seemann/qtls-go1-16 v0.1.5 // indirect diff --git a/go.sum b/go.sum index ff34722db0..aba0d03037 100644 --- a/go.sum +++ b/go.sum @@ -344,7 +344,6 @@ github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9 github.com/ipfs/go-log v1.0.4/go.mod h1:oDCg2FkjogeFOhqqb+N39l2RpTNPL6F/StPkB3kPgcs= github.com/ipfs/go-log v1.0.5 h1:2dOuUCB1Z7uoczMWgAyDck5JLb72zHzrMnGnCNNbvY8= github.com/ipfs/go-log v1.0.5/go.mod h1:j0b8ZoR+7+R99LD9jZ6+AJsrzkPbSXbZfGakb5JPtIo= -github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0= github.com/ipfs/go-log/v2 v2.0.5/go.mod h1:eZs4Xt4ZUJQFM3DlanGhy7TkwwawCZcSByscwkWG+dw= github.com/ipfs/go-log/v2 v2.1.1/go.mod h1:2v2nsGfZsvvAJz13SyFzf9ObaqwHiHxsPLEHntrv9KM= github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g= @@ -442,8 +441,6 @@ github.com/libp2p/go-libp2p-core v0.15.1 h1:0RY+Mi/ARK9DgG1g9xVQLb8dDaaU8tCePMtG github.com/libp2p/go-libp2p-core v0.15.1/go.mod h1:agSaboYM4hzB1cWekgVReqV5M4g5M+2eNNejV+1EEhs= github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g= github.com/libp2p/go-libp2p-mplex v0.5.0/go.mod h1:eLImPJLkj3iG5t5lq68w3Vm5NAQ5BcKwrrb2VmOYb3M= -github.com/libp2p/go-libp2p-nat v0.1.0 h1:vigUi2MEN+fwghe5ijpScxtbbDz+L/6y8XwlzYOJgSY= -github.com/libp2p/go-libp2p-nat v0.1.0/go.mod h1:DQzAG+QbDYjN1/C3B6vXucLtz3u9rEonLVPtZVzQqks= github.com/libp2p/go-libp2p-peerstore v0.4.0/go.mod h1:rDJUFyzEWPpXpEwywkcTYYzDHlwza8riYMaUzaN6hX0= github.com/libp2p/go-libp2p-peerstore v0.6.0 h1:HJminhQSGISBIRb93N6WK3t6Fa8OOTnHd/VBjL4mY5A= github.com/libp2p/go-libp2p-peerstore v0.6.0/go.mod h1:DGEmKdXrcYpK9Jha3sS7MhqYdInxJy84bIPtSu65bKc= @@ -852,7 +849,6 @@ go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= -go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 5a5f039a58..9073639ba1 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -12,6 +12,7 @@ import ( "github.com/libp2p/go-libp2p/p2p/host/autonat" "github.com/libp2p/go-libp2p/p2p/host/pstoremanager" "github.com/libp2p/go-libp2p/p2p/host/relaysvc" + inat "github.com/libp2p/go-libp2p/p2p/net/nat" relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay" "github.com/libp2p/go-libp2p/p2p/protocol/holepunch" "github.com/libp2p/go-libp2p/p2p/protocol/identify" @@ -28,7 +29,6 @@ import ( "github.com/libp2p/go-libp2p-core/record" "github.com/libp2p/go-eventbus" - inat "github.com/libp2p/go-libp2p-nat" "github.com/libp2p/go-netroute" logging "github.com/ipfs/go-log/v2" diff --git a/p2p/host/basic/natmgr.go b/p2p/host/basic/natmgr.go index 8b41bfa820..af1de0ec67 100644 --- a/p2p/host/basic/natmgr.go +++ b/p2p/host/basic/natmgr.go @@ -8,8 +8,10 @@ import ( "sync" "time" + inat "github.com/libp2p/go-libp2p/p2p/net/nat" + "github.com/libp2p/go-libp2p-core/network" - inat "github.com/libp2p/go-libp2p-nat" + ma "github.com/multiformats/go-multiaddr" )