2018-05-01 23:45:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/access"
|
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/tunnel"
|
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/updater"
|
|
|
|
"github.com/cloudflare/cloudflared/log"
|
|
|
|
"github.com/cloudflare/cloudflared/metrics"
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
"github.com/getsentry/raven-go"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
developerPortal = "https://developers.cloudflare.com/argo-tunnel"
|
2018-12-05 00:14:27 +00:00
|
|
|
licenseUrl = developerPortal + "/license/"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Version = "DEV"
|
|
|
|
BuildTime = "unknown"
|
2018-10-08 19:20:28 +00:00
|
|
|
logger = log.CreateLogger()
|
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{})
|
|
|
|
|
|
|
|
app := &cli.App{}
|
|
|
|
app.Name = "cloudflared"
|
2018-10-08 19:20:28 +00:00
|
|
|
app.Usage = "Cloudflare's command-line tool and agent"
|
|
|
|
app.ArgsUsage = "origin-url"
|
2018-05-01 23:45:06 +00:00
|
|
|
app.Copyright = fmt.Sprintf(`(c) %d Cloudflare Inc.
|
|
|
|
Use is subject to the license agreement at %s`, time.Now().Year(), licenseUrl)
|
|
|
|
app.Version = fmt.Sprintf("%s (built %s)", Version, BuildTime)
|
2018-10-08 19:20:28 +00:00
|
|
|
app.Description = `cloudflared connects your machine or user identity to Cloudflare's global network.
|
|
|
|
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
|
|
|
|
app.Commands = commands()
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
func commands() []*cli.Command {
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
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-10-08 19:20:28 +00:00
|
|
|
func action(version string, shutdownC, graceShutdownC chan struct{}) cli.ActionFunc {
|
|
|
|
return func(c *cli.Context) (err error) {
|
|
|
|
tags := make(map[string]string)
|
|
|
|
tags["hostname"] = c.String("hostname")
|
|
|
|
raven.SetTagsContext(tags)
|
|
|
|
raven.CapturePanic(func() { err = tunnel.StartServer(c, version, shutdownC, graceShutdownC) }, nil)
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
2018-10-08 19:20:28 +00:00
|
|
|
raven.CaptureError(err, nil)
|
2018-05-01 23:45:06 +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 {
|
|
|
|
logger.WithError(err).Error("Cannot determine home directory for the user")
|
|
|
|
return "", errors.Wrap(err, "Cannot determine home directory for the user")
|
|
|
|
}
|
|
|
|
return homeDir, nil
|
|
|
|
}
|