downstream: stop sending HTTP OPTIONS request on WEBPUSH REGISTER
We were sending a test notification later anyways. Let's just do that to check that the endpoint accepts our messages.
This commit is contained in:
parent
926dcb37ac
commit
90be9a8ab9
@ -9,7 +9,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -3060,14 +3059,6 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := checkWebPushEndpoint(ctx, endpoint); err != nil {
|
|
||||||
dc.logger.Printf("failed to check Web push endpoint %q: %v", endpoint, err)
|
|
||||||
return ircError{&irc.Message{
|
|
||||||
Command: "FAIL",
|
|
||||||
Params: []string{"WEBPUSH", "INVALID_PARAMS", subcommand, "Invalid endpoint"},
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
|
|
||||||
rawKeys := irc.ParseTags(keysStr)
|
rawKeys := irc.ParseTags(keysStr)
|
||||||
authKey, hasAuthKey := rawKeys["auth"]
|
authKey, hasAuthKey := rawKeys["auth"]
|
||||||
p256dhKey, hasP256dh := rawKeys["p256dh"]
|
p256dhKey, hasP256dh := rawKeys["p256dh"]
|
||||||
@ -3112,16 +3103,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
|
|||||||
networkID = dc.network.ID
|
networkID = dc.network.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: limit max number of subscriptions, prune old ones
|
// Send a test Web Push message, to make sure the endpoint is valid
|
||||||
|
|
||||||
if err := dc.user.srv.db.StoreWebPushSubscription(ctx, dc.user.ID, networkID, &newSub); err != nil {
|
|
||||||
dc.logger.Printf("failed to store Web push subscription: %v", err)
|
|
||||||
return ircError{&irc.Message{
|
|
||||||
Command: "FAIL",
|
|
||||||
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = dc.srv.sendWebPush(ctx, &webpush.Subscription{
|
err = dc.srv.sendWebPush(ctx, &webpush.Subscription{
|
||||||
Endpoint: newSub.Endpoint,
|
Endpoint: newSub.Endpoint,
|
||||||
Keys: webpush.Keys{
|
Keys: webpush.Keys{
|
||||||
@ -3134,6 +3116,20 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dc.logger.Printf("failed to send Web push notification to endpoint %q: %v", newSub.Endpoint, err)
|
dc.logger.Printf("failed to send Web push notification to endpoint %q: %v", newSub.Endpoint, err)
|
||||||
|
return ircError{&irc.Message{
|
||||||
|
Command: "FAIL",
|
||||||
|
Params: []string{"WEBPUSH", "INVALID_PARAMS", subcommand, "Invalid endpoint"},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: limit max number of subscriptions, prune old ones
|
||||||
|
|
||||||
|
if err := dc.user.srv.db.StoreWebPushSubscription(ctx, dc.user.ID, networkID, &newSub); err != nil {
|
||||||
|
dc.logger.Printf("failed to store Web push subscription: %v", err)
|
||||||
|
return ircError{&irc.Message{
|
||||||
|
Command: "FAIL",
|
||||||
|
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
|
||||||
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.SendMessage(&irc.Message{
|
dc.SendMessage(&irc.Message{
|
||||||
@ -3336,35 +3332,3 @@ func sendNames(dc *downstreamConn, ch *upstreamChannel) {
|
|||||||
dc.SendMessage(msg)
|
dc.SendMessage(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkWebPushEndpoint(ctx context.Context, endpoint string) error {
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodOptions, endpoint, nil)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create HTTP request: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("HTTP request failed: %v", err)
|
|
||||||
}
|
|
||||||
resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode/100 != 2 {
|
|
||||||
return fmt.Errorf("HTTP request failed: %v", resp.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
allow := strings.Split(resp.Header.Get("Allow"), ",")
|
|
||||||
found := false
|
|
||||||
for _, method := range allow {
|
|
||||||
if strings.EqualFold(strings.TrimSpace(method), http.MethodPost) {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
return fmt.Errorf("POST missing from Allow header in OPTIONS response")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user