This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
config.go
322 lines (285 loc) · 7.55 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"fmt"
"github.com/Qitmeer/qitmeer/common/util"
"github.com/Qitmeer/qitmeer/params"
"github.com/Qitmeer/qitmeer/services/common"
"github.com/urfave/cli/v2"
"os"
"path/filepath"
)
const (
defaultDataDirname = "relay"
defaultPort = "2001"
defaultIP = "0.0.0.0"
defaultLogDirname = "logs"
defaultLogFilename = "relaynode.log"
defaultRPCKeyFile = "rpc.key"
defaultRPCCertFile = "rpc.cert"
defaultMaxRPCClients = 10
defaultRPCListener = "127.0.0.1:2002"
defaultMaxPeers = 1000
)
var (
defaultHomeDir = util.AppDataDir(".", false)
defaultDataDir = filepath.Join(defaultHomeDir, defaultDataDirname)
defaultRPCCertPath = filepath.Join(defaultDataDir, defaultRPCCertFile)
defaultRPCKeyPath = filepath.Join(defaultDataDir, defaultRPCKeyFile)
conf = &Config{}
HomeDir = &cli.StringFlag{
Name: "appdata",
Aliases: []string{"A"},
Usage: "Path to application home directory",
Value: defaultHomeDir,
Destination: &conf.HomeDir,
}
DataDir = &cli.StringFlag{
Name: "datadir",
Aliases: []string{"b"},
Usage: "Directory to store data",
Value: defaultDataDir,
Destination: &conf.DataDir,
}
PrivateKey = &cli.StringFlag{
Name: "privatekey",
Aliases: []string{"p"},
Usage: "private key",
Destination: &conf.PrivateKey,
}
ExternalIP = &cli.StringFlag{
Name: "externalip",
Aliases: []string{"i"},
Usage: "listen external ip",
Destination: &conf.ExternalIP,
}
Port = &cli.StringFlag{
Name: "port",
Aliases: []string{"o"},
Usage: "listen port",
Value: defaultPort,
Destination: &conf.Port,
}
EnableNoise = &cli.BoolFlag{
Name: "noise",
Aliases: []string{"n"},
Usage: "noise",
Value: false,
Destination: &conf.EnableNoise,
}
Network = &cli.StringFlag{
Name: "network",
Aliases: []string{"e"},
Usage: "Network {mainnet,mixnet,privnet,testnet}",
Value: params.MixNetParam.Name,
Destination: &conf.Network,
}
HostDNS = &cli.StringFlag{
Name: "externaldns",
Aliases: []string{"s"},
Usage: "The DNS address advertised by libp2p. This may be used to advertise an external DNS.",
Value: "",
Destination: &conf.HostDNS,
}
UsePeerStore = &cli.BoolFlag{
Name: "pstore",
Aliases: []string{"r"},
Usage: "P2P Peer store",
Value: true,
Destination: &conf.UsePeerStore,
}
NoFileLogging = &cli.BoolFlag{
Name: "nofilelogging",
Aliases: []string{"l"},
Usage: "Disable file logging.",
Value: false,
Destination: &conf.NoFileLogging,
}
DebugLevel = &cli.StringFlag{
Name: "debuglevel",
Aliases: []string{"dl"},
Usage: "Logging level {trace, debug, info, warn, error, critical} ",
Value: "info",
Destination: &conf.DebugLevel,
}
DisableRPC = &cli.BoolFlag{
Name: "norpc",
Aliases: []string{"nr"},
Usage: "Disable built-in RPC server -- NOTE: The RPC server is disabled by default if no rpcuser/rpcpass",
Value: true,
Destination: &conf.DisableRPC,
}
RPCListeners = &cli.StringSliceFlag{
Name: "rpclisten",
Aliases: []string{"rl"},
Usage: "Add an interface/port to listen for RPC connections",
Destination: &conf.RPCListeners,
}
RPCUser = &cli.StringFlag{
Name: "rpcuser",
Aliases: []string{"ru"},
Usage: "Username for RPC connections",
Value: "test",
Destination: &conf.RPCUser,
}
RPCPass = &cli.StringFlag{
Name: "rpcpass",
Aliases: []string{"rp"},
Usage: "Password for RPC connections",
Value: "test",
Destination: &conf.RPCPass,
}
RPCCert = &cli.StringFlag{
Name: "rpccert",
Aliases: []string{"rc"},
Usage: "File containing the certificate file",
Value: defaultRPCCertPath,
Destination: &conf.RPCCert,
}
RPCKey = &cli.StringFlag{
Name: "rpckey",
Aliases: []string{"rk"},
Usage: "File containing the certificate key",
Value: defaultRPCKeyPath,
Destination: &conf.RPCKey,
}
RPCMaxClients = &cli.IntFlag{
Name: "rpcmaxclients",
Aliases: []string{"rmc"},
Usage: "Max number of RPC clients for standard connections",
Value: defaultMaxRPCClients,
Destination: &conf.RPCMaxClients,
}
DisableTLS = &cli.BoolFlag{
Name: "notls",
Aliases: []string{"nt"},
Usage: "Disable TLS for the RPC server -- NOTE: This is only allowed if the RPC server is bound to localhost",
Value: false,
Destination: &conf.DisableTLS,
}
EnableRelay = &cli.BoolFlag{
Name: "relay",
Aliases: []string{"re"},
Usage: "Enable relay service for node",
Value: false,
Destination: &conf.EnableRelay,
}
MaxPeers = &cli.IntFlag{
Name: "maxpeers",
Aliases: []string{"mp"},
Usage: "Max number of inbound and outbound peers",
Value: defaultMaxPeers,
Destination: &conf.MaxPeers,
}
AppFlags = []cli.Flag{
HomeDir,
DataDir,
PrivateKey,
ExternalIP,
Port,
EnableNoise,
Network,
HostDNS,
UsePeerStore,
NoFileLogging,
DebugLevel,
DisableRPC,
RPCListeners,
RPCUser,
RPCPass,
RPCCert,
RPCKey,
RPCMaxClients,
DisableTLS,
EnableRelay,
MaxPeers,
}
)
type Config struct {
HomeDir string
DataDir string
PrivateKey string
ExternalIP string
Port string
EnableNoise bool
Network string
HostDNS string
UsePeerStore bool
NoFileLogging bool
DebugLevel string
DisableRPC bool
RPCListeners cli.StringSlice
RPCUser string
RPCPass string
RPCCert string
RPCKey string
RPCMaxClients int
DisableTLS bool
EnableRelay bool
MaxPeers int
}
func (c *Config) load() error {
var err error
if c.HomeDir != defaultHomeDir {
c.HomeDir, err = filepath.Abs(c.HomeDir)
if err != nil {
return err
}
if c.DataDir == defaultDataDir {
c.DataDir = filepath.Join(c.HomeDir, defaultDataDirname)
}
}
_, err = os.Stat(c.DataDir)
if err != nil && !os.IsExist(err) {
err = os.MkdirAll(c.DataDir, 0700)
if err != nil {
return err
}
}
// assign active network params while we're at it
numNets := 0
if c.Network == params.TestNetParam.Name {
numNets++
params.ActiveNetParams = ¶ms.TestNetParam
}
if c.Network == params.PrivNetParam.Name {
numNets++
// Also disable dns seeding on the private test network.
params.ActiveNetParams = ¶ms.PrivNetParam
}
if c.Network == params.MixNetParam.Name {
numNets++
params.ActiveNetParams = ¶ms.MixNetParam
}
if numNets == 0 {
numNets++
params.ActiveNetParams = ¶ms.MainNetParam
}
// Multiple networks can't be selected simultaneously.
if numNets > 1 {
str := "%s: the testnet and simnet params can't be " +
"used together -- choose one of the three"
return fmt.Errorf("%s", str)
}
if err := params.ActiveNetParams.PowConfig.Check(); err != nil {
return err
}
// Set logging file if presented
if !c.NoFileLogging {
logDir := filepath.Join(c.DataDir, defaultLogDirname, params.ActiveNetParams.Name)
common.InitLogRotator(filepath.Join(logDir, defaultLogFilename))
}
err = common.ParseAndSetDebugLevels(c.DebugLevel)
if err != nil {
return err
}
if c.RPCCert == defaultRPCCertPath {
c.RPCCert = filepath.Join(c.DataDir, defaultRPCCertFile)
}
if c.RPCKey == defaultRPCKeyPath {
c.RPCKey = filepath.Join(c.DataDir, defaultRPCKeyFile)
}
if len(c.RPCListeners.Value()) <= 0 {
c.RPCListeners = *cli.NewStringSlice(defaultRPCListener)
}
return nil
}