github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/tun/gvisor/open_windows.go (about) 1 package tun 2 3 import ( 4 "crypto/md5" 5 "fmt" 6 "unsafe" 7 8 "github.com/Asutorufa/yuhaiin/pkg/net/netlink" 9 wun "github.com/tailscale/wireguard-go/tun" 10 "golang.org/x/sys/windows" 11 ) 12 13 const ( 14 offset = 0 15 ) 16 17 func init() { 18 wun.WintunTunnelType = "yuhaiin" 19 } 20 21 func OpenWriter(sc netlink.TunScheme, mtu int) (netlink.Tun, error) { 22 if sc.Scheme != "tun" { 23 return nil, fmt.Errorf("invalid tun: %v", sc) 24 } 25 26 device, err := wun.CreateTUNWithRequestedGUID(sc.Name, 27 generateGUIDByDeviceName(sc.Name), mtu) 28 if err != nil { 29 return nil, fmt.Errorf("create tun failed: %w", err) 30 } 31 32 return NewDevice(device, offset), nil 33 } 34 35 func generateGUIDByDeviceName(name string) *windows.GUID { 36 hash := md5.New() 37 hash.Write([]byte("wintun")) 38 hash.Write([]byte("yuhaiin")) 39 hash.Write([]byte(name)) 40 sum := hash.Sum(nil) 41 return (*windows.GUID)(unsafe.Pointer(&sum[0])) 42 }