2013-08-22 18:30:38 +02:00
/ *
2015-05-01 19:10:23 +02:00
* Copyright ( c ) 2013 , 2014 The btcsuite developers
2013-08-22 18:30:38 +02:00
*
* Permission to use , copy , modify , and distribute this software for any
* purpose with or without fee is hereby granted , provided that the above
* copyright notice and this permission notice appear in all copies .
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS . IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL , DIRECT , INDIRECT , OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE , DATA OR PROFITS , WHETHER IN AN
* ACTION OF CONTRACT , NEGLIGENCE OR OTHER TORTIOUS ACTION , ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE .
* /
2013-08-21 16:37:30 +02:00
package main
import (
"fmt"
2013-11-19 18:21:54 +01:00
"net"
2013-08-21 16:37:30 +02:00
"os"
"path/filepath"
2014-06-20 01:11:47 +02:00
"sort"
2013-08-21 16:37:30 +02:00
"strings"
2014-07-03 13:45:40 +02:00
2015-01-15 17:48:58 +01:00
"github.com/btcsuite/btcutil"
2015-11-25 06:21:40 +01:00
"github.com/btcsuite/btcwallet/internal/cfgutil"
2015-05-27 16:09:22 +02:00
"github.com/btcsuite/btcwallet/internal/legacy/keystore"
2015-11-25 06:21:40 +01:00
"github.com/btcsuite/btcwallet/netparams"
2015-01-16 08:00:14 +01:00
flags "github.com/btcsuite/go-flags"
2013-08-21 16:37:30 +02:00
)
const (
2015-04-02 20:13:38 +02:00
defaultCAFilename = "btcd.cert"
defaultConfigFilename = "btcwallet.conf"
defaultLogLevel = "info"
defaultLogDirname = "logs"
defaultLogFilename = "btcwallet.log"
defaultDisallowFree = false
defaultRPCMaxClients = 10
defaultRPCMaxWebsockets = 25
// defaultPubPassphrase is the default public wallet passphrase which is
// used when the user indicates they do not want additional protection
// provided by having all public data in the wallet encrypted by a
// passphrase only known to them.
defaultPubPassphrase = "public"
// maxEmptyAccounts is the number of accounts to scan even if they have no
// transaction history. This is a deviation from BIP044 to make account
// creation easier by allowing a limited number of empty accounts.
maxEmptyAccounts = 100
walletDbName = "wallet.db"
2013-08-21 16:37:30 +02:00
)
var (
2014-01-10 17:34:06 +01:00
btcdHomeDir = btcutil . AppDataDir ( "btcd" , false )
2013-12-03 16:52:09 +01:00
btcwalletHomeDir = btcutil . AppDataDir ( "btcwallet" , false )
2014-01-10 17:34:06 +01:00
btcdHomedirCAFile = filepath . Join ( btcdHomeDir , "rpc.cert" )
2013-12-03 16:52:09 +01:00
defaultConfigFile = filepath . Join ( btcwalletHomeDir , defaultConfigFilename )
defaultDataDir = btcwalletHomeDir
defaultRPCKeyFile = filepath . Join ( btcwalletHomeDir , "rpc.key" )
defaultRPCCertFile = filepath . Join ( btcwalletHomeDir , "rpc.cert" )
2014-06-20 01:11:47 +02:00
defaultLogDir = filepath . Join ( btcwalletHomeDir , defaultLogDirname )
2013-08-21 16:37:30 +02:00
)
type config struct {
2014-07-03 03:36:38 +02:00
ShowVersion bool ` short:"V" long:"version" description:"Display version information and exit" `
2014-10-29 07:43:29 +01:00
Create bool ` long:"create" description:"Create the wallet if it does not exist" `
2015-03-04 18:48:34 +01:00
CreateTemp bool ` long:"createtemp" description:"Create a temporary simulation wallet (pass=password) in the data directory indicated; must call with --datadir" `
2014-07-03 03:36:38 +02:00
CAFile string ` long:"cafile" description:"File containing root certificates to authenticate a TLS connections with btcd" `
RPCConnect string ` short:"c" long:"rpcconnect" description:"Hostname/IP and port of btcd RPC server to connect to (default localhost:18334, mainnet: localhost:8334, simnet: localhost:18556)" `
DebugLevel string ` short:"d" long:"debuglevel" description:"Logging level { trace, debug, info, warn, error, critical}" `
ConfigFile string ` short:"C" long:"configfile" description:"Path to configuration file" `
SvrListeners [ ] string ` long:"rpclisten" description:"Listen for RPC/websocket connections on this interface/port (default port: 18332, mainnet: 8332, simnet: 18554)" `
DataDir string ` short:"D" long:"datadir" description:"Directory to store wallets and transactions" `
LogDir string ` long:"logdir" description:"Directory to log output." `
Username string ` short:"u" long:"username" description:"Username for client and btcd authorization" `
Password string ` short:"P" long:"password" default-mask:"-" description:"Password for client and btcd authorization" `
BtcdUsername string ` long:"btcdusername" description:"Alternative username for btcd authorization" `
BtcdPassword string ` long:"btcdpassword" default-mask:"-" description:"Alternative password for btcd authorization" `
2014-10-29 07:43:29 +01:00
WalletPass string ` long:"walletpass" default-mask:"-" description:"The public wallet password -- Only required if the wallet was created with one" `
2014-07-03 03:36:38 +02:00
RPCCert string ` long:"rpccert" description:"File containing the certificate file" `
RPCKey string ` long:"rpckey" description:"File containing the certificate key" `
RPCMaxClients int64 ` long:"rpcmaxclients" description:"Max number of RPC clients for standard connections" `
RPCMaxWebsockets int64 ` long:"rpcmaxwebsockets" description:"Max number of RPC websocket connections" `
2015-01-09 08:41:42 +01:00
DisableServerTLS bool ` long:"noservertls" description:"Disable TLS for the RPC server -- NOTE: This is only allowed if the RPC server is bound to localhost" `
2015-01-09 10:20:59 +01:00
DisableClientTLS bool ` long:"noclienttls" description:"Disable TLS for the RPC client -- NOTE: This is only allowed if the RPC client is connecting to localhost" `
2014-07-03 03:36:38 +02:00
MainNet bool ` long:"mainnet" description:"Use the main Bitcoin network (default testnet3)" `
SimNet bool ` long:"simnet" description:"Use the simulation test network (default testnet3)" `
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
KeypoolSize uint ` short:"k" long:"keypoolsize" description:"DEPRECATED -- Maximum number of addresses in keypool" `
2014-07-03 03:36:38 +02:00
DisallowFree bool ` long:"disallowfree" description:"Force transactions to always include a fee" `
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" `
Profile string ` long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536" `
2013-08-21 16:37:30 +02:00
}
2013-11-21 16:02:27 +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 ( btcwalletHomeDir )
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 ) )
}
2014-06-20 01:11:47 +02:00
// validLogLevel returns whether or not logLevel is a valid debug log level.
func validLogLevel ( logLevel string ) bool {
switch logLevel {
case "trace" :
fallthrough
case "debug" :
fallthrough
case "info" :
fallthrough
case "warn" :
fallthrough
case "error" :
fallthrough
case "critical" :
return true
}
return false
}
// supportedSubsystems returns a sorted slice of the supported subsystems for
// logging purposes.
func supportedSubsystems ( ) [ ] string {
// Convert the subsystemLoggers map keys to a slice.
subsystems := make ( [ ] string , 0 , len ( subsystemLoggers ) )
for subsysID := range subsystemLoggers {
subsystems = append ( subsystems , subsysID )
}
// Sort the subsytems for stable display.
sort . Strings ( subsystems )
return subsystems
}
// parseAndSetDebugLevels attempts to parse the specified debug level and set
// the levels accordingly. An appropriate error is returned if anything is
// invalid.
func parseAndSetDebugLevels ( debugLevel string ) error {
// When the specified string doesn't have any delimters, treat it as
// the log level for all subsystems.
if ! strings . Contains ( debugLevel , "," ) && ! strings . Contains ( debugLevel , "=" ) {
// Validate debug log level.
if ! validLogLevel ( debugLevel ) {
str := "The specified debug level [%v] is invalid"
return fmt . Errorf ( str , debugLevel )
}
// Change the logging level for all subsystems.
setLogLevels ( debugLevel )
return nil
}
// Split the specified string into subsystem/level pairs while detecting
// issues and update the log levels accordingly.
for _ , logLevelPair := range strings . Split ( debugLevel , "," ) {
if ! strings . Contains ( logLevelPair , "=" ) {
str := "The specified debug level contains an invalid " +
"subsystem/level pair [%v]"
return fmt . Errorf ( str , logLevelPair )
}
// Extract the specified subsystem and log level.
fields := strings . Split ( logLevelPair , "=" )
subsysID , logLevel := fields [ 0 ] , fields [ 1 ]
// Validate subsystem.
if _ , exists := subsystemLoggers [ subsysID ] ; ! exists {
str := "The specified subsystem [%v] is invalid -- " +
"supported subsytems %v"
return fmt . Errorf ( str , subsysID , supportedSubsystems ( ) )
}
// Validate log level.
if ! validLogLevel ( logLevel ) {
str := "The specified debug level [%v] is invalid"
return fmt . Errorf ( str , logLevel )
}
setLogLevel ( subsysID , logLevel )
}
return nil
}
2013-08-21 16:37:30 +02: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 btcwallet 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.
func loadConfig ( ) ( * config , [ ] string , error ) {
// Default config.
cfg := config {
2014-07-03 03:36:38 +02:00
DebugLevel : defaultLogLevel ,
ConfigFile : defaultConfigFile ,
DataDir : defaultDataDir ,
LogDir : defaultLogDir ,
2014-10-29 07:43:29 +01:00
WalletPass : defaultPubPassphrase ,
2014-07-03 03:36:38 +02:00
RPCKey : defaultRPCKeyFile ,
RPCCert : defaultRPCCertFile ,
DisallowFree : defaultDisallowFree ,
RPCMaxClients : defaultRPCMaxClients ,
RPCMaxWebsockets : defaultRPCMaxWebsockets ,
2013-08-21 16:37:30 +02:00
}
// A config file in the current directory takes precedence.
2015-11-25 06:21:40 +01:00
exists , err := cfgutil . FileExists ( defaultConfigFilename )
if err != nil {
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
if exists {
2013-08-21 16:37:30 +02:00
cfg . ConfigFile = defaultConfigFile
}
// Pre-parse the command line options to see if an alternative config
// file or the version flag was specified.
preCfg := cfg
preParser := flags . NewParser ( & preCfg , flags . Default )
2015-11-25 06:21:40 +01:00
_ , err = preParser . Parse ( )
2013-08-21 16:37:30 +02:00
if err != nil {
if e , ok := err . ( * flags . Error ) ; ! ok || e . Type != flags . ErrHelp {
preParser . WriteHelp ( os . Stderr )
}
return nil , nil , err
}
// Show the version and exit if the version flag was specified.
2015-01-09 08:41:42 +01:00
funcName := "loadConfig"
appName := filepath . Base ( os . Args [ 0 ] )
appName = strings . TrimSuffix ( appName , filepath . Ext ( appName ) )
usageMessage := fmt . Sprintf ( "Use %s -h to show usage" , appName )
2013-08-21 16:37:30 +02:00
if preCfg . ShowVersion {
fmt . Println ( appName , "version" , version ( ) )
os . Exit ( 0 )
}
// Load additional config from file.
2013-11-25 18:20:37 +01:00
var configFileError error
2013-08-21 16:37:30 +02:00
parser := flags . NewParser ( & cfg , flags . Default )
2013-11-25 18:20:37 +01:00
err = flags . NewIniParser ( parser ) . ParseFile ( preCfg . ConfigFile )
2013-08-21 16:37:30 +02:00
if err != nil {
if _ , ok := err . ( * os . PathError ) ; ! ok {
fmt . Fprintln ( os . Stderr , err )
parser . WriteHelp ( os . Stderr )
return nil , nil , err
}
2013-11-25 18:20:37 +01:00
configFileError = err
2013-08-21 16:37:30 +02:00
}
// Parse command line options again to ensure they take precedence.
remainingArgs , err := parser . Parse ( )
if err != nil {
if e , ok := err . ( * flags . Error ) ; ! ok || e . Type != flags . ErrHelp {
parser . WriteHelp ( os . Stderr )
}
return nil , nil , err
}
2013-11-25 18:20:37 +01:00
// Warn about missing config file after the final command line parse
// succeeds. This prevents the warning on help messages and invalid
// options.
if configFileError != nil {
log . Warnf ( "%v" , configFileError )
}
2014-05-28 19:55:37 +02:00
// If an alternate data directory was specified, and paths with defaults
// relative to the data dir are unchanged, modify each path to be
// relative to the new data dir.
if cfg . DataDir != defaultDataDir {
if cfg . RPCKey == defaultRPCKeyFile {
cfg . RPCKey = filepath . Join ( cfg . DataDir , "rpc.key" )
}
if cfg . RPCCert == defaultRPCCertFile {
cfg . RPCCert = filepath . Join ( cfg . DataDir , "rpc.cert" )
}
}
2014-07-30 19:17:19 +02:00
// Choose the active network params based on the selected network.
2014-05-29 23:15:32 +02:00
// Multiple networks can't be selected simultaneously.
numNets := 0
2013-10-16 23:29:48 +02:00
if cfg . MainNet {
2015-11-25 06:21:40 +01:00
activeNet = & netparams . MainNetParams
2014-05-29 23:15:32 +02:00
numNets ++
}
if cfg . SimNet {
2015-11-25 06:21:40 +01:00
activeNet = & netparams . SimNetParams
2014-05-29 23:15:32 +02:00
numNets ++
}
if numNets > 1 {
str := "%s: The mainnet and simnet params can't be used " +
2014-06-02 18:15:27 +02:00
"together -- choose one"
2014-05-29 23:15:32 +02:00
err := fmt . Errorf ( str , "loadConfig" )
fmt . Fprintln ( os . Stderr , err )
parser . WriteHelp ( os . Stderr )
return nil , nil , err
}
2014-07-30 19:17:19 +02:00
// Append the network type to the log directory so it is "namespaced"
// per network.
cfg . LogDir = cleanAndExpandPath ( cfg . LogDir )
cfg . LogDir = filepath . Join ( cfg . LogDir , activeNet . Params . Name )
2013-10-16 23:29:48 +02:00
2014-06-20 01:11:47 +02:00
// Special show command to list supported subsystems and exit.
if cfg . DebugLevel == "show" {
fmt . Println ( "Supported subsystems" , supportedSubsystems ( ) )
os . Exit ( 0 )
}
// Initialize logging at the default logging level.
initSeelogLogger ( filepath . Join ( cfg . LogDir , defaultLogFilename ) )
setLogLevels ( defaultLogLevel )
// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels ( cfg . DebugLevel ) ; err != nil {
err := fmt . Errorf ( "%s: %v" , "loadConfig" , err . Error ( ) )
2013-10-29 15:38:51 +01:00
fmt . Fprintln ( os . Stderr , err )
parser . WriteHelp ( os . Stderr )
return nil , nil , err
}
2014-10-29 07:43:29 +01:00
2015-03-04 18:48:34 +01:00
// Exit if you try to use a simulation wallet with a standard
// data directory.
if cfg . DataDir == defaultDataDir && cfg . CreateTemp {
fmt . Fprintln ( os . Stderr , "Tried to create a temporary simulation " +
"wallet, but failed to specify data directory!" )
os . Exit ( 0 )
}
// Exit if you try to use a simulation wallet on anything other than
// simnet or testnet3.
if ! cfg . SimNet && cfg . CreateTemp {
fmt . Fprintln ( os . Stderr , "Tried to create a temporary simulation " +
"wallet for network other than simnet!" )
os . Exit ( 0 )
}
2014-10-29 07:43:29 +01:00
// Ensure the wallet exists or create it when the create flag is set.
netDir := networkDir ( cfg . DataDir , activeNet . Params )
dbPath := filepath . Join ( netDir , walletDbName )
2015-03-04 18:48:34 +01:00
if cfg . CreateTemp && cfg . Create {
err := fmt . Errorf ( "The flags --create and --createtemp can not " +
"be specified together. Use --help for more information." )
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
2015-11-25 06:21:40 +01:00
dbFileExists , err := cfgutil . FileExists ( dbPath )
if err != nil {
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
2015-03-04 18:48:34 +01:00
if cfg . CreateTemp {
tempWalletExists := false
2015-11-25 06:21:40 +01:00
if dbFileExists {
2015-03-04 18:48:34 +01:00
str := fmt . Sprintf ( "The wallet already exists. Loading this " +
"wallet instead." )
fmt . Fprintln ( os . Stdout , str )
tempWalletExists = true
}
// Ensure the data directory for the network exists.
if err := checkCreateDir ( netDir ) ; err != nil {
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
if ! tempWalletExists {
// Perform the initial wallet creation wizard.
if err := createSimulationWallet ( & cfg ) ; err != nil {
fmt . Fprintln ( os . Stderr , "Unable to create wallet:" , err )
return nil , nil , err
}
}
} else if cfg . Create {
2014-10-29 07:43:29 +01:00
// Error if the create flag is set and the wallet already
// exists.
2015-11-25 06:21:40 +01:00
if dbFileExists {
2014-10-29 07:43:29 +01:00
err := fmt . Errorf ( "The wallet already exists." )
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
// Ensure the data directory for the network exists.
if err := checkCreateDir ( netDir ) ; err != nil {
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
// Perform the initial wallet creation wizard.
if err := createWallet ( & cfg ) ; err != nil {
fmt . Fprintln ( os . Stderr , "Unable to create wallet:" , err )
return nil , nil , err
}
// Created successfully, so exit now with success.
os . Exit ( 0 )
2015-11-25 06:21:40 +01:00
} else if ! dbFileExists {
2014-10-29 07:43:29 +01:00
keystorePath := filepath . Join ( netDir , keystore . Filename )
2015-11-25 06:21:40 +01:00
keystoreExists , err := cfgutil . FileExists ( keystorePath )
if err != nil {
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
if ! keystoreExists {
2014-10-29 07:43:29 +01:00
err = fmt . Errorf ( "The wallet does not exist. Run with the " +
"--create option to initialize and create it." )
} else {
err = fmt . Errorf ( "The wallet is in legacy format. Run with the " +
"--create option to import it." )
}
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
2013-10-29 15:38:51 +01:00
2014-05-06 20:25:56 +02:00
if cfg . RPCConnect == "" {
2015-11-25 06:21:40 +01:00
cfg . RPCConnect = net . JoinHostPort ( "localhost" , activeNet . RPCClientPort )
2013-12-09 22:46:38 +01:00
}
2013-11-19 18:21:54 +01:00
// Add default port to connect flag if missing.
2015-11-25 06:21:40 +01:00
cfg . RPCConnect , err = cfgutil . NormalizeAddress ( cfg . RPCConnect ,
activeNet . RPCClientPort )
if err != nil {
fmt . Fprintf ( os . Stderr ,
"Invalid rpcconnect network address: %v\n" , err )
return nil , nil , err
}
2013-11-19 18:21:54 +01:00
2015-01-09 08:41:42 +01:00
localhostListeners := map [ string ] struct { } {
"localhost" : struct { } { } ,
"127.0.0.1" : struct { } { } ,
"::1" : struct { } { } ,
}
2015-01-09 10:20:59 +01:00
RPCHost , _ , err := net . SplitHostPort ( cfg . RPCConnect )
if err != nil {
return nil , nil , err
}
if cfg . DisableClientTLS {
if _ , ok := localhostListeners [ RPCHost ] ; ! ok {
str := "%s: the --noclienttls option may not be used " +
"when connecting RPC to non localhost " +
"addresses: %s"
err := fmt . Errorf ( str , funcName , cfg . RPCConnect )
fmt . Fprintln ( os . Stderr , err )
fmt . Fprintln ( os . Stderr , usageMessage )
return nil , nil , err
}
} else {
// If CAFile is unset, choose either the copy or local btcd cert.
if cfg . CAFile == "" {
cfg . CAFile = filepath . Join ( cfg . DataDir , defaultCAFilename )
// 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.
2015-11-25 06:21:40 +01:00
certExists , err := cfgutil . FileExists ( cfg . CAFile )
if err != nil {
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
if ! certExists {
2015-01-09 10:20:59 +01:00
if _ , ok := localhostListeners [ RPCHost ] ; ok {
2015-11-25 06:21:40 +01:00
btcdCertExists , err := cfgutil . FileExists (
btcdHomedirCAFile )
if err != nil {
fmt . Fprintln ( os . Stderr , err )
return nil , nil , err
}
if btcdCertExists {
2015-01-09 10:20:59 +01:00
cfg . CAFile = btcdHomedirCAFile
}
2014-01-10 17:34:06 +01:00
}
}
}
}
2013-12-05 23:20:52 +01:00
if len ( cfg . SvrListeners ) == 0 {
addrs , err := net . LookupHost ( "localhost" )
if err != nil {
return nil , nil , err
}
cfg . SvrListeners = make ( [ ] string , 0 , len ( addrs ) )
for _ , addr := range addrs {
2015-11-25 06:21:40 +01:00
addr = net . JoinHostPort ( addr , activeNet . RPCServerPort )
2013-12-05 23:20:52 +01:00
cfg . SvrListeners = append ( cfg . SvrListeners , addr )
}
}
// Add default port to all rpc listener addresses if needed and remove
// duplicate addresses.
2015-11-25 06:21:40 +01:00
cfg . SvrListeners , err = cfgutil . NormalizeAddresses (
cfg . SvrListeners , activeNet . RPCServerPort )
if err != nil {
fmt . Fprintf ( os . Stderr ,
"Invalid network address in RPC listeners: %v\n" , err )
return nil , nil , err
}
2013-12-05 23:20:52 +01:00
2015-01-09 08:41:42 +01:00
// Only allow server TLS to be disabled if the RPC is bound to localhost
// addresses.
if cfg . DisableServerTLS {
for _ , addr := range cfg . SvrListeners {
host , _ , err := net . SplitHostPort ( addr )
if err != nil {
str := "%s: RPC listen interface '%s' is " +
"invalid: %v"
err := fmt . Errorf ( str , funcName , addr , err )
fmt . Fprintln ( os . Stderr , err )
fmt . Fprintln ( os . Stderr , usageMessage )
return nil , nil , err
}
if _ , ok := localhostListeners [ host ] ; ! ok {
str := "%s: the --noservertls option may not be used " +
"when binding RPC to non localhost " +
"addresses: %s"
err := fmt . Errorf ( str , funcName , addr )
fmt . Fprintln ( os . Stderr , err )
fmt . Fprintln ( os . Stderr , usageMessage )
return nil , nil , err
}
}
}
2013-11-21 16:02:27 +01:00
// Expand environment variable and leading ~ for filepaths.
cfg . CAFile = cleanAndExpandPath ( cfg . CAFile )
2014-05-16 19:58:33 +02:00
// If the btcd username or password are unset, use the same auth as for
// the client. The two settings were previously shared for btcd and
// client auth, so this avoids breaking backwards compatibility while
// allowing users to use different auth settings for btcd and wallet.
if cfg . BtcdUsername == "" {
cfg . BtcdUsername = cfg . Username
}
if cfg . BtcdPassword == "" {
cfg . BtcdPassword = cfg . Password
}
2013-08-21 16:37:30 +02:00
return & cfg , remainingArgs , nil
}