wireguard-go/main_windows.go

99 lines
2 KiB
Go
Raw Normal View History

2019-02-04 16:29:52 +00:00
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
2019-02-04 16:29:52 +00:00
*/
package main
import (
"fmt"
"os"
"os/signal"
2019-02-07 03:42:35 +00:00
"syscall"
2019-02-05 13:06:25 +00:00
"golang.zx2c4.com/wireguard/conn"
2019-05-14 07:09:52 +00:00
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/ipc"
2019-02-18 03:44:41 +00:00
"golang.zx2c4.com/wireguard/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)
}
interfaceName := os.Args[1]
2019-02-04 16:29:52 +00:00
2019-04-18 22:48:09 +00: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 03:04:41 +00:00
logger := device.NewLogger(
device.LogLevelVerbose,
2019-02-04 16:29:52 +00:00
fmt.Sprintf("(%s) ", interfaceName),
)
logger.Verbosef("Starting wireguard-go version %s", Version)
2019-02-04 16:29:52 +00:00
tun, err := tun.CreateTUN(interfaceName, 0)
2019-02-04 16:29:52 +00:00
if err == nil {
realInterfaceName, err2 := tun.Name()
if err2 == nil {
interfaceName = realInterfaceName
}
} else {
logger.Errorf("Failed to create TUN device: %v", err)
2019-02-04 16:29:52 +00:00
os.Exit(ExitSetupFailed)
}
device := device.NewDevice(tun, conn.NewDefaultBind(), logger)
err = device.Up()
if err != nil {
logger.Errorf("Failed to bring up device: %v", err)
os.Exit(ExitSetupFailed)
}
logger.Verbosef("Device started")
2019-02-04 16:29:52 +00:00
2019-03-03 03:04:41 +00:00
uapi, err := ipc.UAPIListen(interfaceName)
2019-02-04 16:29:52 +00:00
if err != nil {
logger.Errorf("Failed to listen on uapi socket: %v", err)
2019-02-04 16:29:52 +00:00
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 03:04:41 +00:00
go device.IpcHandle(conn)
2019-02-04 16:29:52 +00:00
}
}()
logger.Verbosef("UAPI listener started")
2019-02-04 16:29:52 +00:00
// wait for program to terminate
signal.Notify(term, os.Interrupt)
2019-02-07 03:42:35 +00:00
signal.Notify(term, os.Kill)
signal.Notify(term, syscall.SIGTERM)
2019-02-04 16:29:52 +00:00
select {
case <-term:
case <-errs:
case <-device.Wait():
}
// clean up
uapi.Close()
device.Close()
logger.Verbosef("Shutting down")
2019-02-04 16:29:52 +00:00
}