Skip to content

Commit 4f4d470

Browse files
authored
feat: Add wireguard to port-forward (#3851)
This allows replacement of the WebRTC networking!
1 parent a09ffd6 commit 4f4d470

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

cli/portforward.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -12,11 +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

19-
coderagent "github.com/coder/coder/agent"
20+
"github.com/coder/coder/agent"
2021
"github.com/coder/coder/cli/cliui"
2122
"github.com/coder/coder/codersdk"
2223
)
@@ -26,6 +27,7 @@ func portForward() *cobra.Command {
2627
tcpForwards []string // <port>:<port>
2728
udpForwards []string // <port>:<port>
2829
unixForwards []string // <path>:<path> OR <port>:<path>
30+
wireguard bool
2931
)
3032
cmd := &cobra.Command{
3133
Use: "port-forward <workspace>",
@@ -75,7 +77,7 @@ func portForward() *cobra.Command {
7577
return err
7678
}
7779

78-
workspace, agent, err := getWorkspaceAndAgent(ctx, cmd, client, codersdk.Me, args[0], false)
80+
workspace, workspaceAgent, err := getWorkspaceAndAgent(ctx, cmd, client, codersdk.Me, args[0], false)
7981
if err != nil {
8082
return err
8183
}
@@ -92,16 +94,21 @@ func portForward() *cobra.Command {
9294
err = cliui.Agent(ctx, cmd.ErrOrStderr(), cliui.AgentOptions{
9395
WorkspaceName: workspace.Name,
9496
Fetch: func(ctx context.Context) (codersdk.WorkspaceAgent, error) {
95-
return client.WorkspaceAgent(ctx, agent.ID)
97+
return client.WorkspaceAgent(ctx, workspaceAgent.ID)
9698
},
9799
})
98100
if err != nil {
99101
return xerrors.Errorf("await agent: %w", err)
100102
}
101103

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

@@ -159,11 +166,12 @@ func portForward() *cobra.Command {
159166
cmd.Flags().StringArrayVarP(&tcpForwards, "tcp", "p", []string{}, "Forward a TCP port from the workspace to the local machine")
160167
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")
161168
cmd.Flags().StringArrayVar(&unixForwards, "unix", []string{}, "Forward a Unix socket in the workspace to a local Unix socket or TCP port")
162-
169+
cmd.Flags().BoolVarP(&wireguard, "wireguard", "", false, "Specifies whether to use wireguard networking or not.")
170+
_ = cmd.Flags().MarkHidden("wireguard")
163171
return cmd
164172
}
165173

166-
func listenAndPortForward(ctx context.Context, cmd *cobra.Command, conn coderagent.Conn, wg *sync.WaitGroup, spec portForwardSpec) (net.Listener, error) {
174+
func listenAndPortForward(ctx context.Context, cmd *cobra.Command, conn agent.Conn, wg *sync.WaitGroup, spec portForwardSpec) (net.Listener, error) {
167175
_, _ = fmt.Fprintf(cmd.OutOrStderr(), "Forwarding '%v://%v' locally to '%v://%v' in the workspace\n", spec.listenNetwork, spec.listenAddress, spec.dialNetwork, spec.dialAddress)
168176

169177
var (
@@ -219,7 +227,7 @@ func listenAndPortForward(ctx context.Context, cmd *cobra.Command, conn coderage
219227
}
220228
defer remoteConn.Close()
221229

222-
coderagent.Bicopy(ctx, netConn, remoteConn)
230+
agent.Bicopy(ctx, netConn, remoteConn)
223231
}(netConn)
224232
}
225233
}(spec)
@@ -315,7 +323,7 @@ func parsePort(in string) (uint16, error) {
315323
}
316324

317325
func parseUnixPath(in string) (string, error) {
318-
path, err := coderagent.ExpandRelativeHomePath(strings.TrimSpace(in))
326+
path, err := agent.ExpandRelativeHomePath(strings.TrimSpace(in))
319327
if err != nil {
320328
return "", xerrors.Errorf("tidy path %q: %w", in, err)
321329
}

0 commit comments

Comments
 (0)