From 89209ec596e5a82566e8c2b016996e910df63e94 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Mon, 6 Dec 2021 09:15:39 -0500 Subject: [PATCH] config: Add bitcoind.{config,rpccookie} configuration options Currently, the Bitcoind.Dir configuration option is used as the base directory for locating both the bitcoind configuration file and the RPC cookie file. However, it is quite common for Bitcoin Core to be packaged in such a way that the configuration file and the RPC cookie file reside in different directories: "/etc/bitcoin/bitcoin.conf" and "/var/lib/bitcoind/.cookie". This change makes it such that --bitcoind.config and --bitcoind.rpccookie options can be specified to override the default auto-detection logic, and if either is unspecified, the auto-detection logic will still do its job. --- config.go | 40 ++++++++++++++++++++++++++-------------- lncfg/bitcoind.go | 2 ++ sample-lnd.conf | 12 ++++++++++++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/config.go b/config.go index b75d5cabd..42626581f 100644 --- a/config.go +++ b/config.go @@ -1723,7 +1723,7 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{}, // First, we'll check our node config to make sure the RPC parameters // were set correctly. We'll also determine the path to the conf file // depending on the backend node. - var daemonName, confDir, confFile string + var daemonName, confDir, confFile, confFileBase string switch conf := nodeConfig.(type) { case *lncfg.Btcd: // If both RPCUser and RPCPass are set, we assume those @@ -1737,11 +1737,11 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{}, case chainreg.BitcoinChain: daemonName = "btcd" confDir = conf.Dir - confFile = "btcd" + confFileBase = "btcd" case chainreg.LitecoinChain: daemonName = "ltcd" confDir = conf.Dir - confFile = "ltcd" + confFileBase = "ltcd" } // If only ONE of RPCUser or RPCPass is set, we assume the @@ -1784,11 +1784,13 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{}, case chainreg.BitcoinChain: daemonName = "bitcoind" confDir = conf.Dir - confFile = "bitcoin" + confFile = conf.ConfigPath + confFileBase = "bitcoin" case chainreg.LitecoinChain: daemonName = "litecoind" confDir = conf.Dir - confFile = "litecoin" + confFile = conf.ConfigPath + confFileBase = "litecoin" } // If not all of the parameters are set, we'll assume the user @@ -1813,7 +1815,10 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{}, fmt.Println("Attempting automatic RPC configuration to " + daemonName) - confFile = filepath.Join(confDir, fmt.Sprintf("%v.conf", confFile)) + if confFile == "" { + confFile = filepath.Join(confDir, fmt.Sprintf("%v.conf", + confFileBase)) + } switch cConfig.Node { case "btcd", "ltcd": nConf := nodeConfig.(*lncfg.Btcd) @@ -1827,7 +1832,8 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{}, case "bitcoind", "litecoind": nConf := nodeConfig.(*lncfg.Bitcoind) rpcUser, rpcPass, zmqBlockHost, zmqTxHost, err := - extractBitcoindRPCParams(netParams.Params.Name, confFile) + extractBitcoindRPCParams(netParams.Params.Name, + nConf.Dir, confFile, nConf.RPCCookie) if err != nil { return fmt.Errorf("unable to extract RPC credentials: "+ "%v, cannot start w/o RPC connection", err) @@ -1887,12 +1893,11 @@ func extractBtcdRPCParams(btcdConfigPath string) (string, string, error) { } // extractBitcoindRPCParams attempts to extract the RPC credentials for an -// existing bitcoind node instance. The passed path is expected to be the -// location of bitcoind's bitcoin.conf on the target system. The routine looks -// for a cookie first, optionally following the datadir configuration option in -// the bitcoin.conf. If it doesn't find one, it looks for rpcuser/rpcpassword. -func extractBitcoindRPCParams(networkName string, - bitcoindConfigPath string) (string, string, string, string, error) { +// existing bitcoind node instance. The routine looks for a cookie first, +// optionally following the datadir configuration option in the bitcoin.conf. If +// it doesn't find one, it looks for rpcuser/rpcpassword. +func extractBitcoindRPCParams(networkName, bitcoindDataDir, bitcoindConfigPath, + rpcCookiePath string) (string, string, string, string, error) { // First, we'll open up the bitcoind configuration file found at the // target destination. @@ -1940,6 +1945,9 @@ func extractBitcoindRPCParams(networkName string, // Next, we'll try to find an auth cookie. We need to detect the chain // by seeing if one is specified in the configuration file. dataDir := filepath.Dir(bitcoindConfigPath) + if bitcoindDataDir != "" { + dataDir = bitcoindDataDir + } dataDirRE, err := regexp.Compile(`(?m)^\s*datadir\s*=\s*([^\s]+)`) if err != nil { return "", "", "", "", err @@ -1959,7 +1967,11 @@ func extractBitcoindRPCParams(networkName string, return "", "", "", "", fmt.Errorf("unexpected networkname %v", networkName) } - cookie, err := ioutil.ReadFile(filepath.Join(dataDir, chainDir, ".cookie")) + cookiePath := filepath.Join(dataDir, chainDir, ".cookie") + if rpcCookiePath != "" { + cookiePath = rpcCookiePath + } + cookie, err := ioutil.ReadFile(cookiePath) if err == nil { splitCookie := strings.Split(string(cookie), ":") if len(splitCookie) == 2 { diff --git a/lncfg/bitcoind.go b/lncfg/bitcoind.go index 40bbcd8e9..904f9924b 100644 --- a/lncfg/bitcoind.go +++ b/lncfg/bitcoind.go @@ -4,6 +4,8 @@ package lncfg // bitcoind. type Bitcoind struct { Dir string `long:"dir" description:"The base directory that contains the node's data, logs, configuration file, etc."` + ConfigPath string `long:"config" description:"Configuration filepath. If not set, will default to the default filename under 'dir'."` + RPCCookie string `long:"rpccookie" description:"Authentication cookie file for RPC connections. If not set, will default to .cookie under 'dir'."` RPCHost string `long:"rpchost" description:"The daemon's rpc listening address. If a port is omitted, then the default port for the selected chain parameters will be used."` RPCUser string `long:"rpcuser" description:"Username for RPC connections"` RPCPass string `long:"rpcpass" default-mask:"-" description:"Password for RPC connections"` diff --git a/sample-lnd.conf b/sample-lnd.conf index 42605da7b..82ada3de7 100644 --- a/sample-lnd.conf +++ b/sample-lnd.conf @@ -578,6 +578,12 @@ bitcoin.node=btcd ; etc. ; bitcoind.dir=~/.bitcoin +; Configuration filepath. +; bitcoind.config=~/.bitcoin/bitcoin.conf + +; Authentication cookie file for RPC connections. +; bitcoind.rpccookie=~/.bitcoin/.cookie + ; The host that your local bitcoind daemon is listening on. By default, this ; setting is assumed to be localhost with the default port for the current ; network. @@ -779,6 +785,12 @@ litecoin.node=ltcd ; etc. ; litecoind.dir=~/.litecoin +; Configuration filepath. +; litecoind.config=~/.litecoin/litecoin.conf + +; Authentication cookie file for RPC connections. +; litecoind.rpccookie=~/.litecoin/.cookie + ; The host that your local litecoind daemon is listening on. By default, this ; setting is assumed to be localhost with the default port for the current ; network.