2023-05-24 19:36:01 +00:00
|
|
|
//go:build !noserver
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/SherClockHolmes/webpush-go"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
commands = append(commands, cmdWebPush)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdWebPush = &cli.Command{
|
2023-06-08 18:30:19 +00:00
|
|
|
Name: "webpush",
|
2023-05-29 15:57:21 +00:00
|
|
|
Usage: "Generate keys, in the future manage web push subscriptions",
|
2023-06-14 12:04:16 +00:00
|
|
|
UsageText: "ntfy webpush [keys]",
|
2023-05-24 19:36:01 +00:00
|
|
|
Category: categoryServer,
|
2023-05-29 15:57:21 +00:00
|
|
|
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Action: generateWebPushKeys,
|
2023-06-08 18:30:19 +00:00
|
|
|
Name: "keys",
|
2023-05-29 15:57:21 +00:00
|
|
|
Usage: "Generate VAPID keys to enable browser background push notifications",
|
2023-06-14 12:04:16 +00:00
|
|
|
UsageText: "ntfy webpush keys",
|
2023-05-29 15:57:21 +00:00
|
|
|
Category: categoryServer,
|
|
|
|
},
|
|
|
|
},
|
2023-05-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateWebPushKeys(c *cli.Context) error {
|
|
|
|
privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-08 18:30:19 +00:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, `Web Push keys generated. Add the following lines to your config file:
|
2023-05-29 15:57:21 +00:00
|
|
|
|
2023-05-24 19:36:01 +00:00
|
|
|
web-push-public-key: %s
|
|
|
|
web-push-private-key: %s
|
2023-06-08 18:30:19 +00:00
|
|
|
web-push-subscriptions-file: /var/cache/ntfy/webpush.db # or similar
|
2023-05-24 19:36:01 +00:00
|
|
|
web-push-email-address: <email address>
|
2023-05-29 15:57:21 +00:00
|
|
|
|
2023-06-08 18:30:19 +00:00
|
|
|
See https://ntfy.sh/docs/config/#web-push for details.
|
|
|
|
`, publicKey, privateKey)
|
2023-05-24 19:36:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|