|
| 1 | +// Copyright 2015 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 | + "encoding/json" |
| 22 | + "io/ioutil" |
| 23 | + "net" |
| 24 | + "os" |
| 25 | + "path/filepath" |
| 26 | + |
| 27 | + "github.com/ethereum/go-ethereum/crypto" |
| 28 | + "github.com/ethereum/go-ethereum/logger" |
| 29 | + "github.com/ethereum/go-ethereum/logger/glog" |
| 30 | + "github.com/ethereum/go-ethereum/p2p/discover" |
| 31 | + "github.com/ethereum/go-ethereum/p2p/nat" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + datadirPrivateKey = "nodekey" // Path within the datadir to the node's private key |
| 36 | + datadirStaticNodes = "static-nodes.json" // Path within the datadir to the static node list |
| 37 | + datadirTrustedNodes = "trusted-nodes.json" // Path within the datadir to the trusted node list |
| 38 | + datadirNodeDatabase = "nodes" // Path within the datadir to store the node infos |
| 39 | +) |
| 40 | + |
| 41 | +// Config represents a small collection of configuration values to fine tune the |
| 42 | +// P2P network layer of a protocol stack. These values can be further extended by |
| 43 | +// all registered services. |
| 44 | +type Config struct { |
| 45 | + // DataDir is the file system folder the node should use for any data storage |
| 46 | + // requirements. The configured data directory will not be directly shared with |
| 47 | + // registered services, instead those can use utility methods to create/access |
| 48 | + // databases or flat files. This enables ephemeral nodes which can fully reside |
| 49 | + // in memory. |
| 50 | + DataDir string |
| 51 | + |
| 52 | + // This field should be a valid secp256k1 private key that will be used for both |
| 53 | + // remote peer identification as well as network traffic encryption. If no key |
| 54 | + // is configured, the preset one is loaded from the data dir, generating it if |
| 55 | + // needed. |
| 56 | + PrivateKey *ecdsa.PrivateKey |
| 57 | + |
| 58 | + // Name sets the node name of this server. Use common.MakeName to create a name |
| 59 | + // that follows existing conventions. |
| 60 | + Name string |
| 61 | + |
| 62 | + // NoDiscovery specifies whether the peer discovery mechanism should be started |
| 63 | + // or not. Disabling is usually useful for protocol debugging (manual topology). |
| 64 | + NoDiscovery bool |
| 65 | + |
| 66 | + // Bootstrap nodes used to establish connectivity with the rest of the network. |
| 67 | + BootstrapNodes []*discover.Node |
| 68 | + |
| 69 | + // Network interface address on which the node should listen for inbound peers. |
| 70 | + ListenAddr string |
| 71 | + |
| 72 | + // If set to a non-nil value, the given NAT port mapper is used to make the |
| 73 | + // listening port available to the Internet. |
| 74 | + NAT nat.Interface |
| 75 | + |
| 76 | + // If Dialer is set to a non-nil value, the given Dialer is used to dial outbound |
| 77 | + // peer connections. |
| 78 | + Dialer *net.Dialer |
| 79 | + |
| 80 | + // If NoDial is true, the node will not dial any peers. |
| 81 | + NoDial bool |
| 82 | + |
| 83 | + // MaxPeers is the maximum number of peers that can be connected. If this is |
| 84 | + // set to zero, then only the configured static and trusted peers can connect. |
| 85 | + MaxPeers int |
| 86 | + |
| 87 | + // MaxPendingPeers is the maximum number of peers that can be pending in the |
| 88 | + // handshake phase, counted separately for inbound and outbound connections. |
| 89 | + // Zero defaults to preset values. |
| 90 | + MaxPendingPeers int |
| 91 | +} |
| 92 | + |
| 93 | +// NodeKey retrieves the currently configured private key of the node, checking |
| 94 | +// first any manually set key, falling back to the one found in the configured |
| 95 | +// data folder. If no key can be found, a new one is generated. |
| 96 | +func (c *Config) NodeKey() *ecdsa.PrivateKey { |
| 97 | + // Use any specifically configured key |
| 98 | + if c.PrivateKey != nil { |
| 99 | + return c.PrivateKey |
| 100 | + } |
| 101 | + // Generate ephemeral key if no datadir is being used |
| 102 | + if c.DataDir == "" { |
| 103 | + key, err := crypto.GenerateKey() |
| 104 | + if err != nil { |
| 105 | + glog.Fatalf("Failed to generate ephemeral node key: %v", err) |
| 106 | + } |
| 107 | + return key |
| 108 | + } |
| 109 | + // Fall back to persistent key from the data directory |
| 110 | + keyfile := filepath.Join(c.DataDir, datadirPrivateKey) |
| 111 | + if key, err := crypto.LoadECDSA(keyfile); err == nil { |
| 112 | + return key |
| 113 | + } |
| 114 | + // No persistent key found, generate and store a new one |
| 115 | + key, err := crypto.GenerateKey() |
| 116 | + if err != nil { |
| 117 | + glog.Fatalf("Failed to generate node key: %v", err) |
| 118 | + } |
| 119 | + if err := crypto.SaveECDSA(keyfile, key); err != nil { |
| 120 | + glog.V(logger.Error).Infof("Failed to persist node key: %v", err) |
| 121 | + } |
| 122 | + return key |
| 123 | +} |
| 124 | + |
| 125 | +// StaticNodes returns a list of node enode URLs configured as static nodes. |
| 126 | +func (c *Config) StaticNodes() []*discover.Node { |
| 127 | + return c.parsePersistentNodes(datadirStaticNodes) |
| 128 | +} |
| 129 | + |
| 130 | +// TrusterNodes returns a list of node enode URLs configured as trusted nodes. |
| 131 | +func (c *Config) TrusterNodes() []*discover.Node { |
| 132 | + return c.parsePersistentNodes(datadirTrustedNodes) |
| 133 | +} |
| 134 | + |
| 135 | +// parsePersistentNodes parses a list of discovery node URLs loaded from a .json |
| 136 | +// file from within the data directory. |
| 137 | +func (c *Config) parsePersistentNodes(file string) []*discover.Node { |
| 138 | + // Short circuit if no node config is present |
| 139 | + if c.DataDir == "" { |
| 140 | + return nil |
| 141 | + } |
| 142 | + path := filepath.Join(c.DataDir, file) |
| 143 | + if _, err := os.Stat(path); err != nil { |
| 144 | + return nil |
| 145 | + } |
| 146 | + // Load the nodes from the config file |
| 147 | + blob, err := ioutil.ReadFile(path) |
| 148 | + if err != nil { |
| 149 | + glog.V(logger.Error).Infof("Failed to access nodes: %v", err) |
| 150 | + return nil |
| 151 | + } |
| 152 | + nodelist := []string{} |
| 153 | + if err := json.Unmarshal(blob, &nodelist); err != nil { |
| 154 | + glog.V(logger.Error).Infof("Failed to load nodes: %v", err) |
| 155 | + return nil |
| 156 | + } |
| 157 | + // Interpret the list as a discovery node array |
| 158 | + var nodes []*discover.Node |
| 159 | + for _, url := range nodelist { |
| 160 | + if url == "" { |
| 161 | + continue |
| 162 | + } |
| 163 | + node, err := discover.ParseNode(url) |
| 164 | + if err != nil { |
| 165 | + glog.V(logger.Error).Infof("Node URL %s: %v\n", url, err) |
| 166 | + continue |
| 167 | + } |
| 168 | + nodes = append(nodes, node) |
| 169 | + } |
| 170 | + return nodes |
| 171 | +} |
0 commit comments