diff --git a/go.mod b/go.mod index e4a64444..b99d1f06 100644 --- a/go.mod +++ b/go.mod @@ -59,6 +59,4 @@ require ( gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) -replace golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 => github.com/MetaCubeX/wintun-go v0.0.0-20220319102620-bbc5e6b2015e - replace github.com/vishvananda/netlink v1.2.0-beta.0.20220404152918-5e915e014938 => github.com/MetaCubeX/netlink v1.2.0-beta.0.20220529072258-d6853f887820 diff --git a/go.sum b/go.sum index 5079b59e..49033985 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,6 @@ github.com/Dreamacro/go-shadowsocks2 v0.1.8 h1:Ixejp5JscEc866gAvm/l6TFd7BOBvDviK github.com/Dreamacro/go-shadowsocks2 v0.1.8/go.mod h1:51y4Q6tJoCE7e8TmYXcQRqfoxPfE9Cvn79V6pB6Df7Y= github.com/MetaCubeX/netlink v1.2.0-beta.0.20220529072258-d6853f887820 h1:fGKWZ25VApYnuPZoNeqdH/nZtHa2XMajwH6Yj/OgoVc= github.com/MetaCubeX/netlink v1.2.0-beta.0.20220529072258-d6853f887820/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho= -github.com/MetaCubeX/wintun-go v0.0.0-20220319102620-bbc5e6b2015e h1:GRfT5Lf8HP7RNczKIwTYLoCh1PPuIs/sY9hj+W+3deg= -github.com/MetaCubeX/wintun-go v0.0.0-20220319102620-bbc5e6b2015e/go.mod h1:ARUuShAtcziEJ/vnZ2hgoP+zc0J7Ukcca2S/NPDoQCc= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= @@ -345,6 +343,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U= golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 h1:Ug9qvr1myri/zFN6xL17LSCBGFDnphBBhzmILHsM5TY= +golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224/go.mod h1:deeaetjYA+DHMHg+sMSMI58GrEteJUUzzw7en6TJQcI= golang.zx2c4.com/wireguard v0.0.0-20220407013110-ef5c587f782d h1:q4JksJ2n0fmbXC0Aj0eOs6E0AcPqnKglxWXWFqGD6x0= golang.zx2c4.com/wireguard v0.0.0-20220407013110-ef5c587f782d/go.mod h1:bVQfyl2sCM/QIIGHpWbFGfHPuDvqnCNkT6MQLTCjO/U= golang.zx2c4.com/wireguard/windows v0.5.4-0.20220317000008-6432784c2469 h1:SEYkJAIuYAsSAPkCffOiYLtq5brBDSI+L0mRjSsvSTY= diff --git a/listener/tun/device/tun/wintun/embed_dll/amd64/wintun.dll b/listener/tun/device/tun/driver/amd64/wintun.dll similarity index 100% rename from listener/tun/device/tun/wintun/embed_dll/amd64/wintun.dll rename to listener/tun/device/tun/driver/amd64/wintun.dll diff --git a/listener/tun/device/tun/wintun/embed_dll/arm/wintun.dll b/listener/tun/device/tun/driver/arm/wintun.dll similarity index 100% rename from listener/tun/device/tun/wintun/embed_dll/arm/wintun.dll rename to listener/tun/device/tun/driver/arm/wintun.dll diff --git a/listener/tun/device/tun/wintun/embed_dll/arm64/wintun.dll b/listener/tun/device/tun/driver/arm64/wintun.dll similarity index 100% rename from listener/tun/device/tun/wintun/embed_dll/arm64/wintun.dll rename to listener/tun/device/tun/driver/arm64/wintun.dll diff --git a/listener/tun/device/tun/driver/dll_windows.go b/listener/tun/device/tun/driver/dll_windows.go new file mode 100644 index 00000000..5f4a5dae --- /dev/null +++ b/listener/tun/device/tun/driver/dll_windows.go @@ -0,0 +1,233 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. + */ + +package driver + +import ( + "fmt" + "runtime" + "sync" + "sync/atomic" + "syscall" + "unsafe" + + "github.com/Dreamacro/clash/log" + + "golang.org/x/sys/windows" + "golang.zx2c4.com/wireguard/windows/driver/memmod" +) + +//go:linkname modwintun golang.zx2c4.com/wintun.modwintun + +//go:linkname procWintunCreateAdapter golang.zx2c4.com/wintun.procWintunCreateAdapter + +//go:linkname procWintunOpenAdapter golang.zx2c4.com/wintun.procWintunOpenAdapter + +//go:linkname procWintunCloseAdapter golang.zx2c4.com/wintun.procWintunCloseAdapter + +//go:linkname procWintunDeleteDriver golang.zx2c4.com/wintun.procWintunDeleteDriver + +//go:linkname procWintunGetAdapterLUID golang.zx2c4.com/wintun.procWintunGetAdapterLUID + +//go:linkname procWintunGetRunningDriverVersion golang.zx2c4.com/wintun.procWintunGetRunningDriverVersion + +//go:linkname procWintunAllocateSendPacket golang.zx2c4.com/wintun.procWintunAllocateSendPacket + +//go:linkname procWintunEndSession golang.zx2c4.com/wintun.procWintunEndSession + +//go:linkname procWintunGetReadWaitEvent golang.zx2c4.com/wintun.procWintunGetReadWaitEvent + +//go:linkname procWintunReceivePacket golang.zx2c4.com/wintun.procWintunReceivePacket + +//go:linkname procWintunReleaseReceivePacket golang.zx2c4.com/wintun.procWintunReleaseReceivePacket + +//go:linkname procWintunSendPacket golang.zx2c4.com/wintun.procWintunSendPacket + +//go:linkname procWintunStartSession golang.zx2c4.com/wintun.procWintunStartSession + +var ( + modwintun *lazyDLL + procWintunCreateAdapter *lazyProc + procWintunOpenAdapter *lazyProc + procWintunCloseAdapter *lazyProc + procWintunDeleteDriver *lazyProc + procWintunGetAdapterLUID *lazyProc + procWintunGetRunningDriverVersion *lazyProc + procWintunAllocateSendPacket *lazyProc + procWintunEndSession *lazyProc + procWintunGetReadWaitEvent *lazyProc + procWintunReceivePacket *lazyProc + procWintunReleaseReceivePacket *lazyProc + procWintunSendPacket *lazyProc + procWintunStartSession *lazyProc +) + +type loggerLevel int + +const ( + logInfo loggerLevel = iota + logWarn + logErr +) + +func init() { + modwintun = newLazyDLL("wintun.dll", setupLogger) + procWintunCreateAdapter = modwintun.NewProc("WintunCreateAdapter") + procWintunOpenAdapter = modwintun.NewProc("WintunOpenAdapter") + procWintunCloseAdapter = modwintun.NewProc("WintunCloseAdapter") + procWintunDeleteDriver = modwintun.NewProc("WintunDeleteDriver") + procWintunGetAdapterLUID = modwintun.NewProc("WintunGetAdapterLUID") + procWintunGetRunningDriverVersion = modwintun.NewProc("WintunGetRunningDriverVersion") + procWintunAllocateSendPacket = modwintun.NewProc("WintunAllocateSendPacket") + procWintunEndSession = modwintun.NewProc("WintunEndSession") + procWintunGetReadWaitEvent = modwintun.NewProc("WintunGetReadWaitEvent") + procWintunReceivePacket = modwintun.NewProc("WintunReceivePacket") + procWintunReleaseReceivePacket = modwintun.NewProc("WintunReleaseReceivePacket") + procWintunSendPacket = modwintun.NewProc("WintunSendPacket") + procWintunStartSession = modwintun.NewProc("WintunStartSession") +} + +func InitWintun() (err error) { + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("init wintun.dll error: %v", r) + } + }() + + if err = modwintun.Load(); err != nil { + return + } + + procWintunCreateAdapter.Addr() + procWintunOpenAdapter.Addr() + procWintunCloseAdapter.Addr() + procWintunDeleteDriver.Addr() + procWintunGetAdapterLUID.Addr() + procWintunGetRunningDriverVersion.Addr() + procWintunAllocateSendPacket.Addr() + procWintunEndSession.Addr() + procWintunGetReadWaitEvent.Addr() + procWintunReceivePacket.Addr() + procWintunReleaseReceivePacket.Addr() + procWintunSendPacket.Addr() + procWintunStartSession.Addr() + + return +} + +func newLazyDLL(name string, onLoad func(d *lazyDLL)) *lazyDLL { + return &lazyDLL{Name: name, onLoad: onLoad} +} + +func logMessage(level loggerLevel, _ uint64, msg *uint16) int { + switch level { + case logInfo: + log.Infoln("[TUN] %s", windows.UTF16PtrToString(msg)) + case logWarn: + log.Warnln("[TUN] %s", windows.UTF16PtrToString(msg)) + case logErr: + log.Errorln("[TUN] %s", windows.UTF16PtrToString(msg)) + default: + log.Debugln("[TUN] %s", windows.UTF16PtrToString(msg)) + } + return 0 +} + +func setupLogger(dll *lazyDLL) { + var callback uintptr + if runtime.GOARCH == "386" { + callback = windows.NewCallback(func(level loggerLevel, _, _ uint32, msg *uint16) int { + return logMessage(level, 0, msg) + }) + } else if runtime.GOARCH == "arm" { + callback = windows.NewCallback(func(level loggerLevel, _, _, _ uint32, msg *uint16) int { + return logMessage(level, 0, msg) + }) + } else if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" { + callback = windows.NewCallback(logMessage) + } + _, _, _ = syscall.SyscallN(dll.NewProc("WintunSetLogger").Addr(), callback) +} + +func (d *lazyDLL) NewProc(name string) *lazyProc { + return &lazyProc{dll: d, Name: name} +} + +type lazyProc struct { + Name string + mu sync.Mutex + dll *lazyDLL + addr uintptr +} + +func (p *lazyProc) Find() error { + if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.addr))) != nil { + return nil + } + p.mu.Lock() + defer p.mu.Unlock() + if p.addr != 0 { + return nil + } + + err := p.dll.Load() + if err != nil { + return fmt.Errorf("error loading DLL: %s, MODULE: %s, error: %w", p.dll.Name, p.Name, err) + } + addr, err := p.nameToAddr() + if err != nil { + return fmt.Errorf("error getting %s address: %w", p.Name, err) + } + + atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.addr)), unsafe.Pointer(addr)) + return nil +} + +func (p *lazyProc) Addr() uintptr { + err := p.Find() + if err != nil { + panic(err) + } + return p.addr +} + +func (p *lazyProc) Load() error { + return p.dll.Load() +} + +type lazyDLL struct { + Name string + Base windows.Handle + mu sync.Mutex + module *memmod.Module + onLoad func(d *lazyDLL) +} + +func (d *lazyDLL) Load() error { + if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.module))) != nil { + return nil + } + d.mu.Lock() + defer d.mu.Unlock() + if d.module != nil { + return nil + } + + module, err := memmod.LoadLibrary(dllContent) + if err != nil { + return fmt.Errorf("unable to load library: %w", err) + } + d.Base = windows.Handle(module.BaseAddr()) + + atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.module)), unsafe.Pointer(module)) + if d.onLoad != nil { + d.onLoad(d) + } + return nil +} + +func (p *lazyProc) nameToAddr() (uintptr, error) { + return p.dll.module.ProcAddressByName(p.Name) +} diff --git a/listener/tun/device/tun/driver/dll_windows_386.go b/listener/tun/device/tun/driver/dll_windows_386.go new file mode 100644 index 00000000..12ace4e8 --- /dev/null +++ b/listener/tun/device/tun/driver/dll_windows_386.go @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. + */ + +package driver + +import ( + _ "embed" +) + +//go:embed x86/wintun.dll +var dllContent []byte diff --git a/listener/tun/device/tun/driver/dll_windows_amd64.go b/listener/tun/device/tun/driver/dll_windows_amd64.go new file mode 100644 index 00000000..58f3e01e --- /dev/null +++ b/listener/tun/device/tun/driver/dll_windows_amd64.go @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. + */ + +package driver + +import ( + _ "embed" +) + +//go:embed amd64/wintun.dll +var dllContent []byte diff --git a/listener/tun/device/tun/driver/dll_windows_arm.go b/listener/tun/device/tun/driver/dll_windows_arm.go new file mode 100644 index 00000000..bb941068 --- /dev/null +++ b/listener/tun/device/tun/driver/dll_windows_arm.go @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. + */ + +package driver + +import ( + _ "embed" +) + +//go:embed arm/wintun.dll +var dllContent []byte diff --git a/listener/tun/device/tun/driver/dll_windows_arm64.go b/listener/tun/device/tun/driver/dll_windows_arm64.go new file mode 100644 index 00000000..05bd1cbd --- /dev/null +++ b/listener/tun/device/tun/driver/dll_windows_arm64.go @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. + */ + +package driver + +import ( + _ "embed" +) + +//go:embed arm64/wintun.dll +var dllContent []byte diff --git a/listener/tun/device/tun/wintun/package_info.go b/listener/tun/device/tun/driver/package_info.go old mode 100755 new mode 100644 similarity index 54% rename from listener/tun/device/tun/wintun/package_info.go rename to listener/tun/device/tun/driver/package_info.go index 22e9bd05..0c1bf7e4 --- a/listener/tun/device/tun/wintun/package_info.go +++ b/listener/tun/device/tun/driver/package_info.go @@ -1,11 +1,10 @@ //go:build windows -// +build windows -// Modified from: https://git.zx2c4.com/wireguard-go/tree/tun/wintun +// https://git.zx2c4.com/wireguard-go/tree/tun/wintun /* SPDX-License-Identifier: MIT * * Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved. */ -package wintun +package driver diff --git a/listener/tun/device/tun/wintun/embed_dll/x86/wintun.dll b/listener/tun/device/tun/driver/x86/wintun.dll similarity index 100% rename from listener/tun/device/tun/wintun/embed_dll/x86/wintun.dll rename to listener/tun/device/tun/driver/x86/wintun.dll diff --git a/listener/tun/device/tun/tun_wireguard.go b/listener/tun/device/tun/tun_wireguard.go index 529a1054..1aafae54 100644 --- a/listener/tun/device/tun/tun_wireguard.go +++ b/listener/tun/device/tun/tun_wireguard.go @@ -3,7 +3,6 @@ package tun import ( - "errors" "fmt" "os" "runtime" @@ -43,11 +42,11 @@ func Open(name string, mtu uint32) (_ device.Device, err error) { forcedMTU = int(t.mtu) } - nt, err := tun.CreateTUN(t.name, forcedMTU) // forcedMTU do not work on wintun, need to be setting by other way + nt, err := newDevice(t.name, forcedMTU) // forcedMTU do not work on wintun, need to be setting by other way - // retry if abnormal exit on Windows at last time - if err != nil && runtime.GOOS == "windows" && errors.Is(err, os.ErrExist) { - nt, err = tun.CreateTUN(t.name, forcedMTU) + // retry if abnormal exit at last time on Windows + if err != nil && runtime.GOOS == "windows" && os.IsExist(err) { + nt, err = newDevice(t.name, forcedMTU) } if err != nil { diff --git a/listener/tun/device/tun/tun_wireguard_unix.go b/listener/tun/device/tun/tun_wireguard_unix.go index d88787fb..b6d3addf 100644 --- a/listener/tun/device/tun/tun_wireguard_unix.go +++ b/listener/tun/device/tun/tun_wireguard_unix.go @@ -2,7 +2,13 @@ package tun +import "golang.zx2c4.com/wireguard/tun" + const ( offset = 4 /* 4 bytes TUN_PI */ defaultMTU = 1500 ) + +func newDevice(name string, mtu int) (tun.Device, error) { + return tun.CreateTUN(name, mtu) +} diff --git a/listener/tun/device/tun/tun_wireguard_windows.go b/listener/tun/device/tun/tun_wireguard_windows.go index f73286d8..c5c16a1d 100644 --- a/listener/tun/device/tun/tun_wireguard_windows.go +++ b/listener/tun/device/tun/tun_wireguard_windows.go @@ -1,6 +1,8 @@ package tun import ( + "github.com/Dreamacro/clash/listener/tun/device/tun/driver" + "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/tun" ) @@ -20,3 +22,11 @@ func init() { func (t *TUN) LUID() uint64 { return t.nt.LUID() } + +func newDevice(name string, mtu int) (nt tun.Device, err error) { + if err = driver.InitWintun(); err != nil { + return + } + + return tun.CreateTUN(name, mtu) +} diff --git a/listener/tun/device/tun/wintun/dll_windows.go b/listener/tun/device/tun/wintun/dll_windows.go deleted file mode 100755 index 4368100b..00000000 --- a/listener/tun/device/tun/wintun/dll_windows.go +++ /dev/null @@ -1,94 +0,0 @@ -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. - */ - -package wintun - -import ( - "fmt" - "golang.zx2c4.com/wintun/embed_dll" - "golang.zx2c4.com/wireguard/windows/driver/memmod" - "sync" - "sync/atomic" - "unsafe" -) - -func newLazyDLL(name string, onLoad func(d *lazyDLL)) *lazyDLL { - return &lazyDLL{Name: name, onLoad: onLoad} -} - -func (d *lazyDLL) NewProc(name string) *lazyProc { - return &lazyProc{dll: d, Name: name} -} - -type lazyProc struct { - Name string - mu sync.Mutex - dll *lazyDLL - addr uintptr -} - -func (p *lazyProc) Find() error { - if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.addr))) != nil { - return nil - } - p.mu.Lock() - defer p.mu.Unlock() - if p.addr != 0 { - return nil - } - - err := p.dll.Load() - if err != nil { - return fmt.Errorf("Error loading %v DLL: %w", p.dll.Name, err) - } - addr, err := p.nameToAddr() - if err != nil { - return fmt.Errorf("Error getting %v address: %w", p.Name, err) - } - - atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.addr)), unsafe.Pointer(addr)) - return nil -} - -func (p *lazyProc) Addr() uintptr { - err := p.Find() - if err != nil { - panic(err) - } - return p.addr -} - -type lazyDLL struct { - Name string - mu sync.Mutex - module *memmod.Module - onLoad func(d *lazyDLL) -} - -func (d *lazyDLL) Load() error { - if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.module))) != nil { - return nil - } - d.mu.Lock() - defer d.mu.Unlock() - if d.module != nil { - return nil - } - - module, err := memmod.LoadLibrary(embed_dll.DDlContent) - if err != nil { - return fmt.Errorf("unable to load library: %w", err) - } - - atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.module)), unsafe.Pointer(module)) - if d.onLoad != nil { - d.onLoad(d) - } - return nil -} - -func (p *lazyProc) nameToAddr() (uintptr, error) { - return p.dll.module.ProcAddressByName(p.Name) -} diff --git a/listener/tun/device/tun/wintun/embed_dll/windows_386.go b/listener/tun/device/tun/wintun/embed_dll/windows_386.go deleted file mode 100755 index 05fab743..00000000 --- a/listener/tun/device/tun/wintun/embed_dll/windows_386.go +++ /dev/null @@ -1,21 +0,0 @@ -package embed_dll - -// Copyright 2020 MeshStep Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -import ( - _ "embed" -) - -//go:embed x86/wintun.dll -var DDlContent []byte diff --git a/listener/tun/device/tun/wintun/embed_dll/windows_amd64.go b/listener/tun/device/tun/wintun/embed_dll/windows_amd64.go deleted file mode 100755 index 2ee9f791..00000000 --- a/listener/tun/device/tun/wintun/embed_dll/windows_amd64.go +++ /dev/null @@ -1,21 +0,0 @@ -package embed_dll - -// Copyright 2020 MeshStep Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -import ( - _ "embed" -) - -//go:embed amd64/wintun.dll -var DDlContent []byte diff --git a/listener/tun/device/tun/wintun/embed_dll/windows_arm.go b/listener/tun/device/tun/wintun/embed_dll/windows_arm.go deleted file mode 100755 index 0636f21b..00000000 --- a/listener/tun/device/tun/wintun/embed_dll/windows_arm.go +++ /dev/null @@ -1,21 +0,0 @@ -package embed_dll - -// Copyright 2020 MeshStep Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -import ( - _ "embed" -) - -//go:embed arm/wintun.dll -var DDlContent []byte diff --git a/listener/tun/device/tun/wintun/embed_dll/windows_arm64.go b/listener/tun/device/tun/wintun/embed_dll/windows_arm64.go deleted file mode 100755 index 1e5d605d..00000000 --- a/listener/tun/device/tun/wintun/embed_dll/windows_arm64.go +++ /dev/null @@ -1,21 +0,0 @@ -package embed_dll - -// Copyright 2020 MeshStep Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -import ( - _ "embed" -) - -//go:embed arm64/wintun.dll -var DDlContent []byte diff --git a/listener/tun/device/tun/wintun/go.mod b/listener/tun/device/tun/wintun/go.mod deleted file mode 100755 index 33d4b961..00000000 --- a/listener/tun/device/tun/wintun/go.mod +++ /dev/null @@ -1,8 +0,0 @@ -module golang.zx2c4.com/wintun - -go 1.18 - -require ( - golang.org/x/sys v0.0.0-20220318055525-2edf467146b5 - golang.zx2c4.com/wireguard/windows v0.5.3 -) diff --git a/listener/tun/device/tun/wintun/go.sum b/listener/tun/device/tun/wintun/go.sum deleted file mode 100755 index e1f1e5ec..00000000 --- a/listener/tun/device/tun/wintun/go.sum +++ /dev/null @@ -1,17 +0,0 @@ -github.com/MetaCubeX/Clash.Meta v1.9.1 h1:jHZhVRBxFuaCRBN9vxB/FL5R16wY4kIgNqjszdXPeLs= -github.com/MetaCubeX/Clash.Meta v1.9.1/go.mod h1:/I4cSh+PcgmtS5SEnFp8RANL6aVRd3i9YOult+mKLhU= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf h1:Fm4IcnUL803i92qDlmB0obyHmosDrxZWxJL3gIeNqOw= -golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.zx2c4.com/wireguard/windows v0.5.3 h1:On6j2Rpn3OEMXqBq00QEDC7bWSZrPIHKIus8eIuExIE= -golang.zx2c4.com/wireguard/windows v0.5.3/go.mod h1:9TEe8TJmtwyQebdFwAkEWOPr3prrtqm+REGFifP60hI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/listener/tun/device/tun/wintun/session_windows.go b/listener/tun/device/tun/wintun/session_windows.go deleted file mode 100755 index f023baf7..00000000 --- a/listener/tun/device/tun/wintun/session_windows.go +++ /dev/null @@ -1,90 +0,0 @@ -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. - */ - -package wintun - -import ( - "syscall" - "unsafe" - - "golang.org/x/sys/windows" -) - -type Session struct { - handle uintptr -} - -const ( - PacketSizeMax = 0xffff // Maximum packet size - RingCapacityMin = 0x20000 // Minimum ring capacity (128 kiB) - RingCapacityMax = 0x4000000 // Maximum ring capacity (64 MiB) -) - -// Packet with data -type Packet struct { - Next *Packet // Pointer to next packet in queue - Size uint32 // Size of packet (max WINTUN_MAX_IP_PACKET_SIZE) - Data *[PacketSizeMax]byte // Pointer to layer 3 IPv4 or IPv6 packet -} - -var ( - procWintunAllocateSendPacket = modwintun.NewProc("WintunAllocateSendPacket") - procWintunEndSession = modwintun.NewProc("WintunEndSession") - procWintunGetReadWaitEvent = modwintun.NewProc("WintunGetReadWaitEvent") - procWintunReceivePacket = modwintun.NewProc("WintunReceivePacket") - procWintunReleaseReceivePacket = modwintun.NewProc("WintunReleaseReceivePacket") - procWintunSendPacket = modwintun.NewProc("WintunSendPacket") - procWintunStartSession = modwintun.NewProc("WintunStartSession") -) - -func (wintun *Adapter) StartSession(capacity uint32) (session Session, err error) { - r0, _, e1 := syscall.Syscall(procWintunStartSession.Addr(), 2, uintptr(wintun.handle), uintptr(capacity), 0) - if r0 == 0 { - err = e1 - } else { - session = Session{r0} - } - return -} - -func (session Session) End() { - syscall.Syscall(procWintunEndSession.Addr(), 1, session.handle, 0, 0) - session.handle = 0 -} - -func (session Session) ReadWaitEvent() (handle windows.Handle) { - r0, _, _ := syscall.Syscall(procWintunGetReadWaitEvent.Addr(), 1, session.handle, 0, 0) - handle = windows.Handle(r0) - return -} - -func (session Session) ReceivePacket() (packet []byte, err error) { - var packetSize uint32 - r0, _, e1 := syscall.Syscall(procWintunReceivePacket.Addr(), 2, session.handle, uintptr(unsafe.Pointer(&packetSize)), 0) - if r0 == 0 { - err = e1 - return - } - packet = unsafe.Slice((*byte)(unsafe.Pointer(r0)), packetSize) - return -} - -func (session Session) ReleaseReceivePacket(packet []byte) { - syscall.Syscall(procWintunReleaseReceivePacket.Addr(), 2, session.handle, uintptr(unsafe.Pointer(&packet[0])), 0) -} - -func (session Session) AllocateSendPacket(packetSize int) (packet []byte, err error) { - r0, _, e1 := syscall.Syscall(procWintunAllocateSendPacket.Addr(), 2, session.handle, uintptr(packetSize), 0) - if r0 == 0 { - err = e1 - return - } - packet = unsafe.Slice((*byte)(unsafe.Pointer(r0)), packetSize) - return -} - -func (session Session) SendPacket(packet []byte) { - syscall.Syscall(procWintunSendPacket.Addr(), 2, session.handle, uintptr(unsafe.Pointer(&packet[0])), 0) -} diff --git a/listener/tun/device/tun/wintun/wintun_windows.go b/listener/tun/device/tun/wintun/wintun_windows.go deleted file mode 100755 index 254dc1e8..00000000 --- a/listener/tun/device/tun/wintun/wintun_windows.go +++ /dev/null @@ -1,152 +0,0 @@ -//go:build windows - -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. - */ - -package wintun - -import ( - "log" - "runtime" - "syscall" - "unsafe" - - "golang.org/x/sys/windows" -) - -type loggerLevel int - -const ( - logInfo loggerLevel = iota - logWarn - logErr -) - -const AdapterNameMax = 128 - -type Adapter struct { - handle uintptr -} - -var ( - modwintun = newLazyDLL("wintun.dll", setupLogger) - procWintunCreateAdapter = modwintun.NewProc("WintunCreateAdapter") - procWintunOpenAdapter = modwintun.NewProc("WintunOpenAdapter") - procWintunCloseAdapter = modwintun.NewProc("WintunCloseAdapter") - procWintunDeleteDriver = modwintun.NewProc("WintunDeleteDriver") - procWintunGetAdapterLUID = modwintun.NewProc("WintunGetAdapterLUID") - procWintunGetRunningDriverVersion = modwintun.NewProc("WintunGetRunningDriverVersion") -) - -type TimestampedWriter interface { - WriteWithTimestamp(p []byte, ts int64) (n int, err error) -} - -func logMessage(level loggerLevel, timestamp uint64, msg *uint16) int { - if tw, ok := log.Default().Writer().(TimestampedWriter); ok { - tw.WriteWithTimestamp([]byte(log.Default().Prefix()+windows.UTF16PtrToString(msg)), (int64(timestamp)-116444736000000000)*100) - } else { - log.Println(windows.UTF16PtrToString(msg)) - } - return 0 -} - -func setupLogger(dll *lazyDLL) { - var callback uintptr - if runtime.GOARCH == "386" { - callback = windows.NewCallback(func(level loggerLevel, timestampLow, timestampHigh uint32, msg *uint16) int { - return logMessage(level, uint64(timestampHigh)<<32|uint64(timestampLow), msg) - }) - } else if runtime.GOARCH == "arm" { - callback = windows.NewCallback(func(level loggerLevel, _, timestampLow, timestampHigh uint32, msg *uint16) int { - return logMessage(level, uint64(timestampHigh)<<32|uint64(timestampLow), msg) - }) - } else if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" { - callback = windows.NewCallback(logMessage) - } - syscall.Syscall(dll.NewProc("WintunSetLogger").Addr(), 1, callback, 0, 0) -} - -func closeAdapter(wintun *Adapter) { - syscall.Syscall(procWintunCloseAdapter.Addr(), 1, wintun.handle, 0, 0) -} - -// CreateAdapter creates a Wintun adapter. name is the cosmetic name of the adapter. -// tunnelType represents the type of adapter and should be "Wintun". requestedGUID is -// the GUID of the created network adapter, which then influences NLA generation -// deterministically. If it is set to nil, the GUID is chosen by the system at random, -// and hence a new NLA entry is created for each new adapter. -func CreateAdapter(name string, tunnelType string, requestedGUID *windows.GUID) (wintun *Adapter, err error) { - var name16 *uint16 - name16, err = windows.UTF16PtrFromString(name) - if err != nil { - return - } - var tunnelType16 *uint16 - tunnelType16, err = windows.UTF16PtrFromString(tunnelType) - if err != nil { - return - } - r0, _, e1 := syscall.Syscall(procWintunCreateAdapter.Addr(), 3, uintptr(unsafe.Pointer(name16)), uintptr(unsafe.Pointer(tunnelType16)), uintptr(unsafe.Pointer(requestedGUID))) - if r0 == 0 { - err = e1 - return - } - wintun = &Adapter{handle: r0} - runtime.SetFinalizer(wintun, closeAdapter) - return -} - -// OpenAdapter opens an existing Wintun adapter by name. -func OpenAdapter(name string) (wintun *Adapter, err error) { - var name16 *uint16 - name16, err = windows.UTF16PtrFromString(name) - if err != nil { - return - } - r0, _, e1 := syscall.Syscall(procWintunOpenAdapter.Addr(), 1, uintptr(unsafe.Pointer(name16)), 0, 0) - if r0 == 0 { - err = e1 - return - } - wintun = &Adapter{handle: r0} - runtime.SetFinalizer(wintun, closeAdapter) - return -} - -// Close closes a Wintun adapter. -func (wintun *Adapter) Close() (err error) { - runtime.SetFinalizer(wintun, nil) - r1, _, e1 := syscall.Syscall(procWintunCloseAdapter.Addr(), 1, wintun.handle, 0, 0) - if r1 == 0 { - err = e1 - } - return -} - -// Uninstall removes the driver from the system if no drivers are currently in use. -func Uninstall() (err error) { - r1, _, e1 := syscall.Syscall(procWintunDeleteDriver.Addr(), 0, 0, 0, 0) - if r1 == 0 { - err = e1 - } - return -} - -// RunningVersion returns the version of the loaded driver. -func RunningVersion() (version uint32, err error) { - r0, _, e1 := syscall.Syscall(procWintunGetRunningDriverVersion.Addr(), 0, 0, 0, 0) - version = uint32(r0) - if version == 0 { - err = e1 - } - return -} - -// LUID returns the LUID of the adapter. -func (wintun *Adapter) LUID() (luid uint64) { - syscall.Syscall(procWintunGetAdapterLUID.Addr(), 2, uintptr(wintun.handle), uintptr(unsafe.Pointer(&luid)), 0) - return -}