diff --git a/internal/app/diun.go b/internal/app/diun.go index c2f809af..b77c5869 100644 --- a/internal/app/diun.go +++ b/internal/app/diun.go @@ -42,8 +42,11 @@ func New(cfg *config.Config, location *time.Location) (*Diun, error) { return nil, err } + // User-Agent + userAgent := fmt.Sprintf("diun/%s go/%s %s", cfg.App.Version, runtime.Version()[2:], strings.Title(runtime.GOOS)) + // Notification client - notifcli, err := notif.New(cfg.Notif, cfg.App) + notifcli, err := notif.New(cfg.Notif, cfg.App, userAgent) if err != nil { return nil, err } @@ -55,7 +58,7 @@ func New(cfg *config.Config, location *time.Location) (*Diun, error) { )), db: dbcli, notif: notifcli, - userAgent: fmt.Sprintf("diun/%s go/%s %s", cfg.App.Version, runtime.Version()[2:], strings.Title(runtime.GOOS)), + userAgent: userAgent, }, nil } diff --git a/internal/notif/client.go b/internal/notif/client.go index d803ec39..c68b7b1d 100644 --- a/internal/notif/client.go +++ b/internal/notif/client.go @@ -19,7 +19,7 @@ type Client struct { } // New creates a new notification instance -func New(config model.Notif, app model.App) (*Client, error) { +func New(config model.Notif, app model.App, userAgent string) (*Client, error) { var c = &Client{ cfg: config, app: app, @@ -37,10 +37,10 @@ func New(config model.Notif, app model.App) (*Client, error) { c.notifiers = append(c.notifiers, telegram.New(config.Telegram, app)) } if config.Webhook.Enable { - c.notifiers = append(c.notifiers, webhook.New(config.Webhook, app)) + c.notifiers = append(c.notifiers, webhook.New(config.Webhook, app, userAgent)) } if config.Gotify.Enable { - c.notifiers = append(c.notifiers, gotify.New(config.Gotify, app)) + c.notifiers = append(c.notifiers, gotify.New(config.Gotify, app, userAgent)) } log.Debug().Msgf("%d notifier(s) created", len(c.notifiers)) diff --git a/internal/notif/gotify/client.go b/internal/notif/gotify/client.go index 698993a9..284e4506 100644 --- a/internal/notif/gotify/client.go +++ b/internal/notif/gotify/client.go @@ -19,16 +19,18 @@ import ( // Client represents an active gotify notification object type Client struct { *notifier.Notifier - cfg model.NotifGotify - app model.App + cfg model.NotifGotify + app model.App + userAgent string } // New creates a new gotify notification instance -func New(config model.NotifGotify, app model.App) notifier.Notifier { +func New(config model.NotifGotify, app model.App, userAgent string) notifier.Notifier { return notifier.Notifier{ Handler: &Client{ - cfg: config, - app: app, + cfg: config, + app: app, + userAgent: userAgent, }, } } @@ -77,7 +79,7 @@ func (c *Client) Send(entry model.NotifEntry) error { req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode()))) - req.Header.Set("User-Agent", fmt.Sprintf("%s %s", c.app.Name, c.app.Version)) + req.Header.Set("User-Agent", c.userAgent) resp, err := hc.Do(req) if err != nil { diff --git a/internal/notif/webhook/client.go b/internal/notif/webhook/client.go index 1f0581b4..7bd96219 100644 --- a/internal/notif/webhook/client.go +++ b/internal/notif/webhook/client.go @@ -3,7 +3,6 @@ package webhook import ( "bytes" "encoding/json" - "fmt" "net/http" "time" @@ -15,16 +14,18 @@ import ( // Client represents an active webhook notification object type Client struct { *notifier.Notifier - cfg model.NotifWebhook - app model.App + cfg model.NotifWebhook + app model.App + userAgent string } // New creates a new webhook notification instance -func New(config model.NotifWebhook, app model.App) notifier.Notifier { +func New(config model.NotifWebhook, app model.App, userAgent string) notifier.Notifier { return notifier.Notifier{ Handler: &Client{ - cfg: config, - app: app, + cfg: config, + app: app, + userAgent: userAgent, }, } } @@ -76,7 +77,7 @@ func (c *Client) Send(entry model.NotifEntry) error { } } - req.Header.Set("User-Agent", fmt.Sprintf("%s %s", c.app.Name, c.app.Version)) + req.Header.Set("User-Agent", c.userAgent) _, err = hc.Do(req) return err