gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/node/config.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain 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 "gitlab.com/aquachain/aquachain/aqua/accounts" 29 "gitlab.com/aquachain/aquachain/aqua/accounts/keystore" 30 "gitlab.com/aquachain/aquachain/aqua/accounts/usbwallet" 31 "gitlab.com/aquachain/aquachain/common" 32 "gitlab.com/aquachain/aquachain/common/log" 33 "gitlab.com/aquachain/aquachain/crypto" 34 "gitlab.com/aquachain/aquachain/p2p" 35 "gitlab.com/aquachain/aquachain/p2p/discover" 36 ) 37 38 const ( 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 aquachain is "aquachain". 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 // UseUSB enables hardware wallet monitoring and connectivity. 86 UseUSB 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 // HTTPVirtualHosts is the list of virtual hostnames which are allowed on incoming requests. 109 // This is by default {'localhost'}. Using this prevents attacks like 110 // DNS rebinding, which bypasses SOP by simply masquerading as being within the same 111 // origin. These attacks do not utilize CORS, since they are not cross-domain. 112 // By explicitly checking the Host-header, the server will not allow requests 113 // made against the server with a malicious host domain. 114 // Requests using ip address directly are not affected 115 HTTPVirtualHosts []string `toml:",omitempty"` 116 RPCAllowIP []string `toml:",omitempty"` 117 118 // HTTPModules is a list of API modules to expose via the HTTP RPC interface. 119 // If the module list is empty, all RPC API endpoints designated public will be 120 // exposed. 121 HTTPModules []string `toml:",omitempty"` 122 123 // WSHost is the host interface on which to start the websocket RPC server. If 124 // this field is empty, no websocket API endpoint will be started. 125 WSHost string `toml:",omitempty"` 126 127 // WSPort is the TCP port number on which to start the websocket RPC server. The 128 // default zero value is/ valid and will pick a port number randomly (useful for 129 // ephemeral nodes). 130 WSPort int `toml:",omitempty"` 131 132 // WSOrigins is the list of domain to accept websocket requests from. Please be 133 // aware that the server can only act upon the HTTP request the client sends and 134 // cannot verify the validity of the request header. 135 WSOrigins []string `toml:",omitempty"` 136 137 // WSModules is a list of API modules to expose via the websocket RPC interface. 138 // If the module list is empty, all RPC API endpoints designated public will be 139 // exposed. 140 WSModules []string `toml:",omitempty"` 141 142 // WSExposeAll exposes all API modules via the WebSocket RPC interface rather 143 // than just the public ones. 144 // 145 // *WARNING* Only set this if the node is running in a trusted network, exposing 146 // private APIs to untrusted users is a major security risk. 147 WSExposeAll bool `toml:",omitempty"` 148 149 // Logger is a custom logger to use with the p2p.Server. 150 Logger log.Logger `toml:",omitempty"` 151 152 // NoKeys disables all signing and keystore functions 153 NoKeys bool 154 155 // RPCBehindProxy if true, tried X-FORWARDED-FOR and X-REAL-IP headers to 156 // fetch client's remote IP 157 RPCBehindProxy bool 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 an 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 if name == "aquachain" { 239 name = "Aquachain" 240 } 241 if c.UserIdent != "" { 242 name += "/" + c.UserIdent 243 } 244 if c.Version != "" { 245 name += "/v" + c.Version 246 } 247 name += "/" + runtime.GOOS + "-" + runtime.GOARCH 248 name += "/" + common.ShortGoVersion() 249 return name 250 } 251 252 func (c *Config) name() string { 253 if c.Name == "" { 254 progname := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe") 255 if progname == "" { 256 panic("empty executable name, set Config.Name") 257 } 258 return progname 259 } 260 return c.Name 261 } 262 263 // These resources are resolved differently for "aquachain" instances. 264 var isOldAquachainResource = map[string]bool{ 265 "chaindata": true, 266 "nodes": true, 267 "nodekey": true, 268 "static-nodes.json": true, 269 "trusted-nodes.json": true, 270 } 271 272 // resolvePath resolves path in the instance directory. 273 func (c *Config) resolvePath(path string) string { 274 if filepath.IsAbs(path) { 275 return path 276 } 277 if c.DataDir == "" { 278 return "" 279 } 280 // Backwards-compatibility: ensure that data directory files created 281 // by aquachain 1.4 are used if they exist. 282 if c.name() == "aquachain" && isOldAquachainResource[path] { 283 oldpath := "" 284 if c.Name == "aquachain" { 285 oldpath = filepath.Join(c.DataDir, path) 286 } 287 if oldpath != "" && common.FileExist(oldpath) { 288 // TODO: print warning 289 return oldpath 290 } 291 } 292 return filepath.Join(c.instanceDir(), path) 293 } 294 295 func (c *Config) instanceDir() string { 296 if c.DataDir == "" { 297 return "" 298 } 299 return filepath.Join(c.DataDir, c.name()) 300 } 301 302 // NodeKey retrieves the currently configured private key of the node, checking 303 // first any manually set key, falling back to the one found in the configured 304 // data folder. If no key can be found, a new one is generated. 305 func (c *Config) NodeKey() *ecdsa.PrivateKey { 306 // Use any specifically configured key. 307 if c.P2P.PrivateKey != nil { 308 return c.P2P.PrivateKey 309 } 310 // Generate ephemeral key if no datadir is being used. 311 if c.DataDir == "" { 312 key, err := crypto.GenerateKey() 313 if err != nil { 314 log.Crit(fmt.Sprintf("Failed to generate ephemeral node key: %v", err)) 315 } 316 return key 317 } 318 319 keyfile := c.resolvePath(datadirPrivateKey) 320 if key, err := crypto.LoadECDSA(keyfile); err == nil { 321 return key 322 } 323 // No persistent key found, generate and store a new one. 324 key, err := crypto.GenerateKey() 325 if err != nil { 326 log.Crit(fmt.Sprintf("Failed to generate node key: %v", err)) 327 } 328 instanceDir := filepath.Join(c.DataDir, c.name()) 329 if err := os.MkdirAll(instanceDir, 0700); err != nil { 330 log.Error(fmt.Sprintf("Failed to persist node key: %v", err)) 331 return key 332 } 333 keyfile = filepath.Join(instanceDir, datadirPrivateKey) 334 if err := crypto.SaveECDSA(keyfile, key); err != nil { 335 log.Error(fmt.Sprintf("Failed to persist node key: %v", err)) 336 } 337 return key 338 } 339 340 // StaticNodes returns a list of node enode URLs configured as static nodes. 341 func (c *Config) StaticNodes() []*discover.Node { 342 return c.parsePersistentNodes(c.resolvePath(datadirStaticNodes)) 343 } 344 345 // TrustedNodes returns a list of node enode URLs configured as trusted nodes. 346 func (c *Config) TrustedNodes() []*discover.Node { 347 return c.parsePersistentNodes(c.resolvePath(datadirTrustedNodes)) 348 } 349 350 // parsePersistentNodes parses a list of discovery node URLs loaded from a .json 351 // file from within the data directory. 352 func (c *Config) parsePersistentNodes(path string) []*discover.Node { 353 // Short circuit if no node config is present 354 if c.DataDir == "" { 355 return nil 356 } 357 if _, err := os.Stat(path); err != nil { 358 return nil 359 } 360 // Load the nodes from the config file. 361 var nodelist []string 362 if err := common.LoadJSON(path, &nodelist); err != nil { 363 log.Error(fmt.Sprintf("Can't load node file %s: %v", path, err)) 364 return nil 365 } 366 // Interpret the list as a discovery node array 367 var nodes []*discover.Node 368 for _, url := range nodelist { 369 if url == "" { 370 continue 371 } 372 node, err := discover.ParseNode(url) 373 if err != nil { 374 log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err)) 375 continue 376 } 377 nodes = append(nodes, node) 378 } 379 return nodes 380 } 381 382 // AccountConfig determines the settings for scrypt and keydirectory 383 func (c *Config) AccountConfig() (int, int, string, error) { 384 scryptN := keystore.StandardScryptN 385 scryptP := keystore.StandardScryptP 386 if c.UseLightweightKDF { 387 scryptN = keystore.LightScryptN 388 scryptP = keystore.LightScryptP 389 } 390 if c.NoKeys { 391 return scryptN, scryptP, "", nil 392 } 393 394 var ( 395 keydir string 396 err error 397 ) 398 switch { 399 case filepath.IsAbs(c.KeyStoreDir): 400 keydir = c.KeyStoreDir 401 case c.DataDir != "": 402 if c.KeyStoreDir == "" { 403 keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore) 404 } else { 405 keydir, err = filepath.Abs(c.KeyStoreDir) 406 } 407 case c.KeyStoreDir != "": 408 keydir, err = filepath.Abs(c.KeyStoreDir) 409 } 410 return scryptN, scryptP, keydir, err 411 } 412 413 func makeAccountManager(conf *Config) (*accounts.Manager, string, error) { 414 scryptN, scryptP, keydir, err := conf.AccountConfig() 415 if keydir == "" { 416 return nil, "", nil 417 } 418 var ephemeral string 419 if keydir == "" { 420 // There is no datadir. 421 keydir, err = ioutil.TempDir("", "aquachain-keystore") 422 ephemeral = keydir 423 } 424 425 if err != nil { 426 return nil, "", err 427 } 428 if err := os.MkdirAll(keydir, 0700); err != nil { 429 return nil, "", err 430 } 431 // Assemble the account manager and supported backends 432 backends := []accounts.Backend{ 433 keystore.NewKeyStore(keydir, scryptN, scryptP), 434 } 435 if conf.UseUSB { 436 // Start a USB hub for Ledger hardware wallets 437 if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil { 438 log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err)) 439 } else { 440 backends = append(backends, ledgerhub) 441 } 442 // Start a USB hub for Trezor hardware wallets 443 if trezorhub, err := usbwallet.NewTrezorHub(); err != nil { 444 log.Warn(fmt.Sprintf("Failed to start Trezor hub, disabling: %v", err)) 445 } else { 446 backends = append(backends, trezorhub) 447 } 448 } 449 return accounts.NewManager(backends...), ephemeral, nil 450 }