diff --git a/cloud.go b/cloud.go index cec749e..59adb61 100644 --- a/cloud.go +++ b/cloud.go @@ -223,6 +223,7 @@ func handleCloudRegister(c *gin.Context) { return } apiReq.Header.Set("Content-Type", "application/json") + apiReq.Header.Set("User-Agent", httpUserAgent) apiResp, err := client.Do(apiReq) if err != nil { @@ -537,6 +538,8 @@ func rpcDeregisterDevice() error { } req.Header.Set("Authorization", "Bearer "+config.CloudToken) + req.Header.Set("User-Agent", httpUserAgent) + client := &http.Client{Timeout: CloudAPIRequestTimeout} resp, err := client.Do(req) if err != nil { diff --git a/internal/timesync/http.go b/internal/timesync/http.go index 3a51463..36974b8 100644 --- a/internal/timesync/http.go +++ b/internal/timesync/http.go @@ -57,6 +57,7 @@ func (t *TimeSync) queryMultipleHttp(urls []string, timeout time.Duration) (now ctx, url, timeout, + t.httpUserAgent, ) duration := time.Since(startTime) @@ -111,6 +112,7 @@ func queryHttpTime( ctx context.Context, url string, timeout time.Duration, + httpUserAgent string, ) (now *time.Time, response *http.Response, err error) { client := http.Client{ Timeout: timeout, @@ -119,6 +121,10 @@ func queryHttpTime( if err != nil { return nil, nil, err } + if httpUserAgent != "" { + req.Header.Set("User-Agent", httpUserAgent) + } + resp, err := client.Do(req) if err != nil { return nil, nil, err diff --git a/internal/timesync/timesync.go b/internal/timesync/timesync.go index e956cf9..2d55fdf 100644 --- a/internal/timesync/timesync.go +++ b/internal/timesync/timesync.go @@ -32,6 +32,8 @@ type TimeSync struct { httpUrls []string networkConfig *network.NetworkConfig + httpUserAgent string + rtcDevicePath string rtcDevice *os.File //nolint:unused rtcLock *sync.Mutex @@ -45,6 +47,7 @@ type TimeSyncOptions struct { PreCheckFunc func() (bool, error) Logger *zerolog.Logger NetworkConfig *network.NetworkConfig + HttpUserAgent string } type SyncMode struct { @@ -71,6 +74,7 @@ func NewTimeSync(opts *TimeSyncOptions) *TimeSync { preCheckFunc: opts.PreCheckFunc, ntpServers: defaultNTPServers, httpUrls: defaultHTTPUrls, + httpUserAgent: opts.HttpUserAgent, networkConfig: opts.NetworkConfig, } diff --git a/main.go b/main.go index c25d8b8..e150c95 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package kvm import ( "context" + "fmt" "net/http" "os" "os/signal" @@ -12,6 +13,7 @@ import ( ) var appCtx context.Context +var httpUserAgent string = fmt.Sprintf("jetkvm-app/%s", builtAppVersion) func Main() { LoadConfig() @@ -25,6 +27,8 @@ func Main() { logger.Warn().Err(err).Msg("failed to get local version") } + httpUserAgent = fmt.Sprintf("jetkvm-app/%s jetkvm-system/%s", appVersionLocal, systemVersionLocal) + logger.Info(). Interface("system_version", systemVersionLocal). Interface("app_version", appVersionLocal). diff --git a/ota.go b/ota.go index cf97cc0..2feee62 100644 --- a/ota.go +++ b/ota.go @@ -88,6 +88,7 @@ func fetchUpdateMetadata(ctx context.Context, deviceId string, includePreRelease if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } + req.Header.Set("User-Agent", httpUserAgent) resp, err := http.DefaultClient.Do(req) if err != nil { @@ -131,7 +132,7 @@ func downloadFile(ctx context.Context, path string, url string, downloadProgress if err != nil { return fmt.Errorf("error creating request: %w", err) } - + req.Header.Set("User-Agent", httpUserAgent) client := http.Client{ Timeout: 10 * time.Minute, Transport: &http.Transport{