diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 7de76a54..96770e0a 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -195,6 +195,7 @@ func TunnelCommand(c *cli.Context) error { // --name required // --url or --hello-world required // --hostname optional + if name := c.String("name"); name != "" { hostname, err := validation.ValidateHostname(c.String("hostname")) if err != nil { @@ -213,7 +214,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 +844,21 @@ func configureProxyFlags(shouldHide bool) []cli.Flag { EnvVars: []string{"TUNNEL_URL"}, Hidden: shouldHide, }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "notify", + Usage: "Email address to send the 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..9448aeb5 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,38 @@ 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: `cloudflared-notify` Notification\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 { + + for _, line := range AsciiBox([]string{"`cloudflared-notify` a fork of `cloudflared` by Anol Chakraborty", "Github: https://github.com/AnolChakraborty/cloudflared-notify"}, 2) { + sc.log.Info().Msg(line) + } + sc.log.Info().Msg(disclaimer) sc.log.Info().Msg("Requesting new quick Tunnel on trycloudflare.com...") @@ -62,13 +92,26 @@ func RunQuickTunnel(sc *subcommandContext) error { url = "https://" + url } - for _, line := range AsciiBox([]string{ - "Your quick Tunnel has been created! Visit it at (it may take some time to be reachable):", - url, - }, 2) { + for _, line := range AsciiBox([]string{"Your quick Tunnel has been created! Visit it at (it may take some time to be reachable):", url}, 2) { sc.log.Info().Msg(line) } + if c.IsSet("notify") && c.IsSet("uname") && c.IsSet("key") { + sendMail(url, sc, c) + } else { + 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 receipient mail address specified") + } + } + if !sc.c.IsSet("protocol") { sc.c.Set("protocol", "quic") }