parent
e3f3918242
commit
a94ca01cf4
1 changed files with 51 additions and 6 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
"github.com/conformal/go-flags"
|
"github.com/conformal/go-flags"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -12,8 +13,10 @@ import (
|
||||||
var (
|
var (
|
||||||
btcdHomeDir = btcutil.AppDataDir("btcd", false)
|
btcdHomeDir = btcutil.AppDataDir("btcd", false)
|
||||||
btcctlHomeDir = btcutil.AppDataDir("btcctl", false)
|
btcctlHomeDir = btcutil.AppDataDir("btcctl", false)
|
||||||
|
btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false)
|
||||||
defaultConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf")
|
defaultConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf")
|
||||||
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
|
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
|
||||||
|
defaultWalletCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert")
|
||||||
)
|
)
|
||||||
|
|
||||||
// config defines the configuration options for btcctl.
|
// config defines the configuration options for btcctl.
|
||||||
|
@ -27,7 +30,34 @@ type config struct {
|
||||||
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
|
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
|
||||||
RPCCert string `short:"c" long:"rpccert" description:"RPC server certificate chain for validation"`
|
RPCCert string `short:"c" long:"rpccert" description:"RPC server certificate chain for validation"`
|
||||||
NoTls bool `long:"notls" description:"Disable TLS"`
|
NoTls bool `long:"notls" description:"Disable TLS"`
|
||||||
|
TestNet3 bool `long:"testnet" description:"Connect to testnet"`
|
||||||
TlsSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"`
|
TlsSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"`
|
||||||
|
Wallet bool `long:"wallet" description:"Connect to wallet"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalizeAddress returns addr with the passed default port appended if
|
||||||
|
// there is not already a port specified.
|
||||||
|
func normalizeAddress(addr string, useTestNet3, useWallet bool) string {
|
||||||
|
_, _, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
var defaultPort string
|
||||||
|
if useTestNet3 {
|
||||||
|
if useWallet {
|
||||||
|
defaultPort = "18332"
|
||||||
|
} else {
|
||||||
|
defaultPort = "18334"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if useWallet {
|
||||||
|
defaultPort = "8332"
|
||||||
|
} else {
|
||||||
|
defaultPort = "8334"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return net.JoinHostPort(addr, defaultPort)
|
||||||
|
}
|
||||||
|
return addr
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanAndExpandPath expands environement variables and leading ~ in the
|
// cleanAndExpandPath expands environement variables and leading ~ in the
|
||||||
|
@ -60,8 +90,6 @@ func loadConfig() (*flags.Parser, *config, []string, error) {
|
||||||
// Default config.
|
// Default config.
|
||||||
cfg := config{
|
cfg := config{
|
||||||
ConfigFile: defaultConfigFile,
|
ConfigFile: defaultConfigFile,
|
||||||
RPCServer: "localhost:8334",
|
|
||||||
RPCCert: defaultRPCCertFile,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the home directory if it doesn't already exist.
|
// Create the home directory if it doesn't already exist.
|
||||||
|
@ -102,8 +130,25 @@ func loadConfig() (*flags.Parser, *config, []string, error) {
|
||||||
return parser, nil, nil, err
|
return parser, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Choose a default RPC certificate file if the user did not
|
||||||
|
// specify one.
|
||||||
|
if cfg.RPCCert == "" {
|
||||||
|
if cfg.Wallet {
|
||||||
|
cfg.RPCCert = defaultWalletCertFile
|
||||||
|
} else {
|
||||||
|
cfg.RPCCert = defaultRPCCertFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Handle environment variable expansion in the RPC certificate path.
|
// Handle environment variable expansion in the RPC certificate path.
|
||||||
cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert)
|
cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert)
|
||||||
|
|
||||||
|
// Connect to localhost if the user did not specify a server.
|
||||||
|
if cfg.RPCServer == "" {
|
||||||
|
cfg.RPCServer = "localhost"
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.RPCServer = normalizeAddress(cfg.RPCServer, cfg.TestNet3, cfg.Wallet)
|
||||||
|
|
||||||
return parser, &cfg, remainingArgs, nil
|
return parser, &cfg, remainingArgs, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue