From 8c05e9b25280e21d0c8f77cb5c803d72b4d4a2cc Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Sun, 4 Mar 2018 16:17:49 +0100 Subject: [PATCH] p2p/sim, swarm/network: re-use externalIP method --- p2p/simulations/adapters/exec.go | 33 ++++++++++--------- .../simulations/discovery/discovery_test.go | 16 +-------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/p2p/simulations/adapters/exec.go b/p2p/simulations/adapters/exec.go index 18a8073c62..64a4704d48 100644 --- a/p2p/simulations/adapters/exec.go +++ b/p2p/simulations/adapters/exec.go @@ -338,6 +338,21 @@ type execNodeConfig struct { PeerAddrs map[string]string `json:"peer_addrs,omitempty"` } +// ExternalIP gets an external IP address so that Enode URL is usable +func ExternalIP() net.IP { + addrs, err := net.InterfaceAddrs() + if err != nil { + log.Crit("error getting IP address", "err", err) + } + for _, addr := range addrs { + if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() { + return ip.IP + } + } + log.Crit("unable to determine explicit IP address") + return net.IP{127, 0, 0, 1} +} + // execP2PNode starts a devp2p node when the current binary is executed with // argv[0] being "p2p-node", reading the service / ID from argv[1] / argv[2] // and the node config from the _P2P_NODE_CONFIG environment variable @@ -361,25 +376,11 @@ func execP2PNode() { conf.Stack.P2P.PrivateKey = conf.Node.PrivateKey conf.Stack.Logger = log.New("node.id", conf.Node.ID.String()) - // use explicit IP address in ListenAddr so that Enode URL is usable - externalIP := func() string { - addrs, err := net.InterfaceAddrs() - if err != nil { - log.Crit("error getting IP address", "err", err) - } - for _, addr := range addrs { - if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() { - return ip.IP.String() - } - } - log.Crit("unable to determine explicit IP address") - return "" - } if strings.HasPrefix(conf.Stack.P2P.ListenAddr, ":") { - conf.Stack.P2P.ListenAddr = externalIP() + conf.Stack.P2P.ListenAddr + conf.Stack.P2P.ListenAddr = ExternalIP().String() + conf.Stack.P2P.ListenAddr } if conf.Stack.WSHost == "0.0.0.0" { - conf.Stack.WSHost = externalIP() + conf.Stack.WSHost = ExternalIP().String() } // initialize the devp2p stack diff --git a/swarm/network/simulations/discovery/discovery_test.go b/swarm/network/simulations/discovery/discovery_test.go index 76b67d598f..2782aa84a5 100644 --- a/swarm/network/simulations/discovery/discovery_test.go +++ b/swarm/network/simulations/discovery/discovery_test.go @@ -8,7 +8,6 @@ import ( "fmt" "io/ioutil" "math/rand" - "net" "os" "sync" "testing" @@ -307,21 +306,8 @@ func triggerChecks(trigger chan discover.NodeID, net *simulations.Network, id di return nil } -// getOutboundIP gets preferred outbound ip of this machine/container -func getOutboundIP() net.IP { - conn, err := net.Dial("udp", "8.8.8.8:80") - if err != nil { - panic(err) - } - defer conn.Close() - - localAddr := conn.LocalAddr().(*net.UDPAddr) - - return localAddr.IP -} - func newService(ctx *adapters.ServiceContext) (node.Service, error) { - host := getOutboundIP() + host := adapters.ExternalIP() addr := network.NewAddrFromNodeIDAndPort(ctx.Config.ID, host, ctx.Config.Port)