2023-05-24 21:36:01 +02:00
|
|
|
//go:build !noserver
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-26 07:58:36 +02:00
|
|
|
"os"
|
2023-05-24 21:36:01 +02:00
|
|
|
|
|
|
|
"github.com/SherClockHolmes/webpush-go"
|
|
|
|
"github.com/urfave/cli/v2"
|
2024-06-26 07:58:36 +02:00
|
|
|
"github.com/urfave/cli/v2/altsrc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var flagsWebpush = append(
|
|
|
|
[]cli.Flag{},
|
|
|
|
altsrc.NewStringFlag(&cli.StringFlag{Name: "key-file", Aliases: []string{"f"}, Usage: "write vapid keys to this file"}),
|
2023-05-24 21:36:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
commands = append(commands, cmdWebPush)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdWebPush = &cli.Command{
|
2023-06-08 20:30:19 +02:00
|
|
|
Name: "webpush",
|
2023-05-29 17:57:21 +02:00
|
|
|
Usage: "Generate keys, in the future manage web push subscriptions",
|
2023-06-14 14:04:16 +02:00
|
|
|
UsageText: "ntfy webpush [keys]",
|
2023-05-24 21:36:01 +02:00
|
|
|
Category: categoryServer,
|
2023-05-29 17:57:21 +02:00
|
|
|
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Action: generateWebPushKeys,
|
2023-06-08 20:30:19 +02:00
|
|
|
Name: "keys",
|
2023-05-29 17:57:21 +02:00
|
|
|
Usage: "Generate VAPID keys to enable browser background push notifications",
|
2023-06-14 14:04:16 +02:00
|
|
|
UsageText: "ntfy webpush keys",
|
2023-05-29 17:57:21 +02:00
|
|
|
Category: categoryServer,
|
2024-06-26 07:58:36 +02:00
|
|
|
Flags: flagsWebpush,
|
2023-05-29 17:57:21 +02:00
|
|
|
},
|
|
|
|
},
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateWebPushKeys(c *cli.Context) error {
|
|
|
|
privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-26 07:58:36 +02:00
|
|
|
|
|
|
|
if keyFile := c.String("key-file"); keyFile != "" {
|
|
|
|
contents := fmt.Sprintf(`---
|
|
|
|
web-push-public-key: %s
|
|
|
|
web-push-private-key: %s
|
|
|
|
`, publicKey, privateKey)
|
|
|
|
err = os.WriteFile(keyFile, []byte(contents), 0660)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fmt.Fprintf(c.App.ErrWriter, `Web Push keys written to %s.`, keyFile)
|
|
|
|
} else {
|
|
|
|
_, err = fmt.Fprintf(c.App.ErrWriter, `Web Push keys generated. Add the following lines to your config file:
|
2023-05-29 17:57:21 +02:00
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
web-push-public-key: %s
|
|
|
|
web-push-private-key: %s
|
2023-06-18 03:57:47 +02:00
|
|
|
web-push-file: /var/cache/ntfy/webpush.db # or similar
|
2023-05-24 21:36:01 +02:00
|
|
|
web-push-email-address: <email address>
|
2023-05-29 17:57:21 +02:00
|
|
|
|
2023-06-08 20:30:19 +02:00
|
|
|
See https://ntfy.sh/docs/config/#web-push for details.
|
|
|
|
`, publicKey, privateKey)
|
2024-06-26 07:58:36 +02:00
|
|
|
}
|
2023-06-17 03:59:07 +02:00
|
|
|
return err
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|