From 69f0fe67b63d90e523a5a1241fb1b46c2e8dbe03 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 3 Mar 2019 04:04:41 +0100 Subject: global: begin modularization --- main.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 08f8cc6..a3a04b8 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ package main import ( "fmt" + "golang.zx2c4.com/wireguard/device" + "golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/tun" "os" "os/signal" @@ -76,7 +78,7 @@ func warning() { func main() { if len(os.Args) == 2 && os.Args[1] == "--version" { - fmt.Printf("wireguard-go v%s\n\nUserspace WireGuard daemon for %s-%s.\nInformation available at https://www.wireguard.com.\nCopyright (C) Jason A. Donenfeld .\n", WireGuardGoVersion, runtime.GOOS, runtime.GOARCH) + fmt.Printf("wireguard-go v%s\n\nUserspace WireGuard daemon for %s-%s.\nInformation available at https://www.wireguard.com.\nCopyright (C) Jason A. Donenfeld .\n", device.WireGuardGoVersion, runtime.GOOS, runtime.GOARCH) return } @@ -119,15 +121,15 @@ func main() { logLevel := func() int { switch os.Getenv("LOG_LEVEL") { case "debug": - return LogLevelDebug + return device.LogLevelDebug case "info": - return LogLevelInfo + return device.LogLevelInfo case "error": - return LogLevelError + return device.LogLevelError case "silent": - return LogLevelSilent + return device.LogLevelSilent } - return LogLevelInfo + return device.LogLevelInfo }() // open TUN device (or use supplied fd) @@ -135,7 +137,7 @@ func main() { tun, err := func() (tun.TUNDevice, error) { tunFdStr := os.Getenv(ENV_WG_TUN_FD) if tunFdStr == "" { - return tun.CreateTUN(interfaceName, DefaultMTU) + return tun.CreateTUN(interfaceName, device.DefaultMTU) } // construct tun device from supplied fd @@ -151,7 +153,7 @@ func main() { } file := os.NewFile(uintptr(fd), "") - return tun.CreateTUNFromFile(file, DefaultMTU) + return tun.CreateTUNFromFile(file, device.DefaultMTU) }() if err == nil { @@ -161,12 +163,12 @@ func main() { } } - logger := NewLogger( + logger := device.NewLogger( logLevel, fmt.Sprintf("(%s) ", interfaceName), ) - logger.Info.Println("Starting wireguard-go version", WireGuardGoVersion) + logger.Info.Println("Starting wireguard-go version", device.WireGuardGoVersion) logger.Debug.Println("Debug log enabled") @@ -180,7 +182,7 @@ func main() { fileUAPI, err := func() (*os.File, error) { uapiFdStr := os.Getenv(ENV_WG_UAPI_FD) if uapiFdStr == "" { - return UAPIOpen(interfaceName) + return ipc.UAPIOpen(interfaceName) } // use supplied fd @@ -206,7 +208,7 @@ func main() { env = append(env, fmt.Sprintf("%s=4", ENV_WG_UAPI_FD)) env = append(env, fmt.Sprintf("%s=1", ENV_WG_PROCESS_FOREGROUND)) files := [3]*os.File{} - if os.Getenv("LOG_LEVEL") != "" && logLevel != LogLevelSilent { + if os.Getenv("LOG_LEVEL") != "" && logLevel != device.LogLevelSilent { files[0], _ = os.Open(os.DevNull) files[1] = os.Stdout files[2] = os.Stderr @@ -246,14 +248,14 @@ func main() { return } - device := NewDevice(tun, logger) + device := device.NewDevice(tun, logger) logger.Info.Println("Device started") errs := make(chan error) term := make(chan os.Signal, 1) - uapi, err := UAPIListen(interfaceName, fileUAPI) + uapi, err := ipc.UAPIListen(interfaceName, fileUAPI) if err != nil { logger.Error.Println("Failed to listen on uapi socket:", err) os.Exit(ExitSetupFailed) @@ -266,7 +268,7 @@ func main() { errs <- err return } - go ipcHandle(device, conn) + go device.IpcHandle(conn) } }() -- cgit v1.2.3