Manual Работа с GeoIP

V1adJkee

Участник
Сообщения
293
Реакции
117
Баллы
43
GeoIP - модуль позволяющий узнать местоположение, расстояние от одного пункта до другого.
C++:
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
//     https://alliedmods.net/amxmodx-license

//
// GeoIP Module Functions
//

#if defined geoip_included
    #endinput
#endif
#define _geoip_included

#pragma reqlib geoip
#if !defined AMXMODX_NOAUTOLOAD
    #pragma loadlib geoip
#endif

/**
 * @global  IP addresses passed to these natives can contain ports, the ports will be ignored.
 */

/**
 * Look up the two character country code for a given IP address.
 * e.g: "US", "CA", etc.
 *
 * @param ip        The IP address to lookup.
 * @param result    The result buffer.  If the lookup does not succeed, the buffer is not modified.
 *
 * @return          true on a successful lookup, false on a failed lookup.
 */
native bool:geoip_code2_ex(const ip[], result[3]);

/**
 * Look up the three character country code for a given IP address.
 * e.g: "USA", "cAN", etc.
 *
 * @param ip        The IP address to lookup.
 * @param result    The result buffer.  If the lookup does not succeed, the buffer is not modified.
 *
 * @return          true on a successful lookup, false on a failed lookup.
 */
native bool:geoip_code3_ex(const ip[], result[4]);

/**
 * Lookup the two character country code for a given IP address. Sets the buffer to "error" on
 * an unsuccessful lookup.
 *
 * @deprecated  This native will overflow the buffer by one cell on an unknown ip lookup!
 *              Use geoip_code2_ex instead.
 *
 * @param ip        The IP address to lookup.
 * @param result    The result buffer.
 *
 * @return          The result length.
 */
#pragma deprecated Use geoip_code2_ex() instead.
native geoip_code2(const ip[], ccode[3]);

/**
 * Lookup the three character country code for a given IP address. Sets the buffer to "error" on
 * an unsuccessful lookup.
 *
 * @deprecated  This native will overflow the buffer by one cell on an unknown ip lookup!
 *              Use geoip_code3_ex instead.
 *
 * @param ip        The IP address to lookup.
 * @param result    The result buffer.
 *
 * @return          The result length.
 */
#pragma deprecated Use geoip_code3_ex() instead.
native geoip_code3(const ip[], result[4]);

/**
 * Lookup the full country name for the given IP address.  Sets the buffer to "error" on
 * an unsuccessful lookup.
 *
 * @param ip        The IP address to lookup.
 * @param result    The result of the geoip lookup.
 * @param len       The maximum length of the result buffer.
 *
 * @return          The result length.
 */
#pragma deprecated Use geoip_country_ex() instead.
native geoip_country(const ip[], result[], len = 45);

/**
 * Lookup the full country name for the given IP address.
 *
 * @param ip        The IP address to lookup.
 * @param result    The result of the geoip lookup.
 * @param len       The maximum length of the result buffer.
 * @param id        An optional player's index in order to return the result
 *                  in the player's language, if supported.
 *                  -1: the default language, which is english.
 *                   0: the server language. You can use LANG_SERVER define.
 *                 >=1: the player's language.
 *
 * @return          The result length on successful lookup, 0 otherwise.
 */
native geoip_country_ex(const ip[], result[], len, id = -1);

/**
 * Look up the full city name for the given IP address.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 *
 * @param ip        The IP address to look up.
 * @param result    The result of the geoip look up.
 * @param len       The maximum length of the result buffer.
 * @param id        An optional player's index in order to return the result
 *                  in the player's language, if supported.
 *                  -1: the default language, which is english.
 *                   0: the server language. You can use LANG_SERVER define.
 *                 >=1: the player's language.
 *
 * @return          The result length on successful lookup, 0 otherwise.
 */
native geoip_city(const ip[], result[], len, id = -1);

/**
 * Look up the region/state code for the given IP address.
 * e.g. "US-OH", "DE-HH", IT-82, "FR-U", etc.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 *
 * @param ip        The IP address to look up.
 * @param result    The result of the geoip look up.
 * @param len       The maximum length of the result buffer.
 *
 * @return          The result length on successful lookup, 0 otherwise.
 */
native geoip_region_code(const ip[], result[], len);

/**
 * Look up the full region/state name for the given IP address.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 *
 * @param ip        The IP address to look up.
 * @param result    The result of the geoip look up.
 * @param len       The maximum length of the result buffer.
 * @param id        An optional player's index in order to return the result
 *                  in the player's language, if supported.
 *                  -1: the default language, which is english.
 *                   0: the server language. You can use LANG_SERVER define.
 *                 >=1: the player's language.
 *
 * @return          The result length on successful lookup, 0 otherwise.
 */
native geoip_region_name(const ip[], result[], len, id = -1);

/**
 * Look up the full time zone for the given IP address.
 * e.g. America/Los_Angeles, Europe/Paris.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 *
 * @param ip        The IP address to look up.
 * @param result    The result of the geoip look up.
 * @param len       The maximum length of the result buffer.
 *
 * @return          The result length on successful lookup, 0 otherwise.
 */
native geoip_timezone(const ip[], result[], len);

/**
 * Look up the city's latitude for the given IP address.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 *
 * @param ip        The IP address to look up.
 *
 * @return          The result of the geoip look up, 0 if latitude is not found.
 */
native Float:geoip_latitude(const ip[]);

/**
 * Look up the city's longitude for the given IP address.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 *
 * @param ip        The IP address to look up.
 *
 * @return          The result of the geoip look up, 0 if longitude is not found.
 */
native Float:geoip_longitude(const ip[]);

/**
 * Calculate the distance between geographical coordinates, latitude and longitude.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 *
 * @param lat1      The first IP latitude.
 * @param lon1      The first IP longitude.
 * @param lat2      The second IP latitude.
 * @param lon2      The second IP longitude.
 * @param system    The system of measurement, 0 = Metric(kilometers) or 1 = English(miles).
 *
 * @return          The distance as result in specified system of measurement.
 */
#define SYSTEM_METRIC   0 // kilometers
#define SYSTEM_IMPERIAL 1 // statute miles

native Float:geoip_distance(Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = SYSTEM_METRIC);

/**
 * Look up the continent code for a given IP address.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 * @note  The code can be retrieved as integer (See CONTINENT_* constants.) or string (2 characters).
 * @note  Possible continent codes are AF, AN, AS, EU, NA, OC, SA for
 *        Africa(1), Antarctica(2), Asia(3), Europe(4), North America(5), Oceania(6), South America(7).
 *
 * @param ip        The IP address to look up.
 * @param result    The result of the geoip look up.
 *
 * @return          The continent id on successful lookup, 0 otherwise.
 */
enum Continent
{
    CONTINENT_UNKNOWN = 0,
    CONTINENT_AFRICA,
    CONTINENT_ANTARCTICA,
    CONTINENT_ASIA,
    CONTINENT_EUROPE,
    CONTINENT_NORTH_AMERICA,
    CONTINENT_OCEANIA,
    CONTINENT_SOUTH_AMERICA,
};
native Continent:geoip_continent_code(const ip[], result[3]);

/**
 * Look up the full continent name for the given IP address.
 *
 * @note  This native requires GeoIP City database, which can be retrieved from:
 *        http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
 *
 * @param ip        The IP address to look up.
 * @param result    The result of the geoip look up.
 * @param len       The maximum length of the result buffer.
 * @param id        An optional player's index in order to return the result
 *                  in the player's language, if supported.
 *                  -1: the default language, which is english.
 *                   0: the server language. You can use LANG_SERVER define.
 *                 >=1: the player's language.
 *
 * @return          The result length on successful lookup, 0 otherwise.
 */
native geoip_continent_name(const ip[], result[], len, id = -1);
 
Давайте рассмотрим пример работы на нативе geoip_country_ex

C++:
// native geoip_country(const ip[], result[], len = 45);

// В аргументах видим 3 значения

// Также есть native geoip_country_ex(const ip[], result[], len, id = -1); ( Добавлен в версии 1.8.3-dev либо 1.9.0 , точно не помню )

// В нём имеется 4 аргумента

// 1-ым аргументом мы должны указать ip-адрес игрока

// во 2-ой аргумент будет происходить запись результата

// 3-им результатом мы должны указать размер



public OnShowInfo(id) // Создадим простейшую функцию ( В аргументах передаём индекс игрока )

{



   new szCountry[35]; // В данную переменную мы запишем страну

   new szIp[23]; // В данную переменную мы запишем ip игрока, для последующего использования в функции geoip_country_ex



    get_user_ip(id, szIp, charsmax(szIp),1);  // с помощью данной функции мы получим ip и запишем его в переменную, 4-ым аргументом является тип который нам нужен ( 0 - с клиент-портом, 1 - лишь ip , подробнее [URL='https://www.amxmodx.org/api/amxmodx/get_user_ip']тык[/URL]  )


if(geoip_country_ex(szIp, szCountry, charsmax(szCountry)) == 0) // создадим вызов и вместе с ним проверку ( Если город не найден, то вернет значение равное 0 , данная проверка актуальна для AmxModX >= 1.8.3 )
    {

        formatex(szCountry, charsmax(szCountry), "Неопределён"); // Сюда мы запишем страну, если она не определена.
        client_print_color(id, 0, "Ваша страна - ^4%s", szCountry); // Выведем её, если у вас версия ранее 1.8.3 , то используйте свой колор-чат
    }


}
 
P.s. забыл дополнить,geoip_country был признан устаревшим с версии 1.9.0

Разбито на несколько частей, из-за лимита в 10к символов

Для определение Региона/Города, необходима доп. бд - скачать ( Качать которая в двоичном формате, 1-ый столбец )



C++:
// Закиньте БД с городами в /data/, после этого необходима ПЕРЕЗАГРУЗКА сервера, а не рестарт.

public OnShowInfo(id)
{
   
    new szIp[23], new szRegion[32], new szCity[64];
   
    get_user_ip(id, szIp, charsmax(szIp),1);
   
    if(geoip_city(szIp, szCity, charsmax(szCity)) == 0)
    {

        formatex(szCity, charsmax(szCity), "Неизвестно");

    }

    if(geoip_region_name(szIp, szRegion, charsmax(szRegion)) == 0)
    {

        formatex(szRegion, charsmax(szRegion), "Неизвестно");

    }
   
    client_print_color(id, 0, "^1[^4GeoIP^1] [ ^4%s ^1| ^4%s^1 ]", szRegion, szCity);
   
}
// Завтра дополню мануал, как вычислить растояние и т.д. и т.п.
 
Получаем дистанцию
C++:
public GetDistance(id)
{

    /* Будем использовать:

    geoip_latitude(const ip[])    // 1-айпи, получает широту
    geoip_longitude(const ip[]) // 1-айпи, получает долготу
    geoip_distance(Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = SYSTEM_METRIC) // 1 - широта, 2- долгота ( Первого пункта ), 3 - широта, 4 - долгота( Второго пункта ) , 5 - в каком формате вернуть

    #define SYSTEM_METRIC   0 // kilometers
    #define SYSTEM_IMPERIAL 1 // statute miles

    */

    new szIp[23], szIpTwo[23];

    get_user_ip(id, szIp, charsmax(szIp),1);
    get_user_ip(индекс второго игрока, szIpTwo, charsmax(szIpTwo),1);

    new Float:lat1 = geoip_latitude(szIp);
    new Float:lot1 = geoip_longitude(szIp);
    //  Получаем
    new Float:lat2 = geoip_latitude(szIpTwo);
    new Float:lot2 = geoip_longitude(szIpTwo);
    //Вычисляем

    new distance = geoip_distance(lat1, lot1, lat2, lot2, SYSTEM_METRIC);    // если не ошибаюсь возвращает в integer

    client_print_color(id, 0, "%d km.", distance);
 
@zhorzh78, ну в этой функции только рандомом наверное,писал как пример.
ну а если сравнивать, в аргументы функции добавить ещё 1 аргумент
 
Закинь в шапку, надо было всё таки более практичный пример сразу показать

Пример работы с вычислением расстояний:
 

Вложения

  • example.sma
    3.6 KB · Просмотры: 17
Прикольно, только зачем?
Для тех кто говорит "Я тебя по ip вычислю"? Что бы знали сколько ехать?))
 
@sania886, Это лишь пример, можно сравнивать к примеру по рег. ip ( У кого по нику с паролем, актуально будет )
 
@V1adJkee, не понял. Что сравнить? При чем здесь лог/пар?
 
@sania886, Ну допустим если человек продал/передал привелегию другому человеку, можно заносить 1-ый айпи адрес ( рег. ) и сравнивать его с последующими заходами.
 
@V1adJkee, тогда использовать место положения, норм. Но так как люди могут ехать за город, или включить впн. То будет гемор.
Хоть немного поможет расстояния. Указать что при ... расстоянии, все норм
 
Назад
Сверху