2019-02-04 16:29:52 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-02-05 13:06:25 +00:00
|
|
|
|
|
|
|
"git.zx2c4.com/wireguard-go/tun"
|
2019-02-04 16:29:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ExitSetupSuccess = 0
|
|
|
|
ExitSetupFailed = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-02-05 13:06:25 +00:00
|
|
|
if len(os.Args) != 2 {
|
2019-02-04 16:29:52 +00:00
|
|
|
os.Exit(ExitSetupFailed)
|
|
|
|
}
|
|
|
|
//configFile := os.Args[1]
|
|
|
|
interfaceName := "TODO"
|
|
|
|
|
|
|
|
logger := NewLogger(
|
|
|
|
LogLevelDebug,
|
|
|
|
fmt.Sprintf("(%s) ", interfaceName),
|
|
|
|
)
|
|
|
|
logger.Info.Println("Starting wireguard-go version", WireGuardGoVersion)
|
|
|
|
logger.Debug.Println("Debug log enabled")
|
|
|
|
|
|
|
|
tun, err := tun.CreateTUN(interfaceName)
|
|
|
|
if err == nil {
|
|
|
|
realInterfaceName, err2 := tun.Name()
|
|
|
|
if err2 == nil {
|
|
|
|
interfaceName = realInterfaceName
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Error.Println("Failed to create TUN device:", err)
|
|
|
|
os.Exit(ExitSetupFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
device := NewDevice(tun, logger)
|
2019-02-05 13:06:25 +00:00
|
|
|
device.Up()
|
2019-02-04 16:29:52 +00:00
|
|
|
logger.Info.Println("Device started")
|
|
|
|
|
|
|
|
uapi, err := UAPIListen(interfaceName)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error.Println("Failed to listen on uapi socket:", err)
|
|
|
|
os.Exit(ExitSetupFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := make(chan error)
|
|
|
|
term := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := uapi.Accept()
|
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go ipcHandle(device, conn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
logger.Info.Println("UAPI listener started")
|
|
|
|
|
|
|
|
// wait for program to terminate
|
|
|
|
|
|
|
|
signal.Notify(term, os.Interrupt)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-term:
|
|
|
|
case <-errs:
|
|
|
|
case <-device.Wait():
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
|
|
|
uapi.Close()
|
|
|
|
device.Close()
|
|
|
|
|
|
|
|
logger.Info.Println("Shutting down")
|
|
|
|
}
|