Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/remove release #1

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
test:
name: go-test
runs-on: windows-latest
env:
CGO_ENABLED: 0
steps:
- name: disable-auto-crlf
run: |
git config --global core.autocrlf false
git config --global core.eol lf

- name: clone-repo
uses: actions/checkout@v4

- name: setup-go
uses: actions/setup-go@v4
with:
go-version: '1.21'

- name: go-vet-fmt-test # fmt check see Test_Gofmt
run : |
go vet
go test -v -timeout 120s -tags "-race" ./...


37 changes: 21 additions & 16 deletions adapter.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ package wintun
import (
"context"
"sync"
"syscall"

"github.com/pkg/errors"

@@ -30,7 +31,11 @@ func (a *Adapter) sessionLocked(trap uintptr, args ...uintptr) (r1, r2 uintptr,
} else if a.session == 0 {
return 0, 0, errors.WithStack(ErrAdapterStoped{})
}
return global.calln(trap, append([]uintptr{a.session}, args...)...)
r1, r2, err = syscall.SyscallN(trap, append([]uintptr{a.session}, args...)...)
if err == windows.ERROR_SUCCESS {
err = nil
}
return r1, r2, errors.WithStack(err)
}

func (a *Adapter) Start(capacity uint32) (err error) {
@@ -43,12 +48,12 @@ func (a *Adapter) Start(capacity uint32) (err error) {
if a.handle == 0 {
return errors.WithStack(ErrAdapterClosed{})
}
fd, _, err := global.calln(
global.procStartSession,
fd, _, err := syscall.SyscallN(
procStartSession.Addr(),
a.handle,
uintptr(capacity),
)
if err != nil {
if err != windows.ERROR_SUCCESS {
return err
}
a.session = fd
@@ -63,8 +68,8 @@ func (a *Adapter) Stop() error {

func (a *Adapter) stopLocked() error {
if a.session > 0 {
_, _, err := global.calln(global.procEndSession, uintptr(a.session))
if err != nil {
_, _, err := syscall.SyscallN(procEndSession.Addr(), uintptr(a.session))
if err != windows.ERROR_SUCCESS {
return err
}
a.session = 0
@@ -82,8 +87,8 @@ func (a *Adapter) Close() error {
return err
}

_, _, err = global.calln(global.procCloseAdapter, a.handle)
if err != nil {
_, _, err = syscall.SyscallN(procCloseAdapter.Addr(), a.handle)
if err != windows.ERROR_SUCCESS {
return err
}
a.handle = 0
@@ -99,12 +104,12 @@ func (a *Adapter) GetAdapterLuid() (winipcfg.LUID, error) {
return 0, errors.WithStack(ErrAdapterClosed{})
}
var luid uint64
_, _, err := global.calln(
global.procGetAdapterLuid,
_, _, err := syscall.SyscallN(
procGetAdapterLUID.Addr(),
a.handle,
uintptr(unsafe.Pointer(&luid)),
)
if err != nil {
if err != windows.ERROR_SUCCESS {
return 0, err
}
return winipcfg.LUID(luid), nil
@@ -124,7 +129,7 @@ func (a *Adapter) Index() (int, error) {
}

func (a *Adapter) getReadWaitEvent() (windows.Handle, error) {
r0, _, err := a.sessionLocked(global.procGetReadWaitEvent)
r0, _, err := a.sessionLocked(procGetReadWaitEvent.Addr())
if r0 == 0 {
return 0, err
}
@@ -141,7 +146,7 @@ func (a *Adapter) Recv(ctx context.Context) (ip rpack, err error) {
var size uint32
for {
r0, _, err := a.sessionLocked(
global.procReceivePacket,
procReceivePacket.Addr(),
(uintptr)(unsafe.Pointer(&size)),
)

@@ -187,7 +192,7 @@ func (a *Adapter) Release(p rpack) error {
defer a.mu.RUnlock()

_, _, err := a.sessionLocked(
global.procReleaseReceivePacket,
procReleaseReceivePacket.Addr(),
uintptr(unsafe.Pointer(&p[0])),
)
return err
@@ -203,7 +208,7 @@ func (a *Adapter) Alloc(size int) (spack, error) {
defer a.mu.RUnlock()

r0, _, err := a.sessionLocked(
global.procAllocateSendPacket,
procAllocateSendPacket.Addr(),
uintptr(size),
)
if r0 == 0 {
@@ -223,7 +228,7 @@ func (a *Adapter) Send(ip spack) error {
defer a.mu.RUnlock()

_, _, err := a.sessionLocked(
global.procSendPacket,
procSendPacket.Addr(),
uintptr(unsafe.Pointer(&ip[0])),
)
return err
32 changes: 12 additions & 20 deletions adapter_test.go
Original file line number Diff line number Diff line change
@@ -19,8 +19,7 @@ import (
)

func Test_Invalid_Ring_Capacity(t *testing.T) {
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

t.Run("lesser", func(t *testing.T) {
ap, err := wintun.CreateAdapter("testinvalidringlesser")
@@ -47,8 +46,7 @@ func Test_Invalid_Ring_Capacity(t *testing.T) {
}

func Test_Adapter_Create(t *testing.T) {
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

t.Run("create/start", func(t *testing.T) {
ap, err := wintun.CreateAdapter("createstart")
@@ -82,8 +80,7 @@ func Test_Adapter_Create(t *testing.T) {
}

func Test_Adapter_Stoped_Recv(t *testing.T) {
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

ap, err := wintun.CreateAdapter("testadapterrwstoped")
require.NoError(t, err)
@@ -97,8 +94,7 @@ func Test_Adapter_Stoped_Recv(t *testing.T) {
}

func Test_Adapter_Index(t *testing.T) {
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

name := "testadapterindex"

@@ -122,8 +118,8 @@ func Test_Adapter_Index(t *testing.T) {
}

func Test_Recv(t *testing.T) {
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

var (
ip = netip.AddrFrom4([4]byte{10, 1, 1, 11})
laddr = &net.UDPAddr{IP: ip.AsSlice(), Port: randPort()}
@@ -189,8 +185,7 @@ func Test_Recv(t *testing.T) {
}

func Test_RecvCtx(t *testing.T) {
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

ap, err := wintun.CreateAdapter("rcecvctx")
require.NoError(t, err)
@@ -212,8 +207,7 @@ func Test_RecvCtx(t *testing.T) {

func Test_Recving_Close(t *testing.T) {
// if remove Close and Recv mutex, will fatal Exception
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

for i := 0; i < 0xf; i++ {
func() {
@@ -241,8 +235,8 @@ func Test_Recving_Close(t *testing.T) {
}

func Test_Echo_UDP_Adapter(t *testing.T) {
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

var (
ip = netip.AddrFrom4([4]byte{10, 0, 1, 3})
laddr = &net.UDPAddr{IP: ip.AsSlice(), Port: randPort()}
@@ -316,8 +310,7 @@ func Test_Packet_Sniffing(t *testing.T) {
t.Skip("todo:maybe not route")
// route add 0.0.0.0 mask 0.0.0.0 10.0.1.3 metric 5 if 116

require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

var (
ip = netip.AddrFrom4([4]byte{10, 0, 1, 3})
@@ -379,8 +372,7 @@ func Test_Packet_Sniffing(t *testing.T) {
}

func Test_Session_Restart(t *testing.T) {
require.NoError(t, wintun.Load(wintun.DLL))
defer wintun.Release()
wintun.MustLoad(wintun.DLL)

ap, err := wintun.CreateAdapter("testsessionrestart")
require.NoError(t, err)
65 changes: 0 additions & 65 deletions dll.go

This file was deleted.

2 changes: 2 additions & 0 deletions embed_windows_386.go
Original file line number Diff line number Diff line change
@@ -4,5 +4,7 @@ import (
_ "embed"
)

// from https://www.wintun.net/builds/wintun-0.14.1.zip
//
//go:embed embed/wintun_x86.dll
var DLL Mem
4 changes: 2 additions & 2 deletions embed_windows_amd64.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import (
_ "embed"
)

// https://www.wintun.net/builds/wintun-0.14.1.zip

// from https://www.wintun.net/builds/wintun-0.14.1.zip
//
//go:embed embed/wintun_amd64.dll
var DLL Mem
2 changes: 2 additions & 0 deletions embed_windows_arm.go
Original file line number Diff line number Diff line change
@@ -4,5 +4,7 @@ import (
_ "embed"
)

// from https://www.wintun.net/builds/wintun-0.14.1.zip
//
//go:embed embed/wintun_arm.dll
var DLL Mem
2 changes: 2 additions & 0 deletions embed_windows_arm64.go
Original file line number Diff line number Diff line change
@@ -4,5 +4,7 @@ import (
_ "embed"
)

// from https://www.wintun.net/builds/wintun-0.14.1.zip
//
//go:embed embed/wintun_arm64.dll
var DLL Mem
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ require (
)

require (
github.com/lysShub/divert-go v0.0.0-20240525230502-6f79596abd61
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.16.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ 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/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/lysShub/divert-go v0.0.0-20240525230502-6f79596abd61 h1:qqarPA8zZe+LnIGHaleqDikaQ3QzvlAfDigXYRrboHU=
github.com/lysShub/divert-go v0.0.0-20240525230502-6f79596abd61/go.mod h1:OXuD4Q/Y84FyNiYy/sf9RVshvAC5/rvcHA6J7JvvtFM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3 changes: 2 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"context"
"log/slog"
"runtime"
"syscall"

"github.com/pkg/errors"
"golang.org/x/sys/windows"
@@ -59,7 +60,7 @@ func SetLogger(logger LoggerCallback) error {
}
}

_, _, err := global.calln(global.procSetLogger, callback)
_, _, err := syscall.SyscallN(procSetLogger.Addr(), callback)
if err != windows.ERROR_SUCCESS {
return errors.WithStack(err)
}
Loading