-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
317 lines (307 loc) · 8.46 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
311
312
313
314
315
316
317
package main
import (
"fmt"
"github.com/urfave/cli/v2"
"log"
"os"
"time"
"wgwd/internal/decrypt"
"wgwd/internal/receive/file"
"wgwd/internal/receive/nconnect"
"wgwd/internal/receive/s3"
"wgwd/internal/receive/webdav"
)
func main() {
// get mode
var id string
var allow_insecure bool
var encryption_key string
var interval time.Duration
var endpoint string
var username string
var password string
// get mode file
var filepath string
// get mode s3
var regin string
var sts_token string
var path_style bool
var bucket string
var object_path string
// wireguard
var remote_interface string
var wg_interface string
var wg_peer_key string
cmds := []*cli.Command{
{
Name: "get",
Aliases: []string{"g"},
Usage: "get wireguard endpoint from network information",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "remote_interface",
Aliases: []string{"r"},
Usage: "set remote interface",
Required: true,
Destination: &remote_interface,
},
&cli.StringFlag{
Name: "wg_interface",
Aliases: []string{"wi"},
Usage: "set wireguard interface",
Required: true,
Destination: &wg_interface,
},
&cli.StringFlag{
Name: "wg_peer_key",
Aliases: []string{"wk"},
Usage: "set wireguard peer key",
Destination: &wg_peer_key,
},
&cli.DurationFlag{
Name: "interval",
Aliases: []string{"i"},
Usage: "set send interval",
Destination: &interval,
},
},
Subcommands: []*cli.Command{
{
Name: "file",
Usage: "get network information from filesystem",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "filepath",
Aliases: []string{"f"},
Usage: "set file path",
Required: true,
Destination: &filepath,
},
&cli.StringFlag{
Name: "encryption_key",
Aliases: []string{"e"},
Usage: "set file encryption key",
Destination: &encryption_key,
},
},
Action: func(ctx *cli.Context) error {
if interval != 0 {
file.ReceiveRequestLoop(filepath, []byte(encryption_key), remote_interface, wg_interface, wg_peer_key, interval)
} else {
err := file.ReceiveRequest(filepath, []byte(encryption_key), remote_interface, wg_interface, wg_peer_key)
if err != nil {
return err
}
}
return nil
},
},
{
Name: "s3",
Usage: "get network information from s3 server",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "allow_insecure",
Usage: "set allow insecure connect",
Value: false,
Destination: &allow_insecure,
},
&cli.StringFlag{
Name: "encryption_key",
Aliases: []string{"e"},
Usage: "set file encryption key",
Destination: &encryption_key,
},
&cli.StringFlag{
Name: "endpoint",
Usage: "set s3 server endpoint",
Required: true,
Destination: &endpoint,
},
&cli.StringFlag{
Name: "regin",
Usage: "set s3 server regin",
Value: "us-east-1",
Destination: ®in,
},
&cli.StringFlag{
Name: "access_key_id",
Usage: "set s3 server access key id",
Required: true,
Destination: &username,
},
&cli.StringFlag{
Name: "secret_access_key",
Usage: "set s3 server secret access key",
Required: true,
Destination: &password,
},
&cli.StringFlag{
Name: "sts_token",
Usage: "set s3 server sts token",
Destination: &sts_token,
},
&cli.BoolFlag{
Name: "path_style",
Usage: "set s3 server path style, false: virtual host, true: path",
Value: false,
Destination: &path_style,
},
&cli.StringFlag{
Name: "bucket",
Usage: "set s3 server bucket",
Required: true,
Destination: &bucket,
},
&cli.StringFlag{
Name: "object_path",
Usage: "set s3 server object path",
Required: true,
Destination: &object_path,
},
},
Action: func(ctx *cli.Context) error {
if interval != 0 {
s3.ReceiveRequestLoop(endpoint, regin, username, password, sts_token, path_style, allow_insecure, bucket, object_path, []byte(encryption_key), remote_interface, wg_interface, wg_peer_key, interval)
} else {
err := s3.ReceiveRequest(endpoint, regin, username, password, sts_token, path_style, allow_insecure, bucket, object_path, []byte(encryption_key), remote_interface, wg_interface, wg_peer_key)
if err != nil {
return err
}
}
return nil
},
},
{
Name: "webdav",
Usage: "get network information from webdav server",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "allow_insecure",
Usage: "set allow insecure connect",
Value: false,
Destination: &allow_insecure,
},
&cli.StringFlag{
Name: "encryption_key",
Aliases: []string{"e"},
Usage: "set file encryption key",
Destination: &encryption_key,
},
&cli.StringFlag{
Name: "endpoint",
Usage: "set webdav server endpoint",
Required: true,
Destination: &endpoint,
},
&cli.StringFlag{
Name: "username",
Usage: "set webdav server username",
Destination: &username,
},
&cli.StringFlag{
Name: "password",
Usage: "set webdav server password",
Destination: &password,
},
&cli.StringFlag{
Name: "filepath",
Aliases: []string{"f"},
Usage: "set webdav server filepath",
Required: true,
Destination: &filepath,
},
},
Action: func(ctx *cli.Context) error {
if interval != 0 {
webdav.ReceiveRequestLoop(endpoint, username, password, allow_insecure, filepath, []byte(encryption_key), remote_interface, wg_interface, wg_peer_key, interval)
} else {
err := webdav.ReceiveRequest(endpoint, username, password, allow_insecure, filepath, []byte(encryption_key), remote_interface, wg_interface, wg_peer_key)
if err != nil {
return err
}
}
return nil
},
},
{
Name: "nconnect",
Usage: "get network information from nconnect server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "set id",
Required: true,
Destination: &id,
},
&cli.BoolFlag{
Name: "allow_insecure",
Usage: "set allow insecure connect",
Value: false,
Destination: &allow_insecure,
},
&cli.StringFlag{
Name: "endpoint",
Usage: "set nconnect server endpoint",
Required: true,
Destination: &endpoint,
},
},
Action: func(ctx *cli.Context) error {
if interval != 0 {
nconnect.ReceiveRequestLoop(id, endpoint, username, password, allow_insecure, remote_interface, wg_interface, wg_peer_key, interval)
} else {
err := nconnect.ReceiveRequest(id, endpoint, username, password, allow_insecure, remote_interface, wg_interface, wg_peer_key)
if err != nil {
return err
}
}
return nil
},
},
},
},
{
Name: "decrypt",
Aliases: []string{"d"},
Usage: "decrypt a file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "filepath",
Aliases: []string{"f"},
Usage: "set file path",
Required: true,
Destination: &filepath,
},
&cli.StringFlag{
Name: "encryption_key",
Aliases: []string{"e"},
Usage: "set file encryption key",
Required: true,
Destination: &encryption_key,
},
},
Action: func(ctx *cli.Context) error {
plaintext, err := decrypt.FromFile(filepath, []byte(encryption_key))
if err != nil {
return err
}
fmt.Println(string(plaintext))
return nil
},
},
}
// 打印版本函数
cli.VersionPrinter = func(cCtx *cli.Context) {
fmt.Printf("%s", cCtx.App.Version)
}
app := &cli.App{
Usage: "WireGuard Watchdog",
Version: "v1.21",
Commands: cmds,
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}