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