ipn/localapi: add endpoint to handle APNS payloads (#9972)

* ipn/localapi: add endpoint to handle APNS payloads

Fixes #9971. This adds a new `handle-push-message` local API endpoint. When an APNS payload is delivered to the main app, this endpoint can be used to forward the JSON body of the message to the backend, making a POST request.

cc @bradfitz

Signed-off-by: Andrea Gottardo <andrea@tailscale.com>

* Address comments from code review

Signed-off-by: Andrea Gottardo <andrea@tailscale.com>

---------

Signed-off-by: Andrea Gottardo <andrea@tailscale.com>
This commit is contained in:
Andrea Gottardo 2023-10-30 18:35:53 -07:00 committed by GitHub
parent 57c5b5a77e
commit 95715c4a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -85,6 +85,7 @@
"derpmap": (*Handler).serveDERPMap,
"dev-set-state-store": (*Handler).serveDevSetStateStore,
"set-push-device-token": (*Handler).serveSetPushDeviceToken,
"handle-push-message": (*Handler).serveHandlePushMessage,
"dial": (*Handler).serveDial,
"file-targets": (*Handler).serveFileTargets,
"goroutines": (*Handler).serveGoroutines,
@ -1587,6 +1588,27 @@ func (h *Handler) serveSetPushDeviceToken(w http.ResponseWriter, r *http.Request
w.WriteHeader(http.StatusOK)
}
func (h *Handler) serveHandlePushMessage(w http.ResponseWriter, r *http.Request) {
if !h.PermitWrite {
http.Error(w, "handle push message not allowed", http.StatusForbidden)
return
}
if r.Method != "POST" {
http.Error(w, "unsupported method", http.StatusMethodNotAllowed)
return
}
var pushMessageBody map[string]any
if err := json.NewDecoder(r.Body).Decode(&pushMessageBody); err != nil {
http.Error(w, "failed to decode JSON body: "+err.Error(), http.StatusBadRequest)
return
}
// TODO(bradfitz): do something with pushMessageBody
h.logf("localapi: got push message: %v", logger.AsJSON(pushMessageBody))
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) serveUploadClientMetrics(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "unsupported method", http.StatusMethodNotAllowed)