github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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/kisexp/xdchain/accounts" 30 "github.com/kisexp/xdchain/accounts/external" 31 "github.com/kisexp/xdchain/accounts/keystore" 32 "github.com/kisexp/xdchain/accounts/pluggable" 33 "github.com/kisexp/xdchain/accounts/scwallet" 34 "github.com/kisexp/xdchain/accounts/usbwallet" 35 "github.com/kisexp/xdchain/common" 36 "github.com/kisexp/xdchain/crypto" 37 "github.com/kisexp/xdchain/log" 38 "github.com/kisexp/xdchain/p2p" 39 "github.com/kisexp/xdchain/p2p/enode" 40 "github.com/kisexp/xdchain/params" 41 "github.com/kisexp/xdchain/plugin" 42 "github.com/kisexp/xdchain/rpc" 43 ) 44 45 const ( 46 datadirPrivateKey = "nodekey" // Path within the datadir to the node's private key 47 datadirDefaultKeyStore = "keystore" // Path within the datadir to the keystore 48 datadirStaticNodes = "static-nodes.json" // Path within the datadir to the static node list 49 datadirTrustedNodes = "trusted-nodes.json" // Path within the datadir to the trusted node list 50 datadirNodeDatabase = "nodes" // Path within the datadir to store the node infos 51 ) 52 53 // Config represents a small collection of configuration values to fine tune the 54 // P2P network layer of a protocol stack. These values can be further extended by 55 // all registered services. 56 type Config struct { 57 // Name sets the instance name of the node. It must not contain the / character and is 58 // used in the devp2p node identifier. The instance name of geth is "geth". If no 59 // value is specified, the basename of the current executable is used. 60 Name string `toml:"-"` 61 62 // UserIdent, if set, is used as an additional component in the devp2p node identifier. 63 UserIdent string `toml:",omitempty"` 64 65 // Version should be set to the version number of the program. It is used 66 // in the devp2p node identifier. 67 Version string `toml:"-"` 68 69 // DataDir is the file system folder the node should use for any data storage 70 // requirements. The configured data directory will not be directly shared with 71 // registered services, instead those can use utility methods to create/access 72 // databases or flat files. This enables ephemeral nodes which can fully reside 73 // in memory. 74 DataDir string 75 76 // RaftLogDir is the file system folder the node use for raft-state, raft-snap and 77 // raft-wal folders. 78 RaftLogDir string 79 80 // Configuration of peer-to-peer networking. 81 P2P p2p.Config 82 83 // KeyStoreDir is the file system folder that contains private keys. The directory can 84 // be specified as a relative path, in which case it is resolved relative to the 85 // current directory. 86 // 87 // If KeyStoreDir is empty, the default location is the "keystore" subdirectory of 88 // DataDir. If DataDir is unspecified and KeyStoreDir is empty, an ephemeral directory 89 // is created by New and destroyed when the node is stopped. 90 KeyStoreDir string `toml:",omitempty"` 91 92 // ExternalSigner specifies an external URI for a clef-type signer 93 ExternalSigner string `toml:",omitempty"` 94 95 // UseLightweightKDF lowers the memory and CPU requirements of the key store 96 // scrypt KDF at the expense of security. 97 UseLightweightKDF bool `toml:",omitempty"` 98 99 // InsecureUnlockAllowed allows user to unlock accounts in unsafe http environment. 100 InsecureUnlockAllowed bool `toml:",omitempty"` 101 102 // NoUSB disables hardware wallet monitoring and connectivity. 103 NoUSB bool `toml:",omitempty"` 104 105 // SmartCardDaemonPath is the path to the smartcard daemon's socket 106 SmartCardDaemonPath string `toml:",omitempty"` 107 108 // IPCPath is the requested location to place the IPC endpoint. If the path is 109 // a simple file name, it is placed inside the data directory (or on the root 110 // pipe path on Windows), whereas if it's a resolvable path name (absolute or 111 // relative), then that specific path is enforced. An empty path disables IPC. 112 IPCPath string 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 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 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 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 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 // GraphQLCors is the Cross-Origin Resource Sharing header to send to requesting 173 // clients. Please be aware that CORS is a browser enforced security, it's fully 174 // useless for custom HTTP clients. 175 GraphQLCors []string `toml:",omitempty"` 176 177 // GraphQLVirtualHosts is the list of virtual hostnames which are allowed on incoming requests. 178 // This is by default {'localhost'}. Using this prevents attacks like 179 // DNS rebinding, which bypasses SOP by simply masquerading as being within the same 180 // origin. These attacks do not utilize CORS, since they are not cross-domain. 181 // By explicitly checking the Host-header, the server will not allow requests 182 // made against the server with a malicious host domain. 183 // Requests using ip address directly are not affected 184 GraphQLVirtualHosts []string `toml:",omitempty"` 185 186 // Logger is a custom logger to use with the p2p.Server. 187 Logger log.Logger `toml:",omitempty"` 188 189 staticNodesWarning bool 190 trustedNodesWarning bool 191 oldGethResourceWarning bool 192 Plugins *plugin.Settings `toml:",omitempty"` 193 // Quorum: EnableNodePermission comes from EnableNodePermissionFlag --permissioned. 194 EnableNodePermission bool `toml:",omitempty"` 195 EnableMultitenancy bool `toml:",omitempty"` // comes from MultitenancyFlag flag 196 } 197 198 // IPCEndpoint resolves an IPC endpoint based on a configured value, taking into 199 // account the set data folders as well as the designated platform we're currently 200 // running on. 201 func (c *Config) IPCEndpoint() string { 202 // Short circuit if IPC has not been enabled 203 if c.IPCPath == "" { 204 return "" 205 } 206 // On windows we can only use plain top-level pipes 207 if runtime.GOOS == "windows" { 208 if strings.HasPrefix(c.IPCPath, `\\.\pipe\`) { 209 return c.IPCPath 210 } 211 return `\\.\pipe\` + c.IPCPath 212 } 213 // Resolve names into the data directory full paths otherwise 214 if filepath.Base(c.IPCPath) == c.IPCPath { 215 if c.DataDir == "" { 216 return filepath.Join(os.TempDir(), c.IPCPath) 217 } 218 return filepath.Join(c.DataDir, c.IPCPath) 219 } 220 return c.IPCPath 221 } 222 223 // NodeDB returns the path to the discovery node database. 224 func (c *Config) NodeDB() string { 225 if c.DataDir == "" { 226 return "" // ephemeral 227 } 228 return c.ResolvePath(datadirNodeDatabase) 229 } 230 231 // DefaultIPCEndpoint returns the IPC path used by default. 232 func DefaultIPCEndpoint(clientIdentifier string) string { 233 if clientIdentifier == "" { 234 clientIdentifier = strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe") 235 if clientIdentifier == "" { 236 panic("empty executable name") 237 } 238 } 239 config := &Config{DataDir: DefaultDataDir(), IPCPath: clientIdentifier + ".ipc"} 240 return config.IPCEndpoint() 241 } 242 243 // HTTPEndpoint resolves an HTTP endpoint based on the configured host interface 244 // and port parameters. 245 func (c *Config) HTTPEndpoint() string { 246 if c.HTTPHost == "" { 247 return "" 248 } 249 return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort) 250 } 251 252 // DefaultHTTPEndpoint returns the HTTP endpoint used by default. 253 func DefaultHTTPEndpoint() string { 254 config := &Config{HTTPHost: DefaultHTTPHost, HTTPPort: DefaultHTTPPort} 255 return config.HTTPEndpoint() 256 } 257 258 // WSEndpoint resolves a websocket endpoint based on the configured host interface 259 // and port parameters. 260 func (c *Config) WSEndpoint() string { 261 if c.WSHost == "" { 262 return "" 263 } 264 return fmt.Sprintf("%s:%d", c.WSHost, c.WSPort) 265 } 266 267 // DefaultWSEndpoint returns the websocket endpoint used by default. 268 func DefaultWSEndpoint() string { 269 config := &Config{WSHost: DefaultWSHost, WSPort: DefaultWSPort} 270 return config.WSEndpoint() 271 } 272 273 // ExtRPCEnabled returns the indicator whether node enables the external 274 // RPC(http, ws or graphql). 275 func (c *Config) ExtRPCEnabled() bool { 276 return c.HTTPHost != "" || c.WSHost != "" 277 } 278 279 // NodeName returns the devp2p node identifier. 280 func (c *Config) NodeName() string { 281 name := c.name() 282 // Backwards compatibility: previous versions used title-cased "Geth", keep that. 283 if name == "geth" || name == "geth-testnet" { 284 name = "Geth" 285 } 286 if c.UserIdent != "" { 287 name += "/" + c.UserIdent 288 } 289 if c.Version != "" { 290 name += "/v" + c.Version 291 } 292 name += "/" + runtime.GOOS + "-" + runtime.GOARCH 293 name += "/" + runtime.Version() 294 return name 295 } 296 297 func (c *Config) name() string { 298 if c.Name == "" { 299 progname := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe") 300 if progname == "" { 301 panic("empty executable name, set Config.Name") 302 } 303 return progname 304 } 305 return c.Name 306 } 307 308 // These resources are resolved differently for "geth" instances. 309 var isOldGethResource = map[string]bool{ 310 "chaindata": true, 311 "nodes": true, 312 "nodekey": true, 313 "static-nodes.json": false, // no warning for these because they have their 314 "trusted-nodes.json": false, // own separate warning. 315 } 316 317 // ResolvePath resolves path in the instance directory. 318 func (c *Config) ResolvePath(path string) string { 319 if filepath.IsAbs(path) { 320 return path 321 } 322 if c.DataDir == "" { 323 return "" 324 } 325 // Backwards-compatibility: ensure that data directory files created 326 // by geth 1.4 are used if they exist. 327 if warn, isOld := isOldGethResource[path]; isOld { 328 oldpath := "" 329 if c.name() == "geth" { 330 oldpath = filepath.Join(c.DataDir, path) 331 } 332 if oldpath != "" && common.FileExist(oldpath) { 333 if warn { 334 c.warnOnce(&c.oldGethResourceWarning, "Using deprecated resource file %s, please move this file to the 'geth' subdirectory of datadir.", oldpath) 335 } 336 return oldpath 337 } 338 } 339 return filepath.Join(c.instanceDir(), path) 340 } 341 342 func (c *Config) instanceDir() string { 343 if c.DataDir == "" { 344 return "" 345 } 346 return filepath.Join(c.DataDir, c.name()) 347 } 348 349 // NodeKey retrieves the currently configured private key of the node, checking 350 // first any manually set key, falling back to the one found in the configured 351 // data folder. If no key can be found, a new one is generated. 352 func (c *Config) NodeKey() *ecdsa.PrivateKey { 353 // Use any specifically configured key. 354 if c.P2P.PrivateKey != nil { 355 return c.P2P.PrivateKey 356 } 357 // Generate ephemeral key if no datadir is being used. 358 if c.DataDir == "" { 359 key, err := crypto.GenerateKey() 360 if err != nil { 361 log.Crit(fmt.Sprintf("Failed to generate ephemeral node key: %v", err)) 362 } 363 return key 364 } 365 366 keyfile := c.ResolvePath(datadirPrivateKey) 367 if key, err := crypto.LoadECDSA(keyfile); err == nil { 368 return key 369 } 370 // No persistent key found, generate and store a new one. 371 key, err := crypto.GenerateKey() 372 if err != nil { 373 log.Crit(fmt.Sprintf("Failed to generate node key: %v", err)) 374 } 375 instanceDir := filepath.Join(c.DataDir, c.name()) 376 if err := os.MkdirAll(instanceDir, 0700); err != nil { 377 log.Error(fmt.Sprintf("Failed to persist node key: %v", err)) 378 return key 379 } 380 keyfile = filepath.Join(instanceDir, datadirPrivateKey) 381 if err := crypto.SaveECDSA(keyfile, key); err != nil { 382 log.Error(fmt.Sprintf("Failed to persist node key: %v", err)) 383 } 384 return key 385 } 386 387 // StaticNodes returns a list of node enode URLs configured as static nodes. 388 func (c *Config) StaticNodes() []*enode.Node { 389 return c.parsePersistentNodes(&c.staticNodesWarning, c.ResolvePath(datadirStaticNodes)) 390 } 391 392 // TrustedNodes returns a list of node enode URLs configured as trusted nodes. 393 func (c *Config) TrustedNodes() []*enode.Node { 394 return c.parsePersistentNodes(&c.trustedNodesWarning, c.ResolvePath(datadirTrustedNodes)) 395 } 396 397 // parsePersistentNodes parses a list of discovery node URLs loaded from a .json 398 // file from within the data directory. 399 func (c *Config) parsePersistentNodes(w *bool, path string) []*enode.Node { 400 // Short circuit if no node config is present 401 if c.DataDir == "" { 402 return nil 403 } 404 if _, err := os.Stat(path); err != nil { 405 return nil 406 } 407 c.warnOnce(w, "Found deprecated node list file %s, please use the TOML config file instead.", path) 408 409 // Load the nodes from the config file. 410 var nodelist []string 411 if err := common.LoadJSON(path, &nodelist); err != nil { 412 log.Error(fmt.Sprintf("Can't load node list file: %v", err)) 413 return nil 414 } 415 // Interpret the list as a discovery node array 416 var nodes []*enode.Node 417 for _, url := range nodelist { 418 if url == "" { 419 continue 420 } 421 node, err := enode.Parse(enode.ValidSchemes, url) 422 if err != nil { 423 log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err)) 424 continue 425 } 426 nodes = append(nodes, node) 427 } 428 return nodes 429 } 430 431 // AccountConfig determines the settings for scrypt and keydirectory 432 func (c *Config) AccountConfig() (int, int, string, error) { 433 scryptN := keystore.StandardScryptN 434 scryptP := keystore.StandardScryptP 435 if c.UseLightweightKDF { 436 scryptN = keystore.LightScryptN 437 scryptP = keystore.LightScryptP 438 } 439 440 var ( 441 keydir string 442 err error 443 ) 444 switch { 445 case filepath.IsAbs(c.KeyStoreDir): 446 keydir = c.KeyStoreDir 447 case c.DataDir != "": 448 if c.KeyStoreDir == "" { 449 keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore) 450 } else { 451 keydir, err = filepath.Abs(c.KeyStoreDir) 452 } 453 case c.KeyStoreDir != "": 454 keydir, err = filepath.Abs(c.KeyStoreDir) 455 } 456 return scryptN, scryptP, keydir, err 457 } 458 459 // Quorum 460 // 461 // Make sure plugin base dir exists 462 func (c *Config) ResolvePluginBaseDir() error { 463 if c.Plugins == nil { 464 return nil 465 } 466 baseDir := c.Plugins.BaseDir.String() 467 if baseDir == "" { 468 baseDir = filepath.Join(c.DataDir, "plugins") 469 } 470 if !common.FileExist(baseDir) { 471 if err := os.MkdirAll(baseDir, 0755); err != nil { 472 return err 473 } 474 } 475 absBaseDir, err := filepath.Abs(baseDir) 476 if err != nil { 477 return err 478 } 479 c.Plugins.BaseDir = plugin.EnvironmentAwaredValue(absBaseDir) 480 return nil 481 } 482 483 // check if smart-contract-based permissioning is enabled by reading `--permissioned` flag and checking permission config file 484 func (c *Config) IsPermissionEnabled() bool { 485 fullPath := filepath.Join(c.DataDir, params.PERMISSION_MODEL_CONFIG) 486 if _, err := os.Stat(fullPath); err != nil { 487 log.Warn(fmt.Sprintf("%s file is missing. Smart-contract-based permission service will be disabled", params.PERMISSION_MODEL_CONFIG), "error", err) 488 return false 489 } 490 return true 491 } 492 493 func makeAccountManager(conf *Config) (*accounts.Manager, string, error) { 494 scryptN, scryptP, keydir, err := conf.AccountConfig() 495 var ephemeral string 496 if keydir == "" { 497 // There is no datadir. 498 keydir, err = ioutil.TempDir("", "go-ethereum-keystore") 499 ephemeral = keydir 500 } 501 502 if err != nil { 503 return nil, "", err 504 } 505 if err := os.MkdirAll(keydir, 0700); err != nil { 506 return nil, "", err 507 } 508 // Assemble the account manager and supported backends 509 var backends []accounts.Backend 510 if len(conf.ExternalSigner) > 0 { 511 log.Info("Using external signer", "url", conf.ExternalSigner) 512 if extapi, err := external.NewExternalBackend(conf.ExternalSigner); err == nil { 513 backends = append(backends, extapi) 514 } else { 515 return nil, "", fmt.Errorf("error connecting to external signer: %v", err) 516 } 517 } 518 if len(backends) == 0 { 519 // For now, we're using EITHER external signer OR local signers. 520 // If/when we implement some form of lockfile for USB and keystore wallets, 521 // we can have both, but it's very confusing for the user to see the same 522 // accounts in both externally and locally, plus very racey. 523 backends = append(backends, keystore.NewKeyStore(keydir, scryptN, scryptP)) 524 if !conf.NoUSB { 525 // Start a USB hub for Ledger hardware wallets 526 if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil { 527 log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err)) 528 } else { 529 backends = append(backends, ledgerhub) 530 } 531 // Start a USB hub for Trezor hardware wallets (HID version) 532 if trezorhub, err := usbwallet.NewTrezorHubWithHID(); err != nil { 533 log.Warn(fmt.Sprintf("Failed to start HID Trezor hub, disabling: %v", err)) 534 } else { 535 backends = append(backends, trezorhub) 536 } 537 // Start a USB hub for Trezor hardware wallets (WebUSB version) 538 if trezorhub, err := usbwallet.NewTrezorHubWithWebUSB(); err != nil { 539 log.Warn(fmt.Sprintf("Failed to start WebUSB Trezor hub, disabling: %v", err)) 540 } else { 541 backends = append(backends, trezorhub) 542 } 543 } 544 if len(conf.SmartCardDaemonPath) > 0 { 545 // Start a smart card hub 546 if schub, err := scwallet.NewHub(conf.SmartCardDaemonPath, scwallet.Scheme, keydir); err != nil { 547 log.Warn(fmt.Sprintf("Failed to start smart card hub, disabling: %v", err)) 548 } else { 549 backends = append(backends, schub) 550 } 551 } 552 if conf.Plugins != nil { 553 if _, ok := conf.Plugins.Providers[plugin.AccountPluginInterfaceName]; ok { 554 pluginBackend := pluggable.NewBackend() 555 backends = append(backends, pluginBackend) 556 } 557 } 558 } 559 560 return accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: conf.InsecureUnlockAllowed}, backends...), ephemeral, nil 561 } 562 563 var warnLock sync.Mutex 564 565 func (c *Config) warnOnce(w *bool, format string, args ...interface{}) { 566 warnLock.Lock() 567 defer warnLock.Unlock() 568 569 if *w { 570 return 571 } 572 l := c.Logger 573 if l == nil { 574 l = log.Root() 575 } 576 l.Warn(fmt.Sprintf(format, args...)) 577 *w = true 578 }