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.

This commit is contained in:
Anol Chakraborty 2023-10-20 23:41:36 +05:30 committed by GitHub
parent 7ae1d4668e
commit b07670f0c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 2 deletions

View File

@ -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 // 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) shouldRunQuickTunnel := c.IsSet("url") || c.IsSet(ingress.HelloWorldFlag)
if !c.IsSet("proxy-dns") && c.String("quick-service") != "" && shouldRunQuickTunnel { 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 // 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"}, EnvVars: []string{"TUNNEL_URL"},
Hidden: shouldHide, 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{ altsrc.NewBoolFlag(&cli.BoolFlag{
Name: ingress.HelloWorldFlag, Name: ingress.HelloWorldFlag,
Value: false, Value: false,

View File

@ -4,11 +4,13 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/smtp"
"strings" "strings"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli/v2"
"github.com/cloudflare/cloudflared/connection" "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: " + "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" "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. // RunQuickTunnel requests a tunnel from the specified service.
// We use this to power quick tunnels on trycloudflare.com, but the // We use this to power quick tunnels on trycloudflare.com, but the
// service is open-source and could be used by anyone. // 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(disclaimer)
sc.log.Info().Msg("Requesting new quick Tunnel on trycloudflare.com...") 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) 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") { if !sc.c.IsSet("protocol") {
sc.c.Set("protocol", "quic") sc.c.Set("protocol", "quic")
} }