diff --git a/cmd/cupdate/main.go b/cmd/cupdate/main.go index 1da5d6d..d2502e6 100644 --- a/cmd/cupdate/main.go +++ b/cmd/cupdate/main.go @@ -47,6 +47,10 @@ type Config struct { Address string `env:"ADDRESS"` } `envPrefix:"WEB_"` + HTTP struct { + UserAgent string `env:"USER_AGENT" envDefault:"Cupdate/1.0"` + } `envPrefix:"HTTP_"` + Cache struct { Path string `env:"PATH" envDefault:"cachev1.boltdb"` MaxAge time.Duration `env:"MAX_AGE" envDefault:"24h"` @@ -214,6 +218,7 @@ func main() { wg.Go(func() error { httpClient := httputil.NewClient(cache, config.Cache.MaxAge) + httpClient.UserAgent = config.HTTP.UserAgent prometheus.DefaultRegisterer.MustRegister(httpClient) worker := worker.New(httpClient, writeStore) diff --git a/docs/config.md b/docs/config.md index a1efe46..1e731cc 100644 --- a/docs/config.md +++ b/docs/config.md @@ -12,6 +12,7 @@ done using environment variables. | `CUPDATE_API_PORT` | The port to expose the API on. | `8080` | | `CUPDATE_WEB_DISABLED` | Whether or not to disable the web UI. | `false` | | `CUPDATE_WEB_ADDRESS` | The URL at which the UI is available (such as `https://example.com`). Used for RSS feeds, should generally not be set | Automatically resolved | +| `CUPDATE_HTTP_USER_AGENT` | The User Agent string to use for HTTP requests. | `Cupdate/1.0` | | `CUPDATE_CACHE_PATH` | A path to the boltdb file in which to store cache. | `cachev1.boltdb` | | `CUPDATE_CACHE_MAX_AGE` | The maximum age of cache entries. | `24h` | | `CUPDATE_DB_PATH` | A path to the sqlite file in which to store data. | `dbv1.sqlite` | diff --git a/internal/httputil/client.go b/internal/httputil/client.go index 6aa7c04..2d56799 100644 --- a/internal/httputil/client.go +++ b/internal/httputil/client.go @@ -20,6 +20,9 @@ var _ prometheus.Collector = (*Client)(nil) type Client struct { http.Client + + UserAgent string + cache cache.Cache cacheMaxAge time.Duration @@ -58,6 +61,12 @@ func NewClient(cache cache.Cache, maxAge time.Duration) *Client { // See [http.Client.Do]. func (c *Client) Do(req *http.Request) (*http.Response, error) { + if _, ok := req.Header["User-Agent"]; !ok { + if c.UserAgent != "" { + req.Header.Set("User-Agent", c.UserAgent) + } + } + res, err := c.Client.Do(req) if err == nil { c.requestsCounter.WithLabelValues(req.URL.Host, req.Method, strconv.FormatInt(int64(res.StatusCode), 10)).Inc()