From 95715c4a1229a084fbb3bbd9652e6b177d5602ca Mon Sep 17 00:00:00 2001 From: Andrea Gottardo Date: Mon, 30 Oct 2023 18:35:53 -0700 Subject: [PATCH] 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 * Address comments from code review Signed-off-by: Andrea Gottardo --------- Signed-off-by: Andrea Gottardo --- ipn/localapi/localapi.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ipn/localapi/localapi.go b/ipn/localapi/localapi.go index c9a6a89bf..51a84a62c 100644 --- a/ipn/localapi/localapi.go +++ b/ipn/localapi/localapi.go @@ -85,6 +85,7 @@ var handler = map[string]localAPIHandler{ "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)