1 // Copyright (c) 2013-2015 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
16 "github.com/btcsuite/btcd/btcjson"
17 "github.com/btcsuite/btcutil"
18 flags "github.com/jessevdk/go-flags"
22 // unusableFlags are the command usage flags which this utility are not
23 // able to use. In particular it doesn't support websockets and
24 // consequently notifications.
25 unusableFlags = btcjson.UFWebsocketOnly | btcjson.UFNotification
29 btcdHomeDir = btcutil.AppDataDir("btcd", false)
30 btcctlHomeDir = btcutil.AppDataDir("btcctl", false)
31 btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false)
32 defaultConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf")
33 defaultRPCServer = "localhost"
34 defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
35 defaultWalletCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert")
38 // listCommands categorizes and lists all of the usable commands along with
39 // their one-line usage.
42 categoryChain uint8 = iota
47 // Get a list of registered commands and categorize and filter them.
48 cmdMethods := btcjson.RegisteredCmdMethods()
49 categorized := make([][]string, numCategories)
50 for _, method := range cmdMethods {
51 flags, err := btcjson.MethodUsageFlags(method)
53 // This should never happen since the method was just
54 // returned from the package, but be safe.
58 // Skip the commands that aren't usable from this utility.
59 if flags&unusableFlags != 0 {
63 usage, err := btcjson.MethodUsageText(method)
65 // This should never happen since the method was just
66 // returned from the package, but be safe.
70 // Categorize the command based on the usage flags.
71 category := categoryChain
72 if flags&btcjson.UFWalletOnly != 0 {
73 category = categoryWallet
75 categorized[category] = append(categorized[category], usage)
78 // Display the command according to their categories.
79 categoryTitles := make([]string, numCategories)
80 categoryTitles[categoryChain] = "Chain Server Commands:"
81 categoryTitles[categoryWallet] = "Wallet Server Commands (--wallet):"
82 for category := uint8(0); category < numCategories; category++ {
83 fmt.Println(categoryTitles[category])
84 for _, usage := range categorized[category] {
91 // config defines the configuration options for btcctl.
93 // See loadConfig for details on the configuration load process.
95 ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
96 ListCommands bool `short:"l" long:"listcommands" description:"List all of the supported commands and exit"`
97 ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
98 RPCUser string `short:"u" long:"rpcuser" description:"RPC username"`
99 RPCPassword string `short:"P" long:"rpcpass" default-mask:"-" description:"RPC password"`
100 RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
101 RPCCert string `short:"c" long:"rpccert" description:"RPC server certificate chain for validation"`
102 NoTLS bool `long:"notls" description:"Disable TLS"`
103 Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"`
104 ProxyUser string `long:"proxyuser" description:"Username for proxy server"`
105 ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"`
106 TestNet3 bool `long:"testnet" description:"Connect to testnet"`
107 SimNet bool `long:"simnet" description:"Connect to the simulation test network"`
108 TLSSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"`
109 Wallet bool `long:"wallet" description:"Connect to wallet"`
112 // normalizeAddress returns addr with the passed default port appended if
113 // there is not already a port specified.
114 func normalizeAddress(addr string, useTestNet3, useSimNet, useWallet bool) string {
115 _, _, err := net.SplitHostPort(addr)
117 var defaultPort string
121 defaultPort = "18332"
123 defaultPort = "18334"
127 defaultPort = "18554"
129 defaultPort = "18556"
139 return net.JoinHostPort(addr, defaultPort)
144 // cleanAndExpandPath expands environement variables and leading ~ in the
145 // passed path, cleans the result, and returns it.
146 func cleanAndExpandPath(path string) string {
147 // Expand initial ~ to OS specific home directory.
148 if strings.HasPrefix(path, "~") {
149 homeDir := filepath.Dir(btcctlHomeDir)
150 path = strings.Replace(path, "~", homeDir, 1)
153 // NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
154 // but they variables can still be expanded via POSIX-style $VARIABLE.
155 return filepath.Clean(os.ExpandEnv(path))
158 // loadConfig initializes and parses the config using a config file and command
161 // The configuration proceeds as follows:
162 // 1) Start with a default config with sane settings
163 // 2) Pre-parse the command line to check for an alternative config file
164 // 3) Load configuration file overwriting defaults with any specified options
165 // 4) Parse CLI options and overwrite/add any specified options
167 // The above results in functioning properly without any config settings
168 // while still allowing the user to override settings with config files and
169 // command line options. Command line options always take precedence.
170 func loadConfig() (*config, []string, error) {
173 ConfigFile: defaultConfigFile,
174 RPCServer: defaultRPCServer,
175 RPCCert: defaultRPCCertFile,
178 // Pre-parse the command line options to see if an alternative config
179 // file, the version flag, or the list commands flag was specified. Any
180 // errors aside from the help message error can be ignored here since
181 // they will be caught by the final parse below.
183 preParser := flags.NewParser(&preCfg, flags.HelpFlag)
184 _, err := preParser.Parse()
186 if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
187 fmt.Fprintln(os.Stderr, err)
188 fmt.Fprintln(os.Stderr, "")
189 fmt.Fprintln(os.Stderr, "The special parameter `-` "+
190 "indicates that a parameter should be read "+
191 "from the\nnext unread line from standard "+
197 // Show the version and exit if the version flag was specified.
198 appName := filepath.Base(os.Args[0])
199 appName = strings.TrimSuffix(appName, filepath.Ext(appName))
200 usageMessage := fmt.Sprintf("Use %s -h to show options", appName)
201 if preCfg.ShowVersion {
202 fmt.Println(appName, "version", version())
206 // Show the available commands and exit if the associated flag was
208 if preCfg.ListCommands {
213 if _, err := os.Stat(preCfg.ConfigFile); os.IsNotExist(err) {
214 // Use config file for RPC server to create default btcctl config
215 var serverConfigPath string
217 serverConfigPath = filepath.Join(btcwalletHomeDir, "btcwallet.conf")
219 serverConfigPath = filepath.Join(btcdHomeDir, "btcd.conf")
222 err := createDefaultConfigFile(preCfg.ConfigFile, serverConfigPath)
224 fmt.Fprintf(os.Stderr, "Error creating a default config file: %v\n", err)
228 // Load additional config from file.
229 parser := flags.NewParser(&cfg, flags.Default)
230 err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
232 if _, ok := err.(*os.PathError); !ok {
233 fmt.Fprintf(os.Stderr, "Error parsing config file: %v\n",
235 fmt.Fprintln(os.Stderr, usageMessage)
240 // Parse command line options again to ensure they take precedence.
241 remainingArgs, err := parser.Parse()
243 if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
244 fmt.Fprintln(os.Stderr, usageMessage)
249 // Multiple networks can't be selected simultaneously.
258 str := "%s: The testnet and simnet params can't be used " +
259 "together -- choose one of the two"
260 err := fmt.Errorf(str, "loadConfig")
261 fmt.Fprintln(os.Stderr, err)
265 // Override the RPC certificate if the --wallet flag was specified and
266 // the user did not specify one.
267 if cfg.Wallet && cfg.RPCCert == defaultRPCCertFile {
268 cfg.RPCCert = defaultWalletCertFile
271 // Handle environment variable expansion in the RPC certificate path.
272 cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert)
274 // Add default port to RPC server based on --testnet and --wallet flags
276 cfg.RPCServer = normalizeAddress(cfg.RPCServer, cfg.TestNet3,
277 cfg.SimNet, cfg.Wallet)
279 return &cfg, remainingArgs, nil
282 // createDefaultConfig creates a basic config file at the given destination path.
283 // For this it tries to read the config file for the RPC server (either btcd or
284 // btcwallet), and extract the RPC user and password from it.
285 func createDefaultConfigFile(destinationPath, serverConfigPath string) error {
286 // Read the RPC server config
287 serverConfigFile, err := os.Open(serverConfigPath)
291 defer serverConfigFile.Close()
292 content, err := ioutil.ReadAll(serverConfigFile)
297 // Extract the rpcuser
298 rpcUserRegexp, err := regexp.Compile(`(?m)^\s*rpcuser=([^\s]+)`)
302 userSubmatches := rpcUserRegexp.FindSubmatch(content)
303 if userSubmatches == nil {
304 // No user found, nothing to do
308 // Extract the rpcpass
309 rpcPassRegexp, err := regexp.Compile(`(?m)^\s*rpcpass=([^\s]+)`)
313 passSubmatches := rpcPassRegexp.FindSubmatch(content)
314 if passSubmatches == nil {
315 // No password found, nothing to do
320 noTLSRegexp, err := regexp.Compile(`(?m)^\s*notls=(0|1)(?:\s|$)`)
324 noTLSSubmatches := noTLSRegexp.FindSubmatch(content)
326 // Create the destination directory if it does not exists
327 err = os.MkdirAll(filepath.Dir(destinationPath), 0700)
332 // Create the destination file and write the rpcuser and rpcpass to it
333 dest, err := os.OpenFile(destinationPath,
334 os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
340 destString := fmt.Sprintf("rpcuser=%s\nrpcpass=%s\n",
341 string(userSubmatches[1]), string(passSubmatches[1]))
342 if noTLSSubmatches != nil {
343 destString += fmt.Sprintf("notls=%s\n", noTLSSubmatches[1])
346 dest.WriteString(destString)