Skip to content

Commit 3deef61

Browse files
committed
TUN-3213: Create, route and run named tunnels in one command
1 parent 70114c2 commit 3deef61

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

cmd/cloudflared/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func action(version string, shutdownC, graceShutdownC chan struct{}) cli.ActionF
129129
tags := make(map[string]string)
130130
tags["hostname"] = c.String("hostname")
131131
raven.SetTagsContext(tags)
132-
raven.CapturePanic(func() { err = tunnel.StartServer(c, version, shutdownC, graceShutdownC, nil) }, nil)
132+
raven.CapturePanic(func() { err = tunnel.TunnelCommand(c) }, nil)
133133
exitCode := 0
134134
if err != nil {
135135
handleError(err)

cmd/cloudflared/tunnel/cmd.go

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/cloudflare/cloudflared/sshserver"
3434
"github.com/cloudflare/cloudflared/tlsconfig"
3535
"github.com/cloudflare/cloudflared/tunneldns"
36+
"github.com/cloudflare/cloudflared/tunnelstore"
3637
"github.com/cloudflare/cloudflared/websocket"
3738

3839
"github.com/coreos/go-systemd/daemon"
@@ -169,6 +170,7 @@ func Commands() []*cli.Command {
169170
c.Hidden = false
170171
subcommands = append(subcommands, &c)
171172
}
173+
172174
subcommands = append(subcommands, buildCreateCommand())
173175
subcommands = append(subcommands, buildListCommand())
174176
subcommands = append(subcommands, buildDeleteCommand())
@@ -178,7 +180,7 @@ func Commands() []*cli.Command {
178180

179181
cmds = append(cmds, &cli.Command{
180182
Name: "tunnel",
181-
Action: cliutil.ErrorHandler(tunnel),
183+
Action: cliutil.ErrorHandler(TunnelCommand),
182184
Before: Before,
183185
Category: "Tunnel",
184186
Usage: "Make a locally-running web service accessible over the internet using Argo Tunnel.",
@@ -208,14 +210,63 @@ func Commands() []*cli.Command {
208210
return cmds
209211
}
210212

211-
func tunnel(c *cli.Context) error {
212-
return StartServer(c, version, shutdownC, graceShutdownC, nil)
213+
func TunnelCommand(c *cli.Context) error {
214+
if name := c.String("name"); name != "" {
215+
return adhocNamedTunnel(c, name)
216+
}
217+
logger, err := createLogger(c, false)
218+
if err != nil {
219+
return errors.Wrap(err, "error setting up logger")
220+
}
221+
return StartServer(c, version, shutdownC, graceShutdownC, nil, logger)
213222
}
214223

215224
func Init(v string, s, g chan struct{}) {
216225
version, shutdownC, graceShutdownC = v, s, g
217226
}
218227

228+
// adhocNamedTunnel create, route and run a named tunnel in one command
229+
func adhocNamedTunnel(c *cli.Context, name string) error {
230+
sc, err := newSubcommandContext(c)
231+
if err != nil {
232+
return err
233+
}
234+
235+
tunnel, ok, err := sc.tunnelActive(name)
236+
if err != nil || !ok {
237+
tunnel, err = sc.create(name)
238+
if err != nil {
239+
return errors.Wrap(err, "failed to create tunnel")
240+
}
241+
} else {
242+
sc.logger.Infof("Tunnel already created with ID %s", tunnel.ID)
243+
}
244+
245+
if r, ok := routeFromFlag(c, tunnel.ID); ok {
246+
if err := sc.route(tunnel.ID, r); err != nil {
247+
sc.logger.Errorf("failed to create route, please create it manually. err: %v.", err)
248+
} else {
249+
sc.logger.Infof(r.SuccessSummary())
250+
}
251+
}
252+
253+
if err := sc.run(tunnel.ID); err != nil {
254+
return errors.Wrap(err, "error running tunnel")
255+
}
256+
257+
return nil
258+
}
259+
260+
func routeFromFlag(c *cli.Context, tunnelID uuid.UUID) (tunnelstore.Route, bool) {
261+
if hostname := c.String("hostname"); hostname != "" {
262+
if lbPool := c.String("lb-pool"); lbPool != "" {
263+
return tunnelstore.NewLBRoute(hostname, lbPool), true
264+
}
265+
return tunnelstore.NewDNSRoute(hostname), true
266+
}
267+
return nil, false
268+
}
269+
219270
func createLogger(c *cli.Context, isTransport bool) (logger.Service, error) {
220271
loggerOpts := []logger.Option{}
221272

@@ -240,12 +291,7 @@ func createLogger(c *cli.Context, isTransport bool) (logger.Service, error) {
240291
return logger.New(loggerOpts...)
241292
}
242293

243-
func StartServer(c *cli.Context, version string, shutdownC, graceShutdownC chan struct{}, namedTunnel *origin.NamedTunnelConfig) error {
244-
logger, err := createLogger(c, false)
245-
if err != nil {
246-
return cliutil.PrintLoggerSetupError("error setting up logger", err)
247-
}
248-
294+
func StartServer(c *cli.Context, version string, shutdownC, graceShutdownC chan struct{}, namedTunnel *origin.NamedTunnelConfig, logger logger.Service) error {
249295
_ = raven.SetDSN(sentryDSN)
250296
var wg sync.WaitGroup
251297
listeners := gracenet.Net{}
@@ -614,7 +660,7 @@ func dbConnectCmd() *cli.Command {
614660
cmd.Action = cliutil.ErrorHandler(func(c *cli.Context) error {
615661
err := dbconnect.CmdAction(c)
616662
if err == nil {
617-
err = tunnel(c)
663+
err = TunnelCommand(c)
618664
}
619665
return err
620666
})
@@ -1069,6 +1115,13 @@ func tunnelFlags(shouldHide bool) []cli.Flag {
10691115
EnvVars: []string{"TUNNEL_BASTION"},
10701116
Hidden: shouldHide,
10711117
}),
1118+
altsrc.NewStringFlag(&cli.StringFlag{
1119+
Name: "name",
1120+
Aliases: []string{"n"},
1121+
EnvVars: []string{"TUNNEL_NAME"},
1122+
Usage: "Stable name to identify the tunnel. Using this flag will create, route and run a tunnel. For production usage, execute each command separately",
1123+
Hidden: true,
1124+
}),
10721125
}
10731126
}
10741127

cmd/cloudflared/tunnel/subcommand_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (sc *subcommandContext) run(tunnelID uuid.UUID) error {
236236
if err != nil {
237237
return err
238238
}
239-
return StartServer(sc.c, version, shutdownC, graceShutdownC, &origin.NamedTunnelConfig{Auth: *credentials, ID: tunnelID})
239+
return StartServer(sc.c, version, shutdownC, graceShutdownC, &origin.NamedTunnelConfig{Auth: *credentials, ID: tunnelID}, sc.logger)
240240
}
241241

242242
func (sc *subcommandContext) cleanupConnections(tunnelIDs []uuid.UUID) error {

0 commit comments

Comments
 (0)