geoip.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. GeoIP Include by Whitetiger
  3. -> SQLite GeoIP Include
  4. Credits: Whitetiger, RaekwonDaChef, Y_Less, Andreas Gohr.
  5. MaxMind, GeoIP and related marks are registered trademarks of MaxMind, Inc.
  6. ---------------------------------------------------------------------------------------
  7. */
  8. #if defined _geolocation_included
  9. #endinput
  10. #endif
  11. #define _geolocation_included
  12. #include <a_samp>
  13. /*
  14. ---------------------------------------------------------------------------------------
  15. NATIVES
  16. ---------------------------------------------------------------------------------------
  17. */
  18. /*
  19. native GetPlayerCountry(playerid, string[], const len = sizeof(string));
  20. native GetPlayerISP(playerid, string[], const len = sizeof(string));
  21. native GetPlayerCity(playerid, string[], const len = sizeof(string));
  22. native GetPlayerGMT(playerid);
  23. native GetPlayerProxy(playerid);
  24. */
  25. /*
  26. ---------------------------------------------------------------------------------------
  27. Variables
  28. ---------------------------------------------------------------------------------------
  29. */
  30. #if !defined GeoIP_MainFile
  31. #define GeoIP_MainFile "geoip.db"
  32. #endif
  33. #if !defined GeoIP_CityFile
  34. #define GeoIP_CityFile "geoip_city.db"
  35. #endif
  36. new DB:geoip_db;
  37. new DB:geoip_city;
  38. new DBResult:_result;
  39. stock GetPlayerCountry(playerid, string[], const len = sizeof(string)) {
  40. new ip[24];
  41. GetPlayerIp(playerid, ip, sizeof(ip));
  42. GetIPCountry(ip, string, len);
  43. return true;
  44. }
  45. stock GetPlayerISP(playerid, string[], const len = sizeof(string)) {
  46. new ip[24];
  47. GetPlayerIp(playerid, ip, sizeof(ip));
  48. GetIPISP(ip, string, len);
  49. new placeholder[1];
  50. sscanf_(string, "ss", placeholder, string);
  51. return true;
  52. }
  53. stock GetPlayerCity(playerid, string[], const len = sizeof(string)) {
  54. new ip[24];
  55. GetPlayerIp(playerid, ip, sizeof(ip));
  56. GetIPCity(ip, string, len);
  57. return true;
  58. }
  59. stock GetPlayerGMT(playerid) {
  60. new ip[24];
  61. GetPlayerIp(playerid, ip, sizeof(ip));
  62. return GetIPGMT(ip);
  63. }
  64. stock GetPlayerProxy(playerid) {
  65. new ip[24];
  66. GetPlayerIp(playerid, ip, sizeof(ip));
  67. return GetIPProxy(ip, playerid);
  68. }
  69. /*
  70. ---------------------------------------------------------------------------------------
  71. INTERNAL FUNCTIONS
  72. ---------------------------------------------------------------------------------------
  73. - stock GetIPCountry(ip[], dest[], len = sizeof(dest))
  74. - stock GetIPISP(ip[], dest[], len = sizeof(dest))
  75. - stock GetIPCity(ip[], dest[], len = sizeof(dest))
  76. - stock GetIPGMT(ip[])
  77. - stock GetIPProxy(ip[])
  78. - stock ip2long(ip[])
  79. */
  80. stock GetIPCountry(ip[], dest[], const len = sizeof(dest)) {
  81. new tmp[90];
  82. tmp = ip2long(ip);
  83. new string[500];
  84. format(string, sizeof(string), "SELECT loc.*\n FROM loc_country loc,\n blocks_country blk\n WHERE blk.idx = (%s-(%s %% 65536))\n AND blk.startIpNum < %s\n AND blk.endIpNum > %s\n AND loc.locId = blk.locId LIMIT 1;", tmp, tmp, tmp, tmp);
  85. geoip_db = db_open(GeoIP_MainFile);
  86. _result = db_query(geoip_db, string);
  87. if(db_num_rows(_result) >= 1)
  88. {
  89. db_get_field_assoc(_result,"cn",dest,len);
  90. }
  91. db_free_result(_result);
  92. db_close(geoip_db);
  93. if(!strlen(dest)) format(dest, len, "Unknown");
  94. return true;
  95. }
  96. stock GetIPISP(ip[], dest[], const len = sizeof(dest)) {
  97. new tmp[90];
  98. tmp = ip2long(ip);
  99. new string[500];
  100. format(string, sizeof(string), "SELECT internet_service_provider FROM geo_isp WHERE idx >= (%s-(%s %% 65536)) AND ip_to >= %s AND ip_from < %s LIMIT 1", tmp, tmp, tmp, tmp);
  101. geoip_db = db_open(GeoIP_MainFile);
  102. _result = db_query(geoip_db, string);
  103. if(db_num_rows(_result) >= 1)
  104. {
  105. db_get_field_assoc(_result,"internet_service_provider",dest,len);
  106. }
  107. db_free_result(_result);
  108. db_close(geoip_db);
  109. if(!strlen(dest)) format(dest, len, "Unknown");
  110. return true;
  111. }
  112. stock GetIPCity(ip[], dest[], const len = sizeof(dest)) {
  113. new tmp[90];
  114. tmp = ip2long(ip);
  115. new string[500];
  116. format(string, sizeof(string), "SELECT loc.*\n FROM geolocation loc,\n geoblocks blk\n WHERE blk.idx = (%s-(%s %% 65536))\n AND blk.startIpNum < %s\n AND blk.endIpNum > %s\n AND loc.locId = blk.locId LIMIT 1;", tmp, tmp, tmp, tmp);
  117. geoip_city = db_open(GeoIP_CityFile);
  118. _result = db_query(geoip_city, string);
  119. if(db_num_rows(_result) >= 1)
  120. {
  121. db_get_field_assoc(_result,"city",dest,len);
  122. }
  123. db_free_result(_result);
  124. db_close(geoip_city);
  125. if(!strlen(dest)) format(dest, len, "Unknown");
  126. return true;
  127. }
  128. stock GetIPGMT(ip[]) {
  129. new tmp[90];
  130. tmp = ip2long(ip);
  131. new dest[50];
  132. new string[500];
  133. format(string, sizeof(string), "SELECT loc.*\n FROM geolocation loc,\n geoblocks blk\n WHERE blk.idx = (%s-(%s %% 65536))\n AND blk.startIpNum < %s\n AND blk.endIpNum > %s\n AND loc.locId = blk.locId LIMIT 1;", tmp, tmp, tmp, tmp);
  134. geoip_city = db_open(GeoIP_CityFile);
  135. _result = db_query(geoip_city, string);
  136. if(db_num_rows(_result) >= 1)
  137. {
  138. db_get_field_assoc(_result, "longitude", dest, sizeof(dest));
  139. }
  140. db_free_result(_result);
  141. db_close(geoip_city);
  142. if(!strlen(dest)) format(dest, sizeof(dest), "Unknown");
  143. return floatround( strval( dest ) / 15 );
  144. }
  145. stock GetIPProxy(ip[], playerid) {
  146. if(IsPlayerConnected(playerid) && strlen(ip) > 0) {
  147. new string[500];
  148. format(string, sizeof(string), "SELECT blk.*\n FROM geolocation loc,\n geoblocks blk\n WHERE blk.idx = (%s-(%s %% 65536))\n AND blk.startIpNum < %s\n AND blk.endIpNum > %s\n AND loc.locId = blk.locId LIMIT 1;", tmp, tmp, tmp, tmp);
  149. geoip_city = db_open(GeoIP_CityFile);
  150. _result = db_query(geoip_city, string);
  151. if(db_num_rows(_result) >= 1)
  152. {
  153. db_get_field_assoc(_result, "is_anonymous_proxy", dest, sizeof(dest));
  154. }
  155. db_free_result(_result);
  156. db_close(geoip_city);
  157. return strval(dest);
  158. }
  159. return false;
  160. }
  161. /*forward OnProxyRecieved(index, response_code, data[]);
  162. public OnProxyRecieved(index, response_code, data[]) {
  163. if(IsPlayerConnected(index)) {
  164. new ip[MAX_PLAYER_NAME];
  165. GetPlayerIp(index, ip, sizeof(ip));
  166. if(strlen(IPInfo[index]) > 0 && strlen(ip) > 0 && !strcmp(IPInfo[index], ip, true)) {
  167. CallLocalFunction("OnProxyResponse", "ii", index, strval(data));
  168. }
  169. }
  170. IPInfo[index] = "";
  171. }*/
  172. stock ip2long(ip[]) {
  173. new ips[4];
  174. sscanf_(ip, "p.dddd", ips[0], ips[1], ips[2], ips[3]);
  175. new tmp[90];
  176. format(tmp, sizeof(tmp), "((16777216 * %d) + (65536 * %d) + (256 * %d) + %d)", ips[0], ips[1], ips[2], ips[3]);
  177. // we use a string here so it will not pass the 32-bit integer limits, the math is done later in the sql query
  178. return tmp;
  179. }
  180. /*
  181. ---------------------------------------------------------------------------------------
  182. SECONDARY FUNCTIONS
  183. ---------------------------------------------------------------------------------------
  184. - stock sscanf_(string[], format[], {Float,_}:...)
  185. */
  186. stock sscanf_(string[], format[], {Float,_}:...) // by Y_Less
  187. {
  188. #if defined isnull
  189. if (isnull(string))
  190. #else
  191. if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  192. #endif
  193. {
  194. return format[0];
  195. }
  196. #pragma tabsize 4
  197. new
  198. formatPos = 0,
  199. stringPos = 0,
  200. paramPos = 2,
  201. paramCount = numargs(),
  202. delim = ' ';
  203. while (string[stringPos] && string[stringPos] <= ' ')
  204. {
  205. stringPos++;
  206. }
  207. while (paramPos < paramCount && string[stringPos])
  208. {
  209. switch (format[formatPos++])
  210. {
  211. case '\0':
  212. {
  213. return 0;
  214. }
  215. case 'i', 'd':
  216. {
  217. new
  218. neg = 1,
  219. num = 0,
  220. ch = string[stringPos];
  221. if (ch == '-')
  222. {
  223. neg = -1;
  224. ch = string[++stringPos];
  225. }
  226. do
  227. {
  228. stringPos++;
  229. if ('0' <= ch <= '9')
  230. {
  231. num = (num * 10) + (ch - '0');
  232. }
  233. else
  234. {
  235. return -1;
  236. }
  237. }
  238. while ((ch = string[stringPos]) > ' ' && ch != delim);
  239. setarg(paramPos, 0, num * neg);
  240. }
  241. case 'h', 'x':
  242. {
  243. new
  244. num = 0,
  245. ch = string[stringPos];
  246. do
  247. {
  248. stringPos++;
  249. switch (ch)
  250. {
  251. case 'x', 'X':
  252. {
  253. num = 0;
  254. continue;
  255. }
  256. case '0' .. '9':
  257. {
  258. num = (num << 4) | (ch - '0');
  259. }
  260. case 'a' .. 'f':
  261. {
  262. num = (num << 4) | (ch - ('a' - 10));
  263. }
  264. case 'A' .. 'F':
  265. {
  266. num = (num << 4) | (ch - ('A' - 10));
  267. }
  268. default:
  269. {
  270. return -1;
  271. }
  272. }
  273. }
  274. while ((ch = string[stringPos]) > ' ' && ch != delim);
  275. setarg(paramPos, 0, num);
  276. }
  277. case 'c':
  278. {
  279. setarg(paramPos, 0, string[stringPos++]);
  280. }
  281. case 'f':
  282. {
  283. new changestr[16], changepos = 0, strpos = stringPos;
  284. while(changepos < 16 && string[strpos] && string[strpos] != delim)
  285. {
  286. changestr[changepos++] = string[strpos++];
  287. }
  288. changestr[changepos] = '\0';
  289. setarg(paramPos,0,_:floatstr(changestr));
  290. }
  291. case 'p':
  292. {
  293. delim = format[formatPos++];
  294. continue;
  295. }
  296. case '\'':
  297. {
  298. new
  299. end = formatPos - 1,
  300. ch;
  301. while ((ch = format[++end]) && ch != '\'') {}
  302. if (!ch)
  303. {
  304. return -1;
  305. }
  306. format[end] = '\0';
  307. if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  308. {
  309. if (format[end + 1])
  310. {
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. format[end] = '\'';
  316. stringPos = ch + (end - formatPos);
  317. formatPos = end + 1;
  318. }
  319. case 'u':
  320. {
  321. new
  322. end = stringPos - 1,
  323. id = 0,
  324. bool:num = true,
  325. ch;
  326. while ((ch = string[++end]) && ch != delim)
  327. {
  328. if (num)
  329. {
  330. if ('0' <= ch <= '9')
  331. {
  332. id = (id * 10) + (ch - '0');
  333. }
  334. else
  335. {
  336. num = false;
  337. }
  338. }
  339. }
  340. if (num && IsPlayerConnected(id))
  341. {
  342. setarg(paramPos, 0, id);
  343. }
  344. else
  345. {
  346. #if !defined foreach
  347. #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  348. #define __SSCANF_FOREACH__
  349. #endif
  350. string[end] = '\0';
  351. num = false;
  352. new
  353. name[MAX_PLAYER_NAME];
  354. id = end - stringPos;
  355. foreach (Player, playerid)
  356. {
  357. GetPlayerName(playerid, name, sizeof (name));
  358. if (!strcmp(name, string[stringPos], true, id))
  359. {
  360. setarg(paramPos, 0, playerid);
  361. num = true;
  362. break;
  363. }
  364. }
  365. if (!num)
  366. {
  367. setarg(paramPos, 0, INVALID_PLAYER_ID);
  368. }
  369. string[end] = ch;
  370. #if defined __SSCANF_FOREACH__
  371. #undef foreach
  372. #undef __SSCANF_FOREACH__
  373. #endif
  374. }
  375. stringPos = end;
  376. }
  377. case 's', 'z':
  378. {
  379. new
  380. i = 0,
  381. ch;
  382. if (format[formatPos])
  383. {
  384. while ((ch = string[stringPos++]) && ch != delim)
  385. {
  386. setarg(paramPos, i++, ch);
  387. }
  388. if (!i)
  389. {
  390. return -1;
  391. }
  392. }
  393. else
  394. {
  395. while ((ch = string[stringPos++]))
  396. {
  397. setarg(paramPos, i++, ch);
  398. }
  399. }
  400. stringPos--;
  401. setarg(paramPos, i, '\0');
  402. }
  403. default:
  404. {
  405. continue;
  406. }
  407. }
  408. while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  409. {
  410. stringPos++;
  411. }
  412. while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  413. {
  414. stringPos++;
  415. }
  416. paramPos++;
  417. }
  418. do
  419. {
  420. if ((delim = format[formatPos++]) > ' ')
  421. {
  422. if (delim == '\'')
  423. {
  424. while ((delim = format[formatPos++]) && delim != '\'') {}
  425. }
  426. else if (delim != 'z')
  427. {
  428. return delim;
  429. }
  430. }
  431. }
  432. while (delim > ' ');
  433. return 0;
  434. }