-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsjcpp_geoip.cpp
More file actions
537 lines (445 loc) · 16.9 KB
/
Copy pathwsjcpp_geoip.cpp
File metadata and controls
537 lines (445 loc) · 16.9 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
#include "wsjcpp_geoip.h"
#include <json.hpp>
#include <wsjcpp_core.h>
#include <wsjcpp_validators.h>
#include <curl/curl.h>
// ----------------------------------------------------------------------
WsjcppGeoIPResult::WsjcppGeoIPResult(
const std::string &sServiceName,
const std::string &sIpAddress,
const std::string &sCountry,
const std::string &sRegionName,
const std::string &sCity,
double nLatitude,
double nLongitude
) {
TAG = "WsjcppGeoIPResult-" + sServiceName;
m_bHasError = false;
m_sErrorDescription = "";
m_sServiceName = sServiceName;
m_sIpAddress = sIpAddress;
m_sCountry = sCountry;
m_sRegionName = sRegionName;
m_sCity = sCity;
m_nLatitude = nLatitude;
m_nLongitude = nLongitude;
}
// ----------------------------------------------------------------------
WsjcppGeoIPResult::WsjcppGeoIPResult(
const std::string &sServiceName,
const std::string &sIpAddress,
const std::string &sErrorDescription
) {
TAG = "WsjcppGeoIPResult-" + sServiceName;
m_bHasError = true;
m_sIpAddress = sIpAddress;
m_sErrorDescription = sErrorDescription;
m_sServiceName = sServiceName;
m_sCountry = "";
m_sRegionName = "";
m_sCity = "";
m_nLatitude = 0;
m_nLongitude = 0;
}
// ----------------------------------------------------------------------
bool WsjcppGeoIPResult::hasError() {
return m_bHasError;
}
// ----------------------------------------------------------------------
std::string WsjcppGeoIPResult::getServiceName() {
return m_sServiceName;
}
// ----------------------------------------------------------------------
std::string WsjcppGeoIPResult::getIpAddress() {
return m_sIpAddress;
}
// ----------------------------------------------------------------------
std::string WsjcppGeoIPResult::getErrorDescription() {
if (!m_bHasError) {
WsjcppLog::warn(TAG, "getErrorDescription, Result hasn't error");
}
return m_sErrorDescription;
}
// ----------------------------------------------------------------------
std::string WsjcppGeoIPResult::getCountry() {
if (m_bHasError) {
WsjcppLog::warn(TAG, "getCountry, result has error");
}
return m_sCountry;
}
// ----------------------------------------------------------------------
std::string WsjcppGeoIPResult::getRegionName() {
if (m_bHasError) {
WsjcppLog::warn(TAG, "getRegionName, result has error");
}
return m_sRegionName;
}
// ----------------------------------------------------------------------
std::string WsjcppGeoIPResult::getCity() {
if (m_bHasError) {
WsjcppLog::warn(TAG, "getCity, result has error");
}
return m_sCity;
}
// ----------------------------------------------------------------------
double WsjcppGeoIPResult::getLatitude() {
if (m_bHasError) {
WsjcppLog::warn(TAG, "getLatitude, result has error");
}
return m_nLatitude;
}
// ----------------------------------------------------------------------
double WsjcppGeoIPResult::getLongitude() {
if (m_bHasError) {
WsjcppLog::warn(TAG, "getLongitude, result has error");
}
return m_nLongitude;
}
// ----------------------------------------------------------------------
nlohmann::json WsjcppGeoIPResult::toJson() {
nlohmann::json jsonRet;
jsonRet["serviceName"] = m_sServiceName;
jsonRet["ip"] = m_sIpAddress;
if (m_bHasError) {
jsonRet["error"] = m_sErrorDescription;
} else {
jsonRet["longitude"] = m_nLongitude;
jsonRet["latitude"] = m_nLatitude;
jsonRet["longitude"] = m_sCountry;
jsonRet["regionName"] = m_sRegionName;
jsonRet["city"] = m_sCity;
}
return jsonRet;
}
// ----------------------------------------------------------------------
// WsjcppGeoIPIPv4
WsjcppGeoIPv4::WsjcppGeoIPv4() {
m_arrIP[0] = 0;
m_arrIP[1] = 0;
m_arrIP[2] = 0;
m_arrIP[3] = 0;
}
// ----------------------------------------------------------------------
WsjcppGeoIPv4::WsjcppGeoIPv4(const unsigned char arrIp[4]) {
for (int i = 0; i < 4; i++) {
m_arrIP[i] = arrIp[i];
}
}
// ----------------------------------------------------------------------
WsjcppGeoIPv4::WsjcppGeoIPv4(unsigned char c0, unsigned char c1, unsigned char c2, unsigned char c3) {
m_arrIP[0] = c0;
m_arrIP[1] = c1;
m_arrIP[2] = c2;
m_arrIP[3] = c3;
}
// ----------------------------------------------------------------------
bool WsjcppGeoIPv4::fromString(const std::string &sIpAddress) {
int n = 0;
std::string s[4] = {"", "", "", ""};
for (int i = 0; i < sIpAddress.length(); i++) {
char c = sIpAddress[i];
if (n > 3) {
return false;
}
if (c >= '0' && c <= '9') {
s[n] += c;
} else if (c == '.') {
n++;
} else {
return false;
}
}
for (int i = 0; i < 4; i++) {
if (s[i].length() > 3) {
return false;
}
int p = std::stoi(s[i]);
if (p > 255 || p < 0) {
return false;
}
m_arrIP[i] = p;
}
return true;
}
// ----------------------------------------------------------------------
std::string WsjcppGeoIPv4::toString() {
std::string sRet =
std::to_string(m_arrIP[0]) + "."
+ std::to_string(m_arrIP[1]) + "."
+ std::to_string(m_arrIP[2]) + "."
+ std::to_string(m_arrIP[3]);
return sRet;
}
// ----------------------------------------------------------------------
bool WsjcppGeoIPv4::operator==(const WsjcppGeoIPv4& rhs) {
for (int i = 0; i < 4; i++) {
if (m_arrIP[i] == rhs.m_arrIP[i]) {
continue;
}
if (m_arrIP[i] != rhs.m_arrIP[i]) {
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppGeoIPv4::operator<(const WsjcppGeoIPv4& rhs) {
for (int i = 0; i < 4; i++) {
if (m_arrIP[i] == rhs.m_arrIP[i]) {
continue;
}
if (m_arrIP[i] < rhs.m_arrIP[i]) {
return true;
}
}
return false;
}
// ----------------------------------------------------------------------
bool WsjcppGeoIPv4::operator>(const WsjcppGeoIPv4& rhs) {
for (int i = 0; i < 4; i++) {
if (m_arrIP[i] == rhs.m_arrIP[i]) {
continue;
}
if (m_arrIP[i] > rhs.m_arrIP[i]) {
return true;
}
}
return false;
}
// ----------------------------------------------------------------------
bool WsjcppGeoIPv4::operator<=(const WsjcppGeoIPv4& rhs) {
for (int i = 0; i < 4; i++) {
if (m_arrIP[i] == rhs.m_arrIP[i]) {
continue;
}
if (m_arrIP[i] > rhs.m_arrIP[i]) {
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppGeoIPv4::operator>=(const WsjcppGeoIPv4& rhs) {
for (int i = 0; i < 4; i++) {
if (m_arrIP[i] == rhs.m_arrIP[i]) {
continue;
}
if (m_arrIP[i] < rhs.m_arrIP[i]) {
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
size_t WsjcppGeoIP_CallbackFunc_DataToString(char *data, size_t size, size_t nmemb, std::string *writerData) {
if (writerData == NULL) {
return 0;
}
writerData->append(data, size*nmemb);
return size * nmemb;
}
// ----------------------------------------------------------------------
WsjcppGeoIPResult WsjcppGeoIP::requestToIpApiCom(const std::string &sIpAddress) {
std::string sServiceName = "ip-api.com";
std::string TAG = "requestToIpApiCom";
std::string sError;
bool bValid = false;
if (WsjcppValidators::isValidIPv4(sIpAddress, sError)) {
bValid = true;
WsjcppGeoIPv4 ipv4;
ipv4.fromString(sIpAddress);
if (WsjcppGeoIP::isIPv4InReservedRange(ipv4, sError)) {
return WsjcppGeoIPResult(sServiceName, sIpAddress, "reserved range (" + sError + ")");
}
} else if (WsjcppValidators::isValidIPv6(sIpAddress, sError)) {
// TODO check valid range for ipv6
} else {
return WsjcppGeoIPResult("ipapi.com", sIpAddress, "Expected ip address");
}
std::string sUrl = "http://ip-api.com/json/" + sIpAddress;
std::string sUserAgent = "wsjcpp-geoip";
CURL *curl;
CURLcode res;
curl = curl_easy_init();
std::string sResponse = "";
if (curl) {
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https
curl_easy_setopt(curl, CURLOPT_URL, sUrl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WsjcppGeoIP_CallbackFunc_DataToString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &sResponse);
curl_easy_setopt(curl, CURLOPT_USERAGENT, sUserAgent.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::string sError = "Curl failed, reason " + std::string(curl_easy_strerror(res));
WsjcppLog::err(TAG, sError);
curl_easy_cleanup(curl);
WsjcppGeoIPResult(sServiceName, sIpAddress, sError);
} else {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200) {
WsjcppLog::info(TAG, "end " + std::to_string(response_code));
curl_easy_cleanup(curl);
}
}
// always cleanup
curl_easy_cleanup(curl);
}
return WsjcppGeoIP::parseResponseIpApiCom(sIpAddress, sResponse);
}
// ----------------------------------------------------------------------
WsjcppGeoIPResult WsjcppGeoIP::parseResponseIpApiCom(const std::string &sIpAddress, const std::string &sJson) {
std::string sServiceName = "ip-api.com";
std::string TAG = "parseResponseIpApiCom";
std::string sRequestUrl = "ip-api.com/json/" + sIpAddress;
std::string sCountry;
std::string sRegionName;
std::string sCity;
double nLatitude;
double nLongitude;
std::string sStatus = "";
if (!nlohmann::json::accept(sJson)) {
WsjcppLog::err(TAG, sRequestUrl + ", Expected json: " + sJson);
return WsjcppGeoIPResult(sServiceName, sIpAddress, "Expected json");
}
nlohmann::json obj = nlohmann::json::parse(sJson);
try {
sStatus = obj.at("status").get<std::string>();
} catch (const std::exception &e) {
std::string sError = sRequestUrl + " -> wrong field 'status' in struct \n" + sJson + "\n" + std::string(e.what());
WsjcppLog::err(TAG, sError);
return WsjcppGeoIPResult(sServiceName, sIpAddress, sError);
}
if (sStatus == "fail") {
std::string sErrorMessage = "";
try {
sErrorMessage = obj.at("message").get<std::string>();
} catch (const std::exception &e) {
std::string sError = sRequestUrl + " -> wrong field 'message' in struct \n" + sJson + "\n" + std::string(e.what());
WsjcppLog::err(TAG, sError);
return WsjcppGeoIPResult(sServiceName, sIpAddress, sError);
}
WsjcppLog::err(TAG, sRequestUrl + " -> fail: " + sErrorMessage);
return WsjcppGeoIPResult(sServiceName, sIpAddress, sErrorMessage);
}
try {
sCountry = obj.at("country").get<std::string>();
} catch (const std::exception &e) {
WsjcppLog::err(TAG, sRequestUrl + " -> wrong field 'country' in struct \n" + sJson + "\n" + std::string(e.what()));
sCountry = "";
}
try {
sRegionName = obj.at("regionName").get<std::string>();
} catch (const std::exception &e) {
WsjcppLog::err(TAG, sRequestUrl + " -> wrong field 'regionName' in struct \n" + sJson + "\n" + std::string(e.what()));
sRegionName = "";
}
try {
sCity = obj.at("city").get<std::string>();
} catch (const std::exception &e) {
WsjcppLog::err(TAG, sRequestUrl + " -> wrong field 'city' in struct \n" + sJson + "\n" + std::string(e.what()));
sCity = "";
}
try {
nLatitude = obj.at("lat").get<double>();
} catch (const std::exception &e) {
WsjcppLog::err(TAG, sRequestUrl + " -> wrong field 'lat' in struct \n" + sJson + "\n" + std::string(e.what()));
nLatitude = 0.0;
}
try {
nLongitude = obj.at("lon").get<double>();
} catch (const std::exception &e) {
WsjcppLog::err(TAG, sRequestUrl + " -> wrong field 'lon' in struct \n" + sJson + "\n" + std::string(e.what()));
nLongitude = 0.0;
}
// ok
return WsjcppGeoIPResult(
sServiceName,
sIpAddress,
sCountry,
sRegionName,
sCity,
nLatitude,
nLongitude
);
}
// ----------------------------------------------------------------------
bool WsjcppGeoIP::isIPv4InReservedRange(const WsjcppGeoIPv4& ipV4, std::string &sError) {
sError = "";
WsjcppGeoIPv4 currentIp = ipV4;
// https://en.wikipedia.org/wiki/Reserved_IP_addresses
if (currentIp >= WsjcppGeoIPv4(0,0,0,0) && currentIp <= WsjcppGeoIPv4(0,255,255,255)) {
sError = "Software. Current network (only valid as source address).";
return true;
}
if (currentIp >= WsjcppGeoIPv4(10,0,0,0) && currentIp <= WsjcppGeoIPv4(10,255,255,255)) {
sError = "Private network. Used for local communications within a private network.";
return true;
}
if (currentIp >= WsjcppGeoIPv4(100,64,0,0) && currentIp <= WsjcppGeoIPv4(100,127,255,255)) {
sError = "Private network. Shared address space for communications between a service provider and its subscribers when using a carrier-grade NAT.";
return true;
}
if (currentIp >= WsjcppGeoIPv4(127,0,0,0) && currentIp <= WsjcppGeoIPv4(127,255,255,255)) {
sError = "Host. Used for loopback addresses to the local host.";
return true;
}
if (currentIp >= WsjcppGeoIPv4(169,254,0,0) && currentIp <= WsjcppGeoIPv4(169,254,255,255)) {
sError = "Subnet. Used for link-local addresses between two hosts on a single link when no IP address is otherwise specified, such as would have normally been retrieved from a DHCP server.";
return true;
}
if (currentIp >= WsjcppGeoIPv4(172,16,0,0) && currentIp <= WsjcppGeoIPv4(172,31,255,255)) {
sError = "Private network. Used for local communications within a private network.";
return true;
}
if (currentIp >= WsjcppGeoIPv4(192,0,0,0) && currentIp <= WsjcppGeoIPv4(192,0,0,255)) {
sError = "Private network. IETF Protocol Assignments.";
return true;
}
if (currentIp >= WsjcppGeoIPv4(192,0,2,0) && currentIp <= WsjcppGeoIPv4(192,0,2,255)) {
sError = "Documentation. Assigned as TEST-NET-1, documentation and examples.";
return true;
}
if (currentIp >= WsjcppGeoIPv4(192,88,99,0) && currentIp <= WsjcppGeoIPv4(192,88,99,255)) {
sError = "Internet. Reserved. Formerly used for IPv6 to IPv4 relay (included IPv6 address block 2002::/16).";
return true;
}
if (currentIp >= WsjcppGeoIPv4(192,168,0,0) && currentIp <= WsjcppGeoIPv4(192,168,255,255)) {
sError = "Private network. Used for local communications within a private network.";
return true;
}
if (currentIp >= WsjcppGeoIPv4(198,18,0,0) && currentIp <= WsjcppGeoIPv4(198,19,255,255)) {
sError = "Private network. Used for benchmark testing of inter-network communications between two separate subnets.";
return true;
}
//
// if (currentIp >= WsjcppGeoIPv4(198,51,100,0) && currentIp <= WsjcppGeoIPv4(198,51,100,255)) {
// sError = "Documentation. Assigned as TEST-NET-2, documentation and examples.";
// return true;
// }
// if (currentIp >= WsjcppGeoIPv4(203,0,113,0) && currentIp <= WsjcppGeoIPv4(203,0,113,255)) {
// sError = "Documentation. Assigned as TEST-NET-3, documentation and examples.";
// return true;
// }
if (currentIp >= WsjcppGeoIPv4(224,0,0,0) && currentIp <= WsjcppGeoIPv4(239,255,255,255)) {
sError = "Internet. In use for IP multicast. (Former Class D network).";
return true;
}
if (currentIp >= WsjcppGeoIPv4(240,0,0,0) && currentIp <= WsjcppGeoIPv4(255,255,255,254)) {
sError = "Internet. Reserved for future use. (Former Class E network).";
return true;
}
if (currentIp >= WsjcppGeoIPv4(240,0,0,0) && currentIp <= WsjcppGeoIPv4(255,255,255,254)) {
sError = "Internet. Reserved for future use. (Former Class E network).";
return true;
}
if (currentIp >= WsjcppGeoIPv4(255,255,255,255) && currentIp <= WsjcppGeoIPv4(255,255,255,255)) {
sError = "Subnet. Reserved for the 'limited broadcast' destination address.";
return true;
}
return false;
}