Skip to content

Commit

Permalink
server: extract NAT traversal port forwarding setup into method
Browse files Browse the repository at this point in the history
  • Loading branch information
wpaulino committed Aug 10, 2018
1 parent 91a63f8 commit 95fb13e
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,27 +377,6 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, cc *chainControl,
}
}

// If we were requested to automatically configure port forwarding,
// we'll use the ports that the server will be listening on.
if s.natTraversal != nil {
// We'll only forward the port of the first address the server
// is listening on since our node announcement cannot advertise
// more than one IPv4 address.
port := s.listenAddrs[0].(*net.TCPAddr).Port
addr, err := s.configurePortForwarding(port)
if err != nil {
srvrLog.Errorf("Unable to automatically set up port "+
"forwarding using %s: %v",
s.natTraversal.Name(), err)
} else {
srvrLog.Infof("Automatically set up port forwarding "+
"using %s to advertise external IP",
s.natTraversal.Name())

cfg.ExternalIPs = append(cfg.ExternalIPs, addr)
}
}

// If we were requested to route connections through Tor and to
// automatically create an onion service, we'll initiate our Tor
// controller and establish a connection to the Tor server.
Expand Down Expand Up @@ -888,8 +867,9 @@ func (s *server) Start() error {
}

if s.natTraversal != nil {
s.wg.Add(1)
go s.watchExternalIP()
if err := s.initNATTraversal(); err != nil {
return err
}
}

// Start the notification server. This is used so channel management
Expand Down Expand Up @@ -1012,6 +992,31 @@ func (s *server) Stopped() bool {
return atomic.LoadInt32(&s.shutdown) != 0
}

// initNATTraversal uses the chosen NAT traversal technique to configure port
// forwarding rules, advertise the IP address found, and watch for IP address
// changes.
func (s *server) initNATTraversal() error {
// We'll only forward the port of the first address the server
// is listening on since our node announcement cannot advertise
// more than one IPv4 address.
port := s.listenAddrs[0].(*net.TCPAddr).Port
addr, err := s.configurePortForwarding(port)
if err != nil {
return fmt.Errorf("unable to automatically set up port "+
"forwarding using %s: %v", s.natTraversal.Name(), err)
}

srvrLog.Infof("Automatically set up port forwarding using %s to "+
"advertise external IP", s.natTraversal.Name())

s.currentNodeAnn.Addresses = append(s.currentNodeAnn.Addresses, addr)

s.wg.Add(1)
go s.watchExternalIP()

return nil
}

// configurePortForwarding attempts to set up port forwarding for one of the
// ports the server will be listening on and returns the externally reachable
// address.
Expand Down

0 comments on commit 95fb13e

Please sign in to comment.