From e41091a765e305e12137ef6734e7b7aee4be079e Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 10 Feb 2021 18:36:54 -0800 Subject: [PATCH] lncfg: detect tor resolution error case and fallback to system resolver In this commit, we fix a bug that would cause resolution of items in /etc/hosts (or the like) to fail as Tor wouldn't recognize them as proper host names. With this commit, we'll catch this error and fall back to the system's resolver. --- lncfg/address.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lncfg/address.go b/lncfg/address.go index 9f91552f..4b8c3e9a 100644 --- a/lncfg/address.go +++ b/lncfg/address.go @@ -233,10 +233,27 @@ func ParseAddressString(strAddress string, defaultPort string, // we'll use the system resolver instead. if rawHost == "" || IsLoopback(rawHost) || isIPv6Host(rawHost) { + return net.ResolveTCPAddr("tcp", addrWithPort) } - return tcpResolver("tcp", addrWithPort) + // If we've reached this point, then it's possible that this + // resolve returns an error if it isn't able to resolve the + // host. For eaxmple, local entries in /etc/hosts will fail to + // be resolved by Tor. In order to handle this case, we'll fall + // back to the normal system resolver if we fail with an + // identifiable error. + addr, err := tcpResolver("tcp", addrWithPort) + if err != nil { + torErrStr := "tor host is unreachable" + if strings.Contains(err.Error(), torErrStr) { + return net.ResolveTCPAddr("tcp", addrWithPort) + } + + return nil, err + } + + return addr, nil } }