Skip to content

Commit

Permalink
client/tailscale,cmd/{cli,get-authkey,k8s-operator}: set distinct Use…
Browse files Browse the repository at this point in the history
…r-Agents

This helps better distinguish what is generating activity to the
Tailscale public API.

Updates tailscale/corp#23838

Signed-off-by: Percy Wegmann <[email protected]>
  • Loading branch information
oxtoacart committed Oct 11, 2024
1 parent 33029d4 commit f9949cd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
17 changes: 10 additions & 7 deletions client/tailscale/tailscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Client struct {
// HTTPClient optionally specifies an alternate HTTP client to use.
// If nil, http.DefaultClient is used.
HTTPClient *http.Client

// UserAgent optionally specifies an alternate User-Agent header
UserAgent string
}

func (c *Client) httpClient() *http.Client {
Expand Down Expand Up @@ -97,8 +100,9 @@ func (c *Client) setAuth(r *http.Request) {
// and can be changed manually by the user.
func NewClient(tailnet string, auth AuthMethod) *Client {
return &Client{
tailnet: tailnet,
auth: auth,
tailnet: tailnet,
auth: auth,
UserAgent: "tailscale-client-oss",
}
}

Expand All @@ -110,17 +114,16 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
return nil, errors.New("use of Client without setting I_Acknowledge_This_API_Is_Unstable")
}
c.setAuth(req)
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
return c.httpClient().Do(req)
}

// sendRequest add the authentication key to the request and sends it. It
// receives the response and reads up to 10MB of it.
func (c *Client) sendRequest(req *http.Request) ([]byte, *http.Response, error) {
if !I_Acknowledge_This_API_Is_Unstable {
return nil, nil, errors.New("use of Client without setting I_Acknowledge_This_API_Is_Unstable")
}
c.setAuth(req)
resp, err := c.httpClient().Do(req)
resp, err := c.Do(req)
if err != nil {
return nil, resp, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/get-authkey/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {

ctx := context.Background()
tsClient := tailscale.NewClient("-", nil)
tsClient.UserAgent = "tailscale-get-authkey"
tsClient.HTTPClient = credentials.Client(ctx)
tsClient.BaseURL = baseURL

Expand Down
1 change: 1 addition & 0 deletions cmd/k8s-operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func initTSNet(zlog *zap.SugaredLogger) (*tsnet.Server, *tailscale.Client) {
TokenURL: "https://login.tailscale.com/api/v2/oauth/token",
}
tsClient := tailscale.NewClient("-", nil)
tsClient.UserAgent = "tailscale-k8s-operator"
tsClient.HTTPClient = credentials.Client(context.Background())

s := &tsnet.Server{
Expand Down
1 change: 1 addition & 0 deletions cmd/tailscale/cli/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,7 @@ func resolveAuthKey(ctx context.Context, v, tags string) (string, error) {
}

tsClient := tailscale.NewClient("-", nil)
tsClient.UserAgent = "tailscale-cli"
tsClient.HTTPClient = credentials.Client(ctx)
tsClient.BaseURL = baseURL

Expand Down
1 change: 1 addition & 0 deletions tsnet/tsnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ func (s *Server) APIClient() (*tailscale.Client, error) {
}

c := tailscale.NewClient("-", nil)
c.UserAgent = "tailscale-tsnet"
c.HTTPClient = &http.Client{Transport: s.lb.KeyProvingNoiseRoundTripper()}
return c, nil
}
Expand Down

0 comments on commit f9949cd

Please sign in to comment.