From e7cd25f4e3c3e7c4370c127df07496c1004fd3cb Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 14 Jun 2020 03:04:28 +0200 Subject: [PATCH] Allow to override user-agent Signed-off-by: Knut Ahlers --- cache.go | 11 ++++++++++- main.go | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cache.go b/cache.go index 5e0b3b9..363dfd5 100644 --- a/cache.go +++ b/cache.go @@ -13,7 +13,16 @@ import ( func renewCache(url string) (*meta, error) { var cachePath = urlToCachePath(url) - resp, err := http.Get(url) + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, errors.Wrap(err, "Unable to create request") + } + + if cfg.UserAgent != "" { + req.Header.Set("User-Agent", cfg.UserAgent) + } + + resp, err := http.DefaultClient.Do(req) if err != nil { return nil, errors.Wrap(err, "Unable to fetch source file") } diff --git a/main.go b/main.go index c2a3e7f..c5ebe5b 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ var ( Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"` LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"` StorageDir string `flag:"storage-dir" default:"./data/" description:"Where to store cached files"` + UserAgent string `flag:"user-agent" default:"" description:"Override user-agent"` VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` }{}