github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/node/config.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package node
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"net"
    24  	"os"
    25  	"path/filepath"
    26  	"runtime"
    27  	"strings"
    28  
    29  	"github.com/ethereum/go-ethereum/accounts"
    30  	"github.com/ethereum/go-ethereum/accounts/keystore"
    31  	"github.com/ethereum/go-ethereum/accounts/usbwallet"
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/crypto"
    34  	"github.com/ethereum/go-ethereum/log"
    35  	"github.com/ethereum/go-ethereum/p2p/discover"
    36  	"github.com/ethereum/go-ethereum/p2p/discv5"
    37  	"github.com/ethereum/go-ethereum/p2p/nat"
    38  	"github.com/ethereum/go-ethereum/p2p/netutil"
    39  )
    40  
    41  var (
    42  	datadirPrivateKey      = "nodekey"            // Path within the datadir to the node's private key
    43  	datadirDefaultKeyStore = "keystore"           // Path within the datadir to the keystore
    44  	datadirStaticNodes     = "static-nodes.json"  // Path within the datadir to the static node list
    45  	datadirTrustedNodes    = "trusted-nodes.json" // Path within the datadir to the trusted node list
    46  	datadirNodeDatabase    = "nodes"              // Path within the datadir to store the node infos
    47  )
    48  
    49  // Config represents a small collection of configuration values to fine tune the
    50  // P2P network layer of a protocol stack. These values can be further extended by
    51  // all registered services.
    52  type Config struct {
    53  	// Name sets the instance name of the node. It must not contain the / character and is
    54  	// used in the devp2p node identifier. The instance name of geth is "geth". If no
    55  	// value is specified, the basename of the current executable is used.
    56  	Name string
    57  
    58  	// UserIdent, if set, is used as an additional component in the devp2p node identifier.
    59  	UserIdent string
    60  
    61  	// Version should be set to the version number of the program. It is used
    62  	// in the devp2p node identifier.
    63  	Version string
    64  
    65  	// DataDir is the file system folder the node should use for any data storage
    66  	// requirements. The configured data directory will not be directly shared with
    67  	// registered services, instead those can use utility methods to create/access
    68  	// databases or flat files. This enables ephemeral nodes which can fully reside
    69  	// in memory.
    70  	DataDir string
    71  
    72  	// KeyStoreDir is the file system folder that contains private keys. The directory can
    73  	// be specified as a relative path, in which case it is resolved relative to the
    74  	// current directory.
    75  	//
    76  	// If KeyStoreDir is empty, the default location is the "keystore" subdirectory of
    77  	// DataDir. If DataDir is unspecified and KeyStoreDir is empty, an ephemeral directory
    78  	// is created by New and destroyed when the node is stopped.
    79  	KeyStoreDir string
    80  
    81  	// UseLightweightKDF lowers the memory and CPU requirements of the key store
    82  	// scrypt KDF at the expense of security.
    83  	UseLightweightKDF bool
    84  
    85  	// IPCPath is the requested location to place the IPC endpoint. If the path is
    86  	// a simple file name, it is placed inside the data directory (or on the root
    87  	// pipe path on Windows), whereas if it's a resolvable path name (absolute or
    88  	// relative), then that specific path is enforced. An empty path disables IPC.
    89  	IPCPath string
    90  
    91  	// This field should be a valid secp256k1 private key that will be used for both
    92  	// remote peer identification as well as network traffic encryption. If no key
    93  	// is configured, the preset one is loaded from the data dir, generating it if
    94  	// needed.
    95  	PrivateKey *ecdsa.PrivateKey
    96  
    97  	// NoDiscovery specifies whether the peer discovery mechanism should be started
    98  	// or not. Disabling is usually useful for protocol debugging (manual topology).
    99  	NoDiscovery bool
   100  
   101  	// DiscoveryV5 specifies whether the the new topic-discovery based V5 discovery
   102  	// protocol should be started or not.
   103  	DiscoveryV5 bool
   104  
   105  	// Listener address for the V5 discovery protocol UDP traffic.
   106  	DiscoveryV5Addr string
   107  
   108  	// Restrict communication to white listed IP networks.
   109  	// The whitelist only applies when non-nil.
   110  	NetRestrict *netutil.Netlist
   111  
   112  	// BootstrapNodes used to establish connectivity with the rest of the network.
   113  	BootstrapNodes []*discover.Node
   114  
   115  	// BootstrapNodesV5 used to establish connectivity with the rest of the network
   116  	// using the V5 discovery protocol.
   117  	BootstrapNodesV5 []*discv5.Node
   118  
   119  	// Network interface address on which the node should listen for inbound peers.
   120  	ListenAddr string
   121  
   122  	// If set to a non-nil value, the given NAT port mapper is used to make the
   123  	// listening port available to the Internet.
   124  	NAT nat.Interface
   125  
   126  	// If Dialer is set to a non-nil value, the given Dialer is used to dial outbound
   127  	// peer connections.
   128  	Dialer *net.Dialer
   129  
   130  	// If NoDial is true, the node will not dial any peers.
   131  	NoDial bool
   132  
   133  	// MaxPeers is the maximum number of peers that can be connected. If this is
   134  	// set to zero, then only the configured static and trusted peers can connect.
   135  	MaxPeers int
   136  
   137  	// MaxPendingPeers is the maximum number of peers that can be pending in the
   138  	// handshake phase, counted separately for inbound and outbound connections.
   139  	// Zero defaults to preset values.
   140  	MaxPendingPeers int
   141  
   142  	// HTTPHost is the host interface on which to start the HTTP RPC server. If this
   143  	// field is empty, no HTTP API endpoint will be started.
   144  	HTTPHost string
   145  
   146  	// HTTPPort is the TCP port number on which to start the HTTP RPC server. The
   147  	// default zero value is/ valid and will pick a port number randomly (useful
   148  	// for ephemeral nodes).
   149  	HTTPPort int
   150  
   151  	// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
   152  	// clients. Please be aware that CORS is a browser enforced security, it's fully
   153  	// useless for custom HTTP clients.
   154  	HTTPCors string
   155  
   156  	// HTTPModules is a list of API modules to expose via the HTTP RPC interface.
   157  	// If the module list is empty, all RPC API endpoints designated public will be
   158  	// exposed.
   159  	HTTPModules []string
   160  
   161  	// WSHost is the host interface on which to start the websocket RPC server. If
   162  	// this field is empty, no websocket API endpoint will be started.
   163  	WSHost string
   164  
   165  	// WSPort is the TCP port number on which to start the websocket RPC server. The
   166  	// default zero value is/ valid and will pick a port number randomly (useful for
   167  	// ephemeral nodes).
   168  	WSPort int
   169  
   170  	// WSOrigins is the list of domain to accept websocket requests from. Please be
   171  	// aware that the server can only act upon the HTTP request the client sends and
   172  	// cannot verify the validity of the request header.
   173  	WSOrigins string
   174  
   175  	// WSModules is a list of API modules to expose via the websocket RPC interface.
   176  	// If the module list is empty, all RPC API endpoints designated public will be
   177  	// exposed.
   178  	WSModules []string
   179  }
   180  
   181  // IPCEndpoint resolves an IPC endpoint based on a configured value, taking into
   182  // account the set data folders as well as the designated platform we're currently
   183  // running on.
   184  func (c *Config) IPCEndpoint() string {
   185  	// Short circuit if IPC has not been enabled
   186  	if c.IPCPath == "" {
   187  		return ""
   188  	}
   189  	// On windows we can only use plain top-level pipes
   190  	if runtime.GOOS == "windows" {
   191  		if strings.HasPrefix(c.IPCPath, `\\.\pipe\`) {
   192  			return c.IPCPath
   193  		}
   194  		return `\\.\pipe\` + c.IPCPath
   195  	}
   196  	// Resolve names into the data directory full paths otherwise
   197  	if filepath.Base(c.IPCPath) == c.IPCPath {
   198  		if c.DataDir == "" {
   199  			return filepath.Join(os.TempDir(), c.IPCPath)
   200  		}
   201  		return filepath.Join(c.DataDir, c.IPCPath)
   202  	}
   203  	return c.IPCPath
   204  }
   205  
   206  // NodeDB returns the path to the discovery node database.
   207  func (c *Config) NodeDB() string {
   208  	if c.DataDir == "" {
   209  		return "" // ephemeral
   210  	}
   211  	return c.resolvePath("nodes")
   212  }
   213  
   214  // DefaultIPCEndpoint returns the IPC path used by default.
   215  func DefaultIPCEndpoint(clientIdentifier string) string {
   216  	if clientIdentifier == "" {
   217  		clientIdentifier = strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
   218  		if clientIdentifier == "" {
   219  			panic("empty executable name")
   220  		}
   221  	}
   222  	config := &Config{DataDir: DefaultDataDir(), IPCPath: clientIdentifier + ".ipc"}
   223  	return config.IPCEndpoint()
   224  }
   225  
   226  // HTTPEndpoint resolves an HTTP endpoint based on the configured host interface
   227  // and port parameters.
   228  func (c *Config) HTTPEndpoint() string {
   229  	if c.HTTPHost == "" {
   230  		return ""
   231  	}
   232  	return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort)
   233  }
   234  
   235  // DefaultHTTPEndpoint returns the HTTP endpoint used by default.
   236  func DefaultHTTPEndpoint() string {
   237  	config := &Config{HTTPHost: DefaultHTTPHost, HTTPPort: DefaultHTTPPort}
   238  	return config.HTTPEndpoint()
   239  }
   240  
   241  // WSEndpoint resolves an websocket endpoint based on the configured host interface
   242  // and port parameters.
   243  func (c *Config) WSEndpoint() string {
   244  	if c.WSHost == "" {
   245  		return ""
   246  	}
   247  	return fmt.Sprintf("%s:%d", c.WSHost, c.WSPort)
   248  }
   249  
   250  // DefaultWSEndpoint returns the websocket endpoint used by default.
   251  func DefaultWSEndpoint() string {
   252  	config := &Config{WSHost: DefaultWSHost, WSPort: DefaultWSPort}
   253  	return config.WSEndpoint()
   254  }
   255  
   256  // NodeName returns the devp2p node identifier.
   257  func (c *Config) NodeName() string {
   258  	name := c.name()
   259  	// Backwards compatibility: previous versions used title-cased "Geth", keep that.
   260  	if name == "geth" || name == "geth-testnet" {
   261  		name = "Geth"
   262  	}
   263  	if c.UserIdent != "" {
   264  		name += "/" + c.UserIdent
   265  	}
   266  	if c.Version != "" {
   267  		name += "/v" + c.Version
   268  	}
   269  	name += "/" + runtime.GOOS
   270  	name += "/" + runtime.Version()
   271  	return name
   272  }
   273  
   274  func (c *Config) name() string {
   275  	if c.Name == "" {
   276  		progname := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
   277  		if progname == "" {
   278  			panic("empty executable name, set Config.Name")
   279  		}
   280  		return progname
   281  	}
   282  	return c.Name
   283  }
   284  
   285  // These resources are resolved differently for "geth" instances.
   286  var isOldGethResource = map[string]bool{
   287  	"chaindata":          true,
   288  	"nodes":              true,
   289  	"nodekey":            true,
   290  	"static-nodes.json":  true,
   291  	"trusted-nodes.json": true,
   292  }
   293  
   294  // resolvePath resolves path in the instance directory.
   295  func (c *Config) resolvePath(path string) string {
   296  	if filepath.IsAbs(path) {
   297  		return path
   298  	}
   299  	if c.DataDir == "" {
   300  		return ""
   301  	}
   302  	// Backwards-compatibility: ensure that data directory files created
   303  	// by geth 1.4 are used if they exist.
   304  	if c.name() == "geth" && isOldGethResource[path] {
   305  		oldpath := ""
   306  		if c.Name == "geth" {
   307  			oldpath = filepath.Join(c.DataDir, path)
   308  		}
   309  		if oldpath != "" && common.FileExist(oldpath) {
   310  			// TODO: print warning
   311  			return oldpath
   312  		}
   313  	}
   314  	return filepath.Join(c.instanceDir(), path)
   315  }
   316  
   317  func (c *Config) instanceDir() string {
   318  	if c.DataDir == "" {
   319  		return ""
   320  	}
   321  	return filepath.Join(c.DataDir, c.name())
   322  }
   323  
   324  // NodeKey retrieves the currently configured private key of the node, checking
   325  // first any manually set key, falling back to the one found in the configured
   326  // data folder. If no key can be found, a new one is generated.
   327  func (c *Config) NodeKey() *ecdsa.PrivateKey {
   328  	// Use any specifically configured key.
   329  	if c.PrivateKey != nil {
   330  		return c.PrivateKey
   331  	}
   332  	// Generate ephemeral key if no datadir is being used.
   333  	if c.DataDir == "" {
   334  		key, err := crypto.GenerateKey()
   335  		if err != nil {
   336  			log.Crit(fmt.Sprintf("Failed to generate ephemeral node key: %v", err))
   337  		}
   338  		return key
   339  	}
   340  
   341  	keyfile := c.resolvePath(datadirPrivateKey)
   342  	if key, err := crypto.LoadECDSA(keyfile); err == nil {
   343  		return key
   344  	}
   345  	// No persistent key found, generate and store a new one.
   346  	key, err := crypto.GenerateKey()
   347  	if err != nil {
   348  		log.Crit(fmt.Sprintf("Failed to generate node key: %v", err))
   349  	}
   350  	instanceDir := filepath.Join(c.DataDir, c.name())
   351  	if err := os.MkdirAll(instanceDir, 0700); err != nil {
   352  		log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
   353  		return key
   354  	}
   355  	keyfile = filepath.Join(instanceDir, datadirPrivateKey)
   356  	if err := crypto.SaveECDSA(keyfile, key); err != nil {
   357  		log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
   358  	}
   359  	return key
   360  }
   361  
   362  // StaticNodes returns a list of node enode URLs configured as static nodes.
   363  func (c *Config) StaticNodes() []*discover.Node {
   364  	return c.parsePersistentNodes(c.resolvePath(datadirStaticNodes))
   365  }
   366  
   367  // TrusterNodes returns a list of node enode URLs configured as trusted nodes.
   368  func (c *Config) TrusterNodes() []*discover.Node {
   369  	return c.parsePersistentNodes(c.resolvePath(datadirTrustedNodes))
   370  }
   371  
   372  // parsePersistentNodes parses a list of discovery node URLs loaded from a .json
   373  // file from within the data directory.
   374  func (c *Config) parsePersistentNodes(path string) []*discover.Node {
   375  	// Short circuit if no node config is present
   376  	if c.DataDir == "" {
   377  		return nil
   378  	}
   379  	if _, err := os.Stat(path); err != nil {
   380  		return nil
   381  	}
   382  	// Load the nodes from the config file.
   383  	var nodelist []string
   384  	if err := common.LoadJSON(path, &nodelist); err != nil {
   385  		log.Error(fmt.Sprintf("Can't load node file %s: %v", path, err))
   386  		return nil
   387  	}
   388  	// Interpret the list as a discovery node array
   389  	var nodes []*discover.Node
   390  	for _, url := range nodelist {
   391  		if url == "" {
   392  			continue
   393  		}
   394  		node, err := discover.ParseNode(url)
   395  		if err != nil {
   396  			log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err))
   397  			continue
   398  		}
   399  		nodes = append(nodes, node)
   400  	}
   401  	return nodes
   402  }
   403  
   404  func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
   405  	scryptN := keystore.StandardScryptN
   406  	scryptP := keystore.StandardScryptP
   407  	if conf.UseLightweightKDF {
   408  		scryptN = keystore.LightScryptN
   409  		scryptP = keystore.LightScryptP
   410  	}
   411  
   412  	var (
   413  		keydir    string
   414  		ephemeral string
   415  		err       error
   416  	)
   417  	switch {
   418  	case filepath.IsAbs(conf.KeyStoreDir):
   419  		keydir = conf.KeyStoreDir
   420  	case conf.DataDir != "":
   421  		if conf.KeyStoreDir == "" {
   422  			keydir = filepath.Join(conf.DataDir, datadirDefaultKeyStore)
   423  		} else {
   424  			keydir, err = filepath.Abs(conf.KeyStoreDir)
   425  		}
   426  	case conf.KeyStoreDir != "":
   427  		keydir, err = filepath.Abs(conf.KeyStoreDir)
   428  	default:
   429  		// There is no datadir.
   430  		keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
   431  		ephemeral = keydir
   432  	}
   433  	if err != nil {
   434  		return nil, "", err
   435  	}
   436  	if err := os.MkdirAll(keydir, 0700); err != nil {
   437  		return nil, "", err
   438  	}
   439  	// Assemble the account manager and supported backends
   440  	backends := []accounts.Backend{
   441  		keystore.NewKeyStore(keydir, scryptN, scryptP),
   442  	}
   443  	if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil {
   444  		log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err))
   445  	} else {
   446  		backends = append(backends, ledgerhub)
   447  	}
   448  	return accounts.NewManager(backends...), ephemeral, nil
   449  }