Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5934022

Browse files
committed
feat: use custom wireguard reverse proxy for dev tunnel
1 parent 89dde21 commit 5934022

File tree

5 files changed

+380
-163
lines changed

5 files changed

+380
-163
lines changed

cli/server.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"golang.org/x/mod/semver"
3434
"golang.org/x/oauth2"
3535
xgithub "golang.org/x/oauth2/github"
36+
"golang.org/x/sync/errgroup"
3637
"golang.org/x/xerrors"
3738
"google.golang.org/api/idtoken"
3839
"google.golang.org/api/option"
@@ -86,7 +87,7 @@ func server() *cobra.Command {
8687
tlsKeyFile string
8788
tlsMinVersion string
8889
turnRelayAddress string
89-
tunnel bool
90+
shouldTunnel bool
9091
stunServers []string
9192
trace bool
9293
secureAuthCookie bool
@@ -165,18 +166,18 @@ func server() *cobra.Command {
165166
accessURL = localURL.String()
166167
} else {
167168
// If an access URL is specified, always skip tunneling.
168-
tunnel = false
169+
shouldTunnel = false
169170
}
170171

171172
var (
172-
tunnelErrChan <-chan error
173173
ctxTunnel, closeTunnel = context.WithCancel(cmd.Context())
174+
tunnel = &devtunnel.Tunnel{ErrorChan: make(chan error, 1)}
174175
)
175176
defer closeTunnel()
176177

177178
// If we're attempting to tunnel in dev-mode, the access URL
178179
// needs to be changed to use the tunnel.
179-
if dev && tunnel {
180+
if dev && shouldTunnel {
180181
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), cliui.Styles.Wrap.Render(
181182
"Coder requires a URL accessible by workspaces you provision. "+
182183
"A free tunnel can be created for simple setup. This will "+
@@ -195,10 +196,11 @@ func server() *cobra.Command {
195196
}
196197
}
197198
if err == nil {
198-
accessURL, tunnelErrChan, err = devtunnel.New(ctxTunnel, localURL)
199+
tunnel, err = devtunnel.New(ctxTunnel, logger.Named("devtunnel"))
199200
if err != nil {
200201
return xerrors.Errorf("create tunnel: %w", err)
201202
}
203+
accessURL = tunnel.URL
202204
}
203205
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
204206
}
@@ -327,7 +329,25 @@ func server() *cobra.Command {
327329
return shutdownConnsCtx
328330
},
329331
}
330-
errCh <- server.Serve(listener)
332+
333+
wg := errgroup.Group{}
334+
wg.Go(func() error {
335+
if shouldTunnel {
336+
defer tunnel.Listener.Close()
337+
}
338+
339+
return server.Serve(listener)
340+
})
341+
342+
if shouldTunnel {
343+
wg.Go(func() error {
344+
defer listener.Close()
345+
346+
return server.Serve(tunnel.Listener)
347+
})
348+
}
349+
350+
errCh <- wg.Wait()
331351
}()
332352

333353
config := createConfig(cmd)
@@ -393,7 +413,7 @@ func server() *cobra.Command {
393413
case <-cmd.Context().Done():
394414
coderAPI.Close()
395415
return cmd.Context().Err()
396-
case err := <-tunnelErrChan:
416+
case err := <-tunnel.ErrorChan:
397417
if err != nil {
398418
return err
399419
}
@@ -455,10 +475,10 @@ func server() *cobra.Command {
455475
spin.Stop()
456476
}
457477

458-
if dev && tunnel {
478+
if dev && shouldTunnel {
459479
_, _ = fmt.Fprintf(cmd.OutOrStdout(), cliui.Styles.Prompt.String()+"Waiting for dev tunnel to close...\n")
460480
closeTunnel()
461-
<-tunnelErrChan
481+
<-tunnel.ErrorChan
462482
}
463483

464484
_, _ = fmt.Fprintf(cmd.OutOrStdout(), cliui.Styles.Prompt.String()+"Waiting for WebSocket connections to close...\n")
@@ -504,7 +524,7 @@ func server() *cobra.Command {
504524
"Specifies the path to the private key for the certificate. It requires a PEM-encoded file")
505525
cliflag.StringVarP(root.Flags(), &tlsMinVersion, "tls-min-version", "", "CODER_TLS_MIN_VERSION", "tls12",
506526
`Specifies the minimum supported version of TLS. Accepted values are "tls10", "tls11", "tls12" or "tls13"`)
507-
cliflag.BoolVarP(root.Flags(), &tunnel, "tunnel", "", "CODER_DEV_TUNNEL", true,
527+
cliflag.BoolVarP(root.Flags(), &shouldTunnel, "tunnel", "", "CODER_DEV_TUNNEL", true,
508528
"Specifies whether the dev tunnel will be enabled or not. If specified, the interactive prompt will not display.")
509529
cliflag.StringArrayVarP(root.Flags(), &stunServers, "stun-server", "", "CODER_STUN_SERVERS", []string{
510530
"stun:stun.l.google.com:19302",

0 commit comments

Comments
 (0)