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