Skip to content

Commit e03f240

Browse files
committed
fixup non-darwin tun
1 parent 20fb40e commit e03f240

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

vpn/tun.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build !darwin
2+
3+
package vpn
4+
5+
import "github.com/tailscale/wireguard-go/tun"
6+
7+
// This is a no-op on non-Darwin platforms.
8+
func makeTUN(int) (tun.Device, error) {
9+
return nil, nil
10+
}

vpn/tun_darwin.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build darwin
2+
3+
package vpn
4+
5+
import (
6+
"os"
7+
8+
"github.com/tailscale/wireguard-go/tun"
9+
"golang.org/x/sys/unix"
10+
"golang.org/x/xerrors"
11+
)
12+
13+
func makeTUN(tunFD int) (tun.Device, error) {
14+
dupTunFd, err := unix.Dup(tunFD)
15+
if err != nil {
16+
return nil, xerrors.Errorf("dup tun fd: %w", err)
17+
}
18+
19+
err = unix.SetNonblock(dupTunFd, true)
20+
if err != nil {
21+
unix.Close(dupTunFd)
22+
return nil, xerrors.Errorf("set nonblock: %w", err)
23+
}
24+
fileTun, err := tun.CreateTUNFromFile(os.NewFile(uintptr(dupTunFd), "/dev/tun"), 0)
25+
if err != nil {
26+
unix.Close(dupTunFd)
27+
return nil, xerrors.Errorf("create TUN from File: %w", err)
28+
}
29+
return fileTun, nil
30+
}

vpn/tunnel.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11-
"os"
1211
"reflect"
1312
"strconv"
1413
"sync"
1514
"unicode"
1615

17-
"golang.org/x/sys/unix"
1816
"golang.org/x/xerrors"
1917

2018
"github.com/hashicorp/go-multierror"
21-
"github.com/tailscale/wireguard-go/tun"
2219

2320
"github.com/coder/coder/v2/codersdk"
2421
"github.com/coder/coder/v2/tailnet/proto"
@@ -185,6 +182,7 @@ func (t *Tunnel) start(req *StartRequest) error {
185182
}
186183
sdk.HTTPClient.Transport = transport
187184

185+
// No-op on non-Darwin platforms.
188186
dev, err := makeTUN(int(req.GetTunnelFileDescriptor()))
189187
if err != nil {
190188
return xerrors.Errorf("make TUN: %w", err)
@@ -317,25 +315,6 @@ func quote(key string) string {
317315
return quoted
318316
}
319317

320-
func makeTUN(tunFD int) (tun.Device, error) {
321-
dupTunFd, err := unix.Dup(tunFD)
322-
if err != nil {
323-
return nil, xerrors.Errorf("dup tun fd: %w", err)
324-
}
325-
326-
err = unix.SetNonblock(dupTunFd, true)
327-
if err != nil {
328-
unix.Close(dupTunFd)
329-
return nil, xerrors.Errorf("set nonblock: %w", err)
330-
}
331-
fileTun, err := tun.CreateTUNFromFile(os.NewFile(uintptr(dupTunFd), "/dev/tun"), 0)
332-
if err != nil {
333-
unix.Close(dupTunFd)
334-
return nil, xerrors.Errorf("create TUN from File: %w", err)
335-
}
336-
return fileTun, nil
337-
}
338-
339318
func convertWorkspaceUpdate(update *proto.WorkspaceUpdate) *PeerUpdate {
340319
out := &PeerUpdate{
341320
UpsertedWorkspaces: make([]*Workspace, len(update.UpsertedWorkspaces)),

0 commit comments

Comments
 (0)