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