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

Skip to content

Commit fda65ff

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

File tree

5 files changed

+379
-163
lines changed

5 files changed

+379
-163
lines changed

cli/server.go

Lines changed: 29 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,7 +196,7 @@ 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
}
@@ -327,7 +328,25 @@ func server() *cobra.Command {
327328
return shutdownConnsCtx
328329
},
329330
}
330-
errCh <- server.Serve(listener)
331+
332+
wg := errgroup.Group{}
333+
wg.Go(func() error {
334+
if shouldTunnel {
335+
defer tunnel.Listener.Close()
336+
}
337+
338+
return server.Serve(listener)
339+
})
340+
341+
if shouldTunnel {
342+
wg.Go(func() error {
343+
defer listener.Close()
344+
345+
return server.Serve(tunnel.Listener)
346+
})
347+
}
348+
349+
errCh <- wg.Wait()
331350
}()
332351

333352
config := createConfig(cmd)
@@ -393,7 +412,7 @@ func server() *cobra.Command {
393412
case <-cmd.Context().Done():
394413
coderAPI.Close()
395414
return cmd.Context().Err()
396-
case err := <-tunnelErrChan:
415+
case err := <-tunnel.ErrorChan:
397416
if err != nil {
398417
return err
399418
}
@@ -455,10 +474,10 @@ func server() *cobra.Command {
455474
spin.Stop()
456475
}
457476

458-
if dev && tunnel {
477+
if dev && shouldTunnel {
459478
_, _ = fmt.Fprintf(cmd.OutOrStdout(), cliui.Styles.Prompt.String()+"Waiting for dev tunnel to close...\n")
460479
closeTunnel()
461-
<-tunnelErrChan
480+
<-tunnel.ErrorChan
462481
}
463482

464483
_, _ = fmt.Fprintf(cmd.OutOrStdout(), cliui.Styles.Prompt.String()+"Waiting for WebSocket connections to close...\n")
@@ -504,7 +523,7 @@ func server() *cobra.Command {
504523
"Specifies the path to the private key for the certificate. It requires a PEM-encoded file")
505524
cliflag.StringVarP(root.Flags(), &tlsMinVersion, "tls-min-version", "", "CODER_TLS_MIN_VERSION", "tls12",
506525
`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,
526+
cliflag.BoolVarP(root.Flags(), &shouldTunnel, "tunnel", "", "CODER_DEV_TUNNEL", true,
508527
"Specifies whether the dev tunnel will be enabled or not. If specified, the interactive prompt will not display.")
509528
cliflag.StringArrayVarP(root.Flags(), &stunServers, "stun-server", "", "CODER_STUN_SERVERS", []string{
510529
"stun:stun.l.google.com:19302",

0 commit comments

Comments
 (0)