Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle FCMv1 responses #69

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions rewrite/fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,43 +161,37 @@ func (f FCM) Req(body []byte, req http.Request) (requests []*http.Request, error
}

type fcmResp struct {
Results []struct {
Error string
}
Name string
}

type fcmErr struct {
Message string
}

func (f FCM) RespCode(resp *http.Response) *utils.ProxyError {
b, _ := io.ReadAll(io.LimitReader(resp.Body, 5000))
switch resp.StatusCode / 100 {
case 4: // 4xx
return utils.NewProxyErrS(500, "Error with common-proxies auth or json, not app server, this should not be happening")
out := fcmErr{}
err := json.Unmarshal(b, &out)
if err != nil {
// Not even to extract err from body
return utils.NewProxyErrS(500, "Error with common-proxies auth or json, not app server, this should not be happening")
}
return utils.NewProxyErrS(resp.StatusCode, "FCM error: "+out.Message)
case 5: // 5xx
//TODO implement forced exponential backoff in common-proxies
return utils.NewProxyErrS(429, "slow down")
}

b, _ := io.ReadAll(io.LimitReader(resp.Body, 5000))

out := fcmResp{}
err := json.Unmarshal(b, &out)
if err != nil || len(out.Results) < 1 {
if err != nil {
//
return utils.NewProxyErrS(502, "dunno whats going on, resp is not json or len reults is zero %s", string(b))
return utils.NewProxyErrS(502, "dunno whats going on, resp is not json or not in right schema %s", string(b))
}

givenErr := out.Results[0].Error

switch givenErr {
case "MissingRegistration", "InvalidRegistration", "NotRegistered", "MismatchSenderId":
return utils.NewProxyErrS(404, "Invalid Registration "+givenErr)
//case "InvalidParameters": // doesn't happen because 4xx is handled above
case "MessageTooBig", "InvalidDataKey", "InvalidTtl", "TopicsMessageRateExceeded", "InvalidApnsCredential": // this shouldn't happen, common-proxies has its own checks for size, common-proxies controls the keys, common-proxies doesn't send TTL, common-proxies doesn't deal in topics, idk apns
return utils.NewProxyErrS(502, "Something is very wrong "+givenErr)
case "Unavailable", "InternalServerError", "DeviceMessageRateExceeded":
//delay, TODO implement forced exponential backoff
return utils.NewProxyErrS(429, "slow down")
default:
return utils.NewProxyErrS(201, "")
}
return utils.NewProxyErrS(201, "")
//TODO log
}

Expand Down
Loading