Skip to content

Commit 2e2718b

Browse files
committed
TUN-6459: Add cloudflared user-agent to access calls
1 parent b849def commit 2e2718b

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

cmd/cloudflared/access/cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ const sentryDSN = "https://[email protected]/189878"
5656

5757
var (
5858
shutdownC chan struct{}
59+
userAgent = "DEV"
5960
)
6061

6162
// Init will initialize and store vars from the main program
62-
func Init(shutdown chan struct{}) {
63+
func Init(shutdown chan struct{}, version string) {
6364
shutdownC = shutdown
65+
userAgent = fmt.Sprintf("cloudflared/%s", version)
6466
}
6567

6668
// 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
505507
if err != nil {
506508
return false, errors.Wrap(err, "Could not create access request")
507509
}
508-
510+
req.Header.Set("User-Agent", userAgent)
509511
// Do not follow redirects
510512
client := &http.Client{
511513
CheckRedirect: func(req *http.Request, via []*http.Request) error {

cmd/cloudflared/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/cloudflare/cloudflared/logger"
2222
"github.com/cloudflare/cloudflared/metrics"
2323
"github.com/cloudflare/cloudflared/overwatch"
24+
"github.com/cloudflare/cloudflared/token"
2425
"github.com/cloudflare/cloudflared/tracing"
2526
"github.com/cloudflare/cloudflared/watcher"
2627
)
@@ -85,9 +86,10 @@ func main() {
8586
app.Commands = commands(cli.ShowVersion)
8687

8788
tunnel.Init(bInfo, graceShutdownC) // we need this to support the tunnel sub command...
88-
access.Init(graceShutdownC)
89+
access.Init(graceShutdownC, Version)
8990
updater.Init(Version)
9091
tracing.Init(Version)
92+
token.Init(Version)
9193
runApp(app, graceShutdownC)
9294
}
9395

token/token.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
AccessLoginWorkerPath = "/cdn-cgi/access/login"
3030
)
3131

32+
var (
33+
userAgent = "DEV"
34+
)
35+
3236
type AppInfo struct {
3337
AuthDomain string
3438
AppAUD string
@@ -144,6 +148,10 @@ func isTokenLocked(lockFilePath string) bool {
144148
return exists && err == nil
145149
}
146150

151+
func Init(version string) {
152+
userAgent = fmt.Sprintf("cloudflared/%s", version)
153+
}
154+
147155
// FetchTokenWithRedirect will either load a stored token or generate a new one
148156
// it appends the full url as the redirect URL to the access cli request if opening the browser
149157
func FetchTokenWithRedirect(appURL *url.URL, appInfo *AppInfo, log *zerolog.Logger) (string, error) {
@@ -261,6 +269,7 @@ func GetAppInfo(reqURL *url.URL) (*AppInfo, error) {
261269
if err != nil {
262270
return nil, errors.Wrap(err, "failed to create app info request")
263271
}
272+
appInfoReq.Header.Add("User-Agent", userAgent)
264273
resp, err := client.Do(appInfoReq)
265274
if err != nil {
266275
return nil, errors.Wrap(err, "failed to get app info")
@@ -311,6 +320,7 @@ func exchangeOrgToken(appURL *url.URL, orgToken string) (string, error) {
311320
if err != nil {
312321
return "", errors.Wrap(err, "failed to create app token request")
313322
}
323+
appTokenRequest.Header.Add("User-Agent", userAgent)
314324
resp, err := client.Do(appTokenRequest)
315325
if err != nil {
316326
return "", errors.Wrap(err, "failed to get app token")

token/transfer.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ func transferRequest(requestURL string, log *zerolog.Logger) ([]byte, string, er
113113

114114
// poll the endpoint for the request resource, waiting for the user interaction
115115
func poll(client *http.Client, requestURL string, log *zerolog.Logger) ([]byte, string, error) {
116-
resp, err := client.Get(requestURL)
116+
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
117+
if err != nil {
118+
return nil, "", err
119+
}
120+
req.Header.Set("User-Agent", userAgent)
121+
resp, err := client.Do(req)
117122
if err != nil {
118123
return nil, "", err
119124
}

0 commit comments

Comments
 (0)