generate-version-and-build:
@export GIT_CEILING_DIRECTORIES="$(realpath $(CURDIR)/..)" && \
tag="$$(git describe --dirty 2>/dev/null)" && \
- ver="$$(printf 'package main\nconst WireGuardGoVersion = "%s"\n' "$$tag")" && \
- [ "$$(cat version.go 2>/dev/null)" != "$$ver" ] && \
- echo "$$ver" > version.go && \
- git update-index --assume-unchanged version.go || true
+ ver="$$(printf 'package device\nconst WireGuardGoVersion = "%s"\n' "$$tag")" && \
+ [ "$$(cat device/version.go 2>/dev/null)" != "$$ver" ] && \
+ echo "$$ver" > device/version.go && \
+ git update-index --assume-unchanged device/version.go || true
@$(MAKE) wireguard-go
wireguard-go: $(wildcard *.go) $(wildcard */*.go)
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"errors"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"math/rand"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"math/rand"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import "errors"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"errors"
err := unsafeCloseBind(device)
device.net.Unlock()
return err
-}
+}
\ No newline at end of file
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"net"
* So this code is remains platform dependent.
*/
-package main
+package device
import (
"errors"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"time"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"crypto/hmac"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"testing"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"golang.zx2c4.com/wireguard/ratelimiter"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
/* Create two device instances and simulate full WireGuard interaction
* without network dependencies
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"math/rand"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"crypto/rand"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"net"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"encoding/hex"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"crypto/cipher"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"io"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
func (bind *NativeBind) SetMark(mark uint32) error {
return nil
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"golang.org/x/sys/unix"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"sync/atomic"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"crypto/hmac"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"errors"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"crypto/subtle"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"bytes"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"encoding/base64"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import "sync"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
/* Implementation specific constants */
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"bytes"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"bytes"
* This is based heavily on timers.c from the kernel implementation.
*/
-package main
+package device
import (
"math/rand"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"golang.zx2c4.com/wireguard/tun"
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package device
import (
"bufio"
"fmt"
+ "golang.zx2c4.com/wireguard/ipc"
"io"
"net"
"strconv"
return s.int64
}
-func ipcGetOperation(device *Device, socket *bufio.Writer) *IPCError {
+func (device *Device) IpcGetOperation(socket *bufio.Writer) *IPCError {
device.log.Debug.Println("UAPI: Processing get operation")
for _, line := range lines {
_, err := socket.WriteString(line + "\n")
if err != nil {
- return &IPCError{ipcErrorIO}
+ return &IPCError{ipc.IpcErrorIO}
}
}
return nil
}
-func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
+func (device *Device) IpcSetOperation(socket *bufio.Reader) *IPCError {
scanner := bufio.NewScanner(socket)
logError := device.log.Error
logDebug := device.log.Debug
}
parts := strings.Split(line, "=")
if len(parts) != 2 {
- return &IPCError{ipcErrorProtocol}
+ return &IPCError{ipc.IpcErrorProtocol}
}
key := parts[0]
value := parts[1]
err := sk.FromHex(value)
if err != nil {
logError.Println("Failed to set private_key:", err)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
logDebug.Println("UAPI: Updating private key")
device.SetPrivateKey(sk)
port, err := strconv.ParseUint(value, 10, 16)
if err != nil {
logError.Println("Failed to parse listen_port:", err)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
// update port and rebind
if err := device.BindUpdate(); err != nil {
logError.Println("Failed to set listen_port:", err)
- return &IPCError{ipcErrorPortInUse}
+ return &IPCError{ipc.IpcErrorPortInUse}
}
case "fwmark":
if err != nil {
logError.Println("Invalid fwmark", err)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
logDebug.Println("UAPI: Updating fwmark")
if err := device.BindSetMark(uint32(fwmark)); err != nil {
logError.Println("Failed to update fwmark:", err)
- return &IPCError{ipcErrorPortInUse}
+ return &IPCError{ipc.IpcErrorPortInUse}
}
case "public_key":
case "replace_peers":
if value != "true" {
logError.Println("Failed to set replace_peers, invalid value:", value)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
logDebug.Println("UAPI: Removing all peers")
device.RemoveAllPeers()
default:
logError.Println("Invalid UAPI device key:", key)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
}
err := publicKey.FromHex(value)
if err != nil {
logError.Println("Failed to get peer by public key:", err)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
// ignore peer with public key of device
peer, err = device.NewPeer(publicKey)
if err != nil {
logError.Println("Failed to create new peer:", err)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
logDebug.Println(peer, "- UAPI: Created")
}
if value != "true" {
logError.Println("Failed to set remove, invalid value:", value)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
if !dummy {
logDebug.Println(peer, "- UAPI: Removing")
if err != nil {
logError.Println("Failed to set preshared key:", err)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
case "endpoint":
if err != nil {
logError.Println("Failed to set endpoint:", value)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
case "persistent_keepalive_interval":
secs, err := strconv.ParseUint(value, 10, 16)
if err != nil {
logError.Println("Failed to set persistent keepalive interval:", err)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
old := peer.persistentKeepaliveInterval
if old == 0 && secs != 0 {
if err != nil {
logError.Println("Failed to get tun device status:", err)
- return &IPCError{ipcErrorIO}
+ return &IPCError{ipc.IpcErrorIO}
}
if device.isUp.Get() && !dummy {
peer.SendKeepalive()
if value != "true" {
logError.Println("Failed to replace allowedips, invalid value:", value)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
if dummy {
_, network, err := net.ParseCIDR(value)
if err != nil {
logError.Println("Failed to set allowed ip:", err)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
if dummy {
if value != "1" {
logError.Println("Invalid protocol version:", value)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
default:
logError.Println("Invalid UAPI peer key:", key)
- return &IPCError{ipcErrorInvalid}
+ return &IPCError{ipc.IpcErrorInvalid}
}
}
}
return nil
}
-func ipcHandle(device *Device, socket net.Conn) {
+func (device *Device) IpcHandle(socket net.Conn) {
// create buffered read/writer
switch op {
case "set=1\n":
device.log.Debug.Println("UAPI: Set operation")
- status = ipcSetOperation(device, buffered.Reader)
+ status = device.IpcSetOperation(buffered.Reader)
case "get=1\n":
device.log.Debug.Println("UAPI: Get operation")
- status = ipcGetOperation(device, buffered.Writer)
+ status = device.IpcGetOperation(buffered.Writer)
default:
device.log.Error.Println("Invalid UAPI operation:", op)
--- /dev/null
+package device
+
+const WireGuardGoVersion = "0.0.20181222"
require (
github.com/Microsoft/go-winio v0.4.11
- golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67
- golang.org/x/net v0.0.0-20190213061140-3a22650c66bd
- golang.org/x/sys v0.0.0-20190213121743-983097b1a8a3
+ golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25
+ golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95
+ golang.org/x/sys v0.0.0-20190302025703-b6889370fb10
)
github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
-golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
-golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
-golang.org/x/net v0.0.0-20190213061140-3a22650c66bd h1:HuTn7WObtcDo9uEEU7rEqL0jYthdXAmZ6PP+meazmaU=
-golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/sys v0.0.0-20190213121743-983097b1a8a3 h1:+KlxhGbYkFs8lMfwKn+2ojry1ID5eBSMXprS2u/wqCE=
-golang.org/x/sys v0.0.0-20190213121743-983097b1a8a3/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25 h1:jsG6UpNLt9iAsb0S2AGW28DveNzzgmbXR+ENoPjUeIU=
+golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95 h1:fY7Dsw114eJN4boqzVSbpVHO6rTdhq6/GnXeu+PKnzU=
+golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190302025703-b6889370fb10 h1:xQJI9OEiErEQ++DoXOHqEpzsGMrAv2Q2jyCpi7DmfpQ=
+golang.org/x/sys v0.0.0-20190302025703-b6889370fb10/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package ipc
import (
"errors"
var socketDirectory = "/var/run/wireguard"
const (
- ipcErrorIO = -int64(unix.EIO)
- ipcErrorProtocol = -int64(unix.EPROTO)
- ipcErrorInvalid = -int64(unix.EINVAL)
- ipcErrorPortInUse = -int64(unix.EADDRINUSE)
+ IpcErrorIO = -int64(unix.EIO)
+ IpcErrorProtocol = -int64(unix.EPROTO)
+ IpcErrorInvalid = -int64(unix.EINVAL)
+ IpcErrorPortInUse = -int64(unix.EADDRINUSE)
socketName = "%s.sock"
)
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package ipc
import (
"errors"
var socketDirectory = "/var/run/wireguard"
const (
- ipcErrorIO = -int64(unix.EIO)
- ipcErrorProtocol = -int64(unix.EPROTO)
- ipcErrorInvalid = -int64(unix.EINVAL)
- ipcErrorPortInUse = -int64(unix.EADDRINUSE)
+ IpcErrorIO = -int64(unix.EIO)
+ IpcErrorProtocol = -int64(unix.EPROTO)
+ IpcErrorInvalid = -int64(unix.EINVAL)
+ IpcErrorPortInUse = -int64(unix.EADDRINUSE)
socketName = "%s.sock"
)
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package ipc
import (
"github.com/Microsoft/go-winio"
//TODO: replace these with actual standard windows error numbers from the win package
const (
- ipcErrorIO = -int64(5)
- ipcErrorProtocol = -int64(71)
- ipcErrorInvalid = -int64(22)
- ipcErrorPortInUse = -int64(98)
+ IpcErrorIO = -int64(5)
+ IpcErrorProtocol = -int64(71)
+ IpcErrorInvalid = -int64(22)
+ IpcErrorPortInUse = -int64(98)
)
type UAPIListener struct {
import (
"fmt"
+ "golang.zx2c4.com/wireguard/device"
+ "golang.zx2c4.com/wireguard/ipc"
"golang.zx2c4.com/wireguard/tun"
"os"
"os/signal"
func main() {
if len(os.Args) == 2 && os.Args[1] == "--version" {
- fmt.Printf("wireguard-go v%s\n\nUserspace WireGuard daemon for %s-%s.\nInformation available at https://www.wireguard.com.\nCopyright (C) Jason A. Donenfeld <Jason@zx2c4.com>.\n", WireGuardGoVersion, runtime.GOOS, runtime.GOARCH)
+ fmt.Printf("wireguard-go v%s\n\nUserspace WireGuard daemon for %s-%s.\nInformation available at https://www.wireguard.com.\nCopyright (C) Jason A. Donenfeld <Jason@zx2c4.com>.\n", device.WireGuardGoVersion, runtime.GOOS, runtime.GOARCH)
return
}
logLevel := func() int {
switch os.Getenv("LOG_LEVEL") {
case "debug":
- return LogLevelDebug
+ return device.LogLevelDebug
case "info":
- return LogLevelInfo
+ return device.LogLevelInfo
case "error":
- return LogLevelError
+ return device.LogLevelError
case "silent":
- return LogLevelSilent
+ return device.LogLevelSilent
}
- return LogLevelInfo
+ return device.LogLevelInfo
}()
// open TUN device (or use supplied fd)
tun, err := func() (tun.TUNDevice, error) {
tunFdStr := os.Getenv(ENV_WG_TUN_FD)
if tunFdStr == "" {
- return tun.CreateTUN(interfaceName, DefaultMTU)
+ return tun.CreateTUN(interfaceName, device.DefaultMTU)
}
// construct tun device from supplied fd
}
file := os.NewFile(uintptr(fd), "")
- return tun.CreateTUNFromFile(file, DefaultMTU)
+ return tun.CreateTUNFromFile(file, device.DefaultMTU)
}()
if err == nil {
}
}
- logger := NewLogger(
+ logger := device.NewLogger(
logLevel,
fmt.Sprintf("(%s) ", interfaceName),
)
- logger.Info.Println("Starting wireguard-go version", WireGuardGoVersion)
+ logger.Info.Println("Starting wireguard-go version", device.WireGuardGoVersion)
logger.Debug.Println("Debug log enabled")
fileUAPI, err := func() (*os.File, error) {
uapiFdStr := os.Getenv(ENV_WG_UAPI_FD)
if uapiFdStr == "" {
- return UAPIOpen(interfaceName)
+ return ipc.UAPIOpen(interfaceName)
}
// use supplied fd
env = append(env, fmt.Sprintf("%s=4", ENV_WG_UAPI_FD))
env = append(env, fmt.Sprintf("%s=1", ENV_WG_PROCESS_FOREGROUND))
files := [3]*os.File{}
- if os.Getenv("LOG_LEVEL") != "" && logLevel != LogLevelSilent {
+ if os.Getenv("LOG_LEVEL") != "" && logLevel != device.LogLevelSilent {
files[0], _ = os.Open(os.DevNull)
files[1] = os.Stdout
files[2] = os.Stderr
return
}
- device := NewDevice(tun, logger)
+ device := device.NewDevice(tun, logger)
logger.Info.Println("Device started")
errs := make(chan error)
term := make(chan os.Signal, 1)
- uapi, err := UAPIListen(interfaceName, fileUAPI)
+ uapi, err := ipc.UAPIListen(interfaceName, fileUAPI)
if err != nil {
logger.Error.Println("Failed to listen on uapi socket:", err)
os.Exit(ExitSetupFailed)
errs <- err
return
}
- go ipcHandle(device, conn)
+ go device.IpcHandle(conn)
}
}()
import (
"fmt"
+ "golang.zx2c4.com/wireguard/device"
+ "golang.zx2c4.com/wireguard/ipc"
"os"
"os/signal"
"syscall"
}
interfaceName := os.Args[1]
- logger := NewLogger(
- LogLevelDebug,
+ logger := device.NewLogger(
+ device.LogLevelDebug,
fmt.Sprintf("(%s) ", interfaceName),
)
logger.Info.Println("Starting wireguard-go version", WireGuardGoVersion)
os.Exit(ExitSetupFailed)
}
- device := NewDevice(tun, logger)
+ device := device.NewDevice(tun, logger)
device.Up()
logger.Info.Println("Device started")
- uapi, err := UAPIListen(interfaceName)
+ uapi, err := ipc.UAPIListen(interfaceName)
if err != nil {
logger.Error.Println("Failed to listen on uapi socket:", err)
os.Exit(ExitSetupFailed)
errs <- err
return
}
- go ipcHandle(device, conn)
+ go device.IpcHandle(conn)
}
}()
logger.Info.Println("UAPI listener started")
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
-package main
+package tun
import (
"bytes"
func (tun *NativeTun) GUID() windows.GUID {
return *(*windows.GUID)(tun.wt)
-}
\ No newline at end of file
+}