From 2e2718b7e31298785ea2972004d2380530a4b085 Mon Sep 17 00:00:00 2001 From: Devin Carr Date: Fri, 24 Jun 2022 11:51:53 -0700 Subject: [PATCH] TUN-6459: Add cloudflared user-agent to access calls --- cmd/cloudflared/access/cmd.go | 6 ++++-- cmd/cloudflared/main.go | 4 +++- token/token.go | 10 ++++++++++ token/transfer.go | 7 ++++++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/cmd/cloudflared/access/cmd.go b/cmd/cloudflared/access/cmd.go index 50ccb663..fa06fd08 100644 --- a/cmd/cloudflared/access/cmd.go +++ b/cmd/cloudflared/access/cmd.go @@ -56,11 +56,13 @@ const sentryDSN = "https://56a9c9fa5c364ab28f34b14f35ea0f1b@sentry.io/189878" var ( shutdownC chan struct{} + userAgent = "DEV" ) // Init will initialize and store vars from the main program -func Init(shutdown chan struct{}) { +func Init(shutdown chan struct{}, version string) { shutdownC = shutdown + userAgent = fmt.Sprintf("cloudflared/%s", version) } // Flags return the global flags for Access related commands (hopefully none) @@ -505,7 +507,7 @@ func isTokenValid(options *carrier.StartOptions, log *zerolog.Logger) (bool, err if err != nil { return false, errors.Wrap(err, "Could not create access request") } - + req.Header.Set("User-Agent", userAgent) // Do not follow redirects client := &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { diff --git a/cmd/cloudflared/main.go b/cmd/cloudflared/main.go index 07271eb4..1ac06c3e 100644 --- a/cmd/cloudflared/main.go +++ b/cmd/cloudflared/main.go @@ -21,6 +21,7 @@ import ( "github.com/cloudflare/cloudflared/logger" "github.com/cloudflare/cloudflared/metrics" "github.com/cloudflare/cloudflared/overwatch" + "github.com/cloudflare/cloudflared/token" "github.com/cloudflare/cloudflared/tracing" "github.com/cloudflare/cloudflared/watcher" ) @@ -85,9 +86,10 @@ func main() { app.Commands = commands(cli.ShowVersion) tunnel.Init(bInfo, graceShutdownC) // we need this to support the tunnel sub command... - access.Init(graceShutdownC) + access.Init(graceShutdownC, Version) updater.Init(Version) tracing.Init(Version) + token.Init(Version) runApp(app, graceShutdownC) } diff --git a/token/token.go b/token/token.go index abccf257..329ace79 100644 --- a/token/token.go +++ b/token/token.go @@ -29,6 +29,10 @@ const ( AccessLoginWorkerPath = "/cdn-cgi/access/login" ) +var ( + userAgent = "DEV" +) + type AppInfo struct { AuthDomain string AppAUD string @@ -144,6 +148,10 @@ func isTokenLocked(lockFilePath string) bool { return exists && err == nil } +func Init(version string) { + userAgent = fmt.Sprintf("cloudflared/%s", version) +} + // FetchTokenWithRedirect will either load a stored token or generate a new one // it appends the full url as the redirect URL to the access cli request if opening the browser func FetchTokenWithRedirect(appURL *url.URL, appInfo *AppInfo, log *zerolog.Logger) (string, error) { @@ -261,6 +269,7 @@ func GetAppInfo(reqURL *url.URL) (*AppInfo, error) { if err != nil { return nil, errors.Wrap(err, "failed to create app info request") } + appInfoReq.Header.Add("User-Agent", userAgent) resp, err := client.Do(appInfoReq) if err != nil { return nil, errors.Wrap(err, "failed to get app info") @@ -311,6 +320,7 @@ func exchangeOrgToken(appURL *url.URL, orgToken string) (string, error) { if err != nil { return "", errors.Wrap(err, "failed to create app token request") } + appTokenRequest.Header.Add("User-Agent", userAgent) resp, err := client.Do(appTokenRequest) if err != nil { return "", errors.Wrap(err, "failed to get app token") diff --git a/token/transfer.go b/token/transfer.go index e285c49e..cc40cb69 100644 --- a/token/transfer.go +++ b/token/transfer.go @@ -113,7 +113,12 @@ func transferRequest(requestURL string, log *zerolog.Logger) ([]byte, string, er // poll the endpoint for the request resource, waiting for the user interaction func poll(client *http.Client, requestURL string, log *zerolog.Logger) ([]byte, string, error) { - resp, err := client.Get(requestURL) + req, err := http.NewRequest(http.MethodGet, requestURL, nil) + if err != nil { + return nil, "", err + } + req.Header.Set("User-Agent", userAgent) + resp, err := client.Do(req) if err != nil { return nil, "", err }