Repository navigation
Expand file tree
/
Copy pathwpn.cpp
More file actions
571 lines (548 loc) · 16.7 KB
/
Copy pathwpn.cpp
File metadata and controls
571 lines (548 loc) · 16.7 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/**
* Copyright (c) 2018 Andrei Ivanov <andrei.i.ivanov@commandus.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @file wpn.cpp
*
*/
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <signal.h>
#include <argtable3/argtable3.h>
#include <curl/curl.h>
#include "platform.h"
#include "wpn.h"
#include "wp-storage-file.h"
#include "wp-subscribe.h"
#include "wp-push.h"
#include "sslfactory.h"
#include "mcs/mcsclient.h"
#include "utilqr.h"
#include "utilstring.h"
#include "utilvapid.h"
#include "utilrecv.h"
#include "sslfactory.h"
#include "errlist.h"
#define PROMPT_STARTED "Enter q or press Ctrl+C to quit, p to ping"
#define LINK_SUREPHONE_D "https://surephone.commandus.com/?d="
#define LINK_SUREPHONE_QR "https://surephone.commandus.com/qr/?id="
#define DEF_EMAIL_TEMPLATE "<html><body>$subject<br/>Hi, $name<br/>Click the link below on the phone, tablet or other device on which surephone is installed.\
If the program is not already installed, \
<a href=\"https://play.google.com/store/apps/details?id=com.commandus.surephone\">install it</a>.\
<br/>$body</body></html>"
static int quitFlag = 0;
void signalHandler(int signal)
{
switch(signal)
{
case SIGHUP:
std::cerr << MSG_RELOAD_CONFIG_REQUEST << std::endl;
quitFlag = 2;
fclose(0);
break;
case SIGINT:
std::cerr << MSG_INTERRUPTED << std::endl;
quitFlag = 1;
break;
default:
break;
}
}
#ifdef _MSC_VER
// TODO
void setSignalHandler()
{
}
#else
void setSignalHandler()
{
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = &signalHandler;
sigaction(SIGINT, &action, NULL);
sigaction(SIGHUP, &action, NULL);
}
#endif
#ifdef _MSC_VER
void initWindows()
{
// Initialize Winsock
WSADATA wsaData;
int r = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (r)
{
std::cerr << "WSAStartup error %d" << r << std::endl;
exit(ERR_WSA);
}
}
#endif
void onLog
(
void *env,
int severity,
const char *message
)
{
WpnConfig *c = (WpnConfig *) env;
if (c && c->config && c->config->clientOptions->getVerbosity() >= severity)
{
std::cerr << message;
}
}
/**
* Call dynamically loaded functions to notify user
*/
void onNotify(
void *env,
const char *persistent_id,
const char *from,
const char *appName,
const char *appId,
int64_t sent,
const NotifyMessageC *notification
)
{
WpnConfig *c = (WpnConfig *) env;
if (c && c->config && c->config->clientOptions->getVerbosity() > 2)
{
if (notification)
{
std::cerr << "Body:" << notification->body << std::endl;
}
}
for (std::vector <OnNotifyC>::const_iterator it(((WpnConfig*) env)->onNotifyList.begin()); it != ((WpnConfig*)env)->onNotifyList.end(); ++it)
{
(*it) (env, persistent_id, from, appName, appId, sent, notification);
}
}
void readCommand(
int *quitFlag,
std::istream &strm,
MCSClient *client
)
{
std::string s;
while ((*quitFlag == 0) && (!strm.eof()))
{
strm >> s;
if (s.find_first_of("qQ") == 0)
{
*quitFlag = 1;
break;
}
if (s.find_first_of("pP") == 0)
{
client->ping();
}
}
}
int main(int argc, char** argv)
{
// Signal handler
setSignalHandler();
#ifdef _MSC_VER
initWindows();
#endif
// In windows, this will init the winsock stuff
curl_global_init(CURL_GLOBAL_ALL);
initSSL();
WpnConfig config(argc, argv);
if (config.error())
exit(config.error());
switch (config.cmd)
{
case CMD_LIST:
{
if ((config.outputFormat == 0) && (config.config->clientOptions->getVerbosity() > 0))
std::cout << "subscribeUrl\tsubscribe mode\tendpoint\tauthorized entity\tFCM token\tpushSet" << std::endl;
config.config->subscriptions->write(std::cout, "\t", config.outputFormat);
}
break;
case CMD_LIST_QRCODE:
{
if ((config.outputFormat == 0) && (config.config->clientOptions->getVerbosity() > 0))
std::cout << "FCM QRCodes:" << std::endl;
std::stringstream ss;
std::string foreground = u8"\u2588\u2588";
std::string background = " ";
std::string v = config.config->wpnKeys->getPublicKey();
if (config.invert_qrcode)
v = qr2string(v, foreground, background);
else
v = qr2string(v, background, foreground);
std::cout
<< config.config->wpnKeys->id << "\t"
<< config.config->clientOptions->name << std::endl
<< v << std::endl;
long r = 0;
if (config.config->clientOptions->getVerbosity() > 0) {
for (std::vector<Subscription>::const_iterator it(config.config->subscriptions->list.begin()); it != config.config->subscriptions->list.end(); ++it)
{
std::string v = it->getWpnKeys().getPublicKey();
if (config.invert_qrcode)
v = qr2string(v, foreground, background);
else
v = qr2string(v, background, foreground);
std::cout
<< it->getWpnKeys().id << "\t"
<< it->getName() << std::endl
<< v << std::endl;
}
}
}
break;
case CMD_LIST_LINK:
{
std::stringstream ssBody;
for (std::vector<Subscription>::const_iterator it(config.config->subscriptions->list.begin()); it != config.config->subscriptions->list.end(); ++it)
{
std::stringstream ss;
ss
<< it->getName() << ","
<< it->getAuthorizedEntity() << ","
<< it->getServerKey() << ","
<< it->getToken();
ssBody << LINK_SUREPHONE_D << escapeURLString(ss.str()) << std::endl;
}
std::cout << ssBody.str();
}
break;
case CMD_LIST_EMAIL:
{
if (config.subject.empty()) {
config.subject = "Connect device to wpn";
}
if (config.email_template.empty()) {
config.email_template = DEF_EMAIL_TEMPLATE;
}
std::string m = config.email_template;
size_t p = m.find("$name");
if (p != std::string::npos)
{
m.replace(p, 5, config.cn);
}
p = m.find("$subject");
if (p != std::string::npos)
{
m.replace(p, 8, config.subject);
}
std::stringstream ssBody;
for (std::vector<Subscription>::const_iterator it(config.config->subscriptions->list.begin()); it != config.config->subscriptions->list.end(); ++it)
{
std::stringstream ss;
ss
<< it->getName() << ","
<< it->getAuthorizedEntity() << ","
<< it->getServerKey() << ","
<< it->getToken();
std::string u = escapeURLString(ss.str());
ssBody
<< "<p>"
<< "<a href=\"" << LINK_SUREPHONE_D << u << "\">Connect to "
<< it->getName()
<< "</a>"
<< "</p>"
<< std::endl;
}
p = m.find("$body");
if (p != std::string::npos)
{
m.replace(p, 5, ssBody.str());
}
std::cout << m << std::endl;
}
break;
case CMD_CREDENTIALS:
{
if ((config.outputFormat == 0) && (config.config->clientOptions->getVerbosity() > 0))
std::cout << "application\tandroid\ttoken\tGCM" << std::endl;
config.config->androidCredentials->write(std::cout, "\t", config.outputFormat);
std::cout << std::endl;
}
break;
case CMD_KEYS:
{
if ((config.outputFormat == 0) && (config.config->clientOptions->getVerbosity() > 0))
std::cout << "private\tpublic\tsecret" << std::endl;
config.config->wpnKeys->write(std::cout, "\t", config.outputFormat);
std::cout << std::endl;
}
break;
case CMD_SUBSCRIBE_FCM:
{
std::string d;
std::string headers;
std::string token;
std::string pushset;
int r = subscribeFCM(&d, &headers, token, pushset,
config.config->wpnKeys->getPublicKey(),
config.config->wpnKeys->getAuthSecret(),
config.subscribeUrl, config.getDefaultFCMEndPoint(), config.authorizedEntity,
config.config->clientOptions->getVerbosity());
if ((r < 200) || (r >= 300))
{
std::cerr << "Error " << r << ": " << d << std::endl;
}
else
{
Subscription subscription;
subscription.setToken(token);
subscription.setPushSet(pushset);
subscription.setServerKey(config.serverKey);
subscription.setSubscribeUrl(config.subscribeUrl);
subscription.setSubscribeMode(SUBSCRIBE_FORCE_FIREBASE);
subscription.setEndpoint(config.getDefaultFCMEndPoint());
subscription.setAuthorizedEntity(config.authorizedEntity);
config.config->subscriptions->list.push_back(subscription);
if (config.config->clientOptions->getVerbosity() > 0)
{
subscription.write(std::cout, "\t", config.outputFormat);
}
}
}
break;
case CMD_SUBSCRIBE_VAPID:
{
// TODO
Subscription subscription;
config.config->subscriptions->list.push_back(subscription);
if (config.config->clientOptions->getVerbosity() > 0)
{
subscription.write(std::cout, "\t", config.outputFormat);
}
}
break;
case CMD_UNSUBSCRIBE:
{
if (config.authorizedEntity.empty())
{
// delete all
Subscription f(config.fcm_endpoint, config.authorizedEntity);
for (std::vector<Subscription>::iterator it(config.config->subscriptions->list.begin()); it != config.config->subscriptions->list.end(); ++it)
{
// TODO
}
config.config->subscriptions->list.clear();
}
else
{
Subscription f(config.fcm_endpoint, config.authorizedEntity);
std::vector<Subscription>::iterator it = std::find(config.config->subscriptions->list.begin(),
config.config->subscriptions->list.end(), f);
if (it != config.config->subscriptions->list.end())
config.config->subscriptions->list.erase(it);
}
}
break;
case CMD_PUSH:
{
std::string retval;
std::string token = "";
std::string serverKey;
WpnKeys wpnKeys;
if (!config.private_key.empty()) {
// override Wpn keys
wpnKeys.init(0, 0, config.private_key, config.public_key, config.auth_secret);
}
switch (config.subscriptionMode) {
case SUBSCRIBE_FORCE_FIREBASE:
serverKey = config.serverKey;
break;
case SUBSCRIBE_FORCE_VAPID:
break;
default:
// Load server key from the subscription, by the name
const Subscription *subscription = config.getSubscription(config.name);
if (subscription) {
switch (subscription->getSubscribeMode()) {
case SUBSCRIBE_FORCE_FIREBASE:
config.subscriptionMode = SUBSCRIBE_FORCE_FIREBASE;
serverKey = subscription->getServerKey();
token = subscription->getToken();
break;
case SUBSCRIBE_FORCE_VAPID:
{
config.subscriptionMode = SUBSCRIBE_FORCE_VAPID;
// subscription MUST keep wpn keys
WpnKeys wpnkeys = subscription->getWpnKeys();
wpnKeys.init(subscription->getWpnKeys());
}
break;
default:
break;
}
}
break;
}
std::string body = jsClientNotification("", config.subject, config.body, config.icon, config.link, config.data);
// for VAPID only one endpoint not many
for (std::vector<std::string>::const_iterator it(config.recipientTokens.begin()); it != config.recipientTokens.end(); ++it)
{
int r;
if (config.command.empty())
{
if (config.config->clientOptions->getVerbosity() > 1)
std::cout << "Sending notification to " << *it << std::endl;
switch (config.subscriptionMode) {
case SUBSCRIBE_FORCE_FIREBASE:
r = push2ClientNotificationFCM(&retval, serverKey, *it,
config.subject, config.body, config.icon, config.link, config.data, config.config->clientOptions->getVerbosity());
break;
case SUBSCRIBE_FORCE_VAPID:
if (config.config->clientOptions->getVerbosity() > 3) {
std::cerr << "sender public key: " << wpnKeys.getPublicKey() << std::endl
<< "sender private key: " << wpnKeys.getPrivateKey() << std::endl
<< "endpoint: " << *it << std::endl
<< "public key: " << config.vapid_recipient_p256dh << std::endl
<< "auth secret: " << config.auth_secret << std::endl
<< "body: " << body << std::endl
<< "sub: " << config.sub << std::endl;
}
r = webpushVapid(NULL, retval, wpnKeys.getPublicKey(), wpnKeys.getPrivateKey(), *it, config.vapid_recipient_p256dh, config.auth_secret,
body, config.sub, config.aesgcm ? AESGCM : AES128GCM);
if (config.config->clientOptions->getVerbosity() > 3) {
std::string filename = "aesgcm.bin";
retval = webpushVapidCmd(wpnKeys.getPublicKey(), wpnKeys.getPrivateKey(), filename, *it, config.vapid_recipient_p256dh, config.auth_secret,
body, config.sub, config.aesgcm ? AESGCM : AES128GCM);
}
break;
}
}
else
{
if (config.config->clientOptions->getVerbosity() > 1)
std::cout << "Execute command " << config.command << " on " << *it << std::endl;
switch (config.subscriptionMode) {
case SUBSCRIBE_FORCE_FIREBASE:
r = push2ClientDataFCM(&retval, serverKey, token, *it, "", config.command, 0, "", config.config->clientOptions->getVerbosity());
break;
case SUBSCRIBE_FORCE_VAPID:
r = webpushVapid(NULL, retval, wpnKeys.getPublicKey(), wpnKeys.getPrivateKey(), *it, config.vapid_recipient_p256dh, config.vapid_recipient_auth,
body, config.sub, config.aesgcm ? AESGCM : AES128GCM);
break;
}
}
if (r >= 200 && r < 300)
std::cout << retval << std::endl;
else
std::cerr << "Error " << r << ": " << retval << std::endl;
}
}
break;
case CMD_PRINT_VERSION:
{
std::cout << config.versionString() << std::endl;
break;
}
case CMD_GENERATE_VAPID_KEYS:
{
Subscription subscription;
std::string d;
std::string headers;
config.config->wpnKeys->generate();
/*
int r = subscribe(subscription, SUBSCRIBE_VAPID, *config.wpnKeys,
config.subscribeUrl, config.getDefaultEndPoint(), config.authorizedEntity,
config.serverKey, &d, &headers, config.verbosity);
if ((r < 200) || (r >= 300))
{
std::cerr << "Error " << r << ": " << d << std::endl;
}
else
{
config.subscriptions->list.push_back(subscription);
}
if (config.verbosity > 0)
{
subscription.write(std::cout, "\t", config.outputFormat);
}
*/
std::cout << config.config->wpnKeys->toJsonString() << std::endl;
}
break;
default:
{
do {
quitFlag = 0;
config.loadNotifyFuncs();
MCSClient client(
config.config->subscriptions,
config.config->wpnKeys->getPrivateKey(),
config.config->wpnKeys->getAuthSecret(),
config.config->androidCredentials->getAndroidId(),
config.config->androidCredentials->getSecurityToken(),
onNotify, &config, onLog, &config,
config.config->clientOptions->getVerbosity()
);
// check in
if (config.config->androidCredentials->getAndroidId() == 0)
{
uint64_t androidId = config.config->androidCredentials->getAndroidId();
uint64_t securityToken = config.config->androidCredentials->getSecurityToken();
int r = checkIn(&androidId, &securityToken, config.config->clientOptions->getVerbosity());
if (r < 200 || r >= 300)
{
return ERR_NO_ANDROID_ID_N_TOKEN;
}
config.config->androidCredentials->setAndroidId(androidId);
config.config->androidCredentials->setSecurityToken(securityToken);
}
// register
if (config.config->androidCredentials->getGCMToken().empty())
{
int r;
for (int i = 0; i < 5; i++)
{
std::string gcmToken;
r = registerDevice(&gcmToken,
config.config->androidCredentials->getAndroidId(),
config.config->androidCredentials->getSecurityToken(),
config.config->androidCredentials->getAppId(),
config.config->clientOptions->getVerbosity()
);
if (r >= 200 && r < 300)
{
config.config->androidCredentials->setGCMToken(gcmToken);
break;
}
sleep(1);
}
if (r < 200 || r >= 300)
{
return ERR_NO_FCM_TOKEN;
}
}
client.connect();
std::cerr << PROMPT_STARTED << std::endl;
readCommand(&quitFlag, std::cin, &client);
client.disconnect();
config.unloadNotifyFuncs();
} while (quitFlag == 2);
}
}
config.config->save();
return 0;
}