github.com/core-coin/go-core/v2@v2.1.9/node/config.go (about)

     1  // Copyright 2014 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package node
    18  
    19  import (
    20  	crand "crypto/rand"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"runtime"
    26  	"strings"
    27  	"sync"
    28  
    29  	"github.com/core-coin/go-core/v2/accounts"
    30  	"github.com/core-coin/go-core/v2/accounts/external"
    31  	"github.com/core-coin/go-core/v2/accounts/keystore"
    32  	"github.com/core-coin/go-core/v2/common"
    33  	"github.com/core-coin/go-core/v2/crypto"
    34  	"github.com/core-coin/go-core/v2/log"
    35  	"github.com/core-coin/go-core/v2/p2p"
    36  	"github.com/core-coin/go-core/v2/p2p/enode"
    37  	"github.com/core-coin/go-core/v2/rpc"
    38  )
    39  
    40  const (
    41  	datadirPrivateKey      = "nodekey"            // Path within the datadir to the node's private key
    42  	datadirJWTKey          = "jwtsecret"          // Path within the datadir to the node's jwt secret
    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 gocore is "gocore". If no
    55  	// value is specified, the basename of the current executable is used.
    56  	Name string `toml:"-"`
    57  
    58  	// UserIdent, if set, is used as an additional component in the devp2p node identifier.
    59  	UserIdent string `toml:",omitempty"`
    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 `toml:"-"`
    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  	// Configuration of peer-to-peer networking.
    73  	P2P p2p.Config
    74  
    75  	// KeyStoreDir is the file system folder that contains private keys. The directory can
    76  	// be specified as a relative path, in which case it is resolved relative to the
    77  	// current directory.
    78  	//
    79  	// If KeyStoreDir is empty, the default location is the "keystore" subdirectory of
    80  	// DataDir. If DataDir is unspecified and KeyStoreDir is empty, an ephemeral directory
    81  	// is created by New and destroyed when the node is stopped.
    82  	KeyStoreDir string `toml:",omitempty"`
    83  
    84  	// ExternalSigner specifies an external URI for a clef-type signer
    85  	ExternalSigner string `toml:",omitempty"`
    86  
    87  	// UseLightweightKDF lowers the memory and CPU requirements of the key store
    88  	// scrypt KDF at the expense of security.
    89  	UseLightweightKDF bool `toml:",omitempty"`
    90  
    91  	// InsecureUnlockAllowed allows user to unlock accounts in unsafe http environment.
    92  	InsecureUnlockAllowed bool `toml:",omitempty"`
    93  
    94  	// Led enables Led to indicate if node is operational.
    95  	Led bool `toml:",omitempty"`
    96  
    97  	// LedGPIOPort is the GPIO port number where to enable led.
    98  	LedGPIOPort int `toml:",omitempty"`
    99  
   100  	// IPCPath is the requested location to place the IPC endpoint. If the path is
   101  	// a simple file name, it is placed inside the data directory (or on the root
   102  	// pipe path on Windows), whereas if it's a resolvable path name (absolute or
   103  	// relative), then that specific path is enforced. An empty path disables IPC.
   104  	IPCPath string
   105  
   106  	// HTTPHost is the host interface on which to start the HTTP RPC server. If this
   107  	// field is empty, no HTTP API endpoint will be started.
   108  	HTTPHost string
   109  
   110  	// HTTPPort is the TCP port number on which to start the HTTP RPC server. The
   111  	// default zero value is/ valid and will pick a port number randomly (useful
   112  	// for ephemeral nodes).
   113  	HTTPPort int `toml:",omitempty"`
   114  
   115  	// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
   116  	// clients. Please be aware that CORS is a browser enforced security, it's fully
   117  	// useless for custom HTTP clients.
   118  	HTTPCors []string `toml:",omitempty"`
   119  
   120  	// HTTPVirtualHosts is the list of virtual hostnames which are allowed on incoming requests.
   121  	// This is by default {'localhost'}. Using this prevents attacks like
   122  	// DNS rebinding, which bypasses SOP by simply masquerading as being within the same
   123  	// origin. These attacks do not utilize CORS, since they are not cross-domain.
   124  	// By explicitly checking the Host-header, the server will not allow requests
   125  	// made against the server with a malicious host domain.
   126  	// Requests using ip address directly are not affected
   127  	HTTPVirtualHosts []string `toml:",omitempty"`
   128  
   129  	// HTTPModules is a list of API modules to expose via the HTTP RPC interface.
   130  	// If the module list is empty, all RPC API endpoints designated public will be
   131  	// exposed.
   132  	HTTPModules []string
   133  
   134  	// HTTPTimeouts allows for customization of the timeout values used by the HTTP RPC
   135  	// interface.
   136  	HTTPTimeouts rpc.HTTPTimeouts
   137  
   138  	// HTTPPathPrefix specifies a path prefix on which http-rpc is to be served.
   139  	HTTPPathPrefix string `toml:",omitempty"`
   140  
   141  	// AuthAddr is the listening address on which authenticated APIs are provided.
   142  	AuthAddr string `toml:",omitempty"`
   143  
   144  	// AuthPort is the port number on which authenticated APIs are provided.
   145  	AuthPort int `toml:",omitempty"`
   146  
   147  	// AuthVirtualHosts is the list of virtual hostnames which are allowed on incoming requests
   148  	// for the authenticated api. This is by default {'localhost'}.
   149  	AuthVirtualHosts []string `toml:",omitempty"`
   150  
   151  	// WSHost is the host interface on which to start the websocket RPC server. If
   152  	// this field is empty, no websocket API endpoint will be started.
   153  	WSHost string
   154  
   155  	// WSPort is the TCP port number on which to start the websocket RPC server. The
   156  	// default zero value is/ valid and will pick a port number randomly (useful for
   157  	// ephemeral nodes).
   158  	WSPort int `toml:",omitempty"`
   159  
   160  	// WSPathPrefix specifies a path prefix on which ws-rpc is to be served.
   161  	WSPathPrefix string `toml:",omitempty"`
   162  
   163  	// WSOrigins is the list of domain to accept websocket requests from. Please be
   164  	// aware that the server can only act upon the HTTP request the client sends and
   165  	// cannot verify the validity of the request header.
   166  	WSOrigins []string `toml:",omitempty"`
   167  
   168  	// WSModules is a list of API modules to expose via the websocket RPC interface.
   169  	// If the module list is empty, all RPC API endpoints designated public will be
   170  	// exposed.
   171  	WSModules []string
   172  
   173  	// WSExposeAll exposes all API modules via the WebSocket RPC interface rather
   174  	// than just the public ones.
   175  	//
   176  	// *WARNING* Only set this if the node is running in a trusted network, exposing
   177  	// private APIs to untrusted users is a major security risk.
   178  	WSExposeAll bool `toml:",omitempty"`
   179  
   180  	// GraphQLCors is the Cross-Origin Resource Sharing header to send to requesting
   181  	// clients. Please be aware that CORS is a browser enforced security, it's fully
   182  	// useless for custom HTTP clients.
   183  	GraphQLCors []string `toml:",omitempty"`
   184  
   185  	// GraphQLVirtualHosts is the list of virtual hostnames which are allowed on incoming requests.
   186  	// This is by default {'localhost'}. Using this prevents attacks like
   187  	// DNS rebinding, which bypasses SOP by simply masquerading as being within the same
   188  	// origin. These attacks do not utilize CORS, since they are not cross-domain.
   189  	// By explicitly checking the Host-header, the server will not allow requests
   190  	// made against the server with a malicious host domain.
   191  	// Requests using ip address directly are not affected
   192  	GraphQLVirtualHosts []string `toml:",omitempty"`
   193  
   194  	// Logger is a custom logger to use with the p2p.Server.
   195  	Logger log.Logger `toml:",omitempty"`
   196  
   197  	// BTTP enables blocks broadcasting to trusted peers at first and then to all another connected peers
   198  	BTTP bool
   199  
   200  	// JWTSecret is the path to the hex-encoded jwt secret.
   201  	JWTSecret string `toml:",omitempty"`
   202  
   203  	staticNodesWarning       bool
   204  	trustedNodesWarning      bool
   205  	oldGocoreResourceWarning bool
   206  }
   207  
   208  // IPCEndpoint resolves an IPC endpoint based on a configured value, taking into
   209  // account the set data folders as well as the designated platform we're currently
   210  // running on.
   211  func (c *Config) IPCEndpoint() string {
   212  	// Short circuit if IPC has not been enabled
   213  	if c.IPCPath == "" {
   214  		return ""
   215  	}
   216  	// On windows we can only use plain top-level pipes
   217  	if runtime.GOOS == "windows" {
   218  		if strings.HasPrefix(c.IPCPath, `\\.\pipe\`) {
   219  			return c.IPCPath
   220  		}
   221  		return `\\.\pipe\` + c.IPCPath
   222  	}
   223  	// Resolve names into the data directory full paths otherwise
   224  	if filepath.Base(c.IPCPath) == c.IPCPath {
   225  		if c.DataDir == "" {
   226  			return filepath.Join(os.TempDir(), c.IPCPath)
   227  		}
   228  		return filepath.Join(c.DataDir, c.IPCPath)
   229  	}
   230  	return c.IPCPath
   231  }
   232  
   233  // NodeDB returns the path to the discovery node database.
   234  func (c *Config) NodeDB() string {
   235  	if c.DataDir == "" {
   236  		return "" // ephemeral
   237  	}
   238  	return c.ResolvePath(datadirNodeDatabase)
   239  }
   240  
   241  // DefaultIPCEndpoint returns the IPC path used by default.
   242  func DefaultIPCEndpoint(clientIdentifier string) string {
   243  	if clientIdentifier == "" {
   244  		clientIdentifier = strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
   245  		if clientIdentifier == "" {
   246  			panic("empty executable name")
   247  		}
   248  	}
   249  	config := &Config{DataDir: DefaultDataDir(), IPCPath: clientIdentifier + ".ipc"}
   250  	return config.IPCEndpoint()
   251  }
   252  
   253  // HTTPEndpoint resolves an HTTP endpoint based on the configured host interface
   254  // and port parameters.
   255  func (c *Config) HTTPEndpoint() string {
   256  	if c.HTTPHost == "" {
   257  		return ""
   258  	}
   259  	return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort)
   260  }
   261  
   262  // DefaultHTTPEndpoint returns the HTTP endpoint used by default.
   263  func DefaultHTTPEndpoint() string {
   264  	config := &Config{HTTPHost: DefaultHTTPHost, HTTPPort: DefaultHTTPPort, AuthPort: DefaultAuthPort}
   265  	return config.HTTPEndpoint()
   266  }
   267  
   268  // WSEndpoint resolves a websocket endpoint based on the configured host interface
   269  // and port parameters.
   270  func (c *Config) WSEndpoint() string {
   271  	if c.WSHost == "" {
   272  		return ""
   273  	}
   274  	return fmt.Sprintf("%s:%d", c.WSHost, c.WSPort)
   275  }
   276  
   277  // DefaultWSEndpoint returns the websocket endpoint used by default.
   278  func DefaultWSEndpoint() string {
   279  	config := &Config{WSHost: DefaultWSHost, WSPort: DefaultWSPort}
   280  	return config.WSEndpoint()
   281  }
   282  
   283  // ExtRPCEnabled returns the indicator whether node enables the external
   284  // RPC(http, ws or graphql).
   285  func (c *Config) ExtRPCEnabled() bool {
   286  	return c.HTTPHost != "" || c.WSHost != ""
   287  }
   288  
   289  // NodeName returns the devp2p node identifier.
   290  func (c *Config) NodeName() string {
   291  	name := c.name()
   292  	// Backwards compatibility: previous versions used title-cased "Gocore", keep that.
   293  	if name == "gocore" || name == "gocore-testnet" {
   294  		name = "Gocore"
   295  	}
   296  	if c.UserIdent != "" {
   297  		name += "/" + c.UserIdent
   298  	}
   299  	if c.Version != "" {
   300  		name += "/" + c.Version
   301  	}
   302  	name += "/" + runtime.GOOS + "-" + runtime.GOARCH
   303  	name += "/" + runtime.Version()
   304  	return name
   305  }
   306  
   307  func (c *Config) name() string {
   308  	if c.Name == "" {
   309  		progname := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
   310  		if progname == "" {
   311  			panic("empty executable name, set Config.Name")
   312  		}
   313  		return progname
   314  	}
   315  	return c.Name
   316  }
   317  
   318  // These resources are resolved differently for "gocore" instances.
   319  var isOldGocoreResource = map[string]bool{
   320  	"chaindata":          true,
   321  	"nodes":              true,
   322  	"nodekey":            true,
   323  	"static-nodes.json":  false, // no warning for these because they have their
   324  	"trusted-nodes.json": false, // own separate warning.
   325  }
   326  
   327  // ResolvePath resolves path in the instance directory.
   328  func (c *Config) ResolvePath(path string) string {
   329  	if filepath.IsAbs(path) {
   330  		return path
   331  	}
   332  	if c.DataDir == "" {
   333  		return ""
   334  	}
   335  	// Backwards-compatibility: ensure that data directory files created
   336  	// by gocore 1.4 are used if they exist.
   337  	if warn, isOld := isOldGocoreResource[path]; isOld {
   338  		oldpath := ""
   339  		if c.name() == "gocore" {
   340  			oldpath = filepath.Join(c.DataDir, path)
   341  		}
   342  		if oldpath != "" && common.FileExist(oldpath) {
   343  			if warn {
   344  				c.warnOnce(&c.oldGocoreResourceWarning, "Using deprecated resource file %s, please move this file to the 'gocore' subdirectory of datadir.", oldpath)
   345  			}
   346  			return oldpath
   347  		}
   348  	}
   349  	return filepath.Join(c.instanceDir(), path)
   350  }
   351  
   352  func (c *Config) instanceDir() string {
   353  	if c.DataDir == "" {
   354  		return ""
   355  	}
   356  	return filepath.Join(c.DataDir, c.name())
   357  }
   358  
   359  // NodeKey retrieves the currently configured private key of the node, checking
   360  // first any manually set key, falling back to the one found in the configured
   361  // data folder. If no key can be found, a new one is generated.
   362  func (c *Config) NodeKey() *crypto.PrivateKey {
   363  	// Use any specifically configured key.
   364  	if c.P2P.PrivateKey != nil {
   365  		return c.P2P.PrivateKey
   366  	}
   367  	// Generate ephemeral key if no datadir is being used.
   368  	if c.DataDir == "" {
   369  		key, err := crypto.GenerateKey(crand.Reader)
   370  		if err != nil {
   371  			log.Crit(fmt.Sprintf("Failed to generate ephemeral node key: %v", err))
   372  		}
   373  		return key
   374  	}
   375  
   376  	keyfile := c.ResolvePath(datadirPrivateKey)
   377  	if key, err := crypto.LoadEDDSA(keyfile); err == nil {
   378  		return key
   379  	}
   380  	// No persistent key found, generate and store a new one.
   381  	key, err := crypto.GenerateKey(crand.Reader)
   382  	if err != nil {
   383  		log.Crit(fmt.Sprintf("Failed to generate node key: %v", err))
   384  	}
   385  	instanceDir := filepath.Join(c.DataDir, c.name())
   386  	if err := os.MkdirAll(instanceDir, 0700); err != nil {
   387  		log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
   388  		return key
   389  	}
   390  	keyfile = filepath.Join(instanceDir, datadirPrivateKey)
   391  	if err := crypto.SaveEDDSA(keyfile, key); err != nil {
   392  		log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
   393  	}
   394  	return key
   395  }
   396  
   397  // StaticNodes returns a list of node enode URLs configured as static nodes.
   398  func (c *Config) StaticNodes() []*enode.Node {
   399  	return c.parsePersistentNodes(&c.staticNodesWarning, c.ResolvePath(datadirStaticNodes))
   400  }
   401  
   402  // TrustedNodes returns a list of node enode URLs configured as trusted nodes.
   403  func (c *Config) TrustedNodes() []*enode.Node {
   404  	return c.parsePersistentNodes(&c.trustedNodesWarning, c.ResolvePath(datadirTrustedNodes))
   405  }
   406  
   407  // parsePersistentNodes parses a list of discovery node URLs loaded from a .json
   408  // file from within the data directory.
   409  func (c *Config) parsePersistentNodes(w *bool, path string) []*enode.Node {
   410  	// Short circuit if no node config is present
   411  	if c.DataDir == "" {
   412  		return nil
   413  	}
   414  	if _, err := os.Stat(path); err != nil {
   415  		return nil
   416  	}
   417  	c.warnOnce(w, "Found deprecated node list file %s, please use the TOML config file instead.", path)
   418  
   419  	// Load the nodes from the config file.
   420  	var nodelist []string
   421  	if err := common.LoadJSON(path, &nodelist); err != nil {
   422  		log.Error(fmt.Sprintf("Can't load node list file: %v", err))
   423  		return nil
   424  	}
   425  	// Interpret the list as a discovery node array
   426  	var nodes []*enode.Node
   427  	for _, url := range nodelist {
   428  		if url == "" {
   429  			continue
   430  		}
   431  		node, err := enode.Parse(enode.ValidSchemes, url)
   432  		if err != nil {
   433  			log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err))
   434  			continue
   435  		}
   436  		nodes = append(nodes, node)
   437  	}
   438  	return nodes
   439  }
   440  
   441  // AccountConfig determines the settings for scrypt and keydirectory
   442  func (c *Config) AccountConfig() (int, int, string, error) {
   443  	scryptN := keystore.StandardScryptN
   444  	scryptP := keystore.StandardScryptP
   445  	if c.UseLightweightKDF {
   446  		scryptN = keystore.LightScryptN
   447  		scryptP = keystore.LightScryptP
   448  	}
   449  
   450  	var (
   451  		keydir string
   452  		err    error
   453  	)
   454  	switch {
   455  	case filepath.IsAbs(c.KeyStoreDir):
   456  		keydir = c.KeyStoreDir
   457  	case c.DataDir != "":
   458  		if c.KeyStoreDir == "" {
   459  			keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore)
   460  		} else {
   461  			keydir, err = filepath.Abs(c.KeyStoreDir)
   462  		}
   463  	case c.KeyStoreDir != "":
   464  		keydir, err = filepath.Abs(c.KeyStoreDir)
   465  	}
   466  	return scryptN, scryptP, keydir, err
   467  }
   468  
   469  func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
   470  	scryptN, scryptP, keydir, err := conf.AccountConfig()
   471  	var ephemeral string
   472  	if keydir == "" {
   473  		// There is no datadir.
   474  		keydir, err = ioutil.TempDir("", "go-core-keystore")
   475  		ephemeral = keydir
   476  	}
   477  
   478  	if err != nil {
   479  		return nil, "", err
   480  	}
   481  	if err := os.MkdirAll(keydir, 0700); err != nil {
   482  		return nil, "", err
   483  	}
   484  	// Assemble the account manager and supported backends
   485  	var backends []accounts.Backend
   486  	if len(conf.ExternalSigner) > 0 {
   487  		log.Info("Using external signer", "url", conf.ExternalSigner)
   488  		if extapi, err := external.NewExternalBackend(conf.ExternalSigner); err == nil {
   489  			backends = append(backends, extapi)
   490  		} else {
   491  			return nil, "", fmt.Errorf("error connecting to external signer: %v", err)
   492  		}
   493  	}
   494  	if len(backends) == 0 {
   495  		// For now, we're using EITHER external signer OR local signers.
   496  		// If/when we implement some form of lockfile for USB and keystore wallets,
   497  		// we can have both, but it's very confusing for the user to see the same
   498  		// accounts in both externally and locally, plus very racey.
   499  		backends = append(backends, keystore.NewKeyStore(keydir, scryptN, scryptP))
   500  	}
   501  
   502  	return accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: conf.InsecureUnlockAllowed}, backends...), ephemeral, nil
   503  }
   504  
   505  var warnLock sync.Mutex
   506  
   507  func (c *Config) warnOnce(w *bool, format string, args ...interface{}) {
   508  	warnLock.Lock()
   509  	defer warnLock.Unlock()
   510  
   511  	if *w {
   512  		return
   513  	}
   514  	l := c.Logger
   515  	if l == nil {
   516  		l = log.Root()
   517  	}
   518  	l.Warn(fmt.Sprintf(format, args...))
   519  	*w = true
   520  }