Added the functionality for sending email to a recipient every time a…

… new quick tunnel is created, i.e. sending email the URL of the quick tunnel that is created.
This commit is contained in:
Anol Chakraborty 2023-10-20 22:36:25 +05:30 committed by GitHub
parent 7ae1d4668e
commit 5c5305e906
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
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,

View File

@ -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("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")
}