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