-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwintun.go
124 lines (107 loc) · 3.19 KB
/
wintun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//go:build windows
// +build windows
package wintun
import (
"syscall"
"unsafe"
"github.com/lysShub/divert-go/dll"
"github.com/pkg/errors"
"golang.org/x/sys/windows"
)
func MustLoad[T string | Mem](p T) struct{} {
err := Load(p)
if err != nil && !errors.Is(err, ErrLoaded{}) {
panic(err)
}
return struct{}{}
}
func Load[T string | Mem](p T) error {
if wintun.Loaded() {
return ErrLoaded{}
}
switch p := any(p).(type) {
case string:
dll.ResetLazyDll(wintun, p)
case Mem:
dll.ResetLazyDll(wintun, []byte(p))
default:
panic("")
}
return nil
}
var (
wintun = dll.NewLazyDLL("wintun.dll")
procCreateAdapter = wintun.NewProc("WintunCreateAdapter")
procOpenAdapter = wintun.NewProc("WintunOpenAdapter")
procCloseAdapter = wintun.NewProc("WintunCloseAdapter")
procDeleteDriver = wintun.NewProc("WintunDeleteDriver")
procGetAdapterLUID = wintun.NewProc("WintunGetAdapterLUID")
procGetRunningDriverVersion = wintun.NewProc("WintunGetRunningDriverVersion")
procSetLogger = wintun.NewProc("WintunSetLogger")
procStartSession = wintun.NewProc("WintunStartSession")
procEndSession = wintun.NewProc("WintunEndSession")
procGetReadWaitEvent = wintun.NewProc("WintunGetReadWaitEvent")
procReceivePacket = wintun.NewProc("WintunReceivePacket")
procReleaseReceivePacket = wintun.NewProc("WintunReleaseReceivePacket")
procAllocateSendPacket = wintun.NewProc("WintunAllocateSendPacket")
procSendPacket = wintun.NewProc("WintunSendPacket")
)
func CreateAdapter(name string, opts ...Option) (*Adapter, error) {
if len(name) == 0 {
return nil, errors.New("require adapter name")
}
var o = defaultOptions()
for _, fn := range opts {
fn(o)
}
name16, err := windows.UTF16PtrFromString(name)
if err != nil {
return nil, errors.WithStack(err)
}
tunnelType16, err := windows.UTF16PtrFromString(o.tunType)
if err != nil {
return nil, errors.WithStack(err)
}
r1, _, err := syscall.SyscallN(
procCreateAdapter.Addr(),
uintptr(unsafe.Pointer(name16)),
uintptr(unsafe.Pointer(tunnelType16)),
uintptr(unsafe.Pointer(o.guid)),
)
if err != windows.ERROR_SUCCESS {
return nil, errors.WithStack(err)
}
ap := &Adapter{handle: r1}
return ap, ap.Start(o.ringBuff)
}
func OpenAdapter(name string) (*Adapter, error) {
if len(name) == 0 {
return nil, errors.New("require adapter name")
}
var name16 *uint16
name16, err := windows.UTF16PtrFromString(name)
if err != nil {
return nil, errors.WithStack(err)
}
r1, _, err := syscall.SyscallN(procOpenAdapter.Addr(), uintptr(unsafe.Pointer(name16)))
if err != windows.ERROR_SUCCESS {
return nil, errors.WithStack(err)
}
ap := &Adapter{handle: r1}
return ap, ap.Start(MinRingCapacity)
}
// todo: https://git.zx2c4.com/wintun-go/tree/wintun.go
func DriverVersion() (version uint32, err error) {
r0, _, err := syscall.SyscallN(procGetRunningDriverVersion.Addr())
if err != windows.ERROR_SUCCESS {
return 0, errors.WithStack(err)
}
return uint32(r0), nil
}
func DeleteDriver() error {
_, _, err := syscall.SyscallN(procDeleteDriver.Addr())
if err != windows.ERROR_SUCCESS {
return errors.WithStack(err)
}
return nil
}