mirror of
https://github.com/coder/coder.git
synced 2026-06-04 05:28:20 +00:00
bddb808b25
Fixes all our Go file imports to match the preferred spec that we've _mostly_ been using. For example: ``` import ( "context" "time" "github.com/prometheus/client_golang/prometheus" "golang.org/x/xerrors" "gopkg.in/natefinch/lumberjack.v2" "cdr.dev/slog/v3" "github.com/coder/coder/v2/codersdk/agentsdk" "github.com/coder/serpent" ) ``` 3 groups: standard library, 3rd partly libs, Coder libs. This PR makes the change across the codebase. The PR in the stack above modifies our formatting to maintain this state of affairs, and is a separate PR so it's possible to review that one in detail.
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
//go:build darwin
|
|
|
|
package main
|
|
|
|
import "C"
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/vpn"
|
|
)
|
|
|
|
const (
|
|
ErrDupReadFD = -2
|
|
ErrDupWriteFD = -3
|
|
ErrOpenPipe = -4
|
|
ErrNewTunnel = -5
|
|
)
|
|
|
|
// OpenTunnel creates a new VPN tunnel by `dup`ing the provided 'PIPE'
|
|
// file descriptors for reading and writing.
|
|
//
|
|
//export OpenTunnel
|
|
func OpenTunnel(cReadFD, cWriteFD int32) int32 {
|
|
ctx := context.Background()
|
|
|
|
readFD, err := unix.Dup(int(cReadFD))
|
|
if err != nil {
|
|
return ErrDupReadFD
|
|
}
|
|
|
|
writeFD, err := unix.Dup(int(cWriteFD))
|
|
if err != nil {
|
|
unix.Close(readFD)
|
|
return ErrDupWriteFD
|
|
}
|
|
|
|
conn, err := vpn.NewBidirectionalPipe(uintptr(readFD), uintptr(writeFD))
|
|
if err != nil {
|
|
unix.Close(readFD)
|
|
unix.Close(writeFD)
|
|
return ErrOpenPipe
|
|
}
|
|
|
|
// We log everything, as filtering is done by whatever renders the OS
|
|
// logs.
|
|
_, err = vpn.NewTunnel(ctx, slog.Make().Leveled(slog.LevelDebug), conn,
|
|
vpn.NewClient(),
|
|
vpn.UseOSNetworkingStack(),
|
|
vpn.UseAsLogger(),
|
|
)
|
|
if err != nil {
|
|
unix.Close(readFD)
|
|
unix.Close(writeFD)
|
|
return ErrNewTunnel
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func main() {}
|