1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 09:15:14 +01:00
wireguard-go/main_windows.go

95 lines
1.9 KiB
Go
Raw Normal View History

2019-02-04 17:29:52 +01:00
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
package main
import (
"fmt"
"os"
"os/signal"
2019-02-07 04:42:35 +01:00
"syscall"
2019-02-05 14:06:25 +01:00
2019-05-14 09:09:52 +02:00
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/ipc"
2019-02-18 04:44:41 +01:00
"golang.zx2c4.com/wireguard/tun"
2019-02-04 17:29:52 +01:00
)
const (
ExitSetupSuccess = 0
ExitSetupFailed = 1
)
func main() {
2019-02-05 14:06:25 +01:00
if len(os.Args) != 2 {
2019-02-04 17:29:52 +01:00
os.Exit(ExitSetupFailed)
}
interfaceName := os.Args[1]
2019-02-04 17:29:52 +01:00
2019-04-19 00:48:09 +02:00
fmt.Fprintln(os.Stderr, "Warning: this is a test program for Windows, mainly used for debugging this Go package. For a real WireGuard for Windows client, the repo you want is <https://git.zx2c4.com/wireguard-windows/>, which includes this code as a module.")
2019-03-03 04:04:41 +01:00
logger := device.NewLogger(
device.LogLevelDebug,
2019-02-04 17:29:52 +01:00
fmt.Sprintf("(%s) ", interfaceName),
)
logger.Info.Println("Starting wireguard-go version", device.WireGuardGoVersion)
2019-02-04 17:29:52 +01:00
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)
}
2019-03-03 04:04:41 +01:00
device := device.NewDevice(tun, logger)
2019-02-05 14:06:25 +01:00
device.Up()
2019-02-04 17:29:52 +01:00
logger.Info.Println("Device started")
2019-03-03 04:04:41 +01:00
uapi, err := ipc.UAPIListen(interfaceName)
2019-02-04 17:29:52 +01:00
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
}
2019-03-03 04:04:41 +01:00
go device.IpcHandle(conn)
2019-02-04 17:29:52 +01:00
}
}()
logger.Info.Println("UAPI listener started")
// wait for program to terminate
signal.Notify(term, os.Interrupt)
2019-02-07 04:42:35 +01:00
signal.Notify(term, os.Kill)
signal.Notify(term, syscall.SIGTERM)
2019-02-04 17:29:52 +01:00
select {
case <-term:
case <-errs:
case <-device.Wait():
}
// clean up
uapi.Close()
device.Close()
logger.Info.Println("Shutting down")
}