2018-05-01 23:45:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-10 16:42:09 +00:00
|
|
|
"math/rand"
|
2018-11-06 23:33:08 +00:00
|
|
|
"strings"
|
2018-05-01 23:45:06 +00:00
|
|
|
"time"
|
|
|
|
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/getsentry/raven-go"
|
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli/v2"
|
2021-05-10 18:32:44 +00:00
|
|
|
"go.uber.org/automaxprocs/maxprocs"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/access"
|
2020-06-12 16:20:36 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
|
2021-03-08 16:46:23 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/proxydns"
|
2018-10-08 19:20:28 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/tunnel"
|
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/updater"
|
2021-03-16 22:36:46 +00:00
|
|
|
"github.com/cloudflare/cloudflared/config"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
2018-10-08 19:20:28 +00:00
|
|
|
"github.com/cloudflare/cloudflared/metrics"
|
2020-05-01 15:30:50 +00:00
|
|
|
"github.com/cloudflare/cloudflared/overwatch"
|
2020-04-13 17:22:00 +00:00
|
|
|
"github.com/cloudflare/cloudflared/watcher"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-04-18 15:42:48 +00:00
|
|
|
versionText = "Print the version"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Version = "DEV"
|
|
|
|
BuildTime = "unknown"
|
2018-11-06 23:33:08 +00:00
|
|
|
// Mostly network errors that we don't want reported back to Sentry, this is done by substring match.
|
|
|
|
ignoredErrors = []string{
|
|
|
|
"connection reset by peer",
|
|
|
|
"An existing connection was forcibly closed by the remote host.",
|
|
|
|
"use of closed connection",
|
|
|
|
"You need to enable Argo Smart Routing",
|
|
|
|
"3001 connection closed",
|
|
|
|
"3002 connection dropped",
|
|
|
|
"rpc exception: dial tcp",
|
|
|
|
"rpc exception: EOF",
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-02-10 16:42:09 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2018-05-01 23:45:06 +00:00
|
|
|
metrics.RegisterBuildInfo(BuildTime, Version)
|
|
|
|
raven.SetRelease(Version)
|
2021-05-10 18:32:44 +00:00
|
|
|
maxprocs.Set()
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
// Graceful shutdown channel used by the app. When closed, app must terminate gracefully.
|
2018-05-01 23:45:06 +00:00
|
|
|
// Windows service manager closes this channel when it receives stop command.
|
|
|
|
graceShutdownC := make(chan struct{})
|
|
|
|
|
2019-01-24 21:11:53 +00:00
|
|
|
cli.VersionFlag = &cli.BoolFlag{
|
|
|
|
Name: "version",
|
|
|
|
Aliases: []string{"v", "V"},
|
|
|
|
Usage: versionText,
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
app := &cli.App{}
|
|
|
|
app.Name = "cloudflared"
|
2018-10-08 19:20:28 +00:00
|
|
|
app.Usage = "Cloudflare's command-line tool and agent"
|
2020-06-11 16:08:05 +00:00
|
|
|
app.UsageText = "cloudflared [global options] [command] [command options]"
|
2019-04-18 15:42:48 +00:00
|
|
|
app.Copyright = fmt.Sprintf(
|
|
|
|
`(c) %d Cloudflare Inc.
|
|
|
|
Your installation of cloudflared software constitutes a symbol of your signature indicating that you accept
|
|
|
|
the terms of the Cloudflare License (https://developers.cloudflare.com/argo-tunnel/license/),
|
|
|
|
Terms (https://www.cloudflare.com/terms/) and Privacy Policy (https://www.cloudflare.com/privacypolicy/).`,
|
|
|
|
time.Now().Year(),
|
|
|
|
)
|
2018-05-01 23:45:06 +00:00
|
|
|
app.Version = fmt.Sprintf("%s (built %s)", Version, BuildTime)
|
2019-04-18 15:42:48 +00:00
|
|
|
app.Description = `cloudflared connects your machine or user identity to Cloudflare's global network.
|
2018-10-08 19:20:28 +00:00
|
|
|
You can use it to authenticate a session to reach an API behind Access, route web traffic to this machine,
|
2021-01-25 16:17:00 +00:00
|
|
|
and configure access control.
|
|
|
|
|
|
|
|
See https://developers.cloudflare.com/argo-tunnel/ for more in-depth documentation.`
|
2018-10-08 19:20:28 +00:00
|
|
|
app.Flags = flags()
|
2021-01-25 21:51:58 +00:00
|
|
|
app.Action = action(graceShutdownC)
|
2019-01-24 21:11:53 +00:00
|
|
|
app.Commands = commands(cli.ShowVersion)
|
2018-10-08 19:20:28 +00:00
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
tunnel.Init(Version, graceShutdownC) // we need this to support the tunnel sub command...
|
|
|
|
access.Init(graceShutdownC)
|
2020-08-12 16:16:14 +00:00
|
|
|
updater.Init(Version)
|
2021-01-25 21:51:58 +00:00
|
|
|
runApp(app, graceShutdownC)
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2019-01-24 21:11:53 +00:00
|
|
|
func commands(version func(c *cli.Context)) []*cli.Command {
|
2018-10-08 19:20:28 +00:00
|
|
|
cmds := []*cli.Command{
|
2018-08-15 22:23:34 +00:00
|
|
|
{
|
2020-08-12 16:16:14 +00:00
|
|
|
Name: "update",
|
2021-03-16 22:36:46 +00:00
|
|
|
Action: cliutil.ConfiguredAction(updater.Update),
|
2020-08-12 16:16:14 +00:00
|
|
|
Usage: "Update the agent if a new version exists",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "beta",
|
|
|
|
Usage: "specify if you wish to update to the latest beta version",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
Usage: "specify if you wish to force an upgrade to the latest version regardless of the current version",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "staging",
|
|
|
|
Usage: "specify if you wish to use the staging url for updating",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "version",
|
|
|
|
Usage: "specify a version you wish to upgrade or downgrade to",
|
|
|
|
Hidden: false,
|
|
|
|
},
|
|
|
|
},
|
2018-10-08 19:20:28 +00:00
|
|
|
Description: `Looks for a new version on the official download server.
|
|
|
|
If a new version exists, updates the agent binary and quits.
|
|
|
|
Otherwise, does nothing.
|
2018-08-15 22:23:34 +00:00
|
|
|
|
2021-02-11 14:39:56 +00:00
|
|
|
To determine if an update happened in a script, check for error code 11.`,
|
2018-05-01 23:45:06 +00:00
|
|
|
},
|
2019-01-24 21:11:53 +00:00
|
|
|
{
|
|
|
|
Name: "version",
|
|
|
|
Action: func(c *cli.Context) (err error) {
|
|
|
|
version(c)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Usage: versionText,
|
|
|
|
Description: versionText,
|
|
|
|
},
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2018-10-08 19:20:28 +00:00
|
|
|
cmds = append(cmds, tunnel.Commands()...)
|
2021-03-08 16:46:23 +00:00
|
|
|
cmds = append(cmds, proxydns.Command(false))
|
2018-10-08 19:20:28 +00:00
|
|
|
cmds = append(cmds, access.Commands()...)
|
|
|
|
return cmds
|
2018-08-15 22:23:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
func flags() []cli.Flag {
|
|
|
|
flags := tunnel.Flags()
|
|
|
|
return append(flags, access.Flags()...)
|
|
|
|
}
|
2018-10-06 19:27:35 +00:00
|
|
|
|
2018-12-17 17:18:34 +00:00
|
|
|
func isEmptyInvocation(c *cli.Context) bool {
|
|
|
|
return c.NArg() == 0 && c.NumFlags() == 0
|
|
|
|
}
|
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
func action(graceShutdownC chan struct{}) cli.ActionFunc {
|
2021-03-16 22:36:46 +00:00
|
|
|
return cliutil.ConfiguredAction(func(c *cli.Context) (err error) {
|
2018-12-17 17:18:34 +00:00
|
|
|
if isEmptyInvocation(c) {
|
2021-01-25 21:51:58 +00:00
|
|
|
return handleServiceMode(c, graceShutdownC)
|
2018-12-17 17:18:34 +00:00
|
|
|
}
|
2018-10-08 19:20:28 +00:00
|
|
|
tags := make(map[string]string)
|
|
|
|
tags["hostname"] = c.String("hostname")
|
|
|
|
raven.SetTagsContext(tags)
|
2020-07-30 17:00:57 +00:00
|
|
|
raven.CapturePanic(func() { err = tunnel.TunnelCommand(c) }, nil)
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
2020-11-09 22:53:51 +00:00
|
|
|
captureError(err)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2020-11-09 22:53:51 +00:00
|
|
|
return err
|
|
|
|
})
|
2018-08-15 22:23:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
func userHomeDir() (string, error) {
|
|
|
|
// This returns the home dir of the executing user using OS-specific method
|
|
|
|
// for discovering the home dir. It's not recommended to call this function
|
|
|
|
// when the user has root permission as $HOME depends on what options the user
|
|
|
|
// use with sudo.
|
|
|
|
homeDir, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Cannot determine home directory for the user")
|
|
|
|
}
|
|
|
|
return homeDir, nil
|
|
|
|
}
|
2018-11-06 23:33:08 +00:00
|
|
|
|
|
|
|
// In order to keep the amount of noise sent to Sentry low, typical network errors can be filtered out here by a substring match.
|
2020-11-09 22:53:51 +00:00
|
|
|
func captureError(err error) {
|
2018-11-06 23:33:08 +00:00
|
|
|
errorMessage := err.Error()
|
|
|
|
for _, ignoredErrorMessage := range ignoredErrors {
|
|
|
|
if strings.Contains(errorMessage, ignoredErrorMessage) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
raven.CaptureError(err, nil)
|
|
|
|
}
|
2020-04-13 17:22:00 +00:00
|
|
|
|
|
|
|
// cloudflared was started without any flags
|
2020-11-15 01:49:44 +00:00
|
|
|
func handleServiceMode(c *cli.Context, shutdownC chan struct{}) error {
|
2020-11-25 06:55:13 +00:00
|
|
|
log := logger.CreateLoggerFromContext(c, logger.DisableTerminalLog)
|
2020-04-29 20:51:32 +00:00
|
|
|
|
2020-04-13 17:22:00 +00:00
|
|
|
// start the main run loop that reads from the config file
|
|
|
|
f, err := watcher.NewFile()
|
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Err(err).Msg("Cannot load config file")
|
2020-04-13 17:22:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 21:55:40 +00:00
|
|
|
configPath := config.FindOrCreateConfigPath()
|
2020-11-25 06:55:13 +00:00
|
|
|
configManager, err := config.NewFileManager(f, configPath, log)
|
2020-04-13 17:22:00 +00:00
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Err(err).Msg("Cannot setup config file for monitoring")
|
2020-04-13 17:22:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Info().Msgf("monitoring config file at: %s", configPath)
|
2020-04-13 17:22:00 +00:00
|
|
|
|
2020-06-05 14:26:06 +00:00
|
|
|
serviceCallback := func(t string, name string, err error) {
|
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Err(err).Msgf("%s service: %s encountered an error", t, name)
|
2020-06-05 14:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
serviceManager := overwatch.NewAppManager(serviceCallback)
|
2020-05-01 15:30:50 +00:00
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
appService := NewAppService(configManager, serviceManager, shutdownC, log)
|
2020-04-13 17:22:00 +00:00
|
|
|
if err := appService.Run(); err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Err(err).Msg("Failed to start app service")
|
2020-04-13 17:22:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|