multi: Added a NetInterface to clean up config.go

This commit adds a new interface named NetInterface and two
implementations of it: RegularNet & TorProxyNet. These two structs
are used in config.go in an attempt to clean up the code and
abstract away the dialer and DNS functions.
This commit is contained in:
nsa authored and Olaoluwa Osuntokun committed 2018-02-05 17:37:46 -08:00
1 parent e132ad8433
commit 8c482f2be7
6 files changed
+99 -87

No files matched your search

+6 -47
View File
@@ -20,7 +20,6 @@ import (
flags "github.com/jessevdk/go-flags"
"github.com/lightningnetwork/lnd/brontide"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/torsvc"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcutil"
)
@@ -187,10 +186,7 @@ type config struct {
Alias string `long:"alias" description:"The node alias. Used as a moniker by peers and intelligence services"`
Color string `long:"color" description:"The color of the node in hex format (i.e. '#3399FF'). Used to customize node appearance in intelligence services"`
dial func(string, string) (net.Conn, error)
lookup func(string) ([]string, error)
lookupSRV func(string, string, string) (string, []*net.SRV, error)
resolveTCP func(string, string) (*net.TCPAddr, error)
net NetInterface
}
// loadConfig initializes and parses the config using a config file and command
@@ -300,9 +296,7 @@ func loadConfig() (*config, error) {
// functions. When Tor's proxy is specified, the dial function is set to
// the proxy specific dial function and the DNS resolution functions use
// Tor.
cfg.lookup = net.LookupHost
cfg.lookupSRV = net.LookupSRV
cfg.resolveTCP = net.ResolveTCPAddr
cfg.net = &RegularNet{}
if cfg.TorSocks != "" && cfg.TorDNS != "" {
// Validate Tor port number
torport, err := strconv.Atoi(cfg.TorSocks)
@@ -324,22 +318,13 @@ func loadConfig() (*config, error) {
return nil, err
}
cfg.dial = torsvc.TorDial(cfg.TorSocks)
// If we are using Tor, since we only want connections routed
// through Tor, listening is disabled.
cfg.DisableListen = true
cfg.lookup = func(host string) ([]string, error) {
return torsvc.TorLookupHost(host, cfg.TorSocks)
}
cfg.lookupSRV = func(service, proto, name string) (cname string,
addrs []*net.SRV, err error) {
return torsvc.TorLookupSRV(service, proto, name,
cfg.TorSocks, cfg.TorDNS)
}
cfg.resolveTCP = func(network, address string) (*net.TCPAddr, error) {
return torsvc.TorResolveTCP(address, cfg.TorSocks)
cfg.net = &TorProxyNet{
TorSocks: cfg.TorSocks,
TorDNS: cfg.TorDNS,
}
}
@@ -684,36 +669,10 @@ func supportedSubsystems() []string {
func noiseDial(idPriv *btcec.PrivateKey) func(net.Addr) (net.Conn, error) {
return func(a net.Addr) (net.Conn, error) {
lnAddr := a.(*lnwire.NetAddress)
if cfg.dial == nil {
return brontide.Dial(idPriv, lnAddr)
}
return brontide.Dial(idPriv, lnAddr, cfg.dial)
return brontide.Dial(idPriv, lnAddr, cfg.net.Dial)
}
}
// lndLookup resolves the IP address of a given host using the correct DNS
// function depending on the lookup configuration options. Lookup can be done
// via golang's system resolver or via Tor. When Tor is used, only IPv4
// addresses are returned.
func lndLookup(host string) ([]string, error) {
return cfg.lookup(host)
}
// lndLookupSRV queries a DNS server with SRV requests depending on the
// configuration options. SRV queries can be done via golang's system
// resolver or through (but not by!) Tor.
func lndLookupSRV(service, proto, name string) (string, []*net.SRV, error) {
return cfg.lookupSRV(service, proto, name)
}
// lndResolveTCP resolves TCP addresses into type *net.TCPAddr. Depending on
// configuration options, this resolution can be done via golang's system
// resolver or via Tor.
func lndResolveTCP(network, address string) (*net.TCPAddr, error) {
return cfg.resolveTCP(network, address)
}
func parseRPCParams(cConfig *chainConfig, nodeConfig interface{}, net chainCode,
funcName string) error {
// If the configuration has already set the RPCUser and RPCPass, and