/
extension.cpp
421 lines (338 loc) · 11 KB
/
extension.cpp
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
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod GeoIP Extension
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#define _USE_MATH_DEFINES
#include <sourcemod_version.h>
#include <cmath>
#include "extension.h"
#include "geoip_util.h"
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
GeoIP_Extension g_GeoIP;
MMDB_s mmdb;
SMEXT_LINK(&g_GeoIP);
bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
if (mmdb.filename) // Already loaded.
{
return true;
}
char m_GeoipDir[PLATFORM_MAX_PATH];
g_pSM->BuildPath(Path_SM, m_GeoipDir, sizeof(m_GeoipDir), "configs/geoip");
bool hasEntry = false;
IDirectory *dir = libsys->OpenDirectory(m_GeoipDir);
if (dir)
{
while (dir->MoreFiles())
{
if (dir->IsEntryFile())
{
const char *name = dir->GetEntryName();
size_t len = strlen(name);
if (len >= 5 && strcmp(&name[len-5], ".mmdb") == 0)
{
char database[PLATFORM_MAX_PATH];
libsys->PathFormat(database, sizeof(database), "%s/%s", m_GeoipDir, name);
int status = MMDB_open(database, MMDB_MODE_MMAP, &mmdb);
if (status != MMDB_SUCCESS)
{
ke::SafeStrcpy(error, maxlength, "Failed to open GeoIP2 database.");
libsys->CloseDirectory(dir);
return false;
}
hasEntry = true;
break;
}
}
dir->NextEntry();
}
libsys->CloseDirectory(dir);
}
if (!hasEntry)
{
ke::SafeStrcpy(error, maxlength, "Could not find GeoIP2 database.");
return false;
}
g_pShareSys->AddNatives(myself, geoip_natives);
g_pShareSys->RegisterLibrary(myself, "GeoIP");
char date[40];
const time_t epoch = (const time_t)mmdb.metadata.build_epoch;
strftime(date, 40, "%F %T UTC", gmtime(&epoch));
g_pSM->LogMessage(myself, "GeoIP2 database loaded: %s (%s)", mmdb.metadata.database_type, date);
if (mmdb.metadata.languages.count > 0)
{
char buf[64];
for (size_t i = 0; i < mmdb.metadata.languages.count; i++)
{
if (i == 0)
{
strcpy(buf, mmdb.metadata.languages.names[i]);
}
else
{
strcat(buf, " ");
strcat(buf, mmdb.metadata.languages.names[i]);
}
}
g_pSM->LogMessage(myself, "GeoIP2 supported languages: %s", buf);
}
return true;
}
void GeoIP_Extension::SDK_OnUnload()
{
MMDB_close(&mmdb);
}
const char *GeoIP_Extension::GetExtensionVerString()
{
return SOURCEMOD_VERSION;
}
const char *GeoIP_Extension::GetExtensionDateString()
{
return SOURCEMOD_BUILD_TIME;
}
/*******************************
* *
* GEOIP NATIVE IMPLEMENTATIONS *
* *
*******************************/
inline void StripPort(char *ip)
{
char *tmp = strchr(ip, ':');
if (!tmp)
return;
*tmp = '\0';
}
static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
const char *path[] = {"country", "iso_code", NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
pCtx->StringToLocalUTF8(params[2], 3, ccode, NULL);
return (str.length() != 0) ? 1 : 0;
}
static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
const char *path[] = {"country", "iso_code", NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
for (size_t i = 0; i < SM_ARRAYSIZE(GeoIPCountryCode); i++)
{
if (!strncmp(ccode, GeoIPCountryCode[i], 2))
{
ccode = GeoIPCountryCode3[i];
break;
}
}
pCtx->StringToLocalUTF8(params[2], 4, ccode, NULL);
return (str.length() != 0) ? 1 : 0;
}
static cell_t sm_Geoip_RegionCode(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
size_t length = 0;
char ccode[12] = { 0 };
const char *pathCountry[] = {"country", "iso_code", NULL};
std::string countryCode = lookupString(ip, pathCountry);
if (countryCode.length() != 0)
{
const char *pathRegion[] = {"subdivisions", "0", "iso_code", NULL};
std::string regionCode = lookupString(ip, pathRegion);
length = regionCode.length();
if (length != 0)
{
ke::SafeSprintf(ccode, sizeof(ccode), "%s-%s", countryCode.c_str(), regionCode.c_str());
}
}
pCtx->StringToLocalUTF8(params[2], sizeof(ccode), ccode, NULL);
return (length != 0) ? 1 : 0;
}
static cell_t sm_Geoip_ContinentCode(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
const char *path[] = {"continent", "code", NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
pCtx->StringToLocalUTF8(params[2], 3, ccode, NULL);
return getContinentId(ccode);
}
static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
const char *path[] = {"country", "names", "en", NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL);
return (str.length() != 0) ? 1 : 0;
}
static cell_t sm_Geoip_CountryEx(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
if (params[4] > 0)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(params[4]);
if (!player || !player->IsConnected())
{
return pCtx->ThrowNativeError("Invalid client index %d", params[4]);
}
}
const char *path[] = {"country", "names", getLang(params[4]), NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL);
return (str.length() != 0) ? 1 : 0;
}
static cell_t sm_Geoip_Continent(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
if (params[4] > 0)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(params[4]);
if (!player || !player->IsConnected())
{
return pCtx->ThrowNativeError("Invalid client index %d", params[4]);
}
}
const char *path[] = {"continent", "names", getLang(params[4]), NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL);
return (str.length() != 0) ? 1 : 0;
}
static cell_t sm_Geoip_Region(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
if (params[4] > 0)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(params[4]);
if (!player || !player->IsConnected())
{
return pCtx->ThrowNativeError("Invalid client index %d", params[4]);
}
}
const char *path[] = {"subdivisions", "0", "names", getLang(params[4]), NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL);
return (str.length() != 0) ? 1 : 0;
}
static cell_t sm_Geoip_City(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
if (params[4] > 0)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(params[4]);
if (!player || !player->IsConnected())
{
return pCtx->ThrowNativeError("Invalid client index %d", params[4]);
}
}
const char *path[] = {"city", "names", getLang(params[4]), NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL);
return (str.length() != 0) ? 1 : 0;
}
static cell_t sm_Geoip_Timezone(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
const char *path[] = {"location", "time_zone", NULL};
std::string str = lookupString(ip, path);
const char *ccode = str.c_str();
pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL);
return (str.length() != 0) ? 1 : 0;
}
static cell_t sm_Geoip_Latitude(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
const char *path[] = {"location", "latitude", NULL};
double latitude = lookupDouble(ip, path);
return sp_ftoc(latitude);
}
static cell_t sm_Geoip_Longitude(IPluginContext *pCtx, const cell_t *params)
{
char *ip;
pCtx->LocalToString(params[1], &ip);
StripPort(ip);
const char *path[] = {"location", "longitude", NULL};
double longitude = lookupDouble(ip, path);
return sp_ftoc(longitude);
}
static cell_t sm_Geoip_Distance(IPluginContext *pCtx, const cell_t *params)
{
float earthRadius = params[5] ? 3958.0 : 6370.997; // miles / km
float lat1 = sp_ctof(params[1]) * (M_PI / 180);
float lon1 = sp_ctof(params[2]) * (M_PI / 180);
float lat2 = sp_ctof(params[3]) * (M_PI / 180);
float lon2 = sp_ctof(params[4]) * (M_PI / 180);
return sp_ftoc(earthRadius * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)));
}
const sp_nativeinfo_t geoip_natives[] =
{
{"GeoipCode2", sm_Geoip_Code2},
{"GeoipCode3", sm_Geoip_Code3},
{"GeoipRegionCode", sm_Geoip_RegionCode},
{"GeoipContinentCode", sm_Geoip_ContinentCode},
{"GeoipCountry", sm_Geoip_Country},
{"GeoipCountryEx", sm_Geoip_CountryEx},
{"GeoipContinent", sm_Geoip_Continent},
{"GeoipRegion", sm_Geoip_Region},
{"GeoipCity", sm_Geoip_City},
{"GeoipTimezone", sm_Geoip_Timezone},
{"GeoipLatitude", sm_Geoip_Latitude},
{"GeoipLongitude", sm_Geoip_Longitude},
{"GeoipDistance", sm_Geoip_Distance},
{NULL, NULL},
};