Mirror of Go implementation of WireGuard.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main_windows.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* SPDX-License-Identifier: MIT
  2. *
  3. * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
  4. */
  5. package main
  6. import (
  7. "fmt"
  8. "os"
  9. "os/signal"
  10. "syscall"
  11. "golang.zx2c4.com/wireguard/device"
  12. "golang.zx2c4.com/wireguard/ipc"
  13. "golang.zx2c4.com/wireguard/tun"
  14. )
  15. const (
  16. ExitSetupSuccess = 0
  17. ExitSetupFailed = 1
  18. )
  19. func main() {
  20. if len(os.Args) != 2 {
  21. os.Exit(ExitSetupFailed)
  22. }
  23. interfaceName := os.Args[1]
  24. 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.")
  25. logger := device.NewLogger(
  26. device.LogLevelDebug,
  27. fmt.Sprintf("(%s) ", interfaceName),
  28. )
  29. logger.Info.Println("Starting wireguard-go version", device.WireGuardGoVersion)
  30. logger.Debug.Println("Debug log enabled")
  31. tun, err := tun.CreateTUN(interfaceName, 0)
  32. if err == nil {
  33. realInterfaceName, err2 := tun.Name()
  34. if err2 == nil {
  35. interfaceName = realInterfaceName
  36. }
  37. } else {
  38. logger.Error.Println("Failed to create TUN device:", err)
  39. os.Exit(ExitSetupFailed)
  40. }
  41. device := device.NewDevice(tun, logger)
  42. device.Up()
  43. logger.Info.Println("Device started")
  44. uapi, err := ipc.UAPIListen(interfaceName)
  45. if err != nil {
  46. logger.Error.Println("Failed to listen on uapi socket:", err)
  47. os.Exit(ExitSetupFailed)
  48. }
  49. errs := make(chan error)
  50. term := make(chan os.Signal, 1)
  51. go func() {
  52. for {
  53. conn, err := uapi.Accept()
  54. if err != nil {
  55. errs <- err
  56. return
  57. }
  58. go device.IpcHandle(conn)
  59. }
  60. }()
  61. logger.Info.Println("UAPI listener started")
  62. // wait for program to terminate
  63. signal.Notify(term, os.Interrupt)
  64. signal.Notify(term, os.Kill)
  65. signal.Notify(term, syscall.SIGTERM)
  66. select {
  67. case <-term:
  68. case <-errs:
  69. case <-device.Wait():
  70. }
  71. // clean up
  72. uapi.Close()
  73. device.Close()
  74. logger.Info.Println("Shutting down")
  75. }