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