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