From 69f0fe67b63d90e523a5a1241fb1b46c2e8dbe03 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 3 Mar 2019 04:04:41 +0100 Subject: [PATCH] global: begin modularization --- Makefile | 8 +-- allowedips.go => device/allowedips.go | 2 +- .../allowedips_rand_test.go | 2 +- .../allowedips_test.go | 2 +- bind_test.go => device/bind_test.go | 2 +- conn.go => device/conn.go | 4 +- conn_default.go => device/conn_default.go | 2 +- conn_linux.go => device/conn_linux.go | 2 +- constants.go => device/constants.go | 2 +- cookie.go => device/cookie.go | 2 +- cookie_test.go => device/cookie_test.go | 2 +- device.go => device/device.go | 2 +- device_test.go => device/device_test.go | 2 +- endpoint_test.go => device/endpoint_test.go | 2 +- indextable.go => device/indextable.go | 2 +- ip.go => device/ip.go | 2 +- kdf_test.go => device/kdf_test.go | 2 +- keypair.go => device/keypair.go | 2 +- logger.go => device/logger.go | 2 +- mark_default.go => device/mark_default.go | 2 +- mark_unix.go => device/mark_unix.go | 2 +- misc.go => device/misc.go | 2 +- noise-helpers.go => device/noise-helpers.go | 2 +- noise-protocol.go => device/noise-protocol.go | 2 +- noise-types.go => device/noise-types.go | 2 +- noise_test.go => device/noise_test.go | 2 +- peer.go => device/peer.go | 2 +- pools.go => device/pools.go | 2 +- queueconstants.go => device/queueconstants.go | 2 +- receive.go => device/receive.go | 2 +- send.go => device/send.go | 2 +- timers.go => device/timers.go | 2 +- tun.go => device/tun.go | 2 +- uapi.go => device/uapi.go | 53 ++++++++++--------- device/version.go | 3 ++ go.mod | 6 +-- go.sum | 13 ++--- uapi_bsd.go => ipc/uapi_bsd.go | 10 ++-- uapi_linux.go => ipc/uapi_linux.go | 10 ++-- uapi_windows.go => ipc/uapi_windows.go | 10 ++-- main.go | 32 +++++------ main_windows.go | 12 +++-- helper_test.go => tun/helper_test.go | 2 +- tun/tun_windows.go | 2 +- 44 files changed, 118 insertions(+), 109 deletions(-) rename allowedips.go => device/allowedips.go (99%) rename allowedips_rand_test.go => device/allowedips_rand_test.go (99%) rename allowedips_test.go => device/allowedips_test.go (99%) rename bind_test.go => device/bind_test.go (98%) rename conn.go => device/conn.go (99%) rename conn_default.go => device/conn_default.go (99%) rename conn_linux.go => device/conn_linux.go (99%) rename constants.go => device/constants.go (98%) rename cookie.go => device/cookie.go (99%) rename cookie_test.go => device/cookie_test.go (99%) rename device.go => device/device.go (99%) rename device_test.go => device/device_test.go (98%) rename endpoint_test.go => device/endpoint_test.go (98%) rename indextable.go => device/indextable.go (99%) rename ip.go => device/ip.go (96%) rename kdf_test.go => device/kdf_test.go (99%) rename keypair.go => device/keypair.go (98%) rename logger.go => device/logger.go (98%) rename mark_default.go => device/mark_default.go (93%) rename mark_unix.go => device/mark_unix.go (98%) rename misc.go => device/misc.go (97%) rename noise-helpers.go => device/noise-helpers.go (99%) rename noise-protocol.go => device/noise-protocol.go (99%) rename noise-types.go => device/noise-types.go (99%) rename noise_test.go => device/noise_test.go (99%) rename peer.go => device/peer.go (99%) rename pools.go => device/pools.go (99%) rename queueconstants.go => device/queueconstants.go (96%) rename receive.go => device/receive.go (99%) rename send.go => device/send.go (99%) rename timers.go => device/timers.go (99%) rename tun.go => device/tun.go (98%) rename uapi.go => device/uapi.go (87%) create mode 100644 device/version.go rename uapi_bsd.go => ipc/uapi_bsd.go (95%) rename uapi_linux.go => ipc/uapi_linux.go (94%) rename uapi_windows.go => ipc/uapi_windows.go (90%) rename helper_test.go => tun/helper_test.go (99%) diff --git a/Makefile b/Makefile index cff8a7f..69a8100 100644 --- a/Makefile +++ b/Makefile @@ -24,10 +24,10 @@ MAKEFLAGS += --no-print-directory 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) diff --git a/allowedips.go b/device/allowedips.go similarity index 99% rename from allowedips.go rename to device/allowedips.go index 2c4f601..efc27c0 100644 --- a/allowedips.go +++ b/device/allowedips.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "errors" diff --git a/allowedips_rand_test.go b/device/allowedips_rand_test.go similarity index 99% rename from allowedips_rand_test.go rename to device/allowedips_rand_test.go index 56a31c4..59c10f7 100644 --- a/allowedips_rand_test.go +++ b/device/allowedips_rand_test.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "math/rand" diff --git a/allowedips_test.go b/device/allowedips_test.go similarity index 99% rename from allowedips_test.go rename to device/allowedips_test.go index ca694ab..075ff06 100644 --- a/allowedips_test.go +++ b/device/allowedips_test.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "math/rand" diff --git a/bind_test.go b/device/bind_test.go similarity index 98% rename from bind_test.go rename to device/bind_test.go index c534646..0c2e2cf 100644 --- a/bind_test.go +++ b/device/bind_test.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import "errors" diff --git a/conn.go b/device/conn.go similarity index 99% rename from conn.go rename to device/conn.go index b19a9c2..2594680 100644 --- a/conn.go +++ b/device/conn.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "errors" @@ -177,4 +177,4 @@ func (device *Device) BindClose() error { err := unsafeCloseBind(device) device.net.Unlock() return err -} +} \ No newline at end of file diff --git a/conn_default.go b/device/conn_default.go similarity index 99% rename from conn_default.go rename to device/conn_default.go index 6f17de5..8a86719 100644 --- a/conn_default.go +++ b/device/conn_default.go @@ -5,7 +5,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "net" diff --git a/conn_linux.go b/device/conn_linux.go similarity index 99% rename from conn_linux.go rename to device/conn_linux.go index d3dbb98..49949d5 100644 --- a/conn_linux.go +++ b/device/conn_linux.go @@ -14,7 +14,7 @@ * So this code is remains platform dependent. */ -package main +package device import ( "errors" diff --git a/constants.go b/device/constants.go similarity index 98% rename from constants.go rename to device/constants.go index ab93e5f..27d910f 100644 --- a/constants.go +++ b/device/constants.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "time" diff --git a/cookie.go b/device/cookie.go similarity index 99% rename from cookie.go rename to device/cookie.go index c648bf1..2f21067 100644 --- a/cookie.go +++ b/device/cookie.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "crypto/hmac" diff --git a/cookie_test.go b/device/cookie_test.go similarity index 99% rename from cookie_test.go rename to device/cookie_test.go index 0586260..79a6a86 100644 --- a/cookie_test.go +++ b/device/cookie_test.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "testing" diff --git a/device.go b/device/device.go similarity index 99% rename from device.go rename to device/device.go index 18e1138..d6c96d6 100644 --- a/device.go +++ b/device/device.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "golang.zx2c4.com/wireguard/ratelimiter" diff --git a/device_test.go b/device/device_test.go similarity index 98% rename from device_test.go rename to device/device_test.go index df0ba69..db5a3c0 100644 --- a/device_test.go +++ b/device/device_test.go @@ -3,7 +3,7 @@ * 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 diff --git a/endpoint_test.go b/device/endpoint_test.go similarity index 98% rename from endpoint_test.go rename to device/endpoint_test.go index fe6677c..1896790 100644 --- a/endpoint_test.go +++ b/device/endpoint_test.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "math/rand" diff --git a/indextable.go b/device/indextable.go similarity index 99% rename from indextable.go rename to device/indextable.go index 046113c..4cba970 100644 --- a/indextable.go +++ b/device/indextable.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "crypto/rand" diff --git a/ip.go b/device/ip.go similarity index 96% rename from ip.go rename to device/ip.go index e2e0ff3..9d4fb74 100644 --- a/ip.go +++ b/device/ip.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "net" diff --git a/kdf_test.go b/device/kdf_test.go similarity index 99% rename from kdf_test.go rename to device/kdf_test.go index 3b9a8be..11ea8d5 100644 --- a/kdf_test.go +++ b/device/kdf_test.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "encoding/hex" diff --git a/keypair.go b/device/keypair.go similarity index 98% rename from keypair.go rename to device/keypair.go index af10a58..a9fbfce 100644 --- a/keypair.go +++ b/device/keypair.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "crypto/cipher" diff --git a/logger.go b/device/logger.go similarity index 98% rename from logger.go rename to device/logger.go index 00b1c7d..7c8b704 100644 --- a/logger.go +++ b/device/logger.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "io" diff --git a/mark_default.go b/device/mark_default.go similarity index 93% rename from mark_default.go rename to device/mark_default.go index 7149d69..76b1015 100644 --- a/mark_default.go +++ b/device/mark_default.go @@ -5,7 +5,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device func (bind *NativeBind) SetMark(mark uint32) error { return nil diff --git a/mark_unix.go b/device/mark_unix.go similarity index 98% rename from mark_unix.go rename to device/mark_unix.go index 0ae62b7..ee64cc9 100644 --- a/mark_unix.go +++ b/device/mark_unix.go @@ -5,7 +5,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "golang.org/x/sys/unix" diff --git a/misc.go b/device/misc.go similarity index 97% rename from misc.go rename to device/misc.go index 6786cb5..a38d1c1 100644 --- a/misc.go +++ b/device/misc.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "sync/atomic" diff --git a/noise-helpers.go b/device/noise-helpers.go similarity index 99% rename from noise-helpers.go rename to device/noise-helpers.go index af11f09..4b09bf3 100644 --- a/noise-helpers.go +++ b/device/noise-helpers.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "crypto/hmac" diff --git a/noise-protocol.go b/device/noise-protocol.go similarity index 99% rename from noise-protocol.go rename to device/noise-protocol.go index fb43413..73826e1 100644 --- a/noise-protocol.go +++ b/device/noise-protocol.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "errors" diff --git a/noise-types.go b/device/noise-types.go similarity index 99% rename from noise-types.go rename to device/noise-types.go index 902905e..82b12c1 100644 --- a/noise-types.go +++ b/device/noise-types.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "crypto/subtle" diff --git a/noise_test.go b/device/noise_test.go similarity index 99% rename from noise_test.go rename to device/noise_test.go index 116057a..6ba3f2e 100644 --- a/noise_test.go +++ b/device/noise_test.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "bytes" diff --git a/peer.go b/device/peer.go similarity index 99% rename from peer.go rename to device/peer.go index f021565..af3ef9d 100644 --- a/peer.go +++ b/device/peer.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "encoding/base64" diff --git a/pools.go b/device/pools.go similarity index 99% rename from pools.go rename to device/pools.go index 8a9af0b..98f4ef1 100644 --- a/pools.go +++ b/device/pools.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import "sync" diff --git a/queueconstants.go b/device/queueconstants.go similarity index 96% rename from queueconstants.go rename to device/queueconstants.go index 0dcdd33..3e94b7f 100644 --- a/queueconstants.go +++ b/device/queueconstants.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device /* Implementation specific constants */ diff --git a/receive.go b/device/receive.go similarity index 99% rename from receive.go rename to device/receive.go index fb848eb..5c837c1 100644 --- a/receive.go +++ b/device/receive.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "bytes" diff --git a/send.go b/device/send.go similarity index 99% rename from send.go rename to device/send.go index b7cac04..b4e23c7 100644 --- a/send.go +++ b/device/send.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "bytes" diff --git a/timers.go b/device/timers.go similarity index 99% rename from timers.go rename to device/timers.go index 9c16f13..5f28fcc 100644 --- a/timers.go +++ b/device/timers.go @@ -5,7 +5,7 @@ * This is based heavily on timers.c from the kernel implementation. */ -package main +package device import ( "math/rand" diff --git a/tun.go b/device/tun.go similarity index 98% rename from tun.go rename to device/tun.go index 52bfb68..bc5f1f1 100644 --- a/tun.go +++ b/device/tun.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "golang.zx2c4.com/wireguard/tun" diff --git a/uapi.go b/device/uapi.go similarity index 87% rename from uapi.go rename to device/uapi.go index 4a370b3..5c65917 100644 --- a/uapi.go +++ b/device/uapi.go @@ -3,11 +3,12 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "bufio" "fmt" + "golang.zx2c4.com/wireguard/ipc" "io" "net" "strconv" @@ -28,7 +29,7 @@ func (s *IPCError) ErrorCode() int64 { 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") @@ -101,14 +102,14 @@ func ipcGetOperation(device *Device, socket *bufio.Writer) *IPCError { 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 @@ -128,7 +129,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { } parts := strings.Split(line, "=") if len(parts) != 2 { - return &IPCError{ipcErrorProtocol} + return &IPCError{ipc.IpcErrorProtocol} } key := parts[0] value := parts[1] @@ -143,7 +144,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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) @@ -155,7 +156,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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 @@ -168,7 +169,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if err := device.BindUpdate(); err != nil { logError.Println("Failed to set listen_port:", err) - return &IPCError{ipcErrorPortInUse} + return &IPCError{ipc.IpcErrorPortInUse} } case "fwmark": @@ -185,14 +186,14 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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": @@ -203,14 +204,14 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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} } } @@ -225,7 +226,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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 @@ -244,7 +245,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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") } @@ -255,7 +256,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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") @@ -276,7 +277,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if err != nil { logError.Println("Failed to set preshared key:", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } case "endpoint": @@ -298,7 +299,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if err != nil { logError.Println("Failed to set endpoint:", value) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } case "persistent_keepalive_interval": @@ -310,7 +311,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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 @@ -321,7 +322,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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() @@ -334,7 +335,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if value != "true" { logError.Println("Failed to replace allowedips, invalid value:", value) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } if dummy { @@ -350,7 +351,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { _, 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 { @@ -364,12 +365,12 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { 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} } } } @@ -377,7 +378,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { return nil } -func ipcHandle(device *Device, socket net.Conn) { +func (device *Device) IpcHandle(socket net.Conn) { // create buffered read/writer @@ -403,11 +404,11 @@ func ipcHandle(device *Device, socket net.Conn) { 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) diff --git a/device/version.go b/device/version.go new file mode 100644 index 0000000..9077cdc --- /dev/null +++ b/device/version.go @@ -0,0 +1,3 @@ +package device + +const WireGuardGoVersion = "0.0.20181222" diff --git a/go.mod b/go.mod index cfff5b6..49076a6 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module golang.zx2c4.com/wireguard 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 ) diff --git a/go.sum b/go.sum index c1adf80..76c5f08 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,9 @@ 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= diff --git a/uapi_bsd.go b/ipc/uapi_bsd.go similarity index 95% rename from uapi_bsd.go rename to ipc/uapi_bsd.go index d75f4f2..f66c386 100644 --- a/uapi_bsd.go +++ b/ipc/uapi_bsd.go @@ -5,7 +5,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package ipc import ( "errors" @@ -20,10 +20,10 @@ import ( 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" ) diff --git a/uapi_linux.go b/ipc/uapi_linux.go similarity index 94% rename from uapi_linux.go rename to ipc/uapi_linux.go index d4b89fc..8af3d8c 100644 --- a/uapi_linux.go +++ b/ipc/uapi_linux.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package ipc import ( "errors" @@ -18,10 +18,10 @@ import ( 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" ) diff --git a/uapi_windows.go b/ipc/uapi_windows.go similarity index 90% rename from uapi_windows.go rename to ipc/uapi_windows.go index 64917f5..209d0d2 100644 --- a/uapi_windows.go +++ b/ipc/uapi_windows.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package ipc import ( "github.com/Microsoft/go-winio" @@ -12,10 +12,10 @@ import ( //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 { diff --git a/main.go b/main.go index 08f8cc6..a3a04b8 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ package main import ( "fmt" + "golang.zx2c4.com/wireguard/device" + "golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/tun" "os" "os/signal" @@ -76,7 +78,7 @@ func warning() { 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 .\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 .\n", device.WireGuardGoVersion, runtime.GOOS, runtime.GOARCH) return } @@ -119,15 +121,15 @@ func main() { 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) @@ -135,7 +137,7 @@ func main() { 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 @@ -151,7 +153,7 @@ func main() { } file := os.NewFile(uintptr(fd), "") - return tun.CreateTUNFromFile(file, DefaultMTU) + return tun.CreateTUNFromFile(file, device.DefaultMTU) }() if err == nil { @@ -161,12 +163,12 @@ func main() { } } - 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") @@ -180,7 +182,7 @@ func main() { 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 @@ -206,7 +208,7 @@ func main() { 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 @@ -246,14 +248,14 @@ func main() { 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) @@ -266,7 +268,7 @@ func main() { errs <- err return } - go ipcHandle(device, conn) + go device.IpcHandle(conn) } }() diff --git a/main_windows.go b/main_windows.go index 7104a20..39cdead 100644 --- a/main_windows.go +++ b/main_windows.go @@ -7,6 +7,8 @@ package main import ( "fmt" + "golang.zx2c4.com/wireguard/device" + "golang.zx2c4.com/wireguard/ipc" "os" "os/signal" "syscall" @@ -25,8 +27,8 @@ func main() { } interfaceName := os.Args[1] - logger := NewLogger( - LogLevelDebug, + logger := device.NewLogger( + device.LogLevelDebug, fmt.Sprintf("(%s) ", interfaceName), ) logger.Info.Println("Starting wireguard-go version", WireGuardGoVersion) @@ -43,11 +45,11 @@ func main() { 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) @@ -63,7 +65,7 @@ func main() { errs <- err return } - go ipcHandle(device, conn) + go device.IpcHandle(conn) } }() logger.Info.Println("UAPI listener started") diff --git a/helper_test.go b/tun/helper_test.go similarity index 99% rename from helper_test.go rename to tun/helper_test.go index 3705c97..3e86fc8 100644 --- a/helper_test.go +++ b/tun/helper_test.go @@ -3,7 +3,7 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package tun import ( "bytes" diff --git a/tun/tun_windows.go b/tun/tun_windows.go index 94efe48..d767d79 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -353,4 +353,4 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) { func (tun *NativeTun) GUID() windows.GUID { return *(*windows.GUID)(tun.wt) -} \ No newline at end of file +}