2017-05-30 22:36:49 +02:00
|
|
|
package main
|
|
|
|
|
2017-06-26 13:14:02 +02:00
|
|
|
import (
|
2017-07-20 15:06:24 +02:00
|
|
|
"fmt"
|
2017-06-28 23:45:45 +02:00
|
|
|
"log"
|
2017-06-29 14:39:21 +02:00
|
|
|
"os"
|
2017-08-01 12:14:38 +02:00
|
|
|
"os/signal"
|
2017-07-13 14:32:40 +02:00
|
|
|
"runtime"
|
2017-06-26 13:14:02 +02:00
|
|
|
)
|
2017-06-04 21:48:15 +02:00
|
|
|
|
2017-07-20 15:06:24 +02:00
|
|
|
func printUsage() {
|
|
|
|
fmt.Printf("usage:\n")
|
|
|
|
fmt.Printf("%s [-f/--foreground] INTERFACE-NAME\n", os.Args[0])
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:48:15 +02:00
|
|
|
func main() {
|
2017-06-29 14:39:21 +02:00
|
|
|
|
2017-07-15 13:41:02 +02:00
|
|
|
// parse arguments
|
|
|
|
|
|
|
|
var foreground bool
|
|
|
|
var interfaceName string
|
|
|
|
if len(os.Args) < 2 || len(os.Args) > 3 {
|
2017-07-20 15:06:24 +02:00
|
|
|
printUsage()
|
2017-07-15 13:41:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch os.Args[1] {
|
2017-07-17 16:16:18 +02:00
|
|
|
|
2017-07-15 13:41:02 +02:00
|
|
|
case "-f", "--foreground":
|
|
|
|
foreground = true
|
|
|
|
if len(os.Args) != 3 {
|
2017-07-20 15:06:24 +02:00
|
|
|
printUsage()
|
2017-07-15 13:41:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
interfaceName = os.Args[2]
|
2017-07-17 16:16:18 +02:00
|
|
|
|
2017-07-15 13:41:02 +02:00
|
|
|
default:
|
|
|
|
foreground = false
|
|
|
|
if len(os.Args) != 2 {
|
2017-07-20 15:06:24 +02:00
|
|
|
printUsage()
|
2017-07-15 13:41:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
interfaceName = os.Args[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// daemonize the process
|
|
|
|
|
|
|
|
if !foreground {
|
|
|
|
err := Daemonize()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to daemonize:", err)
|
|
|
|
}
|
2017-06-29 14:39:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-13 14:32:40 +02:00
|
|
|
// increase number of go workers (for Go <1.5)
|
|
|
|
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
2017-07-10 17:20:43 +02:00
|
|
|
// open TUN device
|
2017-06-04 21:48:15 +02:00
|
|
|
|
2017-07-15 13:41:02 +02:00
|
|
|
tun, err := CreateTUN(interfaceName)
|
2017-06-28 23:45:45 +02:00
|
|
|
if err != nil {
|
2017-07-17 16:16:18 +02:00
|
|
|
log.Println("Failed to create tun device:", err)
|
2017-06-28 23:45:45 +02:00
|
|
|
return
|
|
|
|
}
|
2017-06-04 21:48:15 +02:00
|
|
|
|
2017-07-15 13:41:02 +02:00
|
|
|
// create wireguard device
|
|
|
|
|
2017-06-30 14:41:08 +02:00
|
|
|
device := NewDevice(tun, LogLevelDebug)
|
2017-07-15 13:41:02 +02:00
|
|
|
|
|
|
|
logInfo := device.log.Info
|
|
|
|
logError := device.log.Error
|
|
|
|
logInfo.Println("Starting device")
|
2017-06-04 21:48:15 +02:00
|
|
|
|
2017-07-10 17:20:43 +02:00
|
|
|
// start configuration lister
|
2017-06-04 21:48:15 +02:00
|
|
|
|
2017-07-15 13:41:02 +02:00
|
|
|
uapi, err := NewUAPIListener(interfaceName)
|
|
|
|
if err != nil {
|
|
|
|
logError.Fatal("UAPI listen error:", err)
|
|
|
|
}
|
2017-08-01 12:14:38 +02:00
|
|
|
|
|
|
|
errs := make(chan error)
|
|
|
|
term := make(chan os.Signal)
|
|
|
|
wait := device.WaitChannel()
|
2017-07-13 14:32:40 +02:00
|
|
|
|
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-13 14:32:40 +02:00
|
|
|
}
|
2017-07-17 16:16:18 +02:00
|
|
|
}()
|
|
|
|
|
2017-08-01 12:14:38 +02:00
|
|
|
logInfo.Println("UAPI listener started")
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
logInfo.Println("Closing")
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|