Skip to content

Commit dc6c8d0

Browse files
committed
feat: Add wireguard to port-forward
This allows replacement of the WebRTC networking!
1 parent 2e1db6c commit dc6c8d0

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

cli/portforward.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212
"sync"
1313
"syscall"
1414

15+
"cdr.dev/slog"
1516
"github.com/pion/udp"
1617
"github.com/spf13/cobra"
1718
"golang.org/x/xerrors"
1819

20+
"github.com/coder/coder/agent"
1921
coderagent "github.com/coder/coder/agent"
2022
"github.com/coder/coder/cli/cliui"
2123
"github.com/coder/coder/codersdk"
@@ -26,6 +28,7 @@ func portForward() *cobra.Command {
2628
tcpForwards []string // <port>:<port>
2729
udpForwards []string // <port>:<port>
2830
unixForwards []string // <path>:<path> OR <port>:<path>
31+
wireguard bool
2932
)
3033
cmd := &cobra.Command{
3134
Use: "port-forward <workspace>",
@@ -75,7 +78,7 @@ func portForward() *cobra.Command {
7578
return err
7679
}
7780

78-
workspace, agent, err := getWorkspaceAndAgent(ctx, cmd, client, codersdk.Me, args[0], false)
81+
workspace, workspaceAgent, err := getWorkspaceAndAgent(ctx, cmd, client, codersdk.Me, args[0], false)
7982
if err != nil {
8083
return err
8184
}
@@ -92,16 +95,21 @@ func portForward() *cobra.Command {
9295
err = cliui.Agent(ctx, cmd.ErrOrStderr(), cliui.AgentOptions{
9396
WorkspaceName: workspace.Name,
9497
Fetch: func(ctx context.Context) (codersdk.WorkspaceAgent, error) {
95-
return client.WorkspaceAgent(ctx, agent.ID)
98+
return client.WorkspaceAgent(ctx, workspaceAgent.ID)
9699
},
97100
})
98101
if err != nil {
99102
return xerrors.Errorf("await agent: %w", err)
100103
}
101104

102-
conn, err := client.DialWorkspaceAgent(ctx, agent.ID, nil)
105+
var conn agent.Conn
106+
if !wireguard {
107+
conn, err = client.DialWorkspaceAgent(ctx, workspaceAgent.ID, nil)
108+
} else {
109+
conn, err = client.DialWorkspaceAgentTailnet(ctx, slog.Logger{}, workspaceAgent.ID)
110+
}
103111
if err != nil {
104-
return xerrors.Errorf("dial workspace agent: %w", err)
112+
return err
105113
}
106114
defer conn.Close()
107115

@@ -159,7 +167,8 @@ func portForward() *cobra.Command {
159167
cmd.Flags().StringArrayVarP(&tcpForwards, "tcp", "p", []string{}, "Forward a TCP port from the workspace to the local machine")
160168
cmd.Flags().StringArrayVar(&udpForwards, "udp", []string{}, "Forward a UDP port from the workspace to the local machine. The UDP connection has TCP-like semantics to support stateful UDP protocols")
161169
cmd.Flags().StringArrayVar(&unixForwards, "unix", []string{}, "Forward a Unix socket in the workspace to a local Unix socket or TCP port")
162-
170+
cmd.Flags().BoolVarP(&wireguard, "wireguard", "", false, "Specifies whether to use wireguard networking or not.")
171+
_ = cmd.Flags().MarkHidden("wireguard")
163172
return cmd
164173
}
165174

0 commit comments

Comments
 (0)