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-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-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"
|
|
|
|
cli "gopkg.in/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)
|
|
|
|
app.Before = tunnel.Before
|
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)
|
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
|
|
|
{
|
|
|
|
Name: "update",
|
2018-10-08 19:20:28 +00:00
|
|
|
Action: updater.Update,
|
2018-08-15 22:23:34 +00:00
|
|
|
Usage: "Update the agent if a new version exists",
|
|
|
|
ArgsUsage: " ",
|
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()...)
|
|
|
|
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)
|
|
|
|
raven.CapturePanic(func() { err = tunnel.StartServer(c, version, shutdownC, graceShutdownC) }, 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-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 {
|
|
|
|
return errors.Wrap(err, "error setting up logger")
|
|
|
|
}
|
|
|
|
logger.Infof("logging to directory: %s", logDirectory)
|
|
|
|
|
2020-06-08 22:01:48 +00:00
|
|
|
defer log.SharedWriteManager.Shutdown()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := config.FindDefaultConfigPath()
|
|
|
|
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-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
|
|
|
|
}
|