2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2015 The btcsuite developers
|
2015-01-18 21:45:20 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-04 04:14:15 +02:00
|
|
|
"io/ioutil"
|
2014-03-20 02:12:14 +01:00
|
|
|
"net"
|
2013-11-19 21:38:58 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-06-04 04:14:15 +02:00
|
|
|
"regexp"
|
2013-11-20 02:17:34 +01:00
|
|
|
"strings"
|
2014-07-02 15:50:08 +02:00
|
|
|
|
2015-04-06 06:15:49 +02:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
2015-01-15 17:30:38 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2016-12-09 18:54:33 +01:00
|
|
|
flags "github.com/jessevdk/go-flags"
|
2013-11-19 21:38:58 +01:00
|
|
|
)
|
|
|
|
|
2015-01-18 21:45:20 +01:00
|
|
|
const (
|
|
|
|
// unusableFlags are the command usage flags which this utility are not
|
|
|
|
// able to use. In particular it doesn't support websockets and
|
|
|
|
// consequently notifications.
|
|
|
|
unusableFlags = btcjson.UFWebsocketOnly | btcjson.UFNotification
|
|
|
|
)
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
var (
|
2014-03-20 02:12:14 +01:00
|
|
|
btcdHomeDir = btcutil.AppDataDir("btcd", false)
|
|
|
|
btcctlHomeDir = btcutil.AppDataDir("btcctl", false)
|
|
|
|
btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false)
|
|
|
|
defaultConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf")
|
2014-03-20 05:41:01 +01:00
|
|
|
defaultRPCServer = "localhost"
|
2014-03-20 02:12:14 +01:00
|
|
|
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
|
|
|
|
defaultWalletCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert")
|
2013-11-19 21:38:58 +01:00
|
|
|
)
|
|
|
|
|
2015-01-18 21:45:20 +01:00
|
|
|
// listCommands categorizes and lists all of the usable commands along with
|
|
|
|
// their one-line usage.
|
|
|
|
func listCommands() {
|
|
|
|
const (
|
|
|
|
categoryChain uint8 = iota
|
|
|
|
categoryWallet
|
|
|
|
numCategories
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get a list of registered commands and categorize and filter them.
|
|
|
|
cmdMethods := btcjson.RegisteredCmdMethods()
|
|
|
|
categorized := make([][]string, numCategories)
|
|
|
|
for _, method := range cmdMethods {
|
|
|
|
flags, err := btcjson.MethodUsageFlags(method)
|
|
|
|
if err != nil {
|
|
|
|
// This should never happen since the method was just
|
|
|
|
// returned from the package, but be safe.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the commands that aren't usable from this utility.
|
|
|
|
if flags&unusableFlags != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
usage, err := btcjson.MethodUsageText(method)
|
|
|
|
if err != nil {
|
|
|
|
// This should never happen since the method was just
|
|
|
|
// returned from the package, but be safe.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Categorize the command based on the usage flags.
|
|
|
|
category := categoryChain
|
|
|
|
if flags&btcjson.UFWalletOnly != 0 {
|
|
|
|
category = categoryWallet
|
|
|
|
}
|
|
|
|
categorized[category] = append(categorized[category], usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the command according to their categories.
|
|
|
|
categoryTitles := make([]string, numCategories)
|
|
|
|
categoryTitles[categoryChain] = "Chain Server Commands:"
|
|
|
|
categoryTitles[categoryWallet] = "Wallet Server Commands (--wallet):"
|
|
|
|
for category := uint8(0); category < numCategories; category++ {
|
|
|
|
fmt.Println(categoryTitles[category])
|
|
|
|
for _, usage := range categorized[category] {
|
|
|
|
fmt.Println(usage)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
// config defines the configuration options for btcctl.
|
|
|
|
//
|
|
|
|
// See loadConfig for details on the configuration load process.
|
|
|
|
type config struct {
|
2013-11-20 02:17:34 +01:00
|
|
|
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
|
2015-01-18 21:45:20 +01:00
|
|
|
ListCommands bool `short:"l" long:"listcommands" description:"List all of the supported commands and exit"`
|
2013-11-19 21:38:58 +01:00
|
|
|
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
|
2013-11-20 03:35:49 +01:00
|
|
|
RPCUser string `short:"u" long:"rpcuser" description:"RPC username"`
|
|
|
|
RPCPassword string `short:"P" long:"rpcpass" default-mask:"-" description:"RPC password"`
|
|
|
|
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"`
|
2014-07-02 16:25:42 +02:00
|
|
|
NoTLS bool `long:"notls" description:"Disable TLS"`
|
2015-01-18 21:45:20 +01:00
|
|
|
Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"`
|
|
|
|
ProxyUser string `long:"proxyuser" description:"Username for proxy server"`
|
|
|
|
ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"`
|
2014-03-20 02:12:14 +01:00
|
|
|
TestNet3 bool `long:"testnet" description:"Connect to testnet"`
|
2014-05-30 23:35:44 +02:00
|
|
|
SimNet bool `long:"simnet" description:"Connect to the simulation test network"`
|
2014-07-02 16:25:42 +02:00
|
|
|
TLSSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"`
|
2014-03-20 02:12:14 +01:00
|
|
|
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.
|
2014-05-30 23:35:44 +02:00
|
|
|
func normalizeAddress(addr string, useTestNet3, useSimNet, useWallet bool) string {
|
2014-03-20 02:12:14 +01:00
|
|
|
_, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
var defaultPort string
|
2014-05-30 23:35:44 +02:00
|
|
|
switch {
|
|
|
|
case useTestNet3:
|
2014-03-20 02:12:14 +01:00
|
|
|
if useWallet {
|
|
|
|
defaultPort = "18332"
|
|
|
|
} else {
|
|
|
|
defaultPort = "18334"
|
|
|
|
}
|
2014-05-30 23:35:44 +02:00
|
|
|
case useSimNet:
|
|
|
|
if useWallet {
|
|
|
|
defaultPort = "18554"
|
|
|
|
} else {
|
|
|
|
defaultPort = "18556"
|
|
|
|
}
|
|
|
|
default:
|
2014-03-20 02:12:14 +01:00
|
|
|
if useWallet {
|
|
|
|
defaultPort = "8332"
|
|
|
|
} else {
|
|
|
|
defaultPort = "8334"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.JoinHostPort(addr, defaultPort)
|
|
|
|
}
|
|
|
|
return addr
|
2013-11-19 21:38:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-19 21:22:57 +01:00
|
|
|
// cleanAndExpandPath expands environement variables and leading ~ in the
|
|
|
|
// passed path, cleans the result, and returns it.
|
|
|
|
func cleanAndExpandPath(path string) string {
|
|
|
|
// Expand initial ~ to OS specific home directory.
|
|
|
|
if strings.HasPrefix(path, "~") {
|
|
|
|
homeDir := filepath.Dir(btcctlHomeDir)
|
|
|
|
path = strings.Replace(path, "~", homeDir, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
|
|
|
|
// but they variables can still be expanded via POSIX-style $VARIABLE.
|
|
|
|
return filepath.Clean(os.ExpandEnv(path))
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
// loadConfig initializes and parses the config using a config file and command
|
|
|
|
// line options.
|
|
|
|
//
|
|
|
|
// The configuration proceeds as follows:
|
|
|
|
// 1) Start with a default config with sane settings
|
|
|
|
// 2) Pre-parse the command line to check for an alternative config file
|
|
|
|
// 3) Load configuration file overwriting defaults with any specified options
|
|
|
|
// 4) Parse CLI options and overwrite/add any specified options
|
|
|
|
//
|
|
|
|
// The above results in functioning properly without any config settings
|
|
|
|
// while still allowing the user to override settings with config files and
|
|
|
|
// command line options. Command line options always take precedence.
|
2015-01-18 21:45:20 +01:00
|
|
|
func loadConfig() (*config, []string, error) {
|
2013-11-19 21:38:58 +01:00
|
|
|
// Default config.
|
|
|
|
cfg := config{
|
|
|
|
ConfigFile: defaultConfigFile,
|
2014-03-20 05:41:01 +01:00
|
|
|
RPCServer: defaultRPCServer,
|
|
|
|
RPCCert: defaultRPCCertFile,
|
2013-11-19 21:38:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pre-parse the command line options to see if an alternative config
|
2015-01-18 21:45:20 +01:00
|
|
|
// file, the version flag, or the list commands flag was specified. Any
|
|
|
|
// errors aside from the help message error can be ignored here since
|
|
|
|
// they will be caught by the final parse below.
|
2013-11-19 21:38:58 +01:00
|
|
|
preCfg := cfg
|
2015-01-18 21:45:20 +01:00
|
|
|
preParser := flags.NewParser(&preCfg, flags.HelpFlag)
|
2016-09-12 21:58:28 +02:00
|
|
|
_, err := preParser.Parse()
|
2015-01-18 21:45:20 +01:00
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2015-03-13 07:39:55 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "")
|
|
|
|
fmt.Fprintln(os.Stderr, "The special parameter `-` "+
|
|
|
|
"indicates that a parameter should be read "+
|
|
|
|
"from the\nnext unread line from standard "+
|
|
|
|
"input.")
|
2015-01-18 21:45:20 +01:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 21:38:58 +01:00
|
|
|
|
2013-11-20 02:17:34 +01:00
|
|
|
// Show the version and exit if the version flag was specified.
|
2015-01-18 21:45:20 +01:00
|
|
|
appName := filepath.Base(os.Args[0])
|
|
|
|
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
|
|
|
|
usageMessage := fmt.Sprintf("Use %s -h to show options", appName)
|
2013-11-20 02:17:34 +01:00
|
|
|
if preCfg.ShowVersion {
|
|
|
|
fmt.Println(appName, "version", version())
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2015-01-18 21:45:20 +01:00
|
|
|
// Show the available commands and exit if the associated flag was
|
|
|
|
// specified.
|
|
|
|
if preCfg.ListCommands {
|
|
|
|
listCommands()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-06-04 04:14:15 +02:00
|
|
|
if _, err := os.Stat(preCfg.ConfigFile); os.IsNotExist(err) {
|
|
|
|
err := createDefaultConfigFile(preCfg.ConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error creating a default config file: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
// Load additional config from file.
|
2015-01-18 21:45:20 +01:00
|
|
|
parser := flags.NewParser(&cfg, flags.Default)
|
2013-11-24 19:33:36 +01:00
|
|
|
err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
|
2013-11-19 21:38:58 +01:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*os.PathError); !ok {
|
2015-01-18 21:45:20 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "Error parsing config file: %v\n",
|
|
|
|
err)
|
|
|
|
fmt.Fprintln(os.Stderr, usageMessage)
|
|
|
|
return nil, nil, err
|
2013-11-19 21:38:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse command line options again to ensure they take precedence.
|
|
|
|
remainingArgs, err := parser.Parse()
|
|
|
|
if err != nil {
|
2015-01-18 21:45:20 +01:00
|
|
|
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
|
|
|
|
fmt.Fprintln(os.Stderr, usageMessage)
|
|
|
|
}
|
|
|
|
return nil, nil, err
|
2013-11-19 21:38:58 +01:00
|
|
|
}
|
|
|
|
|
2014-05-30 23:35:44 +02:00
|
|
|
// Multiple networks can't be selected simultaneously.
|
|
|
|
numNets := 0
|
|
|
|
if cfg.TestNet3 {
|
|
|
|
numNets++
|
|
|
|
}
|
|
|
|
if cfg.SimNet {
|
|
|
|
numNets++
|
|
|
|
}
|
|
|
|
if numNets > 1 {
|
|
|
|
str := "%s: The testnet and simnet params can't be used " +
|
|
|
|
"together -- choose one of the two"
|
|
|
|
err := fmt.Errorf(str, "loadConfig")
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2015-01-18 21:45:20 +01:00
|
|
|
return nil, nil, err
|
2014-05-30 23:35:44 +02:00
|
|
|
}
|
|
|
|
|
2014-03-20 05:41:01 +01:00
|
|
|
// Override the RPC certificate if the --wallet flag was specified and
|
|
|
|
// the user did not specify one.
|
|
|
|
if cfg.Wallet && cfg.RPCCert == defaultRPCCertFile {
|
|
|
|
cfg.RPCCert = defaultWalletCertFile
|
2014-03-20 02:12:14 +01:00
|
|
|
}
|
|
|
|
|
2014-03-19 21:22:57 +01:00
|
|
|
// Handle environment variable expansion in the RPC certificate path.
|
|
|
|
cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert)
|
|
|
|
|
2014-03-20 05:41:01 +01:00
|
|
|
// Add default port to RPC server based on --testnet and --wallet flags
|
|
|
|
// if needed.
|
2014-05-30 23:35:44 +02:00
|
|
|
cfg.RPCServer = normalizeAddress(cfg.RPCServer, cfg.TestNet3,
|
|
|
|
cfg.SimNet, cfg.Wallet)
|
2014-03-20 02:12:14 +01:00
|
|
|
|
2015-01-18 21:45:20 +01:00
|
|
|
return &cfg, remainingArgs, nil
|
2013-11-19 21:38:58 +01:00
|
|
|
}
|
2016-06-04 04:14:15 +02:00
|
|
|
|
|
|
|
// createDefaultConfig creates a basic config file at the given destination path.
|
|
|
|
// For this it tries to read the btcd config file at its default path, and extract
|
|
|
|
// the RPC user and password from it.
|
|
|
|
func createDefaultConfigFile(destinationPath string) error {
|
|
|
|
// Read btcd.conf from its default path
|
|
|
|
btcdConfigPath := filepath.Join(btcdHomeDir, "btcd.conf")
|
|
|
|
btcdConfigFile, err := os.Open(btcdConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer btcdConfigFile.Close()
|
|
|
|
content, err := ioutil.ReadAll(btcdConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the rpcuser
|
|
|
|
rpcUserRegexp, err := regexp.Compile(`(?m)^\s*rpcuser=([^\s]+)`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userSubmatches := rpcUserRegexp.FindSubmatch(content)
|
|
|
|
if userSubmatches == nil {
|
|
|
|
// No user found, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the rpcpass
|
|
|
|
rpcPassRegexp, err := regexp.Compile(`(?m)^\s*rpcpass=([^\s]+)`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
passSubmatches := rpcPassRegexp.FindSubmatch(content)
|
|
|
|
if passSubmatches == nil {
|
|
|
|
// No password found, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-12 21:58:28 +02:00
|
|
|
// Create the destination directory if it does not exists
|
|
|
|
err = os.MkdirAll(filepath.Dir(destinationPath), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-04 04:14:15 +02:00
|
|
|
// Create the destination file and write the rpcuser and rpcpass to it
|
2016-09-12 21:58:28 +02:00
|
|
|
dest, err := os.OpenFile(destinationPath,
|
|
|
|
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
2016-06-04 04:14:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dest.Close()
|
|
|
|
|
2016-09-12 21:58:28 +02:00
|
|
|
dest.WriteString(fmt.Sprintf("rpcuser=%s\nrpcpass=%s",
|
|
|
|
string(userSubmatches[1]), string(passSubmatches[1])))
|
2016-06-04 04:14:15 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|