diff --git a/src/tun/tun_bsd.go b/src/tun/tun_bsd.go index 5f42f52..da5b329 100644 --- a/src/tun/tun_bsd.go +++ b/src/tun/tun_bsd.go @@ -78,7 +78,7 @@ type in6_ifreq_lifetime struct { func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { iface, err := wgtun.CreateTUN(ifname, int(mtu)) if err != nil { - panic(err) + return fmt.Errorf("failed to create TUN: %w", err) } tun.iface = iface if mtu, err := iface.MTU(); err == nil { diff --git a/src/tun/tun_darwin.go b/src/tun/tun_darwin.go index 8fde93a..71f8270 100644 --- a/src/tun/tun_darwin.go +++ b/src/tun/tun_darwin.go @@ -7,6 +7,7 @@ package tun import ( "encoding/binary" + "fmt" "os" "strconv" "strings" @@ -24,7 +25,7 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { } iface, err := wgtun.CreateTUN(ifname, int(mtu)) if err != nil { - panic(err) + return fmt.Errorf("failed to create TUN: %w", err) } tun.iface = iface if m, err := iface.MTU(); err == nil { @@ -42,17 +43,17 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { func (tun *TunAdapter) setupFD(fd int32, addr string, mtu uint64) error { dfd, err := unix.Dup(int(fd)) if err != nil { - return err + return fmt.Errorf("failed to duplicate FD: %w", err) } err = unix.SetNonblock(dfd, true) if err != nil { unix.Close(dfd) - return err + return fmt.Errorf("failed to set FD as non-blocking: %w", err) } iface, err := wgtun.CreateTUNFromFile(os.NewFile(uintptr(dfd), "/dev/tun"), 0) if err != nil { unix.Close(dfd) - return err + return fmt.Errorf("failed to create TUN from FD: %w", err) } tun.iface = iface if m, err := iface.MTU(); err == nil { @@ -111,7 +112,7 @@ func (tun *TunAdapter) setupAddress(addr string) error { if fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0); err != nil { tun.log.Printf("Create AF_SYSTEM socket failed: %v.", err) - return err + return fmt.Errorf("failed to open AF_SYSTEM: %w", err) } var ar in6_aliasreq @@ -149,13 +150,13 @@ func (tun *TunAdapter) setupAddress(addr string) error { if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(darwin_SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 { // nolint:staticcheck err = errno tun.log.Errorf("Error in darwin_SIOCAIFADDR_IN6: %v", errno) - return err + return fmt.Errorf("failed to call SIOCAIFADDR_IN6: %w", err) } if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(unix.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 { // nolint:staticcheck err = errno tun.log.Errorf("Error in SIOCSIFMTU: %v", errno) - return err + return fmt.Errorf("failed to call SIOCSIFMTU: %w", err) } return err diff --git a/src/tun/tun_linux.go b/src/tun/tun_linux.go index 9cf2144..98d63db 100644 --- a/src/tun/tun_linux.go +++ b/src/tun/tun_linux.go @@ -19,7 +19,7 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { } iface, err := wgtun.CreateTUN(ifname, int(mtu)) if err != nil { - panic(err) + return fmt.Errorf("failed to create TUN: %w", err) } tun.iface = iface if mtu, err := iface.MTU(); err == nil { @@ -45,20 +45,20 @@ func (tun *TunAdapter) setupFD(fd int32, addr string, mtu uint64) error { func (tun *TunAdapter) setupAddress(addr string) error { nladdr, err := netlink.ParseAddr(addr) if err != nil { - return err + return fmt.Errorf("couldn't parse address %q: %w", addr, err) } nlintf, err := netlink.LinkByName(tun.Name()) if err != nil { - return err + return fmt.Errorf("failed to find link by name: %w", err) } if err := netlink.AddrAdd(nlintf, nladdr); err != nil { - return err + return fmt.Errorf("failed to add address to link: %w", err) } if err := netlink.LinkSetMTU(nlintf, int(tun.mtu)); err != nil { - return err + return fmt.Errorf("failed to set link MTU: %w", err) } if err := netlink.LinkSetUp(nlintf); err != nil { - return err + return fmt.Errorf("failed to bring link up: %w", err) } // Friendly output tun.log.Infof("Interface name: %s", tun.Name()) diff --git a/src/tun/tun_other.go b/src/tun/tun_other.go index bf8ac89..0ddd0c1 100644 --- a/src/tun/tun_other.go +++ b/src/tun/tun_other.go @@ -16,7 +16,7 @@ import ( func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { iface, err := wgtun.CreateTUN(ifname, mtu) if err != nil { - panic(err) + return fmt.Errorf("failed to create TUN: %w", err) } tun.iface = iface if mtu, err := iface.MTU(); err == nil {