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