From c844c24a16ff17c9987264a1dc20679809c99737 Mon Sep 17 00:00:00 2001 From: KuroSetsuna29 Date: Sat, 2 Nov 2024 00:22:40 -0400 Subject: [PATCH 1/2] allow configurable web push expiry duration --- cmd/serve.go | 16 ++++++++++++++++ docs/config.md | 6 ++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 62e0a14a..871a5aec 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -100,6 +100,8 @@ var flagsServe = append( altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-file", Aliases: []string{"web_push_file"}, EnvVars: []string{"NTFY_WEB_PUSH_FILE"}, Usage: "file used to store web push subscriptions"}), altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-email-address", Aliases: []string{"web_push_email_address"}, EnvVars: []string{"NTFY_WEB_PUSH_EMAIL_ADDRESS"}, Usage: "e-mail address of sender, required to use browser push services"}), altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-startup-queries", Aliases: []string{"web_push_startup_queries"}, EnvVars: []string{"NTFY_WEB_PUSH_STARTUP_QUERIES"}, Usage: "queries run when the web push database is initialized"}), + altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-expiry-duration", Aliases: []string{"web_push_expiry_duration"}, EnvVars: []string{"NTFY_WEB_PUSH_EXPIRY_DURATION"}, Value: util.FormatDuration(server.DefaultWebPushExpiryDuration), Usage: "send web push warning notification after this time before expiring unused subscriptions"}), + altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-expiry-warning-duration", Aliases: []string{"web_push_expiry_warning_duration"}, EnvVars: []string{"NTFY_WEB_PUSH_EXPIRY_WARNING_DURATION"}, Value: util.FormatDuration(server.DefaultWebPushExpiryWarningDuration), Usage: "automatically expire unused subscriptions after this time"}), ) var cmdServe = &cli.Command{ @@ -140,6 +142,8 @@ func execServe(c *cli.Context) error { webPushFile := c.String("web-push-file") webPushEmailAddress := c.String("web-push-email-address") webPushStartupQueries := c.String("web-push-startup-queries") + webPushExpiryDurationStr := c.String("web-push-expiry-duration") + webPushExpiryWarningDurationStr := c.String("web-push-expiry-warning-duration") cacheFile := c.String("cache-file") cacheDurationStr := c.String("cache-duration") cacheStartupQueries := c.String("cache-startup-queries") @@ -226,6 +230,14 @@ func execServe(c *cli.Context) error { if err != nil { return fmt.Errorf("invalid visitor email limit replenish: %s", visitorEmailLimitReplenishStr) } + webPushExpiryDuration, err := util.ParseDuration(webPushExpiryDurationStr) + if err != nil { + return fmt.Errorf("invalid web push expiry duration: %s", webPushExpiryDurationStr) + } + webPushExpiryWarningDuration, err := util.ParseDuration(webPushExpiryWarningDurationStr) + if err != nil { + return fmt.Errorf("invalid web push expiry warning duration: %s", webPushExpiryWarningDurationStr) + } // Convert sizes to bytes messageSizeLimit, err := util.ParseSize(messageSizeLimitStr) @@ -304,6 +316,8 @@ func execServe(c *cli.Context) error { if messageSizeLimit > 5*1024*1024 { return errors.New("message-size-limit cannot be higher than 5M") } + } else if webPushExpiryWarningDuration > 0 && webPushExpiryWarningDuration > webPushExpiryDuration { + return errors.New("web push expiry warning duration cannot be higher than web push expiry duration") } // Backwards compatibility @@ -417,6 +431,8 @@ func execServe(c *cli.Context) error { conf.WebPushFile = webPushFile conf.WebPushEmailAddress = webPushEmailAddress conf.WebPushStartupQueries = webPushStartupQueries + conf.WebPushExpiryDuration = webPushExpiryDuration + conf.WebPushExpiryWarningDuration = webPushExpiryWarningDuration // Set up hot-reloading of config go sigHandlerConfigReload(config) diff --git a/docs/config.md b/docs/config.md index 9479301a..303173b9 100644 --- a/docs/config.md +++ b/docs/config.md @@ -876,7 +876,9 @@ a database to keep track of the browser's subscriptions, and an admin email addr - `web-push-private-key` is the generated VAPID private key, e.g. AA2BB1234567890abcdefzxcvbnm1234567890 - `web-push-file` is a database file to keep track of browser subscription endpoints, e.g. `/var/cache/ntfy/webpush.db` - `web-push-email-address` is the admin email address send to the push provider, e.g. `sysadmin@example.com` -- `web-push-startup-queries` is an optional list of queries to run on startup` +- `web-push-startup-queries` is an optional list of queries to run on startup` +- `web-push-expiry-warning-duration` defines the duration for which unused subscriptions are sent a warning (default is `7d`) +- `web-push-expiry-duration` defines the duration for which unused subscriptions will expire (default is `9d`) Limitations: @@ -904,7 +906,7 @@ web-push-email-address: sysadmin@example.com ``` The `web-push-file` is used to store the push subscriptions. Unused subscriptions will send out a warning after 7 days, -and will automatically expire after 9 days (not configurable). If the gateway returns an error (e.g. 410 Gone when a user has unsubscribed), +and will automatically expire after 9 days (default). If the gateway returns an error (e.g. 410 Gone when a user has unsubscribed), subscriptions are also removed automatically. The web app refreshes subscriptions on start and regularly on an interval, but this file should be persisted across restarts. If the subscription From 136b656ccbb2d17f0155112899c3196a254641e7 Mon Sep 17 00:00:00 2001 From: KuroSetsuna29 Date: Sat, 2 Nov 2024 08:50:57 -0400 Subject: [PATCH 2/2] fix descriptions --- cmd/serve.go | 4 ++-- docs/config.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 871a5aec..48cd19a0 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -100,8 +100,8 @@ var flagsServe = append( altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-file", Aliases: []string{"web_push_file"}, EnvVars: []string{"NTFY_WEB_PUSH_FILE"}, Usage: "file used to store web push subscriptions"}), altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-email-address", Aliases: []string{"web_push_email_address"}, EnvVars: []string{"NTFY_WEB_PUSH_EMAIL_ADDRESS"}, Usage: "e-mail address of sender, required to use browser push services"}), altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-startup-queries", Aliases: []string{"web_push_startup_queries"}, EnvVars: []string{"NTFY_WEB_PUSH_STARTUP_QUERIES"}, Usage: "queries run when the web push database is initialized"}), - altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-expiry-duration", Aliases: []string{"web_push_expiry_duration"}, EnvVars: []string{"NTFY_WEB_PUSH_EXPIRY_DURATION"}, Value: util.FormatDuration(server.DefaultWebPushExpiryDuration), Usage: "send web push warning notification after this time before expiring unused subscriptions"}), - altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-expiry-warning-duration", Aliases: []string{"web_push_expiry_warning_duration"}, EnvVars: []string{"NTFY_WEB_PUSH_EXPIRY_WARNING_DURATION"}, Value: util.FormatDuration(server.DefaultWebPushExpiryWarningDuration), Usage: "automatically expire unused subscriptions after this time"}), + altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-expiry-duration", Aliases: []string{"web_push_expiry_duration"}, EnvVars: []string{"NTFY_WEB_PUSH_EXPIRY_DURATION"}, Value: util.FormatDuration(server.DefaultWebPushExpiryDuration), Usage: "automatically expire unused subscriptions after this time"}), + altsrc.NewStringFlag(&cli.StringFlag{Name: "web-push-expiry-warning-duration", Aliases: []string{"web_push_expiry_warning_duration"}, EnvVars: []string{"NTFY_WEB_PUSH_EXPIRY_WARNING_DURATION"}, Value: util.FormatDuration(server.DefaultWebPushExpiryWarningDuration), Usage: "send web push warning notification after this time before expiring unused subscriptions"}), ) var cmdServe = &cli.Command{ diff --git a/docs/config.md b/docs/config.md index 303173b9..e04d72c5 100644 --- a/docs/config.md +++ b/docs/config.md @@ -877,8 +877,8 @@ a database to keep track of the browser's subscriptions, and an admin email addr - `web-push-file` is a database file to keep track of browser subscription endpoints, e.g. `/var/cache/ntfy/webpush.db` - `web-push-email-address` is the admin email address send to the push provider, e.g. `sysadmin@example.com` - `web-push-startup-queries` is an optional list of queries to run on startup` -- `web-push-expiry-warning-duration` defines the duration for which unused subscriptions are sent a warning (default is `7d`) -- `web-push-expiry-duration` defines the duration for which unused subscriptions will expire (default is `9d`) +- `web-push-expiry-warning-duration` defines the duration after which unused subscriptions are sent a warning (default is `7d`) +- `web-push-expiry-duration` defines the duration after which unused subscriptions will expire (default is `9d`) Limitations: