1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00
wireguard-go/src/main.go

80 lines
1.2 KiB
Go
Raw Normal View History

package main
2017-06-26 13:14:02 +02:00
import (
"log"
"os"
"runtime"
2017-06-26 13:14:02 +02:00
)
func main() {
// parse arguments
var foreground bool
var interfaceName string
if len(os.Args) < 2 || len(os.Args) > 3 {
return
}
switch os.Args[1] {
case "-f", "--foreground":
foreground = true
if len(os.Args) != 3 {
return
}
interfaceName = os.Args[2]
default:
foreground = false
if len(os.Args) != 2 {
return
}
interfaceName = os.Args[1]
}
// daemonize the process
if !foreground {
err := Daemonize()
if err != nil {
log.Println("Failed to daemonize:", err)
}
return
}
// increase number of go workers (for Go <1.5)
runtime.GOMAXPROCS(runtime.NumCPU())
2017-07-10 17:20:43 +02:00
// open TUN device
tun, err := CreateTUN(interfaceName)
log.Println(tun, err)
if err != nil {
return
}
// create wireguard device
device := NewDevice(tun, LogLevelDebug)
logInfo := device.log.Info
logError := device.log.Error
logInfo.Println("Starting device")
2017-07-10 17:20:43 +02:00
// start configuration lister
uapi, err := NewUAPIListener(interfaceName)
if err != nil {
logError.Fatal("UAPI listen error:", err)
}
defer uapi.Close()
for {
conn, err := uapi.Accept()
if err != nil {
logError.Fatal("accept error:", err)
}
go ipcHandle(device, conn)
}
}