mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 01:43:47 +00:00
p2p/sim, swarm/network: re-use externalIP method
This commit is contained in:
parent
90c0190b7e
commit
8c05e9b252
2 changed files with 18 additions and 31 deletions
|
|
@ -338,6 +338,21 @@ type execNodeConfig struct {
|
||||||
PeerAddrs map[string]string `json:"peer_addrs,omitempty"`
|
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
|
// 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]
|
// 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
|
// 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.P2P.PrivateKey = conf.Node.PrivateKey
|
||||||
conf.Stack.Logger = log.New("node.id", conf.Node.ID.String())
|
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, ":") {
|
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" {
|
if conf.Stack.WSHost == "0.0.0.0" {
|
||||||
conf.Stack.WSHost = externalIP()
|
conf.Stack.WSHost = ExternalIP().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize the devp2p stack
|
// initialize the devp2p stack
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -307,21 +306,8 @@ func triggerChecks(trigger chan discover.NodeID, net *simulations.Network, id di
|
||||||
return nil
|
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) {
|
func newService(ctx *adapters.ServiceContext) (node.Service, error) {
|
||||||
host := getOutboundIP()
|
host := adapters.ExternalIP()
|
||||||
|
|
||||||
addr := network.NewAddrFromNodeIDAndPort(ctx.Config.ID, host, ctx.Config.Port)
|
addr := network.NewAddrFromNodeIDAndPort(ctx.Config.ID, host, ctx.Config.Port)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue