-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
105 lines (95 loc) · 2.76 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
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2021 Kusakabe Si. All Rights Reserved.
*/
package main
import (
"flag"
"fmt"
"os"
"runtime"
"syscall"
"time"
nonSecureRand "math/rand"
"net/http"
_ "net/http/pprof"
"github.com/KusakabeSi/EtherGuard-VPN/gencfg"
"github.com/KusakabeSi/EtherGuard-VPN/ipc"
"github.com/KusakabeSi/EtherGuard-VPN/path"
"github.com/KusakabeSi/EtherGuard-VPN/tap"
)
const (
ExitSetupSuccess = 0
ExitSetupFailed = 1
)
const (
ENV_EG_UAPI_FD = "EG_UAPI_FD"
ENV_EG_UAPI_DIR = "EG_UAPI_DIR"
)
var (
tconfig = flag.String("config", "", "Config path for the interface.")
mode = flag.String("mode", "", "Running mode. [super|edge|solve|gencfg]")
printExample = flag.Bool("example", false, "Print example config")
cfgmode = flag.String("cfgmode", "", "Running mode for generated config. [none|super|p2p]")
bind = flag.String("bind", "linux", "UDP socket bind mode. [linux|std]\nYou may need std mode if you want to run Etherguard under WSL.")
nouapi = flag.Bool("no-uapi", false, "Disable UAPI\nWith UAPI, you can check etherguard status by \"wg\" command")
pprofaddr = flag.String("pprof", "", "pprof listing address")
version = flag.Bool("version", false, "Show version")
help = flag.Bool("help", false, "Show this help")
)
func main() {
flag.Parse()
if *version {
fmt.Printf("etherguard-go %s\n%s-%s\n%s\n\nA full mesh layer 2 VPN powered by Floyd Warshall algorithm.\nInformation available at https://github.com/KusakabeSi/EtherGuard-VPN.\nCopyright (C) Kusakabe Si <si@kskb.eu.org>.\n", Version, runtime.GOOS, runtime.GOARCH, tap.VPP_SUPPORT)
return
}
if *help {
flag.Usage()
return
}
uapiDir := os.Getenv(ENV_EG_UAPI_DIR)
if uapiDir != "" {
ipc.SetsocketDirectory(uapiDir)
}
nonSecureRand.Seed(time.Now().UnixNano())
if *pprofaddr != "" {
go func() {
//内网可访问的pprof地址
err := http.ListenAndServe(*pprofaddr, nil)
if err != nil {
panic(fmt.Errorf("pprof error: %v", err))
}
}()
}
var err error
switch *mode {
case "edge":
err = Edge(*tconfig, !*nouapi, *printExample, *bind)
case "super":
err = Super(*tconfig, !*nouapi, *printExample, *bind)
case "solve":
err = path.Solve(*tconfig, *printExample)
case "gencfg":
switch *cfgmode {
case "super":
err = gencfg.GenSuperCfg(*tconfig, *printExample)
case "static":
err = gencfg.GenNMCfg(*tconfig, false, *printExample)
case "p2p":
err = gencfg.GenNMCfg(*tconfig, true, *printExample)
default:
err = fmt.Errorf("gencfg: generate config for %v mode are not implement", *cfgmode)
}
default:
flag.Usage()
}
if err != nil {
switch err := err.(type) {
case syscall.Errno:
os.Exit(int(err))
default:
fmt.Fprintf(os.Stderr, "Error :%v\n", err)
os.Exit(1)
}
}
}