Added Upnp support behind a configuration option, moved the logic to the server code

This commit is contained in:
John Griffith 2017-12-24 23:15:40 +00:00 committed by Wilmer Paulino
parent 9a3dfaf332
commit b59077109c
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
3 changed files with 37 additions and 25 deletions

View File

@ -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,

25
lnd.go
View File

@ -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(

View File

@ -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))