TUN-6459: Add cloudflared user-agent to access calls

This commit is contained in:
Devin Carr 2022-06-24 11:51:53 -07:00
parent b849def673
commit 2e2718b7e3
4 changed files with 23 additions and 4 deletions

View File

@ -56,11 +56,13 @@ const sentryDSN = "https://56a9c9fa5c364ab28f34b14f35ea0f1b@sentry.io/189878"
var ( var (
shutdownC chan struct{} shutdownC chan struct{}
userAgent = "DEV"
) )
// Init will initialize and store vars from the main program // Init will initialize and store vars from the main program
func Init(shutdown chan struct{}) { func Init(shutdown chan struct{}, version string) {
shutdownC = shutdown shutdownC = shutdown
userAgent = fmt.Sprintf("cloudflared/%s", version)
} }
// Flags return the global flags for Access related commands (hopefully none) // 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 { if err != nil {
return false, errors.Wrap(err, "Could not create access request") return false, errors.Wrap(err, "Could not create access request")
} }
req.Header.Set("User-Agent", userAgent)
// Do not follow redirects // Do not follow redirects
client := &http.Client{ client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {

View File

@ -21,6 +21,7 @@ import (
"github.com/cloudflare/cloudflared/logger" "github.com/cloudflare/cloudflared/logger"
"github.com/cloudflare/cloudflared/metrics" "github.com/cloudflare/cloudflared/metrics"
"github.com/cloudflare/cloudflared/overwatch" "github.com/cloudflare/cloudflared/overwatch"
"github.com/cloudflare/cloudflared/token"
"github.com/cloudflare/cloudflared/tracing" "github.com/cloudflare/cloudflared/tracing"
"github.com/cloudflare/cloudflared/watcher" "github.com/cloudflare/cloudflared/watcher"
) )
@ -85,9 +86,10 @@ func main() {
app.Commands = commands(cli.ShowVersion) app.Commands = commands(cli.ShowVersion)
tunnel.Init(bInfo, graceShutdownC) // we need this to support the tunnel sub command... tunnel.Init(bInfo, graceShutdownC) // we need this to support the tunnel sub command...
access.Init(graceShutdownC) access.Init(graceShutdownC, Version)
updater.Init(Version) updater.Init(Version)
tracing.Init(Version) tracing.Init(Version)
token.Init(Version)
runApp(app, graceShutdownC) runApp(app, graceShutdownC)
} }

View File

@ -29,6 +29,10 @@ const (
AccessLoginWorkerPath = "/cdn-cgi/access/login" AccessLoginWorkerPath = "/cdn-cgi/access/login"
) )
var (
userAgent = "DEV"
)
type AppInfo struct { type AppInfo struct {
AuthDomain string AuthDomain string
AppAUD string AppAUD string
@ -144,6 +148,10 @@ func isTokenLocked(lockFilePath string) bool {
return exists && err == nil 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 // 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 // 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) { 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 { if err != nil {
return nil, errors.Wrap(err, "failed to create app info request") return nil, errors.Wrap(err, "failed to create app info request")
} }
appInfoReq.Header.Add("User-Agent", userAgent)
resp, err := client.Do(appInfoReq) resp, err := client.Do(appInfoReq)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to get app info") 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 { if err != nil {
return "", errors.Wrap(err, "failed to create app token request") return "", errors.Wrap(err, "failed to create app token request")
} }
appTokenRequest.Header.Add("User-Agent", userAgent)
resp, err := client.Do(appTokenRequest) resp, err := client.Do(appTokenRequest)
if err != nil { if err != nil {
return "", errors.Wrap(err, "failed to get app token") return "", errors.Wrap(err, "failed to get app token")

View File

@ -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 // 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) { 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 { if err != nil {
return nil, "", err return nil, "", err
} }