added error reporting to nat.Map

This commit is contained in:
StephenButtolph 2020-06-24 11:41:22 -04:00
parent 63ea29064d
commit eefaed4b1a
3 changed files with 16 additions and 9 deletions

View File

@ -67,13 +67,15 @@ func main() {
mapper := nat.NewPortMapper(log, Config.Nat) mapper := nat.NewPortMapper(log, Config.Nat)
defer mapper.UnmapAllPorts() defer mapper.UnmapAllPorts()
Config.StakingIP.Port = mapper.Map("TCP", Config.StakingLocalPort, "gecko-staking") // Open staking port port, err := mapper.Map("TCP", Config.StakingLocalPort, "gecko-staking") // Open staking port
if Config.HTTPHost != "127.0.0.1" && Config.HTTPHost != "localhost" { // Open HTTP port iff HTTP server not listening on localhost if err == nil {
mapper.Map("TCP", Config.HTTPPort, "gecko-http") Config.StakingIP.Port = port
} else {
log.Warn("NAT traversal has failed. The node will be able to connect to less nodes.")
} }
if Config.StakingIP.IsZero() { if Config.HTTPHost != "127.0.0.1" && Config.HTTPHost != "localhost" { // Open HTTP port iff HTTP server not listening on localhost
log.Warn("NAT traversal has failed. The node will be able to connect to less nodes.") _, _ = mapper.Map("TCP", Config.HTTPPort, "gecko-http")
} }
node := node.Node{} node := node.Node{}

View File

@ -284,16 +284,16 @@ func init() {
Config.DB = memdb.New() Config.DB = memdb.New()
} }
Config.Nat = nat.GetRouter()
var ip net.IP var ip net.IP
// If public IP is not specified, get it using shell command dig // If public IP is not specified, get it using shell command dig
if *consensusIP == "" { if *consensusIP == "" {
Config.Nat = nat.GetRouter()
ip, err = Config.Nat.ExternalIP() ip, err = Config.Nat.ExternalIP()
if err != nil { if err != nil {
ip = net.IPv4zero // Couldn't get my IP...set to 0.0.0.0 ip = net.IPv4zero // Couldn't get my IP...set to 0.0.0.0
} }
} else { } else {
Config.Nat = nat.NewNoRouter()
ip = net.ParseIP(*consensusIP) ip = net.ParseIP(*consensusIP)
} }

View File

@ -4,6 +4,7 @@
package nat package nat
import ( import (
"errors"
"net" "net"
"sync" "sync"
"time" "time"
@ -63,12 +64,16 @@ func NewPortMapper(log logging.Logger, r Router) Mapper {
// Map sets up port mapping using given protocol, internal and external ports // Map sets up port mapping using given protocol, internal and external ports
// and returns the final port mapped. It returns 0 if mapping failed after the // and returns the final port mapped. It returns 0 if mapping failed after the
// maximun number of retries // maximun number of retries
func (dev *Mapper) Map(protocol string, intPort uint16, desc string) uint16 { func (dev *Mapper) Map(protocol string, intPort uint16, desc string) (uint16, error) {
mappedPort := make(chan uint16) mappedPort := make(chan uint16)
go dev.keepPortMapping(mappedPort, protocol, intPort, desc) go dev.keepPortMapping(mappedPort, protocol, intPort, desc)
return <-mappedPort port := <-mappedPort
if port == 0 {
return 0, errors.New("failed to map port")
}
return port, nil
} }
// keepPortMapping runs in the background to keep a port mapped. It renews the // keepPortMapping runs in the background to keep a port mapped. It renews the