Added Upnp support behind a configuration option, moved the logic to the server code
This commit is contained in:
parent
9a3dfaf332
commit
b59077109c
@ -47,6 +47,7 @@ const (
|
|||||||
defaultRPCHost = "localhost"
|
defaultRPCHost = "localhost"
|
||||||
defaultMaxPendingChannels = 1
|
defaultMaxPendingChannels = 1
|
||||||
defaultNoEncryptWallet = false
|
defaultNoEncryptWallet = false
|
||||||
|
defaultUpnpSupport = false
|
||||||
defaultTrickleDelay = 30 * 1000
|
defaultTrickleDelay = 30 * 1000
|
||||||
defaultMaxLogFiles = 3
|
defaultMaxLogFiles = 3
|
||||||
defaultMaxLogFileSize = 10
|
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."`
|
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."`
|
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."`
|
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"`
|
Bitcoin *chainConfig `group:"Bitcoin" namespace:"bitcoin"`
|
||||||
BtcdMode *btcdConfig `group:"btcd" namespace:"btcd"`
|
BtcdMode *btcdConfig `group:"btcd" namespace:"btcd"`
|
||||||
@ -248,6 +250,7 @@ func loadConfig() (*config, error) {
|
|||||||
LogDir: defaultLogDir,
|
LogDir: defaultLogDir,
|
||||||
MaxLogFiles: defaultMaxLogFiles,
|
MaxLogFiles: defaultMaxLogFiles,
|
||||||
MaxLogFileSize: defaultMaxLogFileSize,
|
MaxLogFileSize: defaultMaxLogFileSize,
|
||||||
|
UpnpSupport: defaultUpnpSupport,
|
||||||
Bitcoin: &chainConfig{
|
Bitcoin: &chainConfig{
|
||||||
MinHTLC: defaultBitcoinMinHTLCMSat,
|
MinHTLC: defaultBitcoinMinHTLCMSat,
|
||||||
BaseFee: defaultBitcoinBaseFeeMSat,
|
BaseFee: defaultBitcoinBaseFeeMSat,
|
||||||
|
25
lnd.go
25
lnd.go
@ -26,8 +26,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/NebulousLabs/go-upnp"
|
|
||||||
|
|
||||||
"gopkg.in/macaroon-bakery.v2/bakery"
|
"gopkg.in/macaroon-bakery.v2/bakery"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
@ -312,29 +310,6 @@ func lndMain() error {
|
|||||||
"is proxying over Tor as well", cfg.Tor.StreamIsolation)
|
"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
|
// Set up the core server which will listen for incoming peer
|
||||||
// connections.
|
// connections.
|
||||||
server, err := newServer(
|
server, err := newServer(
|
||||||
|
34
server.go
34
server.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
@ -15,6 +16,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
upnp "github.com/NebulousLabs/go-upnp"
|
||||||
"github.com/coreos/bbolt"
|
"github.com/coreos/bbolt"
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/lightningnetwork/lightning-onion"
|
"github.com/lightningnetwork/lightning-onion"
|
||||||
@ -300,6 +302,38 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl,
|
|||||||
return nil, err
|
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
|
// If external IP addresses have been specified, add those to the list
|
||||||
// of this server's addresses.
|
// of this server's addresses.
|
||||||
selfAddrs := make([]net.Addr, 0, len(cfg.ExternalIPs))
|
selfAddrs := make([]net.Addr, 0, len(cfg.ExternalIPs))
|
||||||
|
Loading…
Reference in New Issue
Block a user