-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
203 lines (178 loc) · 4.07 KB
/
main.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
//go:build !js
package main
import (
"errors"
"fmt"
"net/http"
"os"
"runtime"
"strings"
"github.com/seth-shi/ethereum-wallet-generator-worker/internal/master"
"github.com/seth-shi/ethereum-wallet-generator-worker/internal/utils"
"github.com/seth-shi/ethereum-wallet-generator-worker/internal/worker"
"github.com/urfave/cli/v2"
)
var (
Master *master.Master
Worker *worker.Worker
)
const (
mnemonicCount = 12
)
var (
masterCommand = &cli.Command{
Name: "master",
Usage: "启动 HTTP 服务器, 收集信息",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Value: 8000,
Usage: "HTTP 服务端口",
},
&cli.StringFlag{
Name: "prefix",
Value: "",
Usage: "钱包地址前缀",
},
&cli.StringFlag{
Name: "suffix",
Value: "",
Usage: "钱包地址后缀",
},
},
Before: func(cCtx *cli.Context) (err error) {
var (
prefix = cCtx.String("prefix")
suffix = cCtx.String("suffix")
port = cCtx.Int("port")
)
if cCtx.String("prefix") == "" && cCtx.String("suffix") == "" {
return errors.New("钱包前缀和后缀不能同时为空")
}
if Master, err = master.NewMaster(port, prefix, suffix); err != nil {
return
}
return nil
},
Action: func(cCtx *cli.Context) error {
return Master.Run()
},
}
workerCommand = &cli.Command{
Name: "worker",
Usage: "生成节点",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "server",
Value: "http://localhost:8000",
Usage: "服务器地址",
},
&cli.UintFlag{
Name: "c",
Value: 0,
Usage: "并发线程, 默认 CPU 个数",
},
&cli.StringFlag{
Name: "name",
Value: "",
Usage: "自定义节点名字",
},
},
Before: func(cCtx *cli.Context) error {
var (
c = cCtx.Uint("c")
serverHost = cCtx.String("server")
workerName = cCtx.String("name")
)
if c == 0 {
c = uint(runtime.NumCPU())
}
if workerName == "" {
workerName = utils.GenWorkerName()
}
fmt.Printf("worker:[%s]启动中...\n", workerName)
// 从服务端获取配置
mc, resp, err := utils.GetMatchConfig(serverHost)
if err != nil {
return err
}
if resp.StatusCode() != http.StatusOK {
return errors.New(fmt.Sprintf("获取配置失败[%d]%s", resp.StatusCode(), resp.String()))
}
Worker, err = worker.NewWorker(serverHost, mc, c, workerName)
return err
},
Action: func(cCtx *cli.Context) error {
return Worker.Run()
},
}
decryptCommand = &cli.Command{
Name: "decrypt",
Usage: "解密钱包数据",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "data",
Value: "",
Usage: "加密的数据",
},
&cli.StringFlag{
Name: "key",
Value: "",
Usage: "服务器运行的秘钥",
},
&cli.UintFlag{
Name: "offset",
Value: 0,
Usage: "偏移量",
},
&cli.UintFlag{
Name: "limit",
Value: 12,
Usage: "输出词的数量",
},
},
Action: func(cCtx *cli.Context) error {
var (
key = cCtx.String("key")
data = cCtx.String("data")
offset = cCtx.Uint("offset")
limit = cCtx.Uint("limit")
)
if key == "" {
return errors.New("秘钥不能为空")
}
if data == "" {
return errors.New("加密数据不能为空")
}
count := offset + limit
if count > mnemonicCount {
return errors.New("助记词只能返回12个")
}
decryptBytes, err := utils.AesGcmDecrypt(data, []byte(key))
if err != nil {
return errors.New(fmt.Sprintf("解密失败:%s", err.Error()))
}
decryptData := strings.Split(string(decryptBytes), " ")
if len(decryptData) != mnemonicCount {
return errors.New(fmt.Sprintf("助记词个数不正确:[%s]", decryptBytes))
}
end := limit + offset
fmt.Printf("助记词 %d-%d 开始\n", offset, end)
for i := offset; i < end; i++ {
fmt.Printf("%s ", decryptData[i])
}
fmt.Printf("\n助记词 %d-%d 结束\n", offset, end)
return nil
},
}
)
func main() {
app := &cli.App{
Commands: []*cli.Command{
masterCommand,
workerCommand,
decryptCommand,
},
}
utils.MustError(app.Run(os.Args))
}