-
Notifications
You must be signed in to change notification settings - Fork 9
/
divert_embedded.go
183 lines (157 loc) · 3.66 KB
/
divert_embedded.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//go:build windows && divert_embedded
package divert
import (
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
"github.com/imgk/divert-go/memmod"
)
var (
winDivert = (*lazyDLL)(nil)
winDivertOpen = (*lazyProc)(nil)
winDivertCalcChecksums = (*lazyProc)(nil)
)
// Open is ...
func Open(filter string, layer Layer, priority int16, flags uint64) (h *Handle, err error) {
once.Do(func() {
dll := newLazyDLL("WinDivert.dll", nil)
if er := dll.Load(); er != nil {
err = er
return
}
winDivert = dll
proc := winDivert.NewProc("WinDivertOpen")
if er := proc.Find(); er != nil {
err = er
return
}
winDivertOpen = proc
proc = winDivert.NewProc("WinDivertHelperCalcChecksums")
if er := proc.Find(); er != nil {
err = er
return
}
winDivertCalcChecksums = proc
vers := map[string]struct{}{
"2.0": {},
"2.1": {},
"2.2": {},
}
ver, er := func() (ver string, err error) {
h, err := open("false", LayerNetwork, PriorityDefault, FlagDefault)
if err != nil {
return
}
defer func() {
err = h.Close()
}()
major, err := h.GetParam(VersionMajor)
if err != nil {
return
}
minor, err := h.GetParam(VersionMinor)
if err != nil {
return
}
ver = strings.Join([]string{strconv.Itoa(int(major)), strconv.Itoa(int(minor))}, ".")
return
}()
if er != nil {
err = er
return
}
if _, ok := vers[ver]; !ok {
err = fmt.Errorf("unsupported windivert version: %v", ver)
}
})
if err != nil {
return
}
return open(filter, layer, priority, flags)
}
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
}
const ourModule windows.Handle = 0
resInfo, err := windows.FindResource(ourModule, d.Name, windows.RT_RCDATA)
if err != nil {
return fmt.Errorf("Unable to find \"%v\" RCDATA resource: %w", d.Name, err)
}
data, err := windows.LoadResourceData(ourModule, resInfo)
if err != nil {
return fmt.Errorf("Unable to load resource: %w", err)
}
module, err := memmod.LoadLibrary(data)
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)
}
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
}
func (p *lazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
return syscall.SyscallN(p.Addr(), a...)
}