github.com/luckypickle/go-ethereum-vet@v1.14.2/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  	"os"
    24  	"path/filepath"
    25  	"runtime"
    26  	"strings"
    27  
    28  	"github.com/luckypickle/go-ethereum-vet/accounts"
    29  	"github.com/luckypickle/go-ethereum-vet/accounts/keystore"
    30  	"github.com/luckypickle/go-ethereum-vet/accounts/usbwallet"
    31  	"github.com/luckypickle/go-ethereum-vet/common"
    32  	"github.com/luckypickle/go-ethereum-vet/crypto"
    33  	"github.com/luckypickle/go-ethereum-vet/log"
    34  	"github.com/luckypickle/go-ethereum-vet/p2p"
    35  	"github.com/luckypickle/go-ethereum-vet/p2p/discover"
    36  	"github.com/luckypickle/go-ethereum-vet/rpc"
    37  )
    38  
    39  const (
    40  	datadirPrivateKey      = "nodekey"            // Path within the datadir to the node's private key
    41  	datadirDefaultKeyStore = "keystore"           // Path within the datadir to the keystore
    42  	datadirStaticNodes     = "static-nodes.json"  // Path within the datadir to the static node list
    43  	datadirTrustedNodes    = "trusted-nodes.json" // Path within the datadir to the trusted node list
    44  	datadirNodeDatabase    = "nodes"              // Path within the datadir to store the node infos
    45  )
    46  
    47  // Config represents a small collection of configuration values to fine tune the
    48  // P2P network layer of a protocol stack. These values can be further extended by
    49  // all registered services.
    50  type Config struct {
    51  	// Name sets the instance name of the node. It must not contain the / character and is
    52  	// used in the devp2p node identifier. The instance name of geth is "geth". If no
    53  	// value is specified, the basename of the current executable is used.
    54  	Name string `toml:"-"`
    55  
    56  	// UserIdent, if set, is used as an additional component in the devp2p node identifier.
    57  	UserIdent string `toml:",omitempty"`
    58  
    59  	// Version should be set to the version number of the program. It is used
    60  	// in the devp2p node identifier.
    61  	Version string `toml:"-"`
    62  
    63  	// DataDir is the file system folder the node should use for any data storage
    64  	// requirements. The configured data directory will not be directly shared with
    65  	// registered services, instead those can use utility methods to create/access
    66  	// databases or flat files. This enables ephemeral nodes which can fully reside
    67  	// in memory.
    68  	DataDir string
    69  
    70  	// Configuration of peer-to-peer networking.
    71  	P2P p2p.Config
    72  
    73  	// KeyStoreDir is the file system folder that contains private keys. The directory can
    74  	// be specified as a relative path, in which case it is resolved relative to the
    75  	// current directory.
    76  	//
    77  	// If KeyStoreDir is empty, the default location is the "keystore" subdirectory of
    78  	// DataDir. If DataDir is unspecified and KeyStoreDir is empty, an ephemeral directory
    79  	// is created by New and destroyed when the node is stopped.
    80  	KeyStoreDir string `toml:",omitempty"`
    81  
    82  	// UseLightweightKDF lowers the memory and CPU requirements of the key store
    83  	// scrypt KDF at the expense of security.
    84  	UseLightweightKDF bool `toml:",omitempty"`
    85  
    86  	// NoUSB disables hardware wallet monitoring and connectivity.
    87  	NoUSB bool `toml:",omitempty"`
    88  
    89  	// IPCPath is the requested location to place the IPC endpoint. If the path is
    90  	// a simple file name, it is placed inside the data directory (or on the root
    91  	// pipe path on Windows), whereas if it's a resolvable path name (absolute or
    92  	// relative), then that specific path is enforced. An empty path disables IPC.
    93  	IPCPath string `toml:",omitempty"`
    94  
    95  	// HTTPHost is the host interface on which to start the HTTP RPC server. If this
    96  	// field is empty, no HTTP API endpoint will be started.
    97  	HTTPHost string `toml:",omitempty"`
    98  
    99  	// HTTPPort is the TCP port number on which to start the HTTP RPC server. The
   100  	// default zero value is/ valid and will pick a port number randomly (useful
   101  	// for ephemeral nodes).
   102  	HTTPPort int `toml:",omitempty"`
   103  
   104  	// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
   105  	// clients. Please be aware that CORS is a browser enforced security, it's fully
   106  	// useless for custom HTTP clients.
   107  	HTTPCors []string `toml:",omitempty"`
   108  
   109  	// HTTPVirtualHosts is the list of virtual hostnames which are allowed on incoming requests.
   110  	// This is by default {'localhost'}. Using this prevents attacks like
   111  	// DNS rebinding, which bypasses SOP by simply masquerading as being within the same
   112  	// origin. These attacks do not utilize CORS, since they are not cross-domain.
   113  	// By explicitly checking the Host-header, the server will not allow requests
   114  	// made against the server with a malicious host domain.
   115  	// Requests using ip address directly are not affected
   116  	HTTPVirtualHosts []string `toml:",omitempty"`
   117  
   118  	// HTTPModules is a list of API modules to expose via the HTTP RPC interface.
   119  	// If the module list is empty, all RPC API endpoints designated public will be
   120  	// exposed.
   121  	HTTPModules []string `toml:",omitempty"`
   122  
   123  	// HTTPTimeouts allows for customization of the timeout values used by the HTTP RPC
   124  	// interface.
   125  	HTTPTimeouts rpc.HTTPTimeouts
   126  
   127  	// WSHost is the host interface on which to start the websocket RPC server. If
   128  	// this field is empty, no websocket API endpoint will be started.
   129  	WSHost string `toml:",omitempty"`
   130  
   131  	// WSPort is the TCP port number on which to start the websocket RPC server. The
   132  	// default zero value is/ valid and will pick a port number randomly (useful for
   133  	// ephemeral nodes).
   134  	WSPort int `toml:",omitempty"`
   135  
   136  	// WSOrigins is the list of domain to accept websocket requests from. Please be
   137  	// aware that the server can only act upon the HTTP request the client sends and
   138  	// cannot verify the validity of the request header.
   139  	WSOrigins []string `toml:",omitempty"`
   140  
   141  	// WSModules is a list of API modules to expose via the websocket RPC interface.
   142  	// If the module list is empty, all RPC API endpoints designated public will be
   143  	// exposed.
   144  	WSModules []string `toml:",omitempty"`
   145  
   146  	// WSExposeAll exposes all API modules via the WebSocket RPC interface rather
   147  	// than just the public ones.
   148  	//
   149  	// *WARNING* Only set this if the node is running in a trusted network, exposing
   150  	// private APIs to untrusted users is a major security risk.
   151  	WSExposeAll bool `toml:",omitempty"`
   152  
   153  	// Logger is a custom logger to use with the p2p.Server.
   154  	Logger log.Logger `toml:",omitempty"`
   155  }
   156  
   157  // IPCEndpoint resolves an IPC endpoint based on a configured value, taking into
   158  // account the set data folders as well as the designated platform we're currently
   159  // running on.
   160  func (c *Config) IPCEndpoint() string {
   161  	// Short circuit if IPC has not been enabled
   162  	if c.IPCPath == "" {
   163  		return ""
   164  	}
   165  	// On windows we can only use plain top-level pipes
   166  	if runtime.GOOS == "windows" {
   167  		if strings.HasPrefix(c.IPCPath, `\\.\pipe\`) {
   168  			return c.IPCPath
   169  		}
   170  		return `\\.\pipe\` + c.IPCPath
   171  	}
   172  	// Resolve names into the data directory full paths otherwise
   173  	if filepath.Base(c.IPCPath) == c.IPCPath {
   174  		if c.DataDir == "" {
   175  			return filepath.Join(os.TempDir(), c.IPCPath)
   176  		}
   177  		return filepath.Join(c.DataDir, c.IPCPath)
   178  	}
   179  	return c.IPCPath
   180  }
   181  
   182  // NodeDB returns the path to the discovery node database.
   183  func (c *Config) NodeDB() string {
   184  	if c.DataDir == "" {
   185  		return "" // ephemeral
   186  	}
   187  	return c.ResolvePath(datadirNodeDatabase)
   188  }
   189  
   190  // DefaultIPCEndpoint returns the IPC path used by default.
   191  func DefaultIPCEndpoint(clientIdentifier string) string {
   192  	if clientIdentifier == "" {
   193  		clientIdentifier = strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
   194  		if clientIdentifier == "" {
   195  			panic("empty executable name")
   196  		}
   197  	}
   198  	config := &Config{DataDir: DefaultDataDir(), IPCPath: clientIdentifier + ".ipc"}
   199  	return config.IPCEndpoint()
   200  }
   201  
   202  // HTTPEndpoint resolves an HTTP endpoint based on the configured host interface
   203  // and port parameters.
   204  func (c *Config) HTTPEndpoint() string {
   205  	if c.HTTPHost == "" {
   206  		return ""
   207  	}
   208  	return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort)
   209  }
   210  
   211  // DefaultHTTPEndpoint returns the HTTP endpoint used by default.
   212  func DefaultHTTPEndpoint() string {
   213  	config := &Config{HTTPHost: DefaultHTTPHost, HTTPPort: DefaultHTTPPort}
   214  	return config.HTTPEndpoint()
   215  }
   216  
   217  // WSEndpoint resolves a websocket endpoint based on the configured host interface
   218  // and port parameters.
   219  func (c *Config) WSEndpoint() string {
   220  	if c.WSHost == "" {
   221  		return ""
   222  	}
   223  	return fmt.Sprintf("%s:%d", c.WSHost, c.WSPort)
   224  }
   225  
   226  // DefaultWSEndpoint returns the websocket endpoint used by default.
   227  func DefaultWSEndpoint() string {
   228  	config := &Config{WSHost: DefaultWSHost, WSPort: DefaultWSPort}
   229  	return config.WSEndpoint()
   230  }
   231  
   232  // NodeName returns the devp2p node identifier.
   233  func (c *Config) NodeName() string {
   234  	name := c.name()
   235  	// Backwards compatibility: previous versions used title-cased "Geth", keep that.
   236  	if name == "geth" || name == "geth-testnet" {
   237  		name = "Geth"
   238  	}
   239  	if c.UserIdent != "" {
   240  		name += "/" + c.UserIdent
   241  	}
   242  	if c.Version != "" {
   243  		name += "/v" + c.Version
   244  	}
   245  	name += "/" + runtime.GOOS + "-" + runtime.GOARCH
   246  	name += "/" + runtime.Version()
   247  	return name
   248  }
   249  
   250  func (c *Config) name() string {
   251  	if c.Name == "" {
   252  		progname := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
   253  		if progname == "" {
   254  			panic("empty executable name, set Config.Name")
   255  		}
   256  		return progname
   257  	}
   258  	return c.Name
   259  }
   260  
   261  // These resources are resolved differently for "geth" instances.
   262  var isOldGethResource = map[string]bool{
   263  	"chaindata":          true,
   264  	"nodes":              true,
   265  	"nodekey":            true,
   266  	"static-nodes.json":  true,
   267  	"trusted-nodes.json": true,
   268  }
   269  
   270  // ResolvePath resolves path in the instance directory.
   271  func (c *Config) ResolvePath(path string) string {
   272  	if filepath.IsAbs(path) {
   273  		return path
   274  	}
   275  	if c.DataDir == "" {
   276  		return ""
   277  	}
   278  	// Backwards-compatibility: ensure that data directory files created
   279  	// by geth 1.4 are used if they exist.
   280  	if c.name() == "geth" && isOldGethResource[path] {
   281  		oldpath := ""
   282  		if c.Name == "geth" {
   283  			oldpath = filepath.Join(c.DataDir, path)
   284  		}
   285  		if oldpath != "" && common.FileExist(oldpath) {
   286  			// TODO: print warning
   287  			return oldpath
   288  		}
   289  	}
   290  	return filepath.Join(c.instanceDir(), path)
   291  }
   292  
   293  func (c *Config) instanceDir() string {
   294  	if c.DataDir == "" {
   295  		return ""
   296  	}
   297  	return filepath.Join(c.DataDir, c.name())
   298  }
   299  
   300  // NodeKey retrieves the currently configured private key of the node, checking
   301  // first any manually set key, falling back to the one found in the configured
   302  // data folder. If no key can be found, a new one is generated.
   303  func (c *Config) NodeKey() *ecdsa.PrivateKey {
   304  	// Use any specifically configured key.
   305  	if c.P2P.PrivateKey != nil {
   306  		return c.P2P.PrivateKey
   307  	}
   308  	// Generate ephemeral key if no datadir is being used.
   309  	if c.DataDir == "" {
   310  		key, err := crypto.GenerateKey()
   311  		if err != nil {
   312  			log.Crit(fmt.Sprintf("Failed to generate ephemeral node key: %v", err))
   313  		}
   314  		return key
   315  	}
   316  
   317  	keyfile := c.ResolvePath(datadirPrivateKey)
   318  	if key, err := crypto.LoadECDSA(keyfile); err == nil {
   319  		return key
   320  	}
   321  	// No persistent key found, generate and store a new one.
   322  	key, err := crypto.GenerateKey()
   323  	if err != nil {
   324  		log.Crit(fmt.Sprintf("Failed to generate node key: %v", err))
   325  	}
   326  	instanceDir := filepath.Join(c.DataDir, c.name())
   327  	if err := os.MkdirAll(instanceDir, 0700); err != nil {
   328  		log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
   329  		return key
   330  	}
   331  	keyfile = filepath.Join(instanceDir, datadirPrivateKey)
   332  	if err := crypto.SaveECDSA(keyfile, key); err != nil {
   333  		log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
   334  	}
   335  	return key
   336  }
   337  
   338  // StaticNodes returns a list of node enode URLs configured as static nodes.
   339  func (c *Config) StaticNodes() []*discover.Node {
   340  	return c.parsePersistentNodes(c.ResolvePath(datadirStaticNodes))
   341  }
   342  
   343  // TrustedNodes returns a list of node enode URLs configured as trusted nodes.
   344  func (c *Config) TrustedNodes() []*discover.Node {
   345  	return c.parsePersistentNodes(c.ResolvePath(datadirTrustedNodes))
   346  }
   347  
   348  // parsePersistentNodes parses a list of discovery node URLs loaded from a .json
   349  // file from within the data directory.
   350  func (c *Config) parsePersistentNodes(path string) []*discover.Node {
   351  	// Short circuit if no node config is present
   352  	if c.DataDir == "" {
   353  		return nil
   354  	}
   355  	if _, err := os.Stat(path); err != nil {
   356  		return nil
   357  	}
   358  	// Load the nodes from the config file.
   359  	var nodelist []string
   360  	if err := common.LoadJSON(path, &nodelist); err != nil {
   361  		log.Error(fmt.Sprintf("Can't load node file %s: %v", path, err))
   362  		return nil
   363  	}
   364  	// Interpret the list as a discovery node array
   365  	var nodes []*discover.Node
   366  	for _, url := range nodelist {
   367  		if url == "" {
   368  			continue
   369  		}
   370  		node, err := discover.ParseNode(url)
   371  		if err != nil {
   372  			log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err))
   373  			continue
   374  		}
   375  		nodes = append(nodes, node)
   376  	}
   377  	return nodes
   378  }
   379  
   380  // AccountConfig determines the settings for scrypt and keydirectory
   381  func (c *Config) AccountConfig() (int, int, string, error) {
   382  	scryptN := keystore.StandardScryptN
   383  	scryptP := keystore.StandardScryptP
   384  	if c.UseLightweightKDF {
   385  		scryptN = keystore.LightScryptN
   386  		scryptP = keystore.LightScryptP
   387  	}
   388  
   389  	var (
   390  		keydir string
   391  		err    error
   392  	)
   393  	switch {
   394  	case filepath.IsAbs(c.KeyStoreDir):
   395  		keydir = c.KeyStoreDir
   396  	case c.DataDir != "":
   397  		if c.KeyStoreDir == "" {
   398  			keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore)
   399  		} else {
   400  			keydir, err = filepath.Abs(c.KeyStoreDir)
   401  		}
   402  	case c.KeyStoreDir != "":
   403  		keydir, err = filepath.Abs(c.KeyStoreDir)
   404  	}
   405  	return scryptN, scryptP, keydir, err
   406  }
   407  
   408  func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
   409  	scryptN, scryptP, keydir, err := conf.AccountConfig()
   410  	var ephemeral string
   411  	if keydir == "" {
   412  		// There is no datadir.
   413  		keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
   414  		ephemeral = keydir
   415  	}
   416  
   417  	if err != nil {
   418  		return nil, "", err
   419  	}
   420  	if err := os.MkdirAll(keydir, 0700); err != nil {
   421  		return nil, "", err
   422  	}
   423  	// Assemble the account manager and supported backends
   424  	backends := []accounts.Backend{
   425  		keystore.NewKeyStore(keydir, scryptN, scryptP),
   426  	}
   427  	if !conf.NoUSB {
   428  		// Start a USB hub for Ledger hardware wallets
   429  		if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil {
   430  			log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err))
   431  		} else {
   432  			backends = append(backends, ledgerhub)
   433  		}
   434  		// Start a USB hub for Trezor hardware wallets
   435  		if trezorhub, err := usbwallet.NewTrezorHub(); err != nil {
   436  			log.Warn(fmt.Sprintf("Failed to start Trezor hub, disabling: %v", err))
   437  		} else {
   438  			backends = append(backends, trezorhub)
   439  		}
   440  	}
   441  	return accounts.NewManager(backends...), ephemeral, nil
   442  }