From b4a7d56fe3793acd1f3b8283b077086e5c958d43 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 4 Oct 2018 14:02:59 +0200 Subject: [PATCH] p2p: avoid goroutine for nat.ExtIP This makes the node URL in logs correct immediately after startup. --- p2p/nat/nat.go | 18 ++++++------------ p2p/server.go | 9 ++++++++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/p2p/nat/nat.go b/p2p/nat/nat.go index a254648c66..8fad921c48 100644 --- a/p2p/nat/nat.go +++ b/p2p/nat/nat.go @@ -129,21 +129,15 @@ func Map(m Interface, c chan struct{}, protocol string, extport, intport int, na // ExtIP assumes that the local machine is reachable on the given // external IP address, and that any required ports were mapped manually. // Mapping operations will not return an error but won't actually do anything. -func ExtIP(ip net.IP) Interface { - if ip == nil { - panic("IP must not be nil") - } - return extIP(ip) -} +type ExtIP net.IP -type extIP net.IP - -func (n extIP) ExternalIP() (net.IP, error) { return net.IP(n), nil } -func (n extIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) } +func (n ExtIP) ExternalIP() (net.IP, error) { return net.IP(n), nil } +func (n ExtIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) } // These do nothing. -func (extIP) AddMapping(string, int, int, string, time.Duration) error { return nil } -func (extIP) DeleteMapping(string, int, int) error { return nil } + +func (ExtIP) AddMapping(string, int, int, string, time.Duration) error { return nil } +func (ExtIP) DeleteMapping(string, int, int) error { return nil } // Any returns a port mapper that tries to discover any supported // mechanism on the local network. diff --git a/p2p/server.go b/p2p/server.go index c62a0e77b8..4e3330c136 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -488,7 +488,14 @@ func (srv *Server) setupLocalNode() error { srv.localnode.Set(e) } } - if srv.NAT != nil { + switch srv.NAT.(type) { + case nil: + // No NAT interface, do nothing. + case nat.ExtIP: + // ExtIP doesn't block, set the IP right away. + ip, _ := srv.NAT.ExternalIP() + srv.localnode.SetStaticIP(ip) + default: // Ask the router about the IP. This takes a while and blocks startup, // do it in the background. srv.loopWG.Add(1)