From 6dd7ad9747dff4c204a16782466a0b09a7919a15 Mon Sep 17 00:00:00 2001
From: Filippo Valsorda <hi@filippo.io>
Date: Sun, 20 May 2018 14:34:24 -0400
Subject: [PATCH] Adopt Go workspace best practices

Relative imports break when the project is built from within GOPATH,
where most Go developers prefer to develop:

    device.go:10:2: local import "./ratelimiter" in non-local package
    tun_darwin.go:10:2: local import "./rwcancel" in non-local package
    noise-protocol.go:10:2: local import "./tai64n" in non-local package
    cookie.go:10:2: local import "./xchacha20poly1305" in non-local package

On the other hand, the vendor folder is not supported outside of
GOPATH, so it's unclear how/if that ever worked.

Moreover, using go get instead of dep means that dependencies are not
pinned, and HEAD is used instead, which makes builds unreproducible.

This adds a Makefile that creates a local GOPATH with a symlink
trick, so developing outside GOPATH still works. Not only that, but
a system GOPATH is not required at all as long as the Makefile is used.
And if the project is cloned in the right place in GOPATH all standard
tools will work as expected.

So all workflows should work just as weel or better. The only new
requirement is dep, but that's unavoidable for reproducible builds.

Finally, run goimports on all files to format import statements.
---
 .gitignore                     |  1 +
 Gopkg.lock                     | 36 +++++++++++++++++++++++++++++++++++-
 Makefile                       | 14 ++++++++++++--
 README.md                      | 13 ++++++++++---
 conn.go                        |  3 ++-
 conn_linux.go                  |  5 +++--
 cookie.go                      |  7 ++++---
 device.go                      |  3 ++-
 generate-vendor.sh             | 20 --------------------
 kdf_test.go                    |  3 ++-
 noise-helpers.go               |  3 ++-
 noise-protocol.go              |  7 ++++---
 noise-types.go                 |  1 +
 receive.go                     |  7 ++++---
 rwcancel/rwcancel_unix.go      |  3 ++-
 send.go                        |  7 ++++---
 tun_darwin.go                  |  7 ++++---
 tun_linux.go                   |  7 ++++---
 tun_windows.go                 |  5 +++--
 uapi_darwin.go                 |  3 ++-
 uapi_linux.go                  |  5 +++--
 uapi_windows.go                |  3 ++-
 xchacha20poly1305/xchacha20.go |  1 +
 23 files changed, 107 insertions(+), 57 deletions(-)
 delete mode 100755 generate-vendor.sh

diff --git a/.gitignore b/.gitignore
index 431991b..d77756e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 wireguard-go
 vendor
+.GOPATH
diff --git a/Gopkg.lock b/Gopkg.lock
index 4945829..cca51d2 100644
--- a/Gopkg.lock
+++ b/Gopkg.lock
@@ -1,16 +1,50 @@
-# This was generated by ./generate-vendor.sh
+# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
+
+
+[[projects]]
+  name = "github.com/Microsoft/go-winio"
+  packages = ["."]
+  revision = "7da180ee92d8bd8bb8c37fc560e673e6557c392f"
+  version = "v0.4.7"
+
 [[projects]]
   branch = "master"
   name = "golang.org/x/crypto"
+  packages = [
+    "blake2s",
+    "chacha20poly1305",
+    "curve25519",
+    "internal/chacha20",
+    "poly1305"
+  ]
   revision = "1a580b3eff7814fc9b40602fd35256c63b50f491"
 
 [[projects]]
   branch = "master"
   name = "golang.org/x/net"
+  packages = [
+    "bpf",
+    "internal/iana",
+    "internal/socket",
+    "ipv4",
+    "ipv6"
+  ]
   revision = "2491c5de3490fced2f6cff376127c667efeed857"
 
 [[projects]]
   branch = "master"
   name = "golang.org/x/sys"
+  packages = [
+    "cpu",
+    "unix",
+    "windows",
+    "windows/registry"
+  ]
   revision = "7c87d13f8e835d2fb3a70a2912c811ed0c1d241b"
 
+[solve-meta]
+  analyzer-name = "dep"
+  analyzer-version = 1
+  inputs-digest = "4aa3076861343acd63ca1f40b2c67c0558cdb89ac5eab3fc85a066f51a578c9a"
+  solver-name = "gps-cdcl"
+  solver-version = 1
diff --git a/Makefile b/Makefile
index e1c53c5..cca970f 100644
--- a/Makefile
+++ b/Makefile
@@ -8,15 +8,25 @@ $(error Do not build this for Linux. Instead use the Linux kernel module. See wi
 endif
 endif
 
+IMPORT_PATH := git.zx2c4.com/wireguard-go
+
 all: wireguard-go
 
-wireguard-go: $(wildcard *.go) $(wildcard */*.go)
-	go build -v -o $@
+wireguard-go: $(wildcard *.go) $(wildcard */*.go) Gopkg.* .GOPATH/.ok
+	cd "$(PWD)/.GOPATH/src/$(IMPORT_PATH)" && GOPATH="$(PWD)/.GOPATH" dep ensure
+	GOPATH="$(PWD)/.GOPATH" go build -v $(IMPORT_PATH)
 
 install: wireguard-go
 	@install -v -d "$(DESTDIR)$(BINDIR)" && install -m 0755 -v wireguard-go "$(DESTDIR)$(BINDIR)/wireguard-go"
 
 clean:
 	rm -f wireguard-go
+	rm -rf bin .GOPATH
 
 .PHONY: clean install
+
+.GOPATH/.ok:
+	@mkdir -p ".GOPATH/src/$(IMPORT_PATH)"
+	@rmdir ".GOPATH/src/$(IMPORT_PATH)"
+	@ln -s ../../.. ".GOPATH/src/$(IMPORT_PATH)"
+	@touch $@
diff --git a/README.md b/README.md
index 499fcc5..76b7a87 100644
--- a/README.md
+++ b/README.md
@@ -44,16 +44,23 @@ Work in progress, but nothing yet to share.
 
 ## Building
 
-You can satisfy dependencies with either `go get -d -v` or `dep ensure -vendor-only`. Then run `make`. As this is a Go project, a `GOPATH` is required. For example, wireguard-go can be built with:
+Make sure you have Go and `dep` installed, then run `make`.
 
 ```
 $ git clone https://git.zx2c4.com/wireguard-go
 $ cd wireguard-go
-$ export GOPATH="$PWD/gopath"
-$ go get -d -v
 $ make
 ```
 
+Or, with a properly set up GOPATH, run `dep ensure`.
+
+```
+$ git clone https://git.zx2c4.com/wireguard-go "$(go env GOPATH)/src/git.zx2c4.com/wireguard-go"
+$ cd "$(go env GOPATH)/src/git.zx2c4.com/wireguard-go"
+$ dep ensure
+$ go build
+```
+
 ## License
 
     This program is free software; you can redistribute it and/or modify
diff --git a/conn.go b/conn.go
index ded7419..29d9ea1 100644
--- a/conn.go
+++ b/conn.go
@@ -8,9 +8,10 @@ package main
 
 import (
 	"errors"
+	"net"
+
 	"golang.org/x/net/ipv4"
 	"golang.org/x/net/ipv6"
-	"net"
 )
 
 const (
diff --git a/conn_linux.go b/conn_linux.go
index e5105e3..ba9a902 100644
--- a/conn_linux.go
+++ b/conn_linux.go
@@ -16,13 +16,14 @@
 package main
 
 import (
-	"./rwcancel"
 	"errors"
-	"golang.org/x/sys/unix"
 	"net"
 	"strconv"
 	"sync"
 	"unsafe"
+
+	"git.zx2c4.com/wireguard-go/rwcancel"
+	"golang.org/x/sys/unix"
 )
 
 type IPv4Source struct {
diff --git a/cookie.go b/cookie.go
index c39ad22..861d940 100644
--- a/cookie.go
+++ b/cookie.go
@@ -7,13 +7,14 @@
 package main
 
 import (
-	"./xchacha20poly1305"
 	"crypto/hmac"
 	"crypto/rand"
-	"golang.org/x/crypto/blake2s"
-	"golang.org/x/crypto/chacha20poly1305"
 	"sync"
 	"time"
+
+	"git.zx2c4.com/wireguard-go/xchacha20poly1305"
+	"golang.org/x/crypto/blake2s"
+	"golang.org/x/crypto/chacha20poly1305"
 )
 
 type CookieChecker struct {
diff --git a/device.go b/device.go
index 5f468b2..58d71ce 100644
--- a/device.go
+++ b/device.go
@@ -7,11 +7,12 @@
 package main
 
 import (
-	"./ratelimiter"
 	"runtime"
 	"sync"
 	"sync/atomic"
 	"time"
+
+	"git.zx2c4.com/wireguard-go/ratelimiter"
 )
 
 const (
diff --git a/generate-vendor.sh b/generate-vendor.sh
deleted file mode 100755
index 51d0fff..0000000
--- a/generate-vendor.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/bash
-
-echo "# This was generated by ./generate-vendor.sh" > Gopkg.lock
-echo "# This was generated by ./generate-vendor.sh" > Gopkg.toml
-
-while read -r package; do
-	cat >> Gopkg.lock <<-_EOF
-	[[projects]]
-	  branch = "master"
-	  name = "$package"
-	  revision = "$(< "$GOPATH/src/$package/.git/refs/heads/master")"
-
-	_EOF
-	cat >> Gopkg.toml <<-_EOF
-	[[constraint]]
-	  branch = "master"
-	  name = "$package"
-
-	_EOF
-done < <(sed -n 's/.*"\(golang.org\/x\/[^/]\+\)\/\?.*".*/\1/p' *.go */*.go | sort | uniq)
diff --git a/kdf_test.go b/kdf_test.go
index 897a5bd..5b2b681 100644
--- a/kdf_test.go
+++ b/kdf_test.go
@@ -8,8 +8,9 @@ package main
 
 import (
 	"encoding/hex"
-	"golang.org/x/crypto/blake2s"
 	"testing"
+
+	"golang.org/x/crypto/blake2s"
 )
 
 type KDFTest struct {
diff --git a/noise-helpers.go b/noise-helpers.go
index ce398c4..fabddca 100644
--- a/noise-helpers.go
+++ b/noise-helpers.go
@@ -10,9 +10,10 @@ import (
 	"crypto/hmac"
 	"crypto/rand"
 	"crypto/subtle"
+	"hash"
+
 	"golang.org/x/crypto/blake2s"
 	"golang.org/x/crypto/curve25519"
-	"hash"
 )
 
 /* KDF related functions.
diff --git a/noise-protocol.go b/noise-protocol.go
index c6a95d8..d367101 100644
--- a/noise-protocol.go
+++ b/noise-protocol.go
@@ -7,13 +7,14 @@
 package main
 
 import (
-	"./tai64n"
 	"errors"
+	"sync"
+	"time"
+
+	"git.zx2c4.com/wireguard-go/tai64n"
 	"golang.org/x/crypto/blake2s"
 	"golang.org/x/crypto/chacha20poly1305"
 	"golang.org/x/crypto/poly1305"
-	"sync"
-	"time"
 )
 
 const (
diff --git a/noise-types.go b/noise-types.go
index f4f5a27..4b185b8 100644
--- a/noise-types.go
+++ b/noise-types.go
@@ -10,6 +10,7 @@ import (
 	"crypto/subtle"
 	"encoding/hex"
 	"errors"
+
 	"golang.org/x/crypto/chacha20poly1305"
 )
 
diff --git a/receive.go b/receive.go
index 56e65e7..6d7e8e2 100644
--- a/receive.go
+++ b/receive.go
@@ -9,14 +9,15 @@ package main
 import (
 	"bytes"
 	"encoding/binary"
-	"golang.org/x/crypto/chacha20poly1305"
-	"golang.org/x/net/ipv4"
-	"golang.org/x/net/ipv6"
 	"net"
 	"strconv"
 	"sync"
 	"sync/atomic"
 	"time"
+
+	"golang.org/x/crypto/chacha20poly1305"
+	"golang.org/x/net/ipv4"
+	"golang.org/x/net/ipv6"
 )
 
 type QueueHandshakeElement struct {
diff --git a/rwcancel/rwcancel_unix.go b/rwcancel/rwcancel_unix.go
index 739a8c3..c08ca61 100644
--- a/rwcancel/rwcancel_unix.go
+++ b/rwcancel/rwcancel_unix.go
@@ -7,9 +7,10 @@ package rwcancel
 
 import (
 	"errors"
-	"golang.org/x/sys/unix"
 	"os"
 	"syscall"
+
+	"golang.org/x/sys/unix"
 )
 
 type RWCancel struct {
diff --git a/send.go b/send.go
index 299274d..7ea525f 100644
--- a/send.go
+++ b/send.go
@@ -9,13 +9,14 @@ package main
 import (
 	"bytes"
 	"encoding/binary"
-	"golang.org/x/crypto/chacha20poly1305"
-	"golang.org/x/net/ipv4"
-	"golang.org/x/net/ipv6"
 	"net"
 	"sync"
 	"sync/atomic"
 	"time"
+
+	"golang.org/x/crypto/chacha20poly1305"
+	"golang.org/x/net/ipv4"
+	"golang.org/x/net/ipv6"
 )
 
 /* Outbound flow
diff --git a/tun_darwin.go b/tun_darwin.go
index d2cdcd5..5c306b1 100644
--- a/tun_darwin.go
+++ b/tun_darwin.go
@@ -7,16 +7,17 @@
 package main
 
 import (
-	"./rwcancel"
 	"encoding/binary"
 	"errors"
 	"fmt"
-	"golang.org/x/net/ipv6"
-	"golang.org/x/sys/unix"
 	"io/ioutil"
 	"net"
 	"os"
 	"unsafe"
+
+	"git.zx2c4.com/wireguard-go/rwcancel"
+	"golang.org/x/net/ipv6"
+	"golang.org/x/sys/unix"
 )
 
 const utunControlName = "com.apple.net.utun_control"
diff --git a/tun_linux.go b/tun_linux.go
index f9a27d6..eb9b143 100644
--- a/tun_linux.go
+++ b/tun_linux.go
@@ -12,18 +12,19 @@ package main
  */
 
 import (
-	"./rwcancel"
 	"bytes"
 	"encoding/binary"
 	"errors"
 	"fmt"
-	"golang.org/x/net/ipv6"
-	"golang.org/x/sys/unix"
 	"net"
 	"os"
 	"strconv"
 	"time"
 	"unsafe"
+
+	"git.zx2c4.com/wireguard-go/rwcancel"
+	"golang.org/x/net/ipv6"
+	"golang.org/x/sys/unix"
 )
 
 const (
diff --git a/tun_windows.go b/tun_windows.go
index c13ca72..2233f98 100644
--- a/tun_windows.go
+++ b/tun_windows.go
@@ -10,13 +10,14 @@ import (
 	"encoding/binary"
 	"errors"
 	"fmt"
-	"golang.org/x/sys/windows"
-	"golang.org/x/sys/windows/registry"
 	"net"
 	"sync"
 	"syscall"
 	"time"
 	"unsafe"
+
+	"golang.org/x/sys/windows"
+	"golang.org/x/sys/windows/registry"
 )
 
 /* Relies on the OpenVPN TAP-Windows driver (NDIS 6 version)
diff --git a/uapi_darwin.go b/uapi_darwin.go
index 228021f..1c293c1 100644
--- a/uapi_darwin.go
+++ b/uapi_darwin.go
@@ -9,10 +9,11 @@ package main
 import (
 	"errors"
 	"fmt"
-	"golang.org/x/sys/unix"
 	"net"
 	"os"
 	"path"
+
+	"golang.org/x/sys/unix"
 )
 
 const (
diff --git a/uapi_linux.go b/uapi_linux.go
index 2b5b9a0..a3a5685 100644
--- a/uapi_linux.go
+++ b/uapi_linux.go
@@ -7,13 +7,14 @@
 package main
 
 import (
-	"./rwcancel"
 	"errors"
 	"fmt"
-	"golang.org/x/sys/unix"
 	"net"
 	"os"
 	"path"
+
+	"git.zx2c4.com/wireguard-go/rwcancel"
+	"golang.org/x/sys/unix"
 )
 
 const (
diff --git a/uapi_windows.go b/uapi_windows.go
index 41808f7..fca1d12 100644
--- a/uapi_windows.go
+++ b/uapi_windows.go
@@ -11,9 +11,10 @@ package main
 
 import (
 	"fmt"
+	"net"
+
 	"github.com/Microsoft/go-winio"
 	"golang.org/x/sys/windows"
-	"net"
 )
 
 const (
diff --git a/xchacha20poly1305/xchacha20.go b/xchacha20poly1305/xchacha20.go
index bd27f02..d82e975 100644
--- a/xchacha20poly1305/xchacha20.go
+++ b/xchacha20poly1305/xchacha20.go
@@ -8,6 +8,7 @@ package xchacha20poly1305
 
 import (
 	"encoding/binary"
+
 	"golang.org/x/crypto/chacha20poly1305"
 )
 
-- 
2.16.3

