-
Notifications
You must be signed in to change notification settings - Fork 8
/
syscalls_windows.go
81 lines (71 loc) · 1.71 KB
/
syscalls_windows.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
//go:build windows
// +build windows
package water
import (
"log"
"net/netip"
"golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/tun"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
)
type wintun struct {
dev tun.Device
}
func (w *wintun) Close() error {
return w.dev.Close()
}
func (w *wintun) Write(b []byte) (int, error) {
return w.dev.Write(b, 0)
}
func (w *wintun) Read(b []byte) (int, error) {
return w.dev.Read(b, 0)
}
func openDev(config Config) (ifce *Interface, err error) {
if config.DeviceType == TAP {
return nil, err
}
id := &windows.GUID{
0x0000000,
0xFFFF,
0xFFFF,
[8]byte{0xFF, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e},
}
dev, err := tun.CreateTUNWithRequestedGUID(config.PlatformSpecificParams.Name, id, 0)
if err != nil {
return nil, err
}
nativeTunDevice := dev.(*tun.NativeTun)
link := winipcfg.LUID(nativeTunDevice.LUID())
networks := config.PlatformSpecificParams.Network
if len(networks) == 0 {
panic("network is empty")
}
// set ip addresses
ipPrefix := []netip.Prefix{}
for _, n := range networks {
ip, err := netip.ParsePrefix(n)
if err != nil {
panic(err)
}
ipPrefix = append(ipPrefix, ip)
}
err = link.SetIPAddresses(ipPrefix)
if err != nil {
panic(err)
}
// set dns
servers := []netip.Addr{}
s1, _ := netip.ParseAddr("8.8.8.8")
s2, _ := netip.ParseAddr("1.1.1.1")
servers = append(servers, s1)
servers = append(servers, s2)
domains := []string{"wintun.dns"}
log.Printf("set dns servers:%v domain:%v", servers, domains)
err = link.SetDNS(windows.AF_INET, servers, domains)
if err != nil {
panic(err)
}
wintun := &wintun{dev: dev}
ifce = &Interface{isTAP: (config.DeviceType == TAP), ReadWriteCloser: wintun}
return ifce, nil
}