@@ -18,6 +18,7 @@ import (
18
18
"os"
19
19
"strings"
20
20
21
+ "github.com/jaxxstorm/tscli/pkg/version"
21
22
"github.com/spf13/viper"
22
23
tsapi "tailscale.com/client/tailscale/v2"
23
24
)
@@ -27,6 +28,11 @@ const (
27
28
defaultContentType = "application/json"
28
29
)
29
30
31
+ // getUserAgent returns the properly formatted user agent string
32
+ func getUserAgent () string {
33
+ return fmt .Sprintf ("tscli/%s (Go client)" , version .GetVersion ())
34
+ }
35
+
30
36
func New () (* tsapi.Client , error ) {
31
37
tailnet := viper .GetString ("tailnet" )
32
38
apiKey := viper .GetString ("api-key" )
@@ -37,15 +43,27 @@ func New() (*tsapi.Client, error) {
37
43
return nil , fmt .Errorf ("api-key is required" )
38
44
}
39
45
40
- httpClient := & http.Client {}
46
+ userAgent := getUserAgent ()
47
+
48
+ // Create a custom transport that ensures UserAgent is always set
49
+ transport := & userAgentTransport {
50
+ rt : http .DefaultTransport ,
51
+ userAgent : userAgent ,
52
+ }
53
+
54
+ // Wrap with debug logging if enabled
41
55
if viper .GetBool ("debug" ) {
42
- httpClient .Transport = & logTransport {rt : http .DefaultTransport }
56
+ transport .rt = & logTransport {rt : transport .rt }
57
+ }
58
+
59
+ httpClient := & http.Client {
60
+ Transport : transport ,
43
61
}
44
62
45
63
return & tsapi.Client {
46
64
Tailnet : tailnet ,
47
65
APIKey : apiKey ,
48
- UserAgent : "tscli" ,
66
+ UserAgent : userAgent ,
49
67
HTTP : httpClient ,
50
68
}, nil
51
69
}
@@ -173,3 +191,15 @@ func (t *logTransport) RoundTrip(req *http.Request) (*http.Response, error) {
173
191
}
174
192
return resp , nil
175
193
}
194
+
195
+ // userAgentTransport wraps an http.RoundTripper to ensure UserAgent is always set
196
+ type userAgentTransport struct {
197
+ rt http.RoundTripper
198
+ userAgent string
199
+ }
200
+
201
+ func (t * userAgentTransport ) RoundTrip (req * http.Request ) (* http.Response , error ) {
202
+ // Always set our custom user agent
203
+ req .Header .Set ("User-Agent" , t .userAgent )
204
+ return t .rt .RoundTrip (req )
205
+ }
0 commit comments