github.com/GFW-knocker/wireguard@v1.0.1/main_windows.go (about) 1 /* SPDX-License-Identifier: MIT 2 * 3 * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. 4 */ 5 6 package main 7 8 import ( 9 "fmt" 10 "os" 11 "os/signal" 12 13 "golang.org/x/sys/windows" 14 15 "github.com/GFW-knocker/wireguard/conn" 16 "github.com/GFW-knocker/wireguard/device" 17 "github.com/GFW-knocker/wireguard/ipc" 18 19 "github.com/GFW-knocker/wireguard/tun" 20 ) 21 22 const ( 23 ExitSetupSuccess = 0 24 ExitSetupFailed = 1 25 ) 26 27 func main() { 28 if len(os.Args) != 2 { 29 os.Exit(ExitSetupFailed) 30 } 31 interfaceName := os.Args[1] 32 33 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.") 34 35 logger := device.NewLogger( 36 device.LogLevelVerbose, 37 fmt.Sprintf("(%s) ", interfaceName), 38 ) 39 logger.Verbosef("Starting wireguard-go version %s", Version) 40 41 tun, err := tun.CreateTUN(interfaceName, 0) 42 if err == nil { 43 realInterfaceName, err2 := tun.Name() 44 if err2 == nil { 45 interfaceName = realInterfaceName 46 } 47 } else { 48 logger.Errorf("Failed to create TUN device: %v", err) 49 os.Exit(ExitSetupFailed) 50 } 51 52 device := device.NewDevice(tun, conn.NewDefaultBind(), logger) 53 err = device.Up() 54 if err != nil { 55 logger.Errorf("Failed to bring up device: %v", err) 56 os.Exit(ExitSetupFailed) 57 } 58 logger.Verbosef("Device started") 59 60 uapi, err := ipc.UAPIListen(interfaceName) 61 if err != nil { 62 logger.Errorf("Failed to listen on uapi socket: %v", err) 63 os.Exit(ExitSetupFailed) 64 } 65 66 errs := make(chan error) 67 term := make(chan os.Signal, 1) 68 69 go func() { 70 for { 71 conn, err := uapi.Accept() 72 if err != nil { 73 errs <- err 74 return 75 } 76 go device.IpcHandle(conn) 77 } 78 }() 79 logger.Verbosef("UAPI listener started") 80 81 // wait for program to terminate 82 83 signal.Notify(term, os.Interrupt) 84 signal.Notify(term, os.Kill) 85 signal.Notify(term, windows.SIGTERM) 86 87 select { 88 case <-term: 89 case <-errs: 90 case <-device.Wait(): 91 } 92 93 // clean up 94 95 uapi.Close() 96 device.Close() 97 98 logger.Verbosef("Shutting down") 99 }