wireguard-go/src/main.go

170 lines
2.7 KiB
Go
Raw Normal View History

package main
2017-06-26 13:14:02 +02:00
import (
"fmt"
"os"
2017-08-01 12:14:38 +02:00
"os/signal"
"runtime"
2017-11-14 18:26:28 +01:00
"strconv"
)
const (
EnvWGTunFD = "WG_TUN_FD"
2017-06-26 13:14:02 +02:00
)
func printUsage() {
fmt.Printf("usage:\n")
fmt.Printf("%s [-f/--foreground] INTERFACE-NAME\n", os.Args[0])
}
func main() {
2017-11-14 16:27:53 +01:00
// parse arguments
var foreground bool
var interfaceName string
if len(os.Args) < 2 || len(os.Args) > 3 {
printUsage()
return
}
switch os.Args[1] {
2017-07-17 16:16:18 +02:00
case "-f", "--foreground":
foreground = true
if len(os.Args) != 3 {
printUsage()
return
}
interfaceName = os.Args[2]
2017-07-17 16:16:18 +02:00
default:
foreground = false
if len(os.Args) != 2 {
printUsage()
return
}
interfaceName = os.Args[1]
}
// get log level (default: info)
logLevel := func() int {
switch os.Getenv("LOG_LEVEL") {
case "debug":
return LogLevelDebug
case "info":
return LogLevelInfo
case "error":
return LogLevelError
}
return LogLevelInfo
}()
2017-11-14 18:26:28 +01:00
logger := NewLogger(
logLevel,
fmt.Sprintf("(%s) ", interfaceName),
)
logger.Debug.Println("Debug log enabled")
2017-11-14 18:26:28 +01:00
// open TUN device
2017-11-14 18:26:28 +01:00
tun, err := func() (TUNDevice, error) {
tunFdStr := os.Getenv(EnvWGTunFD)
if tunFdStr == "" {
return CreateTUN(interfaceName)
}
2017-11-14 18:26:28 +01:00
// construct tun device from supplied FD
fd, err := strconv.ParseUint(tunFdStr, 10, 32)
if err != nil {
return nil, err
}
file := os.NewFile(uintptr(fd), "/dev/net/tun")
return CreateTUNFromFile(interfaceName, file)
}()
if err != nil {
logger.Error.Println("Failed to create TUN device:", err)
}
// daemonize the process
if !foreground {
env := os.Environ()
_, ok := os.LookupEnv(EnvWGTunFD)
if !ok {
kvp := fmt.Sprintf("%s=3", EnvWGTunFD)
env = append(env, kvp)
}
attr := &os.ProcAttr{
Files: []*os.File{
nil, // stdin
nil, // stdout
nil, // stderr
tun.File(),
},
Dir: ".",
Env: env,
}
err = Daemonize(attr)
if err != nil {
logger.Error.Println("Failed to daemonize:", err)
}
return
}
// increase number of go workers (for Go <1.5)
runtime.GOMAXPROCS(runtime.NumCPU())
// create wireguard device
device := NewDevice(tun, logger)
logger.Info.Println("Device started")
2017-07-10 17:20:43 +02:00
// start configuration lister
uapi, err := NewUAPIListener(interfaceName)
if err != nil {
2017-11-14 18:26:28 +01:00
logger.Error.Println("UAPI listen error:", err)
return
}
2017-08-01 12:14:38 +02:00
errs := make(chan error)
term := make(chan os.Signal)
wait := device.WaitChannel()
2017-07-17 16:16:18 +02:00
go func() {
for {
conn, err := uapi.Accept()
if err != nil {
2017-08-01 12:14:38 +02:00
errs <- err
return
2017-07-17 16:16:18 +02:00
}
go ipcHandle(device, conn)
}
2017-07-17 16:16:18 +02:00
}()
2017-11-14 18:26:28 +01:00
logger.Info.Println("UAPI listener started")
2017-08-01 12:14:38 +02:00
// wait for program to terminate
signal.Notify(term, os.Kill)
signal.Notify(term, os.Interrupt)
select {
case <-wait:
case <-term:
case <-errs:
}
// clean up UAPI bind
uapi.Close()
2017-11-14 18:26:28 +01:00
logger.Info.Println("Shutting down")
}