diff --git a/olm/olm.go b/olm/olm.go index 5943456..0e622ee 100644 --- a/olm/olm.go +++ b/olm/olm.go @@ -56,6 +56,8 @@ type Config struct { Version string OrgID string // DoNotCreateNewClient bool + + FileDescriptorTun uint32 } var ( @@ -366,16 +368,16 @@ func TunnelProcess(ctx context.Context, config Config, id string, secret string, } tdev, err = func() (tun.Device, error) { - if runtime.GOOS == "darwin" { + if config.FileDescriptorTun != 0 { + return createTUNFromFD(config.FileDescriptorTun, config.MTU) + } + if runtime.GOOS == "darwin" { // this is if we dont pass a fd interfaceName, err := findUnusedUTUN() if err != nil { return nil, err } return tun.CreateTUN(interfaceName, config.MTU) } - if tunFdStr := os.Getenv(ENV_WG_TUN_FD); tunFdStr != "" { - return createTUNFromFD(tunFdStr, config.MTU) - } return tun.CreateTUN(interfaceName, config.MTU) }() diff --git a/olm/unix.go b/olm/unix.go index 4d8e3b6..5f5cf0e 100644 --- a/olm/unix.go +++ b/olm/unix.go @@ -5,25 +5,19 @@ package olm import ( "net" "os" - "strconv" "golang.org/x/sys/unix" "golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/tun" ) -func createTUNFromFD(tunFdStr string, mtuInt int) (tun.Device, error) { - fd, err := strconv.ParseUint(tunFdStr, 10, 32) +func createTUNFromFD(tunFd uint32, mtuInt int) (tun.Device, error) { + err := unix.SetNonblock(int(tunFd), true) if err != nil { return nil, err } - err = unix.SetNonblock(int(fd), true) - if err != nil { - return nil, err - } - - file := os.NewFile(uintptr(fd), "") + file := os.NewFile(uintptr(tunFd), "") return tun.CreateTUNFromFile(file, mtuInt) } func uapiOpen(interfaceName string) (*os.File, error) {