From ebcaa95b35c82e07c619a1c2bebe7116085567ac Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Fri, 10 Jan 2014 11:34:06 -0500 Subject: [PATCH] Use smarter btcd cert path logic. If ~/.btcwallet/btcd.cert does not exist and the CA file has not been explicitly set using the config file or command line flags, it's possible that the cert can be found in ~/.btcd. If connecting to a localhost btcd and the previous statements are true, the default CA file config option is updated for the certificate in them btcd homedir. If ~/.btcwallet/btcd.cert does exist and the CA file has not been set, it is used without checking for a cert in the btcd homedir. --- config.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index f657cea..9fb4b8d 100644 --- a/config.go +++ b/config.go @@ -35,8 +35,10 @@ const ( ) var ( + btcdHomeDir = btcutil.AppDataDir("btcd", false) btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false) defaultCAFile = filepath.Join(btcwalletHomeDir, defaultCAFilename) + btcdHomedirCAFile = filepath.Join(btcdHomeDir, "rpc.cert") defaultConfigFile = filepath.Join(btcwalletHomeDir, defaultConfigFilename) defaultDataDir = btcwalletHomeDir defaultRPCKeyFile = filepath.Join(btcwalletHomeDir, "rpc.key") @@ -136,7 +138,6 @@ func loadConfig() (*config, []string, error) { // Default config. cfg := config{ DebugLevel: defaultLogLevel, - CAFile: defaultCAFile, ConfigFile: defaultConfigFile, DataDir: defaultDataDir, RPCKey: defaultRPCKeyFile, @@ -218,6 +219,32 @@ func loadConfig() (*config, []string, error) { // Add default port to connect flag if missing. cfg.Connect = normalizeAddress(cfg.Connect, activeNetParams.btcdPort) + // If CAFile is unset, choose either the copy or local btcd cert. + if cfg.CAFile == "" { + cfg.CAFile = defaultCAFile + + // If the CA copy does not exist, check if we're connecting to + // a local btcd and switch to its RPC cert if it exists. + if !fileExists(cfg.CAFile) { + host, _, err := net.SplitHostPort(cfg.Connect) + if err != nil { + return nil, nil, err + } + switch host { + case "localhost": + fallthrough + + case "127.0.0.1": + fallthrough + + case "::1": + if fileExists(btcdHomedirCAFile) { + cfg.CAFile = btcdHomedirCAFile + } + } + } + } + if len(cfg.SvrListeners) == 0 { addrs, err := net.LookupHost("localhost") if err != nil {