2023-05-30 19:56:10 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-06-02 13:22:54 +02:00
|
|
|
"net/http"
|
2023-06-02 16:52:35 +02:00
|
|
|
"regexp"
|
2023-06-02 13:22:54 +02:00
|
|
|
|
2023-05-30 19:56:10 +02:00
|
|
|
"github.com/SherClockHolmes/webpush-go"
|
|
|
|
"heckel.io/ntfy/log"
|
2023-06-02 13:22:54 +02:00
|
|
|
"heckel.io/ntfy/user"
|
2023-05-30 19:56:10 +02:00
|
|
|
)
|
|
|
|
|
2023-06-02 16:52:35 +02:00
|
|
|
// test: https://regexr.com/7eqvl
|
|
|
|
// example urls:
|
|
|
|
//
|
|
|
|
// https://android.googleapis.com/XYZ
|
|
|
|
// https://fcm.googleapis.com/XYZ
|
|
|
|
// https://updates.push.services.mozilla.com/XYZ
|
|
|
|
// https://updates-autopush.stage.mozaws.net/XYZ
|
|
|
|
// https://updates-autopush.dev.mozaws.net/XYZ
|
|
|
|
// https://AAA.notify.windows.com/XYZ
|
|
|
|
// https://AAA.push.apple.com/XYZ
|
|
|
|
const (
|
|
|
|
webPushEndpointAllowRegexStr = `^https:\/\/(android\.googleapis\.com|fcm\.googleapis\.com|updates\.push\.services\.mozilla\.com|updates-autopush\.stage\.mozaws\.net|updates-autopush\.dev\.mozaws\.net|.*\.notify\.windows\.com|.*\.push\.apple\.com)\/.*$`
|
|
|
|
webPushTopicSubscribeLimit = 50
|
|
|
|
)
|
|
|
|
|
|
|
|
var webPushEndpointAllowRegex = regexp.MustCompile(webPushEndpointAllowRegexStr)
|
|
|
|
|
2023-06-02 13:22:54 +02:00
|
|
|
func (s *Server) handleWebPushUpdate(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2023-06-09 05:09:38 +02:00
|
|
|
req, err := readJSONWithLimit[apiWebPushUpdateSubscriptionRequest](r.Body, jsonBodyBytesLimit, false)
|
|
|
|
if err != nil || req.Endpoint == "" || req.P256dh == "" || req.Auth == "" {
|
2023-05-30 19:56:10 +02:00
|
|
|
return errHTTPBadRequestWebPushSubscriptionInvalid
|
2023-06-09 05:09:38 +02:00
|
|
|
} else if !webPushEndpointAllowRegex.MatchString(req.Endpoint) {
|
2023-06-02 16:52:35 +02:00
|
|
|
return errHTTPBadRequestWebPushEndpointUnknown
|
2023-06-09 05:09:38 +02:00
|
|
|
} else if len(req.Topics) > webPushTopicSubscribeLimit {
|
2023-06-02 16:52:35 +02:00
|
|
|
return errHTTPBadRequestWebPushTopicCountTooHigh
|
|
|
|
}
|
2023-06-09 05:09:38 +02:00
|
|
|
topics, err := s.topicsFromIDs(req.Topics...)
|
2023-05-30 19:56:10 +02:00
|
|
|
if err != nil {
|
2023-06-02 13:22:54 +02:00
|
|
|
return err
|
2023-05-30 19:56:10 +02:00
|
|
|
}
|
2023-06-02 13:22:54 +02:00
|
|
|
if s.userManager != nil {
|
2023-06-08 18:20:12 +02:00
|
|
|
u := v.User()
|
2023-06-02 13:22:54 +02:00
|
|
|
for _, t := range topics {
|
|
|
|
if err := s.userManager.Authorize(u, t.ID, user.PermissionRead); err != nil {
|
|
|
|
logvr(v, r).With(t).Err(err).Debug("Access to topic %s not authorized", t.ID)
|
|
|
|
return errHTTPForbidden.With(t)
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 19:56:10 +02:00
|
|
|
}
|
2023-06-10 05:17:48 +02:00
|
|
|
if err := s.webPush.UpsertSubscription(req.Endpoint, req.Auth, req.P256dh, v.MaybeUserID(), req.Topics); err != nil {
|
2023-05-30 19:56:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.writeJSON(w, newSuccessResponse())
|
|
|
|
}
|
|
|
|
|
2023-06-11 03:09:01 +02:00
|
|
|
func (s *Server) handleWebPushDelete(w http.ResponseWriter, r *http.Request, _ *visitor) error {
|
|
|
|
req, err := readJSONWithLimit[apiWebPushUpdateSubscriptionRequest](r.Body, jsonBodyBytesLimit, false)
|
|
|
|
if err != nil || req.Endpoint == "" {
|
|
|
|
return errHTTPBadRequestWebPushSubscriptionInvalid
|
|
|
|
}
|
|
|
|
if err := s.webPush.RemoveSubscriptionsByEndpoint(req.Endpoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.writeJSON(w, newSuccessResponse())
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:56:10 +02:00
|
|
|
func (s *Server) publishToWebPushEndpoints(v *visitor, m *message) {
|
2023-05-30 20:23:03 +02:00
|
|
|
subscriptions, err := s.webPush.SubscriptionsForTopic(m.Topic)
|
2023-05-30 19:56:10 +02:00
|
|
|
if err != nil {
|
|
|
|
logvm(v, m).Err(err).Warn("Unable to publish web push messages")
|
|
|
|
return
|
|
|
|
}
|
2023-06-08 18:20:12 +02:00
|
|
|
payload, err := json.Marshal(newWebPushPayload(fmt.Sprintf("%s/%s", s.config.BaseURL, m.Topic), m))
|
|
|
|
if err != nil {
|
|
|
|
log.Tag(tagWebPush).Err(err).Warn("Unable to marshal expiring payload")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, subscription := range subscriptions {
|
2023-06-09 05:09:38 +02:00
|
|
|
ctx := log.Context{"endpoint": subscription.Endpoint, "username": subscription.UserID, "topic": m.Topic, "message_id": m.ID}
|
2023-06-08 18:20:12 +02:00
|
|
|
if err := s.sendWebPushNotification(payload, subscription, &ctx); err != nil {
|
|
|
|
log.Tag(tagWebPush).Err(err).Fields(ctx).Warn("Unable to publish web push message")
|
|
|
|
}
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-30 19:56:10 +02:00
|
|
|
|
2023-06-10 05:17:48 +02:00
|
|
|
func (s *Server) pruneAndNotifyWebPushSubscriptions() {
|
2023-06-09 03:45:52 +02:00
|
|
|
if s.config.WebPushPublicKey == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
2023-06-10 05:17:48 +02:00
|
|
|
if err := s.pruneAndNotifyWebPushSubscriptionsInternal(); err != nil {
|
2023-06-09 03:45:52 +02:00
|
|
|
log.Tag(tagWebPush).Err(err).Warn("Unable to prune or notify web push subscriptions")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-06-10 05:17:48 +02:00
|
|
|
func (s *Server) pruneAndNotifyWebPushSubscriptionsInternal() error {
|
|
|
|
// Expire old subscriptions
|
|
|
|
if err := s.webPush.RemoveExpiredSubscriptions(s.config.WebPushExpiryDuration); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Notify subscriptions that will expire soon
|
|
|
|
subscriptions, err := s.webPush.SubscriptionsExpiring(s.config.WebPushExpiryWarningDuration)
|
2023-06-02 14:45:05 +02:00
|
|
|
if err != nil {
|
2023-06-09 03:45:52 +02:00
|
|
|
return err
|
2023-06-08 18:20:12 +02:00
|
|
|
} else if len(subscriptions) == 0 {
|
2023-06-09 03:45:52 +02:00
|
|
|
return nil
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
2023-06-08 18:20:12 +02:00
|
|
|
payload, err := json.Marshal(newWebPushSubscriptionExpiringPayload())
|
2023-06-02 14:45:05 +02:00
|
|
|
if err != nil {
|
2023-06-08 18:20:12 +02:00
|
|
|
log.Tag(tagWebPush).Err(err).Warn("Unable to marshal expiring payload")
|
2023-06-09 03:45:52 +02:00
|
|
|
return err
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
2023-06-10 05:17:48 +02:00
|
|
|
warningSent := make([]*webPushSubscription, 0)
|
2023-06-09 03:45:52 +02:00
|
|
|
for _, subscription := range subscriptions {
|
2023-06-09 05:09:38 +02:00
|
|
|
ctx := log.Context{"endpoint": subscription.Endpoint}
|
|
|
|
if err := s.sendWebPushNotification(payload, subscription, &ctx); err != nil {
|
2023-06-09 03:45:52 +02:00
|
|
|
log.Tag(tagWebPush).Err(err).Fields(ctx).Warn("Unable to publish expiry imminent warning")
|
2023-06-10 05:17:48 +02:00
|
|
|
continue
|
2023-06-08 18:20:12 +02:00
|
|
|
}
|
2023-06-10 05:17:48 +02:00
|
|
|
warningSent = append(warningSent, subscription)
|
|
|
|
}
|
|
|
|
if err := s.webPush.MarkExpiryWarningSent(warningSent); err != nil {
|
|
|
|
return err
|
2023-06-09 03:45:52 +02:00
|
|
|
}
|
2023-06-10 05:17:48 +02:00
|
|
|
log.Tag(tagWebPush).Debug("Expired old subscriptions and published %d expiry imminent warnings", len(subscriptions))
|
2023-06-09 03:45:52 +02:00
|
|
|
return nil
|
2023-06-08 18:20:12 +02:00
|
|
|
}
|
2023-06-02 14:45:05 +02:00
|
|
|
|
2023-06-08 18:20:12 +02:00
|
|
|
func (s *Server) sendWebPushNotification(message []byte, sub *webPushSubscription, ctx *log.Context) error {
|
2023-06-09 05:09:38 +02:00
|
|
|
resp, err := webpush.SendNotification(message, sub.ToSubscription(), &webpush.Options{
|
2023-06-02 14:45:05 +02:00
|
|
|
Subscriber: s.config.WebPushEmailAddress,
|
|
|
|
VAPIDPublicKey: s.config.WebPushPublicKey,
|
|
|
|
VAPIDPrivateKey: s.config.WebPushPrivateKey,
|
2023-06-08 18:20:12 +02:00
|
|
|
Urgency: webpush.UrgencyHigh, // iOS requires this to ensure delivery
|
2023-06-09 11:32:44 +02:00
|
|
|
TTL: int(s.config.CacheDuration.Seconds()),
|
2023-06-02 14:45:05 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-06-08 18:20:12 +02:00
|
|
|
log.Tag(tagWebPush).Err(err).Fields(*ctx).Debug("Unable to publish web push message, removing endpoint")
|
2023-06-09 05:09:38 +02:00
|
|
|
if err := s.webPush.RemoveSubscriptionsByEndpoint(sub.Endpoint); err != nil {
|
2023-06-08 18:20:12 +02:00
|
|
|
return err
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
2023-06-08 18:20:12 +02:00
|
|
|
return err
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
2023-06-08 18:20:12 +02:00
|
|
|
if (resp.StatusCode < 200 || resp.StatusCode > 299) && resp.StatusCode != 429 {
|
|
|
|
log.Tag(tagWebPush).Fields(*ctx).Field("response_code", resp.StatusCode).Debug("Unable to publish web push message, unexpected response")
|
2023-06-09 05:09:38 +02:00
|
|
|
if err := s.webPush.RemoveSubscriptionsByEndpoint(sub.Endpoint); err != nil {
|
2023-06-08 18:20:12 +02:00
|
|
|
return err
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
2023-06-08 18:20:12 +02:00
|
|
|
return errHTTPInternalErrorWebPushUnableToPublish.Fields(*ctx)
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
2023-06-08 18:20:12 +02:00
|
|
|
return nil
|
2023-05-30 19:56:10 +02:00
|
|
|
}
|