github.com/klaytn/klaytn@v1.10.2/node/config.go (about)

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