-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
254 lines (224 loc) · 6.45 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
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"reflect"
"unicode"
"github.com/MatrixAINetwork/go-matrix/log"
"github.com/MatrixAINetwork/go-matrix/params/manparams"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"io/ioutil"
"github.com/MatrixAINetwork/go-matrix/base58"
"github.com/MatrixAINetwork/go-matrix/common"
"github.com/MatrixAINetwork/go-matrix/crypto/aes"
"github.com/MatrixAINetwork/go-matrix/dashboard"
"github.com/MatrixAINetwork/go-matrix/man"
"github.com/MatrixAINetwork/go-matrix/mc"
"github.com/MatrixAINetwork/go-matrix/params/enstrust"
"github.com/MatrixAINetwork/go-matrix/pod"
"github.com/MatrixAINetwork/go-matrix/run/utils"
"github.com/naoina/toml"
"gopkg.in/urfave/cli.v1"
)
var (
dumpConfigCommand = cli.Command{
Action: utils.MigrateFlags(dumpConfig),
Name: "dumpconfig",
Usage: "Show configuration values",
ArgsUsage: "",
Flags: append(nodeFlags, rpcFlags...),
Category: "MISCELLANEOUS COMMANDS",
Description: `The dumpconfig command shows configuration values.`,
}
configFileFlag = cli.StringFlag{
Name: "config",
Usage: "TOML configuration file",
}
)
// These settings ensure that TOML keys use the same names as Go struct fields.
var tomlSettings = toml.Config{
NormFieldName: func(rt reflect.Type, key string) string {
return key
},
FieldToKey: func(rt reflect.Type, field string) string {
return field
},
MissingField: func(rt reflect.Type, field string) error {
link := ""
if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
}
return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
},
}
type manstatsConfig struct {
URL string `toml:",omitempty"`
}
type gmanConfig struct {
Man man.Config
Node pod.Config
Manstats manstatsConfig
Dashboard dashboard.Config
}
func loadConfig(file string, cfg *gmanConfig) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
// Add file name to errors that have a line number.
if _, ok := err.(*toml.LineError); ok {
err = errors.New(file + ", " + err.Error())
}
return err
}
func defaultNodeConfig() pod.Config {
cfg := pod.DefaultConfig
cfg.Name = clientIdentifier
cfg.Version = manparams.VersionDelta + "_" + gitCommit
cfg.HTTPModules = append(cfg.HTTPModules, "man", "eth", "shh")
cfg.WSModules = append(cfg.WSModules, "man", "eth", "shh")
cfg.IPCPath = "gman.ipc"
return cfg
}
func makeConfigNode(ctx *cli.Context) (*pod.Node, gmanConfig) {
// Load defaults.
cfg := gmanConfig{
Man: man.DefaultConfig,
Node: defaultNodeConfig(),
Dashboard: dashboard.DefaultConfig,
}
// Load config file.
if file := ctx.GlobalString(configFileFlag.Name); file != "" {
if err := loadConfig(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
}
// Apply flags.
utils.SetNodeConfig(ctx, &cfg.Node)
stack, err := pod.New(&cfg.Node)
if err != nil {
utils.Fatalf("Failed to create the protocol stack: %v", err)
}
utils.SetManConfig(ctx, stack, &cfg.Man)
if ctx.GlobalIsSet(utils.ManStatsURLFlag.Name) {
cfg.Manstats.URL = ctx.GlobalString(utils.ManStatsURLFlag.Name)
}
utils.SetDashboardConfig(ctx, &cfg.Dashboard)
return stack, cfg
}
func makeFullNode(ctx *cli.Context) *pod.Node {
Init_Config_PATH(ctx)
stack, cfg := makeConfigNode(ctx)
err := CheckEntrust(ctx)
if err != nil {
log.ERROR("Init", "Entrust File Err", err)
os.Exit(1)
}
utils.RegisterManService(stack, &cfg.Man)
if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
}
// Add the Matrix Stats daemon if requested.
if cfg.Manstats.URL != "" {
utils.RegisterManStatsService(stack, cfg.Manstats.URL)
}
return stack
}
// dumpConfig is the dumpconfig command.
func dumpConfig(ctx *cli.Context) error {
_, cfg := makeConfigNode(ctx)
comment := ""
if cfg.Man.Genesis != nil {
cfg.Man.Genesis = nil
comment += "# Note: this config doesn't contain the genesis block.\n\n"
}
out, err := tomlSettings.Marshal(&cfg)
if err != nil {
return err
}
io.WriteString(os.Stdout, comment)
os.Stdout.Write(out)
return nil
}
func CheckEntrust(ctx *cli.Context) error {
path := ctx.GlobalString(utils.AccountPasswordFileFlag.Name)
if path == "" {
return nil
}
fmt.Println("Please enter the password. Your password's length must be between 8 and 16 characters, and should contain numbers, uppercase letters (A-Z), lowercase letters (a-z) and special characters")
password, err := ReadDecryptPassword(utils.Once, ctx)
f, err := os.Open(path)
if err != nil {
fmt.Println("Failed to open the file", err, "path", path)
return err
}
b, err := ioutil.ReadAll(f)
bytesPass, err := base64.StdEncoding.DecodeString(string(b))
if err != nil {
fmt.Println("Error in file contents", err)
return err
}
tpass, err := aes.AesDecrypt(bytesPass, []byte(password))
if err != nil {
fmt.Println(err)
return err
}
var anss []mc.EntrustInfo
err = json.Unmarshal(tpass, &anss)
if err != nil {
fmt.Println("Decrypt Failed. Password is wrong", err)
return err
}
entrustValue := make(map[common.Address]string, 0)
for _, v := range anss {
addr, err := base58.Base58DecodeToAddress(v.Address)
if err != nil {
return err
}
entrustValue[addr] = v.Password
}
err = entrust.EntrustAccountValue.SetEntrustValue(entrustValue)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func ReadDecryptPassword(inputTimes int, ctx *cli.Context) ([]byte, error) {
if password := ctx.GlobalString(utils.TestEntrustFlag.Name); password != "" {
h := sha256.New()
h.Write([]byte(password))
return h.Sum(nil), nil
}
var passphrase string
var err error
InputCount := 0
for true {
InputCount++
if InputCount > 3 {
return []byte{}, errors.New("You entered wrong passwords for many times")
}
fmt.Printf("This is the %d time you enter the password \n", InputCount)
passphrase, err = utils.GetPassword(inputTimes)
if err != nil {
fmt.Println("Unable to detect your password. Please enter it again", err)
continue
}
if utils.CheckPassword(passphrase) {
break
}
}
h := sha256.New()
h.Write([]byte(passphrase))
return h.Sum(nil), nil
}