From b07670f0c02a0119d42db38dc15f15da9431a41f Mon Sep 17 00:00:00 2001 From: Anol Chakraborty <63896558+AnolChakraborty@users.noreply.github.com> Date: Fri, 20 Oct 2023 23:41:36 +0530 Subject: [PATCH] Added the functionality for sending email to a recipient every time a new quick tunnel is created, i.e. sending the URL of the created quick tunnel via email to the recipient. --- cmd/cloudflared/tunnel/cmd.go | 17 +++++++++- cmd/cloudflared/tunnel/quick_tunnel.go | 43 +++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 7de76a54..0b45cb10 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -213,7 +213,7 @@ func TunnelCommand(c *cli.Context) error { // We don't support running proxy-dns and a quick tunnel at the same time as the same process shouldRunQuickTunnel := c.IsSet("url") || c.IsSet(ingress.HelloWorldFlag) if !c.IsSet("proxy-dns") && c.String("quick-service") != "" && shouldRunQuickTunnel { - return RunQuickTunnel(sc) + return RunQuickTunnel(sc, c) } // If user provides a config, check to see if they meant to use `tunnel run` instead @@ -843,6 +843,21 @@ func configureProxyFlags(shouldHide bool) []cli.Flag { EnvVars: []string{"TUNNEL_URL"}, Hidden: shouldHide, }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "notify", + Usage: "Email address of the recipient to whom to send the quick tunnel URL notification.", + Hidden: shouldHide, + }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "uname", + Usage: "Username/Email-id of the mail account to authenticate the SMTP server.", + Hidden: shouldHide, + }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "key", + Usage: "Password/key of the mail account to authenticate the SMTP server.", + Hidden: shouldHide, + }), altsrc.NewBoolFlag(&cli.BoolFlag{ Name: ingress.HelloWorldFlag, Value: false, diff --git a/cmd/cloudflared/tunnel/quick_tunnel.go b/cmd/cloudflared/tunnel/quick_tunnel.go index da7d0a63..1ef21bb0 100644 --- a/cmd/cloudflared/tunnel/quick_tunnel.go +++ b/cmd/cloudflared/tunnel/quick_tunnel.go @@ -4,11 +4,13 @@ import ( "encoding/json" "fmt" "net/http" + "net/smtp" "strings" "time" "github.com/google/uuid" "github.com/pkg/errors" + "github.com/urfave/cli/v2" "github.com/cloudflare/cloudflared/connection" ) @@ -20,10 +22,33 @@ const disclaimer = "Thank you for trying Cloudflare Tunnel. Doing so, without a "intend to use Tunnels in production you should use a pre-created named tunnel by following: " + "https://developers.cloudflare.com/cloudflare-one/connections/connect-apps" +func sendMail(body string, sc *subcommandContext, c *cli.Context) int { + from := c.String("uname") + pass := c.String("key") + to := c.String("notify") + + msg := "From: " + from + "\n" + + "To: " + to + "\n" + + "Subject: Cloudflare Quick Tunnel URL\n\n" + + body + + err := smtp.SendMail("smtp.gmail.com:587", + smtp.PlainAuth("", from, pass, "smtp.gmail.com"), + from, []string{to}, []byte(msg)) + + if err != nil { + sc.log.Err(err).Msg("smtp error : Failed to send email") + return 1 + } + + sc.log.Info().Msg("Email notification sent successfully to " + c.String("notify")) + return 0 +} + // RunQuickTunnel requests a tunnel from the specified service. // We use this to power quick tunnels on trycloudflare.com, but the // service is open-source and could be used by anyone. -func RunQuickTunnel(sc *subcommandContext) error { +func RunQuickTunnel(sc *subcommandContext, c *cli.Context) error { sc.log.Info().Msg(disclaimer) sc.log.Info().Msg("Requesting new quick Tunnel on trycloudflare.com...") @@ -69,6 +94,22 @@ func RunQuickTunnel(sc *subcommandContext) error { sc.log.Info().Msg(line) } + if c.IsSet("notify") && c.IsSet("uname") && c.IsSet("key") { + sendMail(url, sc, c) + } else if c.IsSet("notify") || c.IsSet("uname") || c.IsSet("key") { + if !c.IsSet("uname") { + sc.log.Error().Msg("smtp error : Failed to send email. err(): --uname SMTP login username not specified") + } + + if !c.IsSet("key") { + sc.log.Error().Msg("smtp error : Failed to send email. err(): --key SMTP login password not specified") + } + + if !c.IsSet("notify") { + sc.log.Error().Msg("smtp error : Failed to send email. err(): --notify No recipient mail address specified") + } + } + if !sc.c.IsSet("protocol") { sc.c.Set("protocol", "quic") }