-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
310 lines (283 loc) · 6.33 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
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
package main
import (
"context"
"fmt"
"github.com/Evanesco-Labs/WhiteNoise/cmd/chat"
"github.com/Evanesco-Labs/WhiteNoise/common/account"
"github.com/Evanesco-Labs/WhiteNoise/common/config"
"github.com/Evanesco-Labs/WhiteNoise/common/log"
"github.com/Evanesco-Labs/WhiteNoise/crypto"
"github.com/Evanesco-Labs/WhiteNoise/network"
"github.com/Evanesco-Labs/WhiteNoise/sdk"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
)
var node *network.Node
var wnSDK *sdk.WhiteNoiseClient
var (
PortFlag = cli.IntFlag{
Name: "port, p",
Value: 3331,
}
BootStrapFlag = cli.StringFlag{
Name: "bootstrap, b",
Usage: "PeerId of the node to bootstrap from.",
Value: "",
}
NodeFlag = cli.StringFlag{
Name: "node, n",
Usage: "PeerId of the node to connect to.",
Value: "",
}
ModeFlag = cli.BoolFlag{
Name: "client, c",
Usage: "Build a client node if flag is on and MainNet Node by default.",
Required: false,
Hidden: false,
}
BootFlag = cli.BoolFlag{
Name: "boot",
Usage: "Build a boot node if flag is on and MainNet Node by default.",
Required: false,
Hidden: false,
}
LogLevelFlag = cli.IntFlag{
Name: "log, l",
Value: 2,
}
NickFlag = cli.StringFlag{
Name: "nick",
Usage: "Set nick name for chat example client",
Value: "Alice",
}
WhiteListFlag = cli.BoolFlag{
Name: "whitelist",
Usage: "Only serves clients in the whitelist.yml",
Required: false,
Hidden: false,
}
AccountFromFileFlag = cli.StringFlag{
Name: "account, acc",
Usage: "Load WhiteNoise account from key file at this path",
Value: "",
}
KeyFlag = cli.StringFlag{
Name: "keytype, ktype",
Usage: "Set key type",
Value: "ed25519",
}
)
func main() {
if err := initApp().Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func initApp() *cli.App {
app := cli.NewApp()
app.Usage = " whitenoise protocol implement"
app.Commands = []cli.Command{
{
Name: "start",
Usage: "Start Service",
Action: Start,
Flags: []cli.Flag{
PortFlag,
BootStrapFlag,
ModeFlag,
LogLevelFlag,
BootFlag,
WhiteListFlag,
AccountFromFileFlag,
KeyFlag,
},
},
{
Name: "chat",
Usage: "Start chat",
Action: StartChat,
Flags: []cli.Flag{
BootStrapFlag,
NodeFlag,
LogLevelFlag,
NickFlag,
AccountFromFileFlag,
KeyFlag,
},
},
}
return app
}
func Start(ctx *cli.Context) {
port := ctx.Int("port")
bootstrap := ctx.String("bootstrap")
clientMode := ctx.Bool("client")
bootMode := ctx.Bool("boot")
whitelist := ctx.Bool("whitelist")
pemPath := ctx.String("account")
keyTypeStr := ctx.String("keytype")
//decode keytype
keyType := 0
switch keyTypeStr {
case "ed25519":
keyType = crypto.Ed25519
case "secp256k1":
keyType = crypto.Secpk1
case "ecdsa":
keyType = crypto.ECDSA
default:
keyType = crypto.Ed25519
}
logLevel := ctx.Int("log")
log.InitLog(logLevel, os.Stdout, log.PATH)
con := context.Background()
cfg := config.NetworkConfig{
RendezvousString: "whitenoise",
ListenHost: "127.0.0.1",
ListenPort: port,
BootStrapPeers: bootstrap,
Mode: config.ServerMode,
WhiteList: whitelist,
}
if clientMode {
cfg.Mode = config.ClientMode
}
if bootMode {
cfg.Mode = config.BootMode
}
// read whitelist from config
if whitelist {
InitWhiteList()
}
acc := account.GetAccountFromFile(pemPath)
if acc == nil || acc.KeyType != keyType {
switch keyType {
case crypto.ECDSA:
acc = account.GetAccount(crypto.ECDSA)
case crypto.Ed25519:
acc = account.GetAccount(crypto.Ed25519)
case crypto.Secpk1:
acc = account.GetAccount(crypto.Secpk1)
default:
panic("key type not support")
}
}
var err error
node, err = network.NewNode(con, &cfg, acc)
if err != nil {
panic(err)
}
node.Start(&cfg)
waitToExit()
}
func StartChat(ctx *cli.Context) {
logLevel := ctx.Int("log")
log.InitLog(logLevel, os.Stdout, log.PATH)
bootstrap := ctx.String("bootstrap")
n := ctx.String("node")
con := context.Background()
nick := ctx.String("nick")
pemPath := ctx.String("account")
keyTypeStr := ctx.String("keytype")
//decode keytype
keyType := 0
switch keyTypeStr {
case "ed25519":
keyType = crypto.Ed25519
case "secp256k1":
keyType = crypto.Secpk1
case "ecdsa":
keyType = crypto.ECDSA
default:
keyType = crypto.Ed25519
}
sdk.BootStrapPeers = bootstrap
acc := account.GetAccountFromFile(pemPath)
if acc == nil || acc.KeyType != keyType {
switch keyType {
case crypto.ECDSA:
acc = account.GetAccount(crypto.ECDSA)
case crypto.Ed25519:
acc = account.GetAccount(crypto.Ed25519)
case crypto.Secpk1:
acc = account.GetAccount(crypto.Secpk1)
default:
panic("key type not support")
}
}
var err error
wnSDK, err = sdk.NewClient(con, acc)
if err != nil {
panic(err)
}
peers, err := wnSDK.GetMainNetPeers(10)
if err != nil {
panic(err)
}
if len(peers) == 0 {
panic("No peers exist")
}
index := rand.New(rand.NewSource(time.Now().UnixNano())).Int() % len(peers)
entry := peers[index]
log.Info("entry:", entry.String(), ",index:", index)
err = wnSDK.Register(entry)
if err != nil {
panic(err)
}
time.Sleep(time.Millisecond * 100)
if n != "" {
_, sessionID, err := wnSDK.Dial(n)
if err != nil {
panic(err)
}
log.Debug("NewCircuit done")
chat.Chat(nick, wnSDK.GetWhiteNoiseID(), sessionID, wnSDK)
waitToExit()
} else {
chat.Chat(nick, wnSDK.GetWhiteNoiseID(), "", wnSDK)
waitToExit()
}
}
func InitWhiteList() {
config.WhiteListPeers = make(map[peer.ID]bool)
var ymlConfig = config.YmlConfig{Whitelist: make([]string, 0)}
whitelistConfig, err := ioutil.ReadFile("./whitelist.yml")
if err != nil {
panic(err)
}
err = yaml.Unmarshal(whitelistConfig, &ymlConfig)
if err != nil {
panic(err)
}
for _, c := range ymlConfig.Whitelist {
whiteNoiseID, err := crypto.WhiteNoiseIDfromString(c)
if err != nil {
panic(err)
}
peerId, err := whiteNoiseID.GetPeerID()
if err != nil {
panic(err)
}
config.WhiteListPeers[peerId] = true
}
}
func waitToExit() {
exit := make(chan bool, 0)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
for sig := range sc {
fmt.Printf("received exit signal:%v", sig.String())
close(exit)
break
}
}()
<-exit
}