github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/tun/gvisor/open_unix.go (about) 1 //go:build unix && !linux 2 3 package tun 4 5 import ( 6 "fmt" 7 "os" 8 "strconv" 9 10 "github.com/Asutorufa/yuhaiin/pkg/net/netlink" 11 wun "github.com/tailscale/wireguard-go/tun" 12 "golang.org/x/sys/unix" 13 ) 14 15 const ( 16 offset = 4 17 ) 18 19 func OpenWriter(sc netlink.TunScheme, mtu int) (netlink.Tun, error) { 20 if len(sc.Name) >= unix.IFNAMSIZ { 21 return nil, fmt.Errorf("interface name too long: %s", sc.Name) 22 } 23 24 var device wun.Device 25 var err error 26 switch sc.Scheme { 27 case "fd": 28 device, err = wun.CreateTUNFromFile(os.NewFile(uintptr(sc.Fd), strconv.Itoa(sc.Fd)), mtu) 29 case "tun": 30 device, err = wun.CreateTUN(sc.Name, mtu) 31 } 32 if err != nil { 33 return nil, fmt.Errorf("create tun failed: %w", err) 34 } 35 return NewDevice(device, offset), nil 36 }