diff --git a/config.go b/config.go index 091a7f4d..55960acf 100644 --- a/config.go +++ b/config.go @@ -47,6 +47,7 @@ const ( defaultRPCHost = "localhost" defaultMaxPendingChannels = 1 defaultNoEncryptWallet = false + defaultUpnpSupport = false defaultTrickleDelay = 30 * 1000 defaultMaxLogFiles = 3 defaultMaxLogFileSize = 10 @@ -195,6 +196,7 @@ type config struct { UnsafeDisconnect bool `long:"unsafe-disconnect" description:"Allows the rpcserver to intentionally disconnect from peers with open channels. USED FOR TESTING ONLY."` UnsafeReplay bool `long:"unsafe-replay" description:"Causes a link to replay the adds on its commitment txn after starting up, this enables testing of the sphinx replay logic."` MaxPendingChannels int `long:"maxpendingchannels" description:"The maximum number of incoming pending channels permitted per peer."` + UpnpSupport bool `long:"upnpsupport" description:"Toggle Upnp support for auto network discovery"` Bitcoin *chainConfig `group:"Bitcoin" namespace:"bitcoin"` BtcdMode *btcdConfig `group:"btcd" namespace:"btcd"` @@ -248,6 +250,7 @@ func loadConfig() (*config, error) { LogDir: defaultLogDir, MaxLogFiles: defaultMaxLogFiles, MaxLogFileSize: defaultMaxLogFileSize, + UpnpSupport: defaultUpnpSupport, Bitcoin: &chainConfig{ MinHTLC: defaultBitcoinMinHTLCMSat, BaseFee: defaultBitcoinBaseFeeMSat, diff --git a/lnd.go b/lnd.go index 88a6da79..edae743a 100644 --- a/lnd.go +++ b/lnd.go @@ -26,8 +26,6 @@ import ( "sync" "time" - "github.com/NebulousLabs/go-upnp" - "gopkg.in/macaroon-bakery.v2/bakery" "golang.org/x/net/context" @@ -312,29 +310,6 @@ func lndMain() error { "is proxying over Tor as well", cfg.Tor.StreamIsolation) } - // Connect to router - d, err := upnp.Discover() - if err != nil { - fmt.Printf("Unable to discover router %v\n", err) - return err - } - - // Get external IP - ip, err := d.ExternalIP() - if err != nil { - fmt.Printf("Unable to get external ip %v\n", err) - return err - } - - ltndLog.Infof("Your external IP is: %s", ip) - - // Forward peer port - err = d.Forward(uint16(cfg.PeerPort), "lnd pear port") - if err != nil { - fmt.Printf("Unable to forward pear port ip %v\n", err) - return err - } - // Set up the core server which will listen for incoming peer // connections. server, err := newServer( diff --git a/server.go b/server.go index 5f0becd3..817d591b 100644 --- a/server.go +++ b/server.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "context" "crypto/rand" "crypto/sha256" "encoding/hex" @@ -15,6 +16,7 @@ import ( "sync/atomic" "time" + upnp "github.com/NebulousLabs/go-upnp" "github.com/coreos/bbolt" "github.com/go-errors/errors" "github.com/lightningnetwork/lightning-onion" @@ -300,6 +302,38 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, return nil, err } + // Gather external IPs from config + externalIPs := cfg.ExternalIPs + + if cfg.UpnpSupport { + + // Connect to router + d, err := upnp.DiscoverCtx(context.Background()) + if err != nil { + fmt.Printf("Upnp: Unable to discover router %v\n", err) + return nil, err + } + + // Get external IP + ip, err := d.ExternalIP() + if err != nil { + fmt.Printf("Upnp: Unable to get external ip %v\n", err) + return nil, err + } + + ltndLog.Infof("Your external IP is: %s", ip) + + // Forward peer port + err = d.Forward(uint16(cfg.PeerPort), "lnd peer port") + if err != nil { + fmt.Printf("Upnp: Unable to forward pear port ip %v\n", err) + return nil, err + } + + externalIPs = append(externalIPs, ip) + + } + // If external IP addresses have been specified, add those to the list // of this server's addresses. selfAddrs := make([]net.Addr, 0, len(cfg.ExternalIPs))