2018-05-01 23:45:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-06 23:33:08 +00:00
|
|
|
"strings"
|
2018-05-01 23:45:06 +00:00
|
|
|
"time"
|
|
|
|
|
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"
|
2020-04-13 17:22:00 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
|
2018-10-08 19:20:28 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/tunnel"
|
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/updater"
|
2020-06-08 22:01:48 +00:00
|
|
|
log "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-10-09 17:07:08 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunneldns"
|
2020-04-13 17:22:00 +00:00
|
|
|
"github.com/cloudflare/cloudflared/watcher"
|
2018-10-08 19:20:28 +00:00
|
|
|
|
2019-04-18 15:42:48 +00:00
|
|
|
raven "github.com/getsentry/raven-go"
|
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
2020-08-05 10:49:53 +00:00
|
|
|
cli "github.com/urfave/cli/v2"
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
|
|
|
metrics.RegisterBuildInfo(BuildTime, Version)
|
|
|
|
raven.SetRelease(Version)
|
|
|
|
|
|
|
|
// Force shutdown channel used by the app. When closed, app must terminate.
|
|
|
|
// Windows service manager closes this channel when it receives shutdown command.
|
|
|
|
shutdownC := make(chan struct{})
|
|
|
|
// Graceful shutdown channel used by the app. When closed, app must terminate.
|
|
|
|
// 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,
|
|
|
|
and configure access control.`
|
|
|
|
app.Flags = flags()
|
|
|
|
app.Action = action(Version, shutdownC, graceShutdownC)
|
2020-10-09 17:07:08 +00:00
|
|
|
app.Before = tunnel.SetFlagsFromConfigFile
|
2019-01-24 21:11:53 +00:00
|
|
|
app.Commands = commands(cli.ShowVersion)
|
2018-10-08 19:20:28 +00:00
|
|
|
|
|
|
|
tunnel.Init(Version, shutdownC, graceShutdownC) // we need this to support the tunnel sub command...
|
2018-10-19 20:44:35 +00:00
|
|
|
access.Init(shutdownC, graceShutdownC)
|
2020-08-12 16:16:14 +00:00
|
|
|
updater.Init(Version)
|
2018-10-08 19:20:28 +00:00
|
|
|
runApp(app, shutdownC, graceShutdownC)
|
|
|
|
}
|
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",
|
|
|
|
Action: cliutil.ErrorHandler(updater.Update),
|
|
|
|
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
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
To determine if an update happened in a script, check for error code 64.`,
|
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()...)
|
2020-10-09 17:07:08 +00:00
|
|
|
cmds = append(cmds, tunneldns.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
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
func action(version string, shutdownC, graceShutdownC chan struct{}) cli.ActionFunc {
|
|
|
|
return func(c *cli.Context) (err error) {
|
2018-12-17 17:18:34 +00:00
|
|
|
if isEmptyInvocation(c) {
|
2020-04-13 17:22:00 +00:00
|
|
|
return handleServiceMode(shutdownC)
|
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)
|
2019-05-29 16:34:10 +00:00
|
|
|
exitCode := 0
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
2018-11-06 23:33:08 +00:00
|
|
|
handleError(err)
|
2019-05-29 16:34:10 +00:00
|
|
|
exitCode = 1
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2019-05-29 16:34:10 +00:00
|
|
|
// we already handle error printing, so we pass an empty string so we
|
|
|
|
// don't have to print again.
|
|
|
|
return cli.Exit("", exitCode)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
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.
|
|
|
|
func handleError(err error) {
|
|
|
|
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
|
|
|
|
func handleServiceMode(shutdownC chan struct{}) error {
|
2020-06-12 16:20:36 +00:00
|
|
|
defer log.SharedWriteManager.Shutdown()
|
2020-04-29 20:51:32 +00:00
|
|
|
logDirectory, logLevel := config.FindLogSettings()
|
|
|
|
|
2020-06-08 22:01:48 +00:00
|
|
|
logger, err := log.New(log.DefaultFile(logDirectory), log.LogLevelString(logLevel))
|
2020-04-29 20:51:32 +00:00
|
|
|
if err != nil {
|
2020-06-12 16:20:36 +00:00
|
|
|
return cliutil.PrintLoggerSetupError("error setting up logger", err)
|
2020-04-29 20:51:32 +00:00
|
|
|
}
|
|
|
|
logger.Infof("logging to directory: %s", logDirectory)
|
|
|
|
|
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-04-29 20:51:32 +00:00
|
|
|
logger.Errorf("Cannot load config file: %s", err)
|
2020-04-13 17:22:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 21:55:40 +00:00
|
|
|
configPath := config.FindOrCreateConfigPath()
|
2020-04-13 17:22:00 +00:00
|
|
|
configManager, err := config.NewFileManager(f, configPath, logger)
|
|
|
|
if err != nil {
|
2020-04-29 20:51:32 +00:00
|
|
|
logger.Errorf("Cannot setup config file for monitoring: %s", err)
|
2020-04-13 17:22:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-26 23:17:34 +00:00
|
|
|
logger.Infof("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 {
|
|
|
|
logger.Errorf("%s service: %s encountered an error: %s", t, name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serviceManager := overwatch.NewAppManager(serviceCallback)
|
2020-05-01 15:30:50 +00:00
|
|
|
|
|
|
|
appService := NewAppService(configManager, serviceManager, shutdownC, logger)
|
2020-04-13 17:22:00 +00:00
|
|
|
if err := appService.Run(); err != nil {
|
2020-04-29 20:51:32 +00:00
|
|
|
logger.Errorf("Failed to start app service: %s", err)
|
2020-04-13 17:22:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|