Compare commits
6 Commits
0.0.201810
...
0.0.201810
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
738d027f0b | ||
|
|
60848b9c72 | ||
|
|
2e772194cf | ||
|
|
85b2378a07 | ||
|
|
fddb949002 | ||
|
|
5d6083df7e |
42
Gopkg.lock
generated
42
Gopkg.lock
generated
@@ -1,42 +0,0 @@
|
|||||||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
|
||||||
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
branch = "master"
|
|
||||||
name = "golang.org/x/crypto"
|
|
||||||
packages = [
|
|
||||||
"blake2s",
|
|
||||||
"chacha20poly1305",
|
|
||||||
"curve25519",
|
|
||||||
"internal/chacha20",
|
|
||||||
"poly1305"
|
|
||||||
]
|
|
||||||
revision = "ab813273cd59e1333f7ae7bff5d027d4aadf528c"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
branch = "master"
|
|
||||||
name = "golang.org/x/net"
|
|
||||||
packages = [
|
|
||||||
"bpf",
|
|
||||||
"internal/iana",
|
|
||||||
"internal/socket",
|
|
||||||
"ipv4",
|
|
||||||
"ipv6"
|
|
||||||
]
|
|
||||||
revision = "dfa909b99c79129e1100513e5cd36307665e5723"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
branch = "master"
|
|
||||||
name = "golang.org/x/sys"
|
|
||||||
packages = [
|
|
||||||
"cpu",
|
|
||||||
"unix"
|
|
||||||
]
|
|
||||||
revision = "c11f84a56e43e20a78cee75a7c034031ecf57d1f"
|
|
||||||
|
|
||||||
[solve-meta]
|
|
||||||
analyzer-name = "dep"
|
|
||||||
analyzer-version = 1
|
|
||||||
inputs-digest = "d85ae9d2b4afafc3d7535505c46368cbbbec350cf876616302c1bcf44f6ec103"
|
|
||||||
solver-name = "gps-cdcl"
|
|
||||||
solver-version = 1
|
|
||||||
15
Gopkg.toml
15
Gopkg.toml
@@ -1,15 +0,0 @@
|
|||||||
[[constraint]]
|
|
||||||
branch = "master"
|
|
||||||
name = "golang.org/x/crypto"
|
|
||||||
|
|
||||||
[[constraint]]
|
|
||||||
branch = "master"
|
|
||||||
name = "golang.org/x/net"
|
|
||||||
|
|
||||||
[[constraint]]
|
|
||||||
branch = "master"
|
|
||||||
name = "golang.org/x/sys"
|
|
||||||
|
|
||||||
[prune]
|
|
||||||
go-tests = true
|
|
||||||
unused-packages = true
|
|
||||||
55
Makefile
55
Makefile
@@ -1,51 +1,42 @@
|
|||||||
PREFIX ?= /usr
|
PREFIX ?= /usr
|
||||||
DESTDIR ?=
|
DESTDIR ?=
|
||||||
BINDIR ?= $(PREFIX)/bin
|
BINDIR ?= $(PREFIX)/bin
|
||||||
|
export GOPATH ?= $(CURDIR)/.gopath
|
||||||
|
export GO111MODULE := on
|
||||||
|
|
||||||
ifeq ($(shell go env GOOS),linux)
|
all: generate-version-and-build
|
||||||
ifeq ($(wildcard .git),)
|
|
||||||
|
ifeq ($(shell go env GOOS)|$(wildcard .git),linux|)
|
||||||
$(error Do not build this for Linux. Instead use the Linux kernel module. See wireguard.com/install/ for more info.)
|
$(error Do not build this for Linux. Instead use the Linux kernel module. See wireguard.com/install/ for more info.)
|
||||||
else
|
else
|
||||||
$(shell printf 'package main\nconst UseTheKernelModuleInstead = 0xdeadbabe\n' > ireallywantobuildon_linux.go)
|
ireallywantobuildon_linux.go:
|
||||||
endif
|
@printf "WARNING: This software is meant for use on non-Linux\nsystems. For Linux, please use the kernel module\ninstead. See wireguard.com/install/ for more info.\n\n" >&2
|
||||||
|
@printf 'package main\nconst UseTheKernelModuleInstead = 0xdeadbabe\n' > "$@"
|
||||||
|
clean-ireallywantobuildon_linux.go:
|
||||||
|
@rm -f ireallywantobuildon_linux.go
|
||||||
|
.PHONY: clean-ireallywantobuildon_linux.go
|
||||||
|
clean: clean-ireallywantobuildon_linux.go
|
||||||
|
wireguard-go: ireallywantobuildon_linux.go
|
||||||
endif
|
endif
|
||||||
|
|
||||||
all: wireguard-go
|
MAKEFLAGS += --no-print-directory
|
||||||
|
|
||||||
export GOPATH := $(CURDIR)/.gopath
|
generate-version-and-build:
|
||||||
export PATH := $(PATH):$(CURDIR)/.gopath/bin
|
|
||||||
GO_IMPORT_PATH := git.zx2c4.com/wireguard-go
|
|
||||||
|
|
||||||
version.go:
|
|
||||||
@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 main\nconst WireGuardGoVersion = "%s"\n' "$$tag")" && \
|
||||||
[ "$$(cat $@ 2>/dev/null)" != "$$ver" ] && \
|
[ "$$(cat version.go 2>/dev/null)" != "$$ver" ] && \
|
||||||
echo "$$ver" > $@ && \
|
echo "$$ver" > version.go && \
|
||||||
git update-index --assume-unchanged $@ || true
|
git update-index --assume-unchanged version.go || true
|
||||||
|
@$(MAKE) wireguard-go
|
||||||
|
|
||||||
.gopath/.created:
|
wireguard-go: $(wildcard *.go) $(wildcard */*.go)
|
||||||
rm -rf .gopath
|
go build -v -o "$@"
|
||||||
mkdir -p $(dir .gopath/src/$(GO_IMPORT_PATH))
|
|
||||||
ln -s ../../.. .gopath/src/$(GO_IMPORT_PATH)
|
|
||||||
touch $@
|
|
||||||
|
|
||||||
vendor/.created: Gopkg.toml Gopkg.lock | .gopath/.created
|
|
||||||
command -v dep >/dev/null || go get -v github.com/golang/dep/cmd/dep
|
|
||||||
export PWD; cd .gopath/src/$(GO_IMPORT_PATH) && dep ensure -vendor-only -v
|
|
||||||
touch $@
|
|
||||||
|
|
||||||
wireguard-go: $(wildcard *.go) $(wildcard */*.go) .gopath/.created vendor/.created version.go
|
|
||||||
go build -v $(GO_IMPORT_PATH)
|
|
||||||
|
|
||||||
install: wireguard-go
|
install: wireguard-go
|
||||||
@install -v -d "$(DESTDIR)$(BINDIR)" && install -v -m 0755 wireguard-go "$(DESTDIR)$(BINDIR)/wireguard-go"
|
@install -v -d "$(DESTDIR)$(BINDIR)" && install -v -m 0755 "$<" "$(DESTDIR)$(BINDIR)/wireguard-go"
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f wireguard-go
|
rm -f wireguard-go
|
||||||
|
|
||||||
update-dep: | .gopath/.created
|
.PHONY: all clean install generate-version-and-build
|
||||||
command -v dep >/dev/null || go get -v github.com/golang/dep/cmd/dep
|
|
||||||
cd .gopath/src/$(GO_IMPORT_PATH) && dep ensure -update -v
|
|
||||||
|
|
||||||
.PHONY: clean install update-dep version.go
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ This will run on OpenBSD. It does not yet support sticky sockets. Fwmark is mapp
|
|||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
This requires an installation of [go](https://golang.org) and of [dep](https://github.com/golang/dep). If dep is not installed, it will be downloaded and built as part of the build process.
|
This requires an installation of [go](https://golang.org) ≥ 1.11.
|
||||||
|
|
||||||
```
|
```
|
||||||
$ git clone https://git.zx2c4.com/wireguard-go
|
$ git clone https://git.zx2c4.com/wireguard-go
|
||||||
|
|||||||
7
go.mod
Normal file
7
go.mod
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module git.zx2c4.com/wireguard-go
|
||||||
|
|
||||||
|
require (
|
||||||
|
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4
|
||||||
|
golang.org/x/net v0.0.0-20181005035420-146acd28ed58
|
||||||
|
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e
|
||||||
|
)
|
||||||
6
go.sum
Normal file
6
go.sum
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4 h1:Vk3wNqEZwyGyei9yq5ekj7frek2u7HUfffJ1/opblzc=
|
||||||
|
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
|
golang.org/x/net v0.0.0-20181005035420-146acd28ed58 h1:otZG8yDCO4LVps5+9bxOeNiCvgmOyt96J3roHTYs7oE=
|
||||||
|
golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e h1:EfdBzeKbFSvOjoIqSZcfS8wp0FBLokGBEs9lz1OtSg0=
|
||||||
|
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
@@ -37,7 +37,8 @@ type sockaddrCtl struct {
|
|||||||
|
|
||||||
type nativeTun struct {
|
type nativeTun struct {
|
||||||
name string
|
name string
|
||||||
fd *os.File
|
tunFile *os.File
|
||||||
|
fd uintptr
|
||||||
rwcancel *rwcancel.RWCancel
|
rwcancel *rwcancel.RWCancel
|
||||||
events chan TUNEvent
|
events chan TUNEvent
|
||||||
errors chan error
|
errors chan error
|
||||||
@@ -171,14 +172,15 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
||||||
|
|
||||||
tun := &nativeTun{
|
tun := &nativeTun{
|
||||||
fd: file,
|
tunFile: file,
|
||||||
events: make(chan TUNEvent, 10),
|
fd: file.Fd(),
|
||||||
errors: make(chan error, 1),
|
events: make(chan TUNEvent, 10),
|
||||||
|
errors: make(chan error, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
name, err := tun.Name()
|
name, err := tun.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,19 +192,19 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
return iface.Index, nil
|
return iface.Index, nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tun.rwcancel, err = rwcancel.NewRWCancel(int(file.Fd()))
|
tun.rwcancel, err = rwcancel.NewRWCancel(int(tun.fd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +228,7 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
|
|
||||||
_, _, errno := unix.Syscall6(
|
_, _, errno := unix.Syscall6(
|
||||||
unix.SYS_GETSOCKOPT,
|
unix.SYS_GETSOCKOPT,
|
||||||
uintptr(tun.fd.Fd()),
|
uintptr(tun.fd),
|
||||||
2, /* #define SYSPROTO_CONTROL 2 */
|
2, /* #define SYSPROTO_CONTROL 2 */
|
||||||
2, /* #define UTUN_OPT_IFNAME 2 */
|
2, /* #define UTUN_OPT_IFNAME 2 */
|
||||||
uintptr(unsafe.Pointer(&ifName)),
|
uintptr(unsafe.Pointer(&ifName)),
|
||||||
@@ -241,7 +243,7 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *nativeTun) File() *os.File {
|
||||||
return tun.fd
|
return tun.tunFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Events() chan TUNEvent {
|
func (tun *nativeTun) Events() chan TUNEvent {
|
||||||
@@ -254,7 +256,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
default:
|
default:
|
||||||
buff := buff[offset-4:]
|
buff := buff[offset-4:]
|
||||||
n, err := tun.fd.Read(buff[:])
|
n, err := tun.tunFile.Read(buff[:])
|
||||||
if n < 4 {
|
if n < 4 {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -294,13 +296,13 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
|
|
||||||
// write
|
// write
|
||||||
|
|
||||||
return tun.fd.Write(buff)
|
return tun.tunFile.Write(buff)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Close() error {
|
func (tun *nativeTun) Close() error {
|
||||||
var err3 error
|
var err3 error
|
||||||
err1 := tun.rwcancel.Cancel()
|
err1 := tun.rwcancel.Cancel()
|
||||||
err2 := tun.fd.Close()
|
err2 := tun.tunFile.Close()
|
||||||
if tun.routeSocket != -1 {
|
if tun.routeSocket != -1 {
|
||||||
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
|
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
|
||||||
err3 = unix.Close(tun.routeSocket)
|
err3 = unix.Close(tun.routeSocket)
|
||||||
|
|||||||
@@ -51,7 +51,8 @@ type ifstat struct {
|
|||||||
|
|
||||||
type nativeTun struct {
|
type nativeTun struct {
|
||||||
name string
|
name string
|
||||||
fd *os.File
|
tunFile *os.File
|
||||||
|
fd uintptr
|
||||||
rwcancel *rwcancel.RWCancel
|
rwcancel *rwcancel.RWCancel
|
||||||
events chan TUNEvent
|
events chan TUNEvent
|
||||||
errors chan error
|
errors chan error
|
||||||
@@ -237,15 +238,15 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
return nil, fmt.Errorf("interface %s already exists", name)
|
return nil, fmt.Errorf("interface %s already exists", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
tunfile, err := os.OpenFile("/dev/tun", unix.O_RDWR, 0)
|
tunFile, err := os.OpenFile("/dev/tun", unix.O_RDWR, 0)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tunfd := tunfile.Fd()
|
tunfd := tunFile.Fd()
|
||||||
assignedName, err := tunName(tunfd)
|
assignedName, err := tunName(tunfd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tunfile.Close()
|
tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,25 +294,26 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
uintptr(unsafe.Pointer(&ifr)),
|
uintptr(unsafe.Pointer(&ifr)),
|
||||||
)
|
)
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
tunfile.Close()
|
tunFile.Close()
|
||||||
tunDestroy(name)
|
tunDestroy(name)
|
||||||
return nil, fmt.Errorf("failed to rename %s to %s: %s", assignedName, name, errno.Error())
|
return nil, fmt.Errorf("failed to rename %s to %s: %s", assignedName, name, errno.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return CreateTUNFromFile(tunfile, mtu)
|
return CreateTUNFromFile(tunFile, mtu)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
||||||
|
|
||||||
tun := &nativeTun{
|
tun := &nativeTun{
|
||||||
fd: file,
|
tunFile: file,
|
||||||
events: make(chan TUNEvent, 10),
|
fd: file.Fd(),
|
||||||
errors: make(chan error, 1),
|
events: make(chan TUNEvent, 10),
|
||||||
|
errors: make(chan error, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
name, err := tun.Name()
|
name, err := tun.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,19 +325,19 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
return iface.Index, nil
|
return iface.Index, nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tun.rwcancel, err = rwcancel.NewRWCancel(int(file.Fd()))
|
tun.rwcancel, err = rwcancel.NewRWCancel(int(tun.fd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -351,7 +353,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Name() (string, error) {
|
func (tun *nativeTun) Name() (string, error) {
|
||||||
name, err := tunName(tun.fd.Fd())
|
name, err := tunName(tun.fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -360,7 +362,7 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *nativeTun) File() *os.File {
|
||||||
return tun.fd
|
return tun.tunFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Events() chan TUNEvent {
|
func (tun *nativeTun) Events() chan TUNEvent {
|
||||||
@@ -373,7 +375,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
default:
|
default:
|
||||||
buff := buff[offset-4:]
|
buff := buff[offset-4:]
|
||||||
n, err := tun.fd.Read(buff[:])
|
n, err := tun.tunFile.Read(buff[:])
|
||||||
if n < 4 {
|
if n < 4 {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -413,13 +415,13 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
|
|
||||||
// write
|
// write
|
||||||
|
|
||||||
return tun.fd.Write(buff)
|
return tun.tunFile.Write(buff)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Close() error {
|
func (tun *nativeTun) Close() error {
|
||||||
var err4 error
|
var err4 error
|
||||||
err1 := tun.rwcancel.Cancel()
|
err1 := tun.rwcancel.Cancel()
|
||||||
err2 := tun.fd.Close()
|
err2 := tun.tunFile.Close()
|
||||||
err3 := tunDestroy(tun.name)
|
err3 := tunDestroy(tun.name)
|
||||||
if tun.routeSocket != -1 {
|
if tun.routeSocket != -1 {
|
||||||
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
|
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type nativeTun struct {
|
type nativeTun struct {
|
||||||
fd *os.File
|
tunFile *os.File
|
||||||
|
fd uintptr
|
||||||
fdCancel *rwcancel.RWCancel
|
fdCancel *rwcancel.RWCancel
|
||||||
index int32 // if index
|
index int32 // if index
|
||||||
name string // name of interface
|
name string // name of interface
|
||||||
@@ -43,7 +44,7 @@ type nativeTun struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *nativeTun) File() *os.File {
|
||||||
return tun.fd
|
return tun.tunFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) routineHackListener() {
|
func (tun *nativeTun) routineHackListener() {
|
||||||
@@ -51,7 +52,7 @@ func (tun *nativeTun) routineHackListener() {
|
|||||||
/* This is needed for the detection to work across network namespaces
|
/* This is needed for the detection to work across network namespaces
|
||||||
* If you are reading this and know a better method, please get in touch.
|
* If you are reading this and know a better method, please get in touch.
|
||||||
*/
|
*/
|
||||||
fd := int(tun.fd.Fd())
|
fd := int(tun.fd)
|
||||||
for {
|
for {
|
||||||
_, err := unix.Write(fd, nil)
|
_, err := unix.Write(fd, nil)
|
||||||
switch err {
|
switch err {
|
||||||
@@ -266,7 +267,7 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
var ifr [ifReqSize]byte
|
var ifr [ifReqSize]byte
|
||||||
_, _, errno := unix.Syscall(
|
_, _, errno := unix.Syscall(
|
||||||
unix.SYS_IOCTL,
|
unix.SYS_IOCTL,
|
||||||
tun.fd.Fd(),
|
tun.fd,
|
||||||
uintptr(unix.TUNGETIFF),
|
uintptr(unix.TUNGETIFF),
|
||||||
uintptr(unsafe.Pointer(&ifr[0])),
|
uintptr(unsafe.Pointer(&ifr[0])),
|
||||||
)
|
)
|
||||||
@@ -307,7 +308,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
|
|
||||||
// write
|
// write
|
||||||
|
|
||||||
return tun.fd.Write(buff)
|
return tun.tunFile.Write(buff)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
|
func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
|
||||||
@@ -316,10 +317,10 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
default:
|
default:
|
||||||
if tun.nopi {
|
if tun.nopi {
|
||||||
return tun.fd.Read(buff[offset:])
|
return tun.tunFile.Read(buff[offset:])
|
||||||
} else {
|
} else {
|
||||||
buff := buff[offset-4:]
|
buff := buff[offset-4:]
|
||||||
n, err := tun.fd.Read(buff[:])
|
n, err := tun.tunFile.Read(buff[:])
|
||||||
if n < 4 {
|
if n < 4 {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -354,7 +355,7 @@ func (tun *nativeTun) Close() error {
|
|||||||
} else if tun.events != nil {
|
} else if tun.events != nil {
|
||||||
close(tun.events)
|
close(tun.events)
|
||||||
}
|
}
|
||||||
err2 := tun.fd.Close()
|
err2 := tun.tunFile.Close()
|
||||||
err3 := tun.fdCancel.Cancel()
|
err3 := tun.fdCancel.Cancel()
|
||||||
|
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
@@ -413,23 +414,24 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
|
|
||||||
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
||||||
tun := &nativeTun{
|
tun := &nativeTun{
|
||||||
fd: file,
|
tunFile: file,
|
||||||
|
fd: file.Fd(),
|
||||||
events: make(chan TUNEvent, 5),
|
events: make(chan TUNEvent, 5),
|
||||||
errors: make(chan error, 5),
|
errors: make(chan error, 5),
|
||||||
statusListenersShutdown: make(chan struct{}),
|
statusListenersShutdown: make(chan struct{}),
|
||||||
nopi: false,
|
nopi: false,
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
tun.fdCancel, err = rwcancel.NewRWCancel(int(file.Fd()))
|
tun.fdCancel, err = rwcancel.NewRWCancel(int(tun.fd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = tun.Name()
|
_, err = tun.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,12 +444,12 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
|
|
||||||
tun.netlinkSock, err = createNetlinkSocket()
|
tun.netlinkSock, err = createNetlinkSocket()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tun.netlinkCancel, err = rwcancel.NewRWCancel(tun.netlinkSock)
|
tun.netlinkCancel, err = rwcancel.NewRWCancel(tun.netlinkSock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ const _TUNSIFMODE = 0x8004745d
|
|||||||
|
|
||||||
type nativeTun struct {
|
type nativeTun struct {
|
||||||
name string
|
name string
|
||||||
fd *os.File
|
tunFile *os.File
|
||||||
rwcancel *rwcancel.RWCancel
|
rwcancel *rwcancel.RWCancel
|
||||||
events chan TUNEvent
|
events chan TUNEvent
|
||||||
errors chan error
|
errors chan error
|
||||||
@@ -144,14 +144,14 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
||||||
|
|
||||||
tun := &nativeTun{
|
tun := &nativeTun{
|
||||||
fd: file,
|
tunFile: file,
|
||||||
events: make(chan TUNEvent, 10),
|
events: make(chan TUNEvent, 10),
|
||||||
errors: make(chan error, 1),
|
errors: make(chan error, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
name, err := tun.Name()
|
name, err := tun.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,19 +163,19 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
return iface.Index, nil
|
return iface.Index, nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tun.rwcancel, err = rwcancel.NewRWCancel(int(file.Fd()))
|
tun.rwcancel, err = rwcancel.NewRWCancel(int(file.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.fd.Close()
|
tun.tunFile.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,7 +191,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Name() (string, error) {
|
func (tun *nativeTun) Name() (string, error) {
|
||||||
gostat, err := tun.fd.Stat()
|
gostat, err := tun.tunFile.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.name = ""
|
tun.name = ""
|
||||||
return "", err
|
return "", err
|
||||||
@@ -202,7 +202,7 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *nativeTun) File() *os.File {
|
||||||
return tun.fd
|
return tun.tunFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Events() chan TUNEvent {
|
func (tun *nativeTun) Events() chan TUNEvent {
|
||||||
@@ -215,7 +215,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
default:
|
default:
|
||||||
buff := buff[offset-4:]
|
buff := buff[offset-4:]
|
||||||
n, err := tun.fd.Read(buff[:])
|
n, err := tun.tunFile.Read(buff[:])
|
||||||
if n < 4 {
|
if n < 4 {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -255,13 +255,13 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
|
|
||||||
// write
|
// write
|
||||||
|
|
||||||
return tun.fd.Write(buff)
|
return tun.tunFile.Write(buff)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Close() error {
|
func (tun *nativeTun) Close() error {
|
||||||
var err3 error
|
var err3 error
|
||||||
err1 := tun.rwcancel.Cancel()
|
err1 := tun.rwcancel.Cancel()
|
||||||
err2 := tun.fd.Close()
|
err2 := tun.tunFile.Close()
|
||||||
if tun.routeSocket != -1 {
|
if tun.routeSocket != -1 {
|
||||||
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
|
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
|
||||||
err3 = unix.Close(tun.routeSocket)
|
err3 = unix.Close(tun.routeSocket)
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
package main
|
package main
|
||||||
const WireGuardGoVersion = "0.0.20181001"
|
const WireGuardGoVersion = "0.0.20181018"
|
||||||
|
|||||||
Reference in New Issue
Block a user