server: add support to advertise onion addresses
In this commit, we allow `lnd` to properly parse onion addresses in order to advertise them to the network when set through the `--externalip` flag. Co-Authored-By: Eugene <crypt-iq@users.noreply.github.com>
This commit is contained in:
parent
978fc7ba08
commit
3669a40c52
53
server.go
53
server.go
@ -28,6 +28,7 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/lnwallet"
|
"github.com/lightningnetwork/lnd/lnwallet"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/lightningnetwork/lnd/routing"
|
"github.com/lightningnetwork/lnd/routing"
|
||||||
|
"github.com/lightningnetwork/lnd/tor"
|
||||||
"github.com/roasbeef/btcd/btcec"
|
"github.com/roasbeef/btcd/btcec"
|
||||||
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
||||||
"github.com/roasbeef/btcd/connmgr"
|
"github.com/roasbeef/btcd/connmgr"
|
||||||
@ -139,6 +140,42 @@ type server struct {
|
|||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseAddr parses an address from its string format to a net.Addr.
|
||||||
|
func parseAddr(address string) (net.Addr, error) {
|
||||||
|
var (
|
||||||
|
host string
|
||||||
|
port int
|
||||||
|
)
|
||||||
|
|
||||||
|
// Split the address into its host and port components.
|
||||||
|
h, p, err := net.SplitHostPort(address)
|
||||||
|
if err != nil {
|
||||||
|
// If a port wasn't specified, we'll assume the address only
|
||||||
|
// contains the host so we'll use the default port.
|
||||||
|
host = address
|
||||||
|
port = defaultPeerPort
|
||||||
|
} else {
|
||||||
|
// Otherwise, we'll note both the host and ports.
|
||||||
|
host = h
|
||||||
|
portNum, err := strconv.Atoi(p)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
port = portNum
|
||||||
|
}
|
||||||
|
|
||||||
|
if tor.IsOnionHost(host) {
|
||||||
|
return &tor.OnionAddr{OnionService: host, Port: port}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the host is part of a TCP address, we'll use the network
|
||||||
|
// specific ResolveTCPAddr function in order to resolve these
|
||||||
|
// addresses over Tor in order to prevent leaking your real IP
|
||||||
|
// address.
|
||||||
|
hostPort := net.JoinHostPort(host, strconv.Itoa(port))
|
||||||
|
return cfg.net.ResolveTCPAddr("tcp", hostPort)
|
||||||
|
}
|
||||||
|
|
||||||
// newServer creates a new instance of the server which is to listen using the
|
// newServer creates a new instance of the server which is to listen using the
|
||||||
// passed listener address.
|
// passed listener address.
|
||||||
func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl,
|
func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl,
|
||||||
@ -251,25 +288,15 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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. We need to use the cfg.net.ResolveTCPAddr
|
// of this server's addresses.
|
||||||
// function in case we wish to resolve hosts over Tor since domains
|
|
||||||
// CAN be passed into the ExternalIPs configuration option.
|
|
||||||
selfAddrs := make([]net.Addr, 0, len(cfg.ExternalIPs))
|
selfAddrs := make([]net.Addr, 0, len(cfg.ExternalIPs))
|
||||||
for _, ip := range cfg.ExternalIPs {
|
for _, ip := range cfg.ExternalIPs {
|
||||||
var addr string
|
addr, err := parseAddr(ip)
|
||||||
_, _, err = net.SplitHostPort(ip)
|
|
||||||
if err != nil {
|
|
||||||
addr = net.JoinHostPort(ip, strconv.Itoa(defaultPeerPort))
|
|
||||||
} else {
|
|
||||||
addr = ip
|
|
||||||
}
|
|
||||||
|
|
||||||
lnAddr, err := cfg.net.ResolveTCPAddr("tcp", addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
selfAddrs = append(selfAddrs, lnAddr)
|
selfAddrs = append(selfAddrs, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
chanGraph := chanDB.ChannelGraph()
|
chanGraph := chanDB.ChannelGraph()
|
||||||
|
Loading…
Reference in New Issue
Block a user