-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapns.cpp
More file actions
283 lines (241 loc) · 10.2 KB
/
apns.cpp
File metadata and controls
283 lines (241 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <fmt/chrono.h>
#include <oxenc/bt_producer.h>
#include <oxenc/hex.h>
#include <oxenmq/auth.h>
#include <oxenmq/oxenmq.h>
#include <CLI/CLI.hpp>
#include <CLI/Error.hpp>
#include <CLI/Validators.hpp>
#include <chrono>
#include <exception>
#include <nlohmann/json.hpp>
#include <oxen/log.hpp>
#include <oxen/log/format.hpp>
#include <oxen/log/level.hpp>
#include <oxen/log/type.hpp>
#include <random>
#include <stdexcept>
#include "apns_auth.hpp"
#include "http2_notifier.hpp"
#include "notifiers.hpp"
static auto cat = oxen::log::Cat("apns");
namespace spns::notifier::apns {
namespace log = oxen::log;
using namespace log::literals;
int run(int argc, char* argv[]) {
CLI::App app{"SPNS APNs notifier"};
NotifierBase nb{app, "apns"};
std::string team_id;
std::string key_id;
std::filesystem::path key_file;
std::string app_id;
bool sandbox = false;
app.add_option("--team-id", team_id, "Apple team identifier (10 characters)")->required();
app.add_option("--key-id", key_id, "Apple key id for authentication (10 characters)")
->required();
app.add_option(
"--privkey",
key_file,
"PEM file containing the Apple secret key associed with --key-id used for request "
"authentication")
->required()
->check(CLI::ExistingFile);
app.add_option("--app-id", app_id, "Apple application/bundle ID")->required();
app.add_flag(
"--sandbox",
sandbox,
"Run in sandbox mode with pushes to the sandbox APNs; requires a sandbox key");
try {
app.parse(argc, argv);
} catch (const CLI::ParseError& e) {
return app.exit(e);
}
std::optional<AuthSigner> tokensigner;
try {
tokensigner.emplace(std::move(key_id), std::move(team_id), std::move(key_file));
} catch (const std::exception& e) {
throw std::runtime_error{"Unable to initialize auth signer: {}"_format(e.what())};
}
auto& omq = nb.init();
auto [token, expires] = tokensigner->new_auth_token();
auto hn = std::make_optional<APNSHTTP>(app_id, token, sandbox);
auto ncat = omq.add_category("notifier", oxenmq::AuthLevel::basic);
ncat.add_request_command("validate", [&nb](oxenmq::Message& m) {
// notifier.validate: Called when a device registered for push notifications. We get passed
// some json containing the registration details. Currently all we look for is a "token"
// key containing a length-64 hex value.
//
// Returns a two-part or three-part value: a stringified numeric code from SUBSCRIBE, and an
// opaque value for the main SPNS to store and feed back to us when this account is to be
// notified. (Currently we simply return the device token for this value). The third value
// contains "extra" context data that will be fed back to us in push notifications if we
// provide it, but we currently don't provide it.
if (m.data.size() != 2) {
log::warning(cat, "Internal error: invalid input to notifier.validate");
m.send_reply("ERROR", "Invalid validate request data");
return;
}
if (m.data[0] != nb.id()) {
log::warning(
cat,
"Internal error: notifier validate called with unexpected notifier id '{}' != "
"'{}'",
m.data[0],
nb.id());
m.send_reply("ERROR", "Invalid notifier value");
return;
}
std::string token;
try {
auto data = nlohmann::json::parse(m.data[1]);
token = data["token"].get<std::string>();
} catch (const std::exception& e) {
log::warning(cat, "Unable to parse notifier.validate JSON input: {}", e.what());
m.send_reply(
"{}"_format(static_cast<int>(SUBSCRIBE::BAD_INPUT)),
"Unparseable JSON notification request data");
return;
}
if (token.size() != 64 || !oxenc::is_hex(token)) {
log::warning(
cat,
"notifier.validate called with invalid notification token: expected 64 hex "
"digits, "
"got {} (length {})",
token,
token.size());
m.send_reply(
"{}"_format(static_cast<int>(SUBSCRIBE::BAD_INPUT)),
"Invalid APNs device token (expected 64 hex, got {}B)"_format(token.size()));
return;
}
log::debug(cat, "notifier.validate validated token {}", token);
m.send_reply("{}"_format(static_cast<int>(SUBSCRIBE::OK)), token);
});
std::function<void(notification n, int code, std::string resp_body)> handle_response;
handle_response = [&hn, &nb, &handle_response](
notification n, int code, std::string resp_body) {
bool invalid_token = false;
bool hit_quota = false;
// Error codes: see
// https://developer.apple.com/documentation/usernotifications/handling-notification-responses-from-apns
if (code == 200) {
nb.stats.success++;
if (n.attempts)
nb.stats.retry_success++;
log::trace(cat, "successful notification to {}", n.token);
return;
}
// Otherwise, for an error, there is supposed to be a json body with a "reason" key to
// figure out exactly what happened:
std::string reason;
try {
reason = nlohmann::json::parse(resp_body)["reason"].get<std::string>();
} catch (...) {
log::warning(
cat,
"Unable to parse error {} response; will retry soon. Response:\n{}",
code,
resp_body);
}
log::debug(cat, "Received non-success response {} (with reason: {})", code, reason);
if (code == 400 && (reason == "BadDeviceToken"sv || reason == "DeviceTokenNotForTopic"sv)) {
// This means the token we were given is invalid, and so we should drop it.
log::warning(cat, "{} '{}'; dropping device subscription(s)", reason, n.token);
invalid_token = true;
} else if (code == 410) {
if (reason == "ExpiredToken")
log::warning(
cat, "Token '{}' is expired; dropping device subscription(s)", n.token);
else if (reason == "Unregistered")
log::warning(
cat,
"Token '{}' is no longer active; dropping device subscription(s)",
n.token);
else
log::warning(
cat, "Unknown 410 reason '{}'; dropping device subscription(s)", reason);
invalid_token = true;
} else if (code == 429 && reason == "TooManyRequests") {
log::warning(
cat,
"Too many requests to token '{}'; cooldown down that device for ~1min",
n.token);
hit_quota = true;
} else {
log::error(
cat,
"Error submitting PN for '{}': {} ({}). Will retry soon",
n.token,
code,
reason);
}
if (invalid_token) {
nb.stats.failures++;
nb.bad_token(n.token);
hn->ignore(std::move(n.token));
return;
}
double retry_seconds = 1 << n.attempts++;
// If we get here then it failed, but can be retried. We use an exponential backoff with
// jitter, because that's a reasonable Google recommendations and because Apple as usual has
// no useful documented recommendation or instructions.
if (n.attempts >= MAX_ATTEMPTS) {
log::warning(
cat,
"Too many notification attempts ({}) for token {}; dropping notification",
n.attempts,
n.token);
nb.stats.failures++;
return;
}
if (hit_quota && retry_seconds < 60)
retry_seconds = 60;
retry_seconds *= retry_jitter(rng);
log::debug(cat, "Retrying push to token {} in {:.3f}s", n.token, retry_seconds);
auto retry_in = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::duration<double>{retry_seconds});
hn->send_later(retry_in, std::move(n), handle_response);
};
ncat.add_command("push", [&nb, &hn, &handle_response](oxenmq::Message& m) {
// notifier.push: Called to send a push notification. This is a command rather than a
// request (i.e. it does not reply). The message is a single part, bt-encoded dict (see
// comments in spns/hivemind.cpp).
try {
auto n = notification::parse_spns(m.data.at(0), nb.id());
hn->send(std::move(n), handle_response);
} catch (const std::exception& e) {
log::warning(cat, "Invalid push: {}", e.what());
}
});
log::info(cat, "SPNS APNs Notifier initialized, starting up");
nb.start(hn->loop);
// Run this timer every 50m to refresh the token. (There's no errors to worry about retrying
// anything if it fails, because the token is simply a self-signed value.)
omq.add_timer(
[&expires, &token, &tokensigner, &hn] {
try {
std::tie(token, expires) = tokensigner->new_auth_token();
} catch (const std::exception& e) {
log::critical(cat, "Token signing FAILED: {}", e.what());
return;
}
hn->update_auth_token(token);
},
50min);
nb.run();
log::info(cat, "Stopping HTTP2 notification client...");
hn->stop();
hn.reset();
log::info(cat, "Shutdown complete.");
return 0;
}
} // namespace spns::notifier::apns
int main(int argc, char* argv[]) {
try {
return spns::notifier::apns::run(argc, argv);
} catch (const std::exception& e) {
oxen::log::error(cat, "{}", e.what());
return 1;
}
}