mirror of
https://git.zx2c4.com/wireguard-go
synced 2024-11-14 16:55:15 +01:00
3bb8fec7e4
Accept packet vectors for reading and writing in the tun.Device and conn.Bind interfaces, so that the internal plumbing between these interfaces now passes a vector of packets. Vectors move untouched between these interfaces, i.e. if 128 packets are received from conn.Bind.Read(), 128 packets are passed to tun.Device.Write(). There is no internal buffering. Currently, existing implementations are only adjusted to have vectors of length one. Subsequent patches will improve that. Also, as a related fixup, use the unix and windows packages rather than the syscall package when possible. Co-authored-by: James Tucker <james@tailscale.com> Signed-off-by: James Tucker <james@tailscale.com> Signed-off-by: Jordan Whited <jordan@tailscale.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
100 lines
2.0 KiB
Go
100 lines
2.0 KiB
Go
/* SPDX-License-Identifier: MIT
|
|
*
|
|
* Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
|
"golang.zx2c4.com/wireguard/device"
|
|
"golang.zx2c4.com/wireguard/ipc"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
|
)
|
|
|
|
const (
|
|
ExitSetupSuccess = 0
|
|
ExitSetupFailed = 1
|
|
)
|
|
|
|
func main() {
|
|
if len(os.Args) != 2 {
|
|
os.Exit(ExitSetupFailed)
|
|
}
|
|
interfaceName := os.Args[1]
|
|
|
|
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.")
|
|
|
|
logger := device.NewLogger(
|
|
device.LogLevelVerbose,
|
|
fmt.Sprintf("(%s) ", interfaceName),
|
|
)
|
|
logger.Verbosef("Starting wireguard-go version %s", Version)
|
|
|
|
tun, err := tun.CreateTUN(interfaceName, 0)
|
|
if err == nil {
|
|
realInterfaceName, err2 := tun.Name()
|
|
if err2 == nil {
|
|
interfaceName = realInterfaceName
|
|
}
|
|
} else {
|
|
logger.Errorf("Failed to create TUN device: %v", err)
|
|
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")
|
|
|
|
uapi, err := ipc.UAPIListen(interfaceName)
|
|
if err != nil {
|
|
logger.Errorf("Failed to listen on uapi socket: %v", 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
|
|
}
|
|
go device.IpcHandle(conn)
|
|
}
|
|
}()
|
|
logger.Verbosef("UAPI listener started")
|
|
|
|
// wait for program to terminate
|
|
|
|
signal.Notify(term, os.Interrupt)
|
|
signal.Notify(term, os.Kill)
|
|
signal.Notify(term, windows.SIGTERM)
|
|
|
|
select {
|
|
case <-term:
|
|
case <-errs:
|
|
case <-device.Wait():
|
|
}
|
|
|
|
// clean up
|
|
|
|
uapi.Close()
|
|
device.Close()
|
|
|
|
logger.Verbosef("Shutting down")
|
|
}
|