-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
444 lines (422 loc) · 11.8 KB
/
Copy pathmain.go
File metadata and controls
444 lines (422 loc) · 11.8 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package main
import (
"context"
"fmt"
"ipsync/internal/preload"
"ipsync/internal/receive"
"ipsync/internal/send"
"ipsync/internal/wireguard"
"log"
"os"
"time"
"github.com/unix755/xtools/xNet"
"github.com/urfave/cli/v3"
)
func main() {
// 文件加密 key
var Key string
// 是否允许不安全连接(tls 自签证书)
var skipTLSVerify bool
// 启用循环, 每一次运行之前的时间间隔
var interval time.Duration
// file 模式下本地存储文件地址, webdav 模式下服务端存储文件地址
var path string
// s3 模式下服务器端点, webdav 模式下服务器端点
var endpoint string
// s3 模式下 access_key_id, webdav 模式下账号
var username string
// s3 模式下 secret_access_key, webdav 模式下密码
var password string
// s3 模式下独有变量
var region string
var stsToken string
var pathStyle bool
var bucket string
var objectPath string
// wireguard watchdog
var wgRemoteInterface string
var wgInterface string
var wgPeerKey string
// flags
var (
CommonFlags = []cli.Flag{
&cli.StringFlag{
Name: "key",
Aliases: []string{"k"},
Usage: "key used for encryption",
Value: "",
Sources: cli.EnvVars("KEY"),
Destination: &Key,
},
&cli.BoolFlag{
Name: "skipTLSVerify",
Aliases: []string{"s"},
Usage: "skip TLS verification in server connection",
Value: false,
Sources: cli.EnvVars("SKIP_TLS_VERIFY"),
Destination: &skipTLSVerify,
},
&cli.DurationFlag{
Name: "interval",
Aliases: []string{"i"},
Usage: "interval between repetitive tasks",
Value: 0 * time.Second,
Sources: cli.EnvVars("INTERVAL"),
Destination: &interval,
},
}
FileFlags = []cli.Flag{
&cli.StringFlag{
Name: "file_path",
Usage: "file path used in file protocol",
Required: true,
Value: "",
Sources: cli.EnvVars("FILE_PATH"),
Destination: &path,
},
}
S3Flags = []cli.Flag{
&cli.StringFlag{
Name: "s3_endpoint",
Usage: "endpoint used in s3 protocol",
Required: true,
Value: "",
Sources: cli.EnvVars("S3_ENDPOINT"),
Destination: &endpoint,
},
&cli.StringFlag{
Name: "s3_region",
Usage: "region used in s3 protocol",
Value: "us-east-1",
Sources: cli.EnvVars("S3_REGION"),
Destination: ®ion,
},
&cli.StringFlag{
Name: "s3_access_key_id",
Usage: "access key id used in s3 protocol",
Required: true,
Value: "",
Sources: cli.EnvVars("S3_ACCESS_KEY_ID"),
Destination: &username,
},
&cli.StringFlag{
Name: "s3_secret_access_key",
Usage: "secret access key used in s3 protocol",
Required: true,
Value: "",
Sources: cli.EnvVars("S3_SECRET_ACCESS_KEY"),
Destination: &password,
},
&cli.StringFlag{
Name: "s3_sts_token",
Usage: "sts token used in s3 protocol",
Value: "",
Sources: cli.EnvVars("S3_STS_TOKEN"),
Destination: &stsToken,
},
&cli.BoolFlag{
Name: "s3_path_style",
Usage: "path style used in s3 protocol",
Value: false,
Sources: cli.EnvVars("S3_PATH_STYLE"),
Destination: &pathStyle,
},
&cli.StringFlag{
Name: "s3_bucket",
Usage: "bucket used in s3 protocol",
Required: true,
Value: "",
Sources: cli.EnvVars("S3_BUCKET"),
Destination: &bucket,
},
&cli.StringFlag{
Name: "s3_object_path",
Usage: "object path used in s3 protocol",
Required: true,
Value: "",
Sources: cli.EnvVars("S3_OBJECT_PATH"),
Destination: &objectPath,
},
}
WebDAVFlags = []cli.Flag{
&cli.StringFlag{
Name: "webdav_endpoint",
Usage: "endpoint used in webdav protocol",
Required: true,
Value: "",
Sources: cli.EnvVars("WEBDAV_ENDPOINT"),
Destination: &endpoint,
},
&cli.StringFlag{
Name: "webdav_username",
Usage: "username used in webdav protocol",
Value: "",
Sources: cli.EnvVars("WEBDAV_USERNAME"),
Destination: &username,
},
&cli.StringFlag{
Name: "webdav_password",
Usage: "password used in webdav protocol",
Value: "",
Sources: cli.EnvVars("WEBDAV_PASSWORD"),
Destination: &password,
},
&cli.StringFlag{
Name: "webdav_path",
Usage: "path used in webdav protocol",
Required: true,
Value: "",
Sources: cli.EnvVars("WEBDAV_PATH"),
Destination: &path,
},
}
)
cmds := []*cli.Command{
{
Name: "send",
Aliases: []string{"s"},
Usage: "send network information to remote server or file",
Flags: CommonFlags,
Commands: []*cli.Command{
{
Name: "file",
Usage: "send to file",
Flags: FileFlags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
if interval != 0 {
send.ToFileLoop(path, []byte(Key), interval)
} else {
return send.ToFile(path, []byte(Key))
}
return nil
},
},
{
Name: "s3",
Usage: "send to s3 server",
Flags: S3Flags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
if interval != 0 {
send.ToS3Loop(endpoint, region, username, password, stsToken, pathStyle, skipTLSVerify, bucket, objectPath, []byte(Key), interval)
} else {
_, err = send.ToS3(endpoint, region, username, password, stsToken, pathStyle, skipTLSVerify, bucket, objectPath, []byte(Key))
if err != nil {
return err
}
}
return nil
},
},
{
Name: "webdav",
Usage: "send to webdav server",
Flags: WebDAVFlags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
if interval != 0 {
send.ToWebDAVLoop(endpoint, username, password, skipTLSVerify, path, []byte(Key), interval)
} else {
_, err = send.ToWebDAV(endpoint, username, password, skipTLSVerify, path, []byte(Key))
if err != nil {
return err
}
}
return nil
},
},
},
},
{
Name: "print",
Aliases: []string{"p"},
Usage: "print network information from remote server or file",
Flags: []cli.Flag{CommonFlags[0], CommonFlags[1]},
Commands: []*cli.Command{
{
Name: "file",
Usage: "print network information from filesystem",
Flags: FileFlags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
// 获取 preload
p, err := receive.FromFile(path, []byte(Key))
if err != nil {
return err
}
// 打印 preload
bytes, err := preload.Marshal(p, "json", []byte{})
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
},
},
{
Name: "s3",
Usage: "print network information from s3 server",
Flags: S3Flags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
// 获取 preload
p, err := receive.FromS3(endpoint, region, username, password, stsToken, pathStyle, skipTLSVerify, bucket, objectPath, []byte(Key))
if err != nil {
return err
}
// 打印 preload
bytes, err := preload.Marshal(p, "json", []byte{})
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
},
},
{
Name: "webdav",
Usage: "print network information from WebDAV server",
Flags: WebDAVFlags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
// 获取 preload
p, err := receive.FromWebDAV(endpoint, username, password, skipTLSVerify, path, []byte(Key))
if err != nil {
return err
}
// 打印 preload
bytes, err := preload.Marshal(p, "json", []byte{})
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
},
},
{
Name: "local",
Usage: "print network information from localhost",
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
// 获取 preload
p, err := preload.NewPreload()
if err != nil {
return err
}
// 打印 preload
bytes, err := preload.Marshal(p, "json", []byte{})
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
},
},
},
},
{
Name: "wireguard",
Aliases: []string{"w"},
Usage: "WireGuard endpoint IP watchdog",
Flags: append(CommonFlags,
&cli.StringFlag{
Name: "wg_remote_interface",
Usage: "remote interface used for WireGuard watchdog",
Required: true,
Value: "",
Sources: cli.EnvVars("WG_REMOTE_INTERFACE"),
Destination: &wgRemoteInterface,
},
&cli.StringFlag{
Name: "wg_interface",
Usage: "interface used for WireGuard watchdog",
Required: true,
Value: "",
Sources: cli.EnvVars("WG_INTERFACE"),
Destination: &wgInterface,
},
&cli.StringFlag{
Name: "wg_peer_key",
Usage: "peer key used for WireGuard watchdog",
Value: "",
Sources: cli.EnvVars("WG_PEER_KEY"),
Destination: &wgPeerKey,
}),
Commands: []*cli.Command{
{
Name: "file",
Usage: "receive endpoint IP from file",
Flags: FileFlags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
// 获取 preload
p, err := receive.FromFile(path, []byte(Key))
if err != nil {
return err
}
for _, ip := range append(p.GetNetInterface(wgRemoteInterface).Ipv6, p.GetNetInterface(wgRemoteInterface).Ipv4...) {
isPublic, err := xNet.IsPublic(ip)
if err != nil {
return err
}
if isPublic {
return wireguard.UpdateEndPointLoop(wgInterface, wgPeerKey, ip, -1, interval)
}
}
return fmt.Errorf("no public ip found")
},
},
{
Name: "s3",
Usage: "receive endpoint IP from s3 server",
Flags: S3Flags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
// 获取 preload
p, err := receive.FromS3(endpoint, region, username, password, stsToken, pathStyle, skipTLSVerify, bucket, objectPath, []byte(Key))
if err != nil {
return err
}
for _, ip := range append(p.GetNetInterface(wgRemoteInterface).Ipv6, p.GetNetInterface(wgRemoteInterface).Ipv4...) {
isPublic, err := xNet.IsPublic(ip)
if err != nil {
return err
}
if isPublic {
return wireguard.UpdateEndPointLoop(wgInterface, wgPeerKey, ip, -1, interval)
}
}
return fmt.Errorf("no public ip found")
},
},
{
Name: "webdav",
Usage: "receive endpoint IP from webdav server",
Flags: WebDAVFlags,
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
// 获取 preload
p, err := receive.FromWebDAV(endpoint, username, password, skipTLSVerify, path, []byte(Key))
if err != nil {
return err
}
for _, ip := range append(p.GetNetInterface(wgRemoteInterface).Ipv6, p.GetNetInterface(wgRemoteInterface).Ipv4...) {
isPublic, err := xNet.IsPublic(ip)
if err != nil {
return err
}
if isPublic {
return wireguard.UpdateEndPointLoop(wgInterface, wgPeerKey, ip, -1, interval)
}
}
return fmt.Errorf("no public ip found")
},
},
},
},
}
// 打印版本函数
cli.VersionPrinter = func(cmd *cli.Command) {
fmt.Printf("%s\n", cmd.Root().Version)
}
cmd := &cli.Command{
Usage: "IP Sync Tool from https://github.com/unix755/ipsync",
Version: "v3.33",
Commands: cmds,
}
err := cmd.Run(context.Background(), os.Args)
if err != nil {
log.Fatal(err)
}
}