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>
pull/10029/head
Andrea Gottardo 1 year ago committed by GitHub
parent 57c5b5a77e
commit 95715c4a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -85,6 +85,7 @@ var handler = map[string]localAPIHandler{
"derpmap": (*Handler).serveDERPMap, "derpmap": (*Handler).serveDERPMap,
"dev-set-state-store": (*Handler).serveDevSetStateStore, "dev-set-state-store": (*Handler).serveDevSetStateStore,
"set-push-device-token": (*Handler).serveSetPushDeviceToken, "set-push-device-token": (*Handler).serveSetPushDeviceToken,
"handle-push-message": (*Handler).serveHandlePushMessage,
"dial": (*Handler).serveDial, "dial": (*Handler).serveDial,
"file-targets": (*Handler).serveFileTargets, "file-targets": (*Handler).serveFileTargets,
"goroutines": (*Handler).serveGoroutines, "goroutines": (*Handler).serveGoroutines,
@ -1587,6 +1588,27 @@ func (h *Handler) serveSetPushDeviceToken(w http.ResponseWriter, r *http.Request
w.WriteHeader(http.StatusOK) 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) { func (h *Handler) serveUploadClientMetrics(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
http.Error(w, "unsupported method", http.StatusMethodNotAllowed) http.Error(w, "unsupported method", http.StatusMethodNotAllowed)

Loading…
Cancel
Save