|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | +"golang.org/x/xerrors" |
| 7 | + |
| 8 | +"cdr.dev/slog" |
| 9 | +"github.com/coder/coder/v2/vpn" |
| 10 | +"github.com/coder/serpent" |
| 11 | +) |
| 12 | + |
| 13 | +func (r*RootCmd)vpnDaemonRun()*serpent.Command { |
| 14 | +var ( |
| 15 | +rpcReadFDint64 |
| 16 | +rpcWriteFDint64 |
| 17 | +) |
| 18 | + |
| 19 | +cmd:=&serpent.Command{ |
| 20 | +Use:"run", |
| 21 | +Short:"Run the VPN daemon on macOS.", |
| 22 | +Middleware:serpent.Chain( |
| 23 | +serpent.RequireNArgs(0), |
| 24 | +), |
| 25 | +Options: serpent.OptionSet{ |
| 26 | +{ |
| 27 | +Flag:"rpc-read-fd", |
| 28 | +Env:"CODER_VPN_DAEMON_RPC_READ_FD", |
| 29 | +Description:"The file descriptor for the pipe to read from the RPC connection.", |
| 30 | +Value:serpent.Int64Of(&rpcReadFD), |
| 31 | +Required:true, |
| 32 | +}, |
| 33 | +{ |
| 34 | +Flag:"rpc-write-fd", |
| 35 | +Env:"CODER_VPN_DAEMON_RPC_WRITE_FD", |
| 36 | +Description:"The file descriptor for the pipe to write to the RPC connection.", |
| 37 | +Value:serpent.Int64Of(&rpcWriteFD), |
| 38 | +Required:true, |
| 39 | +}, |
| 40 | +}, |
| 41 | +Handler:func(inv*serpent.Invocation)error { |
| 42 | +ctx:=inv.Context() |
| 43 | + |
| 44 | +ifrpcReadFD<0||rpcWriteFD<0 { |
| 45 | +returnxerrors.Errorf("rpc-read-fd (%v) and rpc-write-fd (%v) must be positive",rpcReadFD,rpcWriteFD) |
| 46 | +} |
| 47 | +ifrpcReadFD==rpcWriteFD { |
| 48 | +returnxerrors.Errorf("rpc-read-fd (%v) and rpc-write-fd (%v) must be different",rpcReadFD,rpcWriteFD) |
| 49 | +} |
| 50 | + |
| 51 | +pipe,err:=vpn.NewBidirectionalPipe(uintptr(rpcReadFD),uintptr(rpcWriteFD)) |
| 52 | +iferr!=nil { |
| 53 | +returnxerrors.Errorf("create bidirectional RPC pipe: %w",err) |
| 54 | +} |
| 55 | +deferpipe.Close() |
| 56 | + |
| 57 | +tunnel,err:=vpn.NewTunnel(ctx,slog.Make().Leveled(slog.LevelDebug),pipe, |
| 58 | +vpn.NewClient(), |
| 59 | +vpn.UseOSNetworkingStack(), |
| 60 | +vpn.UseAsLogger(), |
| 61 | +) |
| 62 | +iferr!=nil { |
| 63 | +returnxerrors.Errorf("create new tunnel for client: %w",err) |
| 64 | +} |
| 65 | +defertunnel.Close() |
| 66 | + |
| 67 | +<-ctx.Done() |
| 68 | +returnnil |
| 69 | +}, |
| 70 | +} |
| 71 | + |
| 72 | +returncmd |
| 73 | +} |