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