Browse Source

p2p: allow listener with no external connection

pull/665/head
Ethan Buchman 7 years ago
parent
commit
ddb3d8945d
3 changed files with 9 additions and 16 deletions
  1. +1
    -12
      node/node.go
  2. +7
    -4
      p2p/listener.go
  3. +1
    -0
      rpc/lib/types/types.go

+ 1
- 12
node/node.go View File

@ -309,7 +309,7 @@ func NewNode(config *cfg.Config,
// OnStart starts the Node. It implements cmn.Service. // OnStart starts the Node. It implements cmn.Service.
func (n *Node) OnStart() error { func (n *Node) OnStart() error {
// Create & add listener // Create & add listener
protocol, address := ProtocolAndAddress(n.config.P2P.ListenAddress)
protocol, address := cmn.ProtocolAndAddress(n.config.P2P.ListenAddress)
l := p2p.NewDefaultListener(protocol, address, n.config.P2P.SkipUPNP, n.Logger.With("module", "p2p")) l := p2p.NewDefaultListener(protocol, address, n.config.P2P.SkipUPNP, n.Logger.With("module", "p2p"))
n.sw.AddListener(l) n.sw.AddListener(l)
@ -535,15 +535,4 @@ func (n *Node) DialSeeds(seeds []string) error {
return n.sw.DialSeeds(n.addrBook, seeds) return n.sw.DialSeeds(n.addrBook, seeds)
} }
// ProtocolAndAddress returns the transport protocol
// and the ip address from the given string. Defaults to tcp.
func ProtocolAndAddress(listenAddr string) (string, string) {
protocol, address := "tcp", listenAddr
parts := strings.SplitN(address, "://", 2)
if len(parts) == 2 {
protocol, address = parts[0], parts[1]
}
return protocol, address
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

+ 7
- 4
p2p/listener.go View File

@ -87,7 +87,7 @@ func NewDefaultListener(protocol string, lAddr string, skipUPNP bool, logger log
} }
// Otherwise just use the local address... // Otherwise just use the local address...
if extAddr == nil { if extAddr == nil {
extAddr = getNaiveExternalAddress(listenerPort)
extAddr = getNaiveExternalAddress(listenerPort, false, logger)
} }
if extAddr == nil { if extAddr == nil {
cmn.PanicCrisis("Could not determine external address!") cmn.PanicCrisis("Could not determine external address!")
@ -197,7 +197,7 @@ func getUPNPExternalAddress(externalPort, internalPort int, logger log.Logger) *
} }
// TODO: use syscalls: http://pastebin.com/9exZG4rh // TODO: use syscalls: http://pastebin.com/9exZG4rh
func getNaiveExternalAddress(port int) *NetAddress {
func getNaiveExternalAddress(port int, settleForLocal bool, logger log.Logger) *NetAddress {
addrs, err := net.InterfaceAddrs() addrs, err := net.InterfaceAddrs()
if err != nil { if err != nil {
cmn.PanicCrisis(cmn.Fmt("Could not fetch interface addresses: %v", err)) cmn.PanicCrisis(cmn.Fmt("Could not fetch interface addresses: %v", err))
@ -209,10 +209,13 @@ func getNaiveExternalAddress(port int) *NetAddress {
continue continue
} }
v4 := ipnet.IP.To4() v4 := ipnet.IP.To4()
if v4 == nil || v4[0] == 127 {
if v4 == nil || (!settleForLocal && v4[0] == 127) {
continue continue
} // loopback } // loopback
return NewNetAddressIPPort(ipnet.IP, uint16(port)) return NewNetAddressIPPort(ipnet.IP, uint16(port))
} }
return nil
// try again, but settle for local
logger.Info("Node may not be connected to internet. Settling for local address")
return getNaiveExternalAddress(port, true, logger)
} }

+ 1
- 0
rpc/lib/types/types.go View File

@ -144,6 +144,7 @@ type WSRPCContext struct {
// //
// Determine if its a unix or tcp socket. // Determine if its a unix or tcp socket.
// If tcp, must specify the port; `0.0.0.0` will return incorrectly as "unix" since there's no port // If tcp, must specify the port; `0.0.0.0` will return incorrectly as "unix" since there's no port
// TODO: deprecate
func SocketType(listenAddr string) string { func SocketType(listenAddr string) string {
socketType := "unix" socketType := "unix"
if len(strings.Split(listenAddr, ":")) >= 2 { if len(strings.Split(listenAddr, ":")) >= 2 {


Loading…
Cancel
Save