global: begin modularization

This commit is contained in:
Jason A. Donenfeld 2019-03-03 04:04:41 +01:00
parent d435be35ca
commit 69f0fe67b6
44 changed files with 118 additions and 109 deletions

View file

@ -24,10 +24,10 @@ MAKEFLAGS += --no-print-directory
generate-version-and-build: generate-version-and-build:
@export GIT_CEILING_DIRECTORIES="$(realpath $(CURDIR)/..)" && \ @export GIT_CEILING_DIRECTORIES="$(realpath $(CURDIR)/..)" && \
tag="$$(git describe --dirty 2>/dev/null)" && \ tag="$$(git describe --dirty 2>/dev/null)" && \
ver="$$(printf 'package main\nconst WireGuardGoVersion = "%s"\n' "$$tag")" && \ ver="$$(printf 'package device\nconst WireGuardGoVersion = "%s"\n' "$$tag")" && \
[ "$$(cat version.go 2>/dev/null)" != "$$ver" ] && \ [ "$$(cat device/version.go 2>/dev/null)" != "$$ver" ] && \
echo "$$ver" > version.go && \ echo "$$ver" > device/version.go && \
git update-index --assume-unchanged version.go || true git update-index --assume-unchanged device/version.go || true
@$(MAKE) wireguard-go @$(MAKE) wireguard-go
wireguard-go: $(wildcard *.go) $(wildcard */*.go) wireguard-go: $(wildcard *.go) $(wildcard */*.go)

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"errors" "errors"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"math/rand" "math/rand"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"math/rand" "math/rand"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import "errors" import "errors"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"errors" "errors"

View file

@ -5,7 +5,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"net" "net"

View file

@ -14,7 +14,7 @@
* So this code is remains platform dependent. * So this code is remains platform dependent.
*/ */
package main package device
import ( import (
"errors" "errors"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"time" "time"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"crypto/hmac" "crypto/hmac"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"testing" "testing"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"golang.zx2c4.com/wireguard/ratelimiter" "golang.zx2c4.com/wireguard/ratelimiter"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
/* Create two device instances and simulate full WireGuard interaction /* Create two device instances and simulate full WireGuard interaction
* without network dependencies * without network dependencies

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"math/rand" "math/rand"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"crypto/rand" "crypto/rand"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"net" "net"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"encoding/hex" "encoding/hex"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"crypto/cipher" "crypto/cipher"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"io" "io"

View file

@ -5,7 +5,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
func (bind *NativeBind) SetMark(mark uint32) error { func (bind *NativeBind) SetMark(mark uint32) error {
return nil return nil

View file

@ -5,7 +5,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"sync/atomic" "sync/atomic"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"crypto/hmac" "crypto/hmac"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"errors" "errors"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"crypto/subtle" "crypto/subtle"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"bytes" "bytes"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"encoding/base64" "encoding/base64"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import "sync" import "sync"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
/* Implementation specific constants */ /* Implementation specific constants */

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"bytes" "bytes"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"bytes" "bytes"

View file

@ -5,7 +5,7 @@
* This is based heavily on timers.c from the kernel implementation. * This is based heavily on timers.c from the kernel implementation.
*/ */
package main package device
import ( import (
"math/rand" "math/rand"

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun"

View file

@ -3,11 +3,12 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package device
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"golang.zx2c4.com/wireguard/ipc"
"io" "io"
"net" "net"
"strconv" "strconv"
@ -28,7 +29,7 @@ func (s *IPCError) ErrorCode() int64 {
return s.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") device.log.Debug.Println("UAPI: Processing get operation")
@ -101,14 +102,14 @@ func ipcGetOperation(device *Device, socket *bufio.Writer) *IPCError {
for _, line := range lines { for _, line := range lines {
_, err := socket.WriteString(line + "\n") _, err := socket.WriteString(line + "\n")
if err != nil { if err != nil {
return &IPCError{ipcErrorIO} return &IPCError{ipc.IpcErrorIO}
} }
} }
return nil return nil
} }
func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { func (device *Device) IpcSetOperation(socket *bufio.Reader) *IPCError {
scanner := bufio.NewScanner(socket) scanner := bufio.NewScanner(socket)
logError := device.log.Error logError := device.log.Error
logDebug := device.log.Debug logDebug := device.log.Debug
@ -128,7 +129,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
} }
parts := strings.Split(line, "=") parts := strings.Split(line, "=")
if len(parts) != 2 { if len(parts) != 2 {
return &IPCError{ipcErrorProtocol} return &IPCError{ipc.IpcErrorProtocol}
} }
key := parts[0] key := parts[0]
value := parts[1] value := parts[1]
@ -143,7 +144,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
err := sk.FromHex(value) err := sk.FromHex(value)
if err != nil { if err != nil {
logError.Println("Failed to set private_key:", err) logError.Println("Failed to set private_key:", err)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
logDebug.Println("UAPI: Updating private key") logDebug.Println("UAPI: Updating private key")
device.SetPrivateKey(sk) device.SetPrivateKey(sk)
@ -155,7 +156,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
port, err := strconv.ParseUint(value, 10, 16) port, err := strconv.ParseUint(value, 10, 16)
if err != nil { if err != nil {
logError.Println("Failed to parse listen_port:", err) logError.Println("Failed to parse listen_port:", err)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
// update port and rebind // update port and rebind
@ -168,7 +169,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
if err := device.BindUpdate(); err != nil { if err := device.BindUpdate(); err != nil {
logError.Println("Failed to set listen_port:", err) logError.Println("Failed to set listen_port:", err)
return &IPCError{ipcErrorPortInUse} return &IPCError{ipc.IpcErrorPortInUse}
} }
case "fwmark": case "fwmark":
@ -185,14 +186,14 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
if err != nil { if err != nil {
logError.Println("Invalid fwmark", err) logError.Println("Invalid fwmark", err)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
logDebug.Println("UAPI: Updating fwmark") logDebug.Println("UAPI: Updating fwmark")
if err := device.BindSetMark(uint32(fwmark)); err != nil { if err := device.BindSetMark(uint32(fwmark)); err != nil {
logError.Println("Failed to update fwmark:", err) logError.Println("Failed to update fwmark:", err)
return &IPCError{ipcErrorPortInUse} return &IPCError{ipc.IpcErrorPortInUse}
} }
case "public_key": case "public_key":
@ -203,14 +204,14 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
case "replace_peers": case "replace_peers":
if value != "true" { if value != "true" {
logError.Println("Failed to set replace_peers, invalid value:", value) logError.Println("Failed to set replace_peers, invalid value:", value)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
logDebug.Println("UAPI: Removing all peers") logDebug.Println("UAPI: Removing all peers")
device.RemoveAllPeers() device.RemoveAllPeers()
default: default:
logError.Println("Invalid UAPI device key:", key) 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) err := publicKey.FromHex(value)
if err != nil { if err != nil {
logError.Println("Failed to get peer by public key:", err) logError.Println("Failed to get peer by public key:", err)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
// ignore peer with public key of device // ignore peer with public key of device
@ -244,7 +245,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
peer, err = device.NewPeer(publicKey) peer, err = device.NewPeer(publicKey)
if err != nil { if err != nil {
logError.Println("Failed to create new peer:", err) logError.Println("Failed to create new peer:", err)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
logDebug.Println(peer, "- UAPI: Created") logDebug.Println(peer, "- UAPI: Created")
} }
@ -255,7 +256,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
if value != "true" { if value != "true" {
logError.Println("Failed to set remove, invalid value:", value) logError.Println("Failed to set remove, invalid value:", value)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
if !dummy { if !dummy {
logDebug.Println(peer, "- UAPI: Removing") logDebug.Println(peer, "- UAPI: Removing")
@ -276,7 +277,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
if err != nil { if err != nil {
logError.Println("Failed to set preshared key:", err) logError.Println("Failed to set preshared key:", err)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
case "endpoint": case "endpoint":
@ -298,7 +299,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
if err != nil { if err != nil {
logError.Println("Failed to set endpoint:", value) logError.Println("Failed to set endpoint:", value)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
case "persistent_keepalive_interval": case "persistent_keepalive_interval":
@ -310,7 +311,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
secs, err := strconv.ParseUint(value, 10, 16) secs, err := strconv.ParseUint(value, 10, 16)
if err != nil { if err != nil {
logError.Println("Failed to set persistent keepalive interval:", err) logError.Println("Failed to set persistent keepalive interval:", err)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
old := peer.persistentKeepaliveInterval old := peer.persistentKeepaliveInterval
@ -321,7 +322,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
if old == 0 && secs != 0 { if old == 0 && secs != 0 {
if err != nil { if err != nil {
logError.Println("Failed to get tun device status:", err) logError.Println("Failed to get tun device status:", err)
return &IPCError{ipcErrorIO} return &IPCError{ipc.IpcErrorIO}
} }
if device.isUp.Get() && !dummy { if device.isUp.Get() && !dummy {
peer.SendKeepalive() peer.SendKeepalive()
@ -334,7 +335,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
if value != "true" { if value != "true" {
logError.Println("Failed to replace allowedips, invalid value:", value) logError.Println("Failed to replace allowedips, invalid value:", value)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
if dummy { if dummy {
@ -350,7 +351,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
_, network, err := net.ParseCIDR(value) _, network, err := net.ParseCIDR(value)
if err != nil { if err != nil {
logError.Println("Failed to set allowed ip:", err) logError.Println("Failed to set allowed ip:", err)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
if dummy { if dummy {
@ -364,12 +365,12 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError {
if value != "1" { if value != "1" {
logError.Println("Invalid protocol version:", value) logError.Println("Invalid protocol version:", value)
return &IPCError{ipcErrorInvalid} return &IPCError{ipc.IpcErrorInvalid}
} }
default: default:
logError.Println("Invalid UAPI peer key:", key) 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 return nil
} }
func ipcHandle(device *Device, socket net.Conn) { func (device *Device) IpcHandle(socket net.Conn) {
// create buffered read/writer // create buffered read/writer
@ -403,11 +404,11 @@ func ipcHandle(device *Device, socket net.Conn) {
switch op { switch op {
case "set=1\n": case "set=1\n":
device.log.Debug.Println("UAPI: Set operation") device.log.Debug.Println("UAPI: Set operation")
status = ipcSetOperation(device, buffered.Reader) status = device.IpcSetOperation(buffered.Reader)
case "get=1\n": case "get=1\n":
device.log.Debug.Println("UAPI: Get operation") device.log.Debug.Println("UAPI: Get operation")
status = ipcGetOperation(device, buffered.Writer) status = device.IpcGetOperation(buffered.Writer)
default: default:
device.log.Error.Println("Invalid UAPI operation:", op) device.log.Error.Println("Invalid UAPI operation:", op)

3
device/version.go Normal file
View file

@ -0,0 +1,3 @@
package device
const WireGuardGoVersion = "0.0.20181222"

6
go.mod
View file

@ -2,7 +2,7 @@ module golang.zx2c4.com/wireguard
require ( require (
github.com/Microsoft/go-winio v0.4.11 github.com/Microsoft/go-winio v0.4.11
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95
golang.org/x/sys v0.0.0-20190213121743-983097b1a8a3 golang.org/x/sys v0.0.0-20190302025703-b6889370fb10
) )

13
go.sum
View file

@ -1,8 +1,9 @@
github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q= github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA= 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-20190228161510-8dd112bcdc25 h1:jsG6UpNLt9iAsb0S2AGW28DveNzzgmbXR+ENoPjUeIU=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd h1:HuTn7WObtcDo9uEEU7rEqL0jYthdXAmZ6PP+meazmaU= golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95 h1:fY7Dsw114eJN4boqzVSbpVHO6rTdhq6/GnXeu+PKnzU=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95/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-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190213121743-983097b1a8a3/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=

View file

@ -5,7 +5,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package ipc
import ( import (
"errors" "errors"
@ -20,10 +20,10 @@ import (
var socketDirectory = "/var/run/wireguard" var socketDirectory = "/var/run/wireguard"
const ( const (
ipcErrorIO = -int64(unix.EIO) IpcErrorIO = -int64(unix.EIO)
ipcErrorProtocol = -int64(unix.EPROTO) IpcErrorProtocol = -int64(unix.EPROTO)
ipcErrorInvalid = -int64(unix.EINVAL) IpcErrorInvalid = -int64(unix.EINVAL)
ipcErrorPortInUse = -int64(unix.EADDRINUSE) IpcErrorPortInUse = -int64(unix.EADDRINUSE)
socketName = "%s.sock" socketName = "%s.sock"
) )

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package ipc
import ( import (
"errors" "errors"
@ -18,10 +18,10 @@ import (
var socketDirectory = "/var/run/wireguard" var socketDirectory = "/var/run/wireguard"
const ( const (
ipcErrorIO = -int64(unix.EIO) IpcErrorIO = -int64(unix.EIO)
ipcErrorProtocol = -int64(unix.EPROTO) IpcErrorProtocol = -int64(unix.EPROTO)
ipcErrorInvalid = -int64(unix.EINVAL) IpcErrorInvalid = -int64(unix.EINVAL)
ipcErrorPortInUse = -int64(unix.EADDRINUSE) IpcErrorPortInUse = -int64(unix.EADDRINUSE)
socketName = "%s.sock" socketName = "%s.sock"
) )

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package ipc
import ( import (
"github.com/Microsoft/go-winio" "github.com/Microsoft/go-winio"
@ -12,10 +12,10 @@ import (
//TODO: replace these with actual standard windows error numbers from the win package //TODO: replace these with actual standard windows error numbers from the win package
const ( const (
ipcErrorIO = -int64(5) IpcErrorIO = -int64(5)
ipcErrorProtocol = -int64(71) IpcErrorProtocol = -int64(71)
ipcErrorInvalid = -int64(22) IpcErrorInvalid = -int64(22)
ipcErrorPortInUse = -int64(98) IpcErrorPortInUse = -int64(98)
) )
type UAPIListener struct { type UAPIListener struct {

32
main.go
View file

@ -9,6 +9,8 @@ package main
import ( import (
"fmt" "fmt"
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/ipc"
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun"
"os" "os"
"os/signal" "os/signal"
@ -76,7 +78,7 @@ func warning() {
func main() { func main() {
if len(os.Args) == 2 && os.Args[1] == "--version" { 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 return
} }
@ -119,15 +121,15 @@ func main() {
logLevel := func() int { logLevel := func() int {
switch os.Getenv("LOG_LEVEL") { switch os.Getenv("LOG_LEVEL") {
case "debug": case "debug":
return LogLevelDebug return device.LogLevelDebug
case "info": case "info":
return LogLevelInfo return device.LogLevelInfo
case "error": case "error":
return LogLevelError return device.LogLevelError
case "silent": case "silent":
return LogLevelSilent return device.LogLevelSilent
} }
return LogLevelInfo return device.LogLevelInfo
}() }()
// open TUN device (or use supplied fd) // open TUN device (or use supplied fd)
@ -135,7 +137,7 @@ func main() {
tun, err := func() (tun.TUNDevice, error) { tun, err := func() (tun.TUNDevice, error) {
tunFdStr := os.Getenv(ENV_WG_TUN_FD) tunFdStr := os.Getenv(ENV_WG_TUN_FD)
if tunFdStr == "" { if tunFdStr == "" {
return tun.CreateTUN(interfaceName, DefaultMTU) return tun.CreateTUN(interfaceName, device.DefaultMTU)
} }
// construct tun device from supplied fd // construct tun device from supplied fd
@ -151,7 +153,7 @@ func main() {
} }
file := os.NewFile(uintptr(fd), "") file := os.NewFile(uintptr(fd), "")
return tun.CreateTUNFromFile(file, DefaultMTU) return tun.CreateTUNFromFile(file, device.DefaultMTU)
}() }()
if err == nil { if err == nil {
@ -161,12 +163,12 @@ func main() {
} }
} }
logger := NewLogger( logger := device.NewLogger(
logLevel, logLevel,
fmt.Sprintf("(%s) ", interfaceName), 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") logger.Debug.Println("Debug log enabled")
@ -180,7 +182,7 @@ func main() {
fileUAPI, err := func() (*os.File, error) { fileUAPI, err := func() (*os.File, error) {
uapiFdStr := os.Getenv(ENV_WG_UAPI_FD) uapiFdStr := os.Getenv(ENV_WG_UAPI_FD)
if uapiFdStr == "" { if uapiFdStr == "" {
return UAPIOpen(interfaceName) return ipc.UAPIOpen(interfaceName)
} }
// use supplied fd // 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=4", ENV_WG_UAPI_FD))
env = append(env, fmt.Sprintf("%s=1", ENV_WG_PROCESS_FOREGROUND)) env = append(env, fmt.Sprintf("%s=1", ENV_WG_PROCESS_FOREGROUND))
files := [3]*os.File{} 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[0], _ = os.Open(os.DevNull)
files[1] = os.Stdout files[1] = os.Stdout
files[2] = os.Stderr files[2] = os.Stderr
@ -246,14 +248,14 @@ func main() {
return return
} }
device := NewDevice(tun, logger) device := device.NewDevice(tun, logger)
logger.Info.Println("Device started") logger.Info.Println("Device started")
errs := make(chan error) errs := make(chan error)
term := make(chan os.Signal, 1) term := make(chan os.Signal, 1)
uapi, err := UAPIListen(interfaceName, fileUAPI) uapi, err := ipc.UAPIListen(interfaceName, fileUAPI)
if err != nil { if err != nil {
logger.Error.Println("Failed to listen on uapi socket:", err) logger.Error.Println("Failed to listen on uapi socket:", err)
os.Exit(ExitSetupFailed) os.Exit(ExitSetupFailed)
@ -266,7 +268,7 @@ func main() {
errs <- err errs <- err
return return
} }
go ipcHandle(device, conn) go device.IpcHandle(conn)
} }
}() }()

View file

@ -7,6 +7,8 @@ package main
import ( import (
"fmt" "fmt"
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/ipc"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@ -25,8 +27,8 @@ func main() {
} }
interfaceName := os.Args[1] interfaceName := os.Args[1]
logger := NewLogger( logger := device.NewLogger(
LogLevelDebug, device.LogLevelDebug,
fmt.Sprintf("(%s) ", interfaceName), fmt.Sprintf("(%s) ", interfaceName),
) )
logger.Info.Println("Starting wireguard-go version", WireGuardGoVersion) logger.Info.Println("Starting wireguard-go version", WireGuardGoVersion)
@ -43,11 +45,11 @@ func main() {
os.Exit(ExitSetupFailed) os.Exit(ExitSetupFailed)
} }
device := NewDevice(tun, logger) device := device.NewDevice(tun, logger)
device.Up() device.Up()
logger.Info.Println("Device started") logger.Info.Println("Device started")
uapi, err := UAPIListen(interfaceName) uapi, err := ipc.UAPIListen(interfaceName)
if err != nil { if err != nil {
logger.Error.Println("Failed to listen on uapi socket:", err) logger.Error.Println("Failed to listen on uapi socket:", err)
os.Exit(ExitSetupFailed) os.Exit(ExitSetupFailed)
@ -63,7 +65,7 @@ func main() {
errs <- err errs <- err
return return
} }
go ipcHandle(device, conn) go device.IpcHandle(conn)
} }
}() }()
logger.Info.Println("UAPI listener started") logger.Info.Println("UAPI listener started")

View file

@ -3,7 +3,7 @@
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/ */
package main package tun
import ( import (
"bytes" "bytes"