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