From 2e0484be19e1a146895489b1f25cdfce8b23f589 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 2 May 2018 02:33:17 -0400 Subject: [PATCH] multi: ensure addresses are no longer assumed to be TCP addresses only In this commit, we go through the codebase looking for TCP address assumptions and modifying them to include the recently introduced onion addresses. This enables us to fully support onion addresses within the daemon. --- channeldb/nodes.go | 4 +--- discovery/bootstrapper.go | 14 +++++++------- pilot.go | 17 ++++++----------- server.go | 40 ++++++++++++++++++++------------------- 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/channeldb/nodes.go b/channeldb/nodes.go index 10fe69b5ee4..8c3ff79e656 100644 --- a/channeldb/nodes.go +++ b/channeldb/nodes.go @@ -54,8 +54,6 @@ type LinkNode struct { // Addresses is a list of IP address in which either we were able to // reach the node over in the past, OR we received an incoming // authenticated connection for the stored identity public key. - // - // TODO(roasbeef): also need to support hidden service addrs Addresses []net.Addr db *DB @@ -85,7 +83,7 @@ func (l *LinkNode) UpdateLastSeen(lastSeen time.Time) error { // AddAddress appends the specified TCP address to the list of known addresses // this node is/was known to be reachable at. -func (l *LinkNode) AddAddress(addr *net.TCPAddr) error { +func (l *LinkNode) AddAddress(addr net.Addr) error { for _, a := range l.Addresses { if a.String() == addr.String() { return nil diff --git a/discovery/bootstrapper.go b/discovery/bootstrapper.go index 96695fb619e..be192ef18aa 100644 --- a/discovery/bootstrapper.go +++ b/discovery/bootstrapper.go @@ -165,12 +165,12 @@ func (c *ChannelGraphBootstrapper) SampleNodeAddrs(numAddrs uint32, // If we haven't yet reached our limit, then // we'll copy over the details of this node // into the set of addresses to be returned. - tcpAddr, ok := nodeAddr.(*net.TCPAddr) - if !ok { - // If this isn't a valid TCP address, - // then we'll ignore it as currently - // we'll only attempt to connect out to - // TCP peers. + switch nodeAddr.(type) { + case *net.TCPAddr, *tor.OnionAddr: + default: + // If this isn't a valid address + // supported by the protocol, then we'll + // skip this node. return nil } @@ -179,7 +179,7 @@ func (c *ChannelGraphBootstrapper) SampleNodeAddrs(numAddrs uint32, // error. a = append(a, &lnwire.NetAddress{ IdentityKey: node.PubKey(), - Address: tcpAddr, + Address: nodeAddr, }) } diff --git a/pilot.go b/pilot.go index 91cb1285c12..2acb76a310c 100644 --- a/pilot.go +++ b/pilot.go @@ -7,6 +7,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd/autopilot" "github.com/lightningnetwork/lnd/lnwire" + "github.com/lightningnetwork/lnd/tor" "github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/wire" "github.com/roasbeef/btcutil" @@ -49,18 +50,12 @@ func (c *chanController) OpenChannel(target *btcec.PublicKey, // advertised IP addresses, or have made a connection. var connected bool for _, addr := range addrs { - // If the address doesn't already have a port, then - // we'll assume the current default port. - tcpAddr, ok := addr.(*net.TCPAddr) - if !ok { - return fmt.Errorf("TCP address required instead "+ - "have %T", addr) + switch addr.(type) { + case *net.TCPAddr, *tor.OnionAddr: + lnAddr.Address = addr + default: + return fmt.Errorf("unknown address type %T", addr) } - if tcpAddr.Port == 0 { - tcpAddr.Port = defaultPeerPort - } - - lnAddr.Address = tcpAddr // TODO(roasbeef): make perm connection in server after // chan open? diff --git a/server.go b/server.go index a99893c20bf..04fddd0b834 100644 --- a/server.go +++ b/server.go @@ -1016,17 +1016,7 @@ func (s *server) establishPersistentConnections() error { return err } for _, node := range linkNodes { - for _, address := range node.Addresses { - switch addr := address.(type) { - case *net.TCPAddr: - if addr.Port == 0 { - addr.Port = defaultPeerPort - } - } - - } pubStr := string(node.IdentityPub.SerializeCompressed()) - nodeAddrs := &nodeAddresses{ pubKey: node.IdentityPub, addresses: node.Addresses, @@ -1059,17 +1049,29 @@ func (s *server) establishPersistentConnections() error { linkNodeAddrs, ok := nodeAddrsMap[pubStr] if ok { for _, lnAddress := range linkNodeAddrs.addresses { - lnAddrTCP, ok := lnAddress.(*net.TCPAddr) - if !ok { + var addrHost string + switch addr := lnAddress.(type) { + case *net.TCPAddr: + addrHost = addr.IP.String() + case *tor.OnionAddr: + addrHost = addr.OnionService + default: continue } var addrMatched bool for _, polAddress := range policy.Node.Addresses { - polTCPAddr, ok := polAddress.(*net.TCPAddr) - if ok && polTCPAddr.IP.Equal(lnAddrTCP.IP) { - addrMatched = true - addrs = append(addrs, polTCPAddr) + switch addr := polAddress.(type) { + case *net.TCPAddr: + if addr.IP.String() == addrHost { + addrMatched = true + addrs = append(addrs, addr) + } + case *tor.OnionAddr: + if addr.OnionService == addrHost { + addrMatched = true + addrs = append(addrs, addr) + } } } if !addrMatched { @@ -1078,9 +1080,9 @@ func (s *server) establishPersistentConnections() error { } } else { for _, addr := range policy.Node.Addresses { - polTCPAddr, ok := addr.(*net.TCPAddr) - if ok { - addrs = append(addrs, polTCPAddr) + switch addr.(type) { + case *net.TCPAddr, *tor.OnionAddr: + addrs = append(addrs, addr) } } }