2019-01-02 01:55:51 +01:00
/ * SPDX - License - Identifier : MIT
2018-05-03 15:04:00 +02:00
*
2021-12-04 15:46:36 +01:00
* Copyright ( C ) 2017 - 2021 Kusakabe Si . All Rights Reserved .
2018-05-03 15:04:00 +02:00
* /
2017-05-30 22:36:49 +02:00
package main
2017-06-26 13:14:02 +02:00
import (
2021-08-16 20:58:15 +02:00
"flag"
2017-07-20 15:06:24 +02:00
"fmt"
2017-06-29 14:39:21 +02:00
"os"
2018-05-04 19:50:08 +02:00
"runtime"
2021-12-02 18:13:48 +01:00
"syscall"
2021-12-05 22:36:50 +01:00
"time"
nonSecureRand "math/rand"
2022-01-21 07:42:27 +01:00
"net/http"
_ "net/http/pprof"
2019-05-14 09:09:52 +02:00
2021-12-07 21:39:19 +01:00
"github.com/KusakabeSi/EtherGuard-VPN/gencfg"
2021-12-02 18:13:48 +01:00
"github.com/KusakabeSi/EtherGuard-VPN/ipc"
"github.com/KusakabeSi/EtherGuard-VPN/path"
"github.com/KusakabeSi/EtherGuard-VPN/tap"
2017-11-14 18:26:28 +01:00
)
2017-11-30 23:30:29 +01:00
const (
ExitSetupSuccess = 0
ExitSetupFailed = 1
)
2017-11-14 18:26:28 +01:00
const (
2021-08-24 10:43:55 +02:00
ENV_EG_UAPI_FD = "EG_UAPI_FD"
ENV_EG_UAPI_DIR = "EG_UAPI_DIR"
2017-06-26 13:14:02 +02:00
)
2017-06-04 21:48:15 +02:00
2021-08-16 20:58:15 +02:00
var (
2021-08-20 19:32:50 +02:00
tconfig = flag . String ( "config" , "" , "Config path for the interface." )
2021-12-04 15:46:36 +01:00
mode = flag . String ( "mode" , "" , "Running mode. [super|edge|solve|gencfg]" )
2021-08-20 19:32:50 +02:00
printExample = flag . Bool ( "example" , false , "Print example config" )
2021-12-04 15:46:36 +01:00
cfgmode = flag . String ( "cfgmode" , "" , "Running mode for generated config. [none|super|p2p]" )
2021-09-30 23:15:23 +02:00
bind = flag . String ( "bind" , "linux" , "UDP socket bind mode. [linux|std]\nYou may need std mode if you want to run Etherguard under WSL." )
2021-09-23 13:31:01 +02:00
nouapi = flag . Bool ( "no-uapi" , false , "Disable UAPI\nWith UAPI, you can check etherguard status by \"wg\" command" )
2022-01-21 07:42:27 +01:00
pprofaddr = flag . String ( "pprof" , "" , "pprof listing address" )
2021-08-20 19:32:50 +02:00
version = flag . Bool ( "version" , false , "Show version" )
help = flag . Bool ( "help" , false , "Show this help" )
2021-08-16 20:58:15 +02:00
)
2018-05-04 19:50:08 +02:00
func main ( ) {
2021-08-16 20:58:15 +02:00
flag . Parse ( )
2021-12-09 08:46:15 +01:00
if * version {
2021-12-02 18:13:48 +01:00
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 )
2018-05-24 01:52:22 +02:00
return
}
2021-12-09 08:46:15 +01:00
if * help {
2021-08-16 20:58:15 +02:00
flag . Usage ( )
2017-07-15 13:41:02 +02:00
return
}
2021-08-24 10:43:55 +02:00
uapiDir := os . Getenv ( ENV_EG_UAPI_DIR )
2021-08-16 20:58:15 +02:00
if uapiDir != "" {
ipc . SetsocketDirectory ( uapiDir )
2018-05-04 21:11:38 +02:00
}
2021-12-05 22:36:50 +01:00
nonSecureRand . Seed ( time . Now ( ) . UnixNano ( ) )
2022-01-21 07:42:27 +01:00
if * pprofaddr != "" {
go func ( ) {
//内网可访问的pprof地址
err := http . ListenAndServe ( * pprofaddr , nil )
if err != nil {
panic ( fmt . Errorf ( "pprof error: %v" , err ) )
}
} ( )
}
2018-05-04 21:11:38 +02:00
2021-08-24 10:43:55 +02:00
var err error
2021-08-16 20:58:15 +02:00
switch * mode {
case "edge" :
2021-09-23 13:31:01 +02:00
err = Edge ( * tconfig , ! * nouapi , * printExample , * bind )
2021-08-16 20:58:15 +02:00
case "super" :
2021-09-23 13:31:01 +02:00
err = Super ( * tconfig , ! * nouapi , * printExample , * bind )
2021-08-25 15:21:26 +02:00
case "solve" :
err = path . Solve ( * tconfig , * printExample )
2021-12-04 15:46:36 +01:00
case "gencfg" :
switch * cfgmode {
case "super" :
2021-12-07 21:39:19 +01:00
err = gencfg . GenSuperCfg ( * tconfig , * printExample )
2021-12-09 08:46:15 +01:00
case "static" :
2021-12-09 23:39:37 +01:00
err = gencfg . GenNMCfg ( * tconfig , false , * printExample )
case "p2p" :
err = gencfg . GenNMCfg ( * tconfig , true , * printExample )
2021-12-04 15:46:36 +01:00
default :
2021-12-07 21:39:19 +01:00
err = fmt . Errorf ( "gencfg: generate config for %v mode are not implement" , * cfgmode )
2021-12-04 15:46:36 +01:00
}
2021-08-16 20:58:15 +02:00
default :
flag . Usage ( )
2017-08-01 12:14:38 +02:00
}
2021-08-24 10:43:55 +02:00
if err != nil {
2021-12-09 08:46:15 +01:00
switch err := err . ( type ) {
2021-12-02 18:13:48 +01:00
case syscall . Errno :
2021-12-09 08:46:15 +01:00
os . Exit ( int ( err ) )
2021-12-02 18:13:48 +01:00
default :
fmt . Fprintf ( os . Stderr , "Error :%v\n" , err )
os . Exit ( 1 )
}
2021-08-24 10:43:55 +02:00
}
2017-05-30 22:36:49 +02:00
}