1. Spawner10 Разработчик

    Регистрация:
    14.07.15
    Сообщения:
    188
    Симпатии:
    95
    So i'm making a plugin that show's on connect Country and City.
    Before you say to me why don't you use geoip, because it returns always an error in country and city.

    Using my plugin will return about 10% error in City 100% correct country.
    Using geoip will return 60% error in City 80% Error in country.

    So is there a way to remove the lag/beug? ( I've've tested on a full server 32/32 ) and when 2 players connect at the server bug and freeze for 3 or 2 seconds.
    Код:
    /* Plugin generated by AMXX-Studio */
    
    #include <amxmodx>
    #include <amxmisc>
    #include <sockets>
    #include <regex>
    
    #define REPEAT_TIME 15
    #define hostConnect "freegeoip.net"
       
    new const errorReturn[]            =    "error";
    new gSocket, count = 0
    
    new const GeoIP_country_name[][] =
    { "N/A",
        "Asia/Pacific Region",                         "Europe",
        "Andorra",                                     "United Arab Emirates",
        "Afghanistan",                                 "Antigua and Barbuda",
        "Anguilla",
        "Albania",                                     "Armenia",
        "Curacao",
        "Angola",
        "Antarctica",                                  "Argentina",
        "American Samoa",                              "Austria",
        "Australia",                                   "Aruba",
        "Azerbaijan",
        "Bosnia and Herzegovina",                      "Barbados",
        "Bangladesh",
        "Belgium",                                     "Burkina Faso",
        "Bulgaria",                                    "Bahrain",
        "Burundi",                                     "Benin",
        "Bermuda",
        "Brunei Darussalam",                           "Bolivia",
        "Brazil",
        "Bahamas",                                     "Bhutan",
        "Bouvet Island",                               "Botswana",
        "Belarus",                                     "Belize",
        "Canada",
        "Cocos (Keeling) Islands",
        "Congo, The Democratic Republic of the",
        "Central African Republic",
        "Congo",                                       "Switzerland",
        "Cote D'Ivoire",                               "Cook Islands",
        "Chile",                                       "Cameroon",
        "China",
        "Colombia",                                    "Costa Rica",
        "Cuba",
        "Cape Verde",                                  "Christmas Island",
        "Cyprus",                                      "Czech Republic",
        "Germany",                                     "Djibouti",
        "Denmark",
        "Dominica",                                    "Dominican Republic",
        "Algeria",
        "Ecuador",                                     "Estonia",
        "Egypt",                                       "Western Sahara",
        "Eritrea",                                     "Spain",
        "Ethiopia",
        "Finland",                                     "Fiji",
        "Falkland Islands (Malvinas)",
        "Micronesia, Federated States of",             "Faroe Islands",
        "France",                                      "Sint Maarten (Dutch part)",
        "Gabon",                                       "United Kingdom",
        "Grenada",
        "Georgia",                                     "French Guiana",
        "Ghana",
        "Gibraltar",                                   "Greenland",
        "Gambia",                                      "Guinea",
        "Guadeloupe",                                  "Equatorial Guinea",
        "Greece",
        "South Georgia and the South Sandwich Islands","Guatemala",
        "Guam",
        "Guinea-Bissau",                               "Guyana",
        "Hong Kong",
        "Heard Island and McDonald Islands",
        "Honduras",                                    "Croatia",
        "Haiti",
        "Hungary",                                     "Indonesia",
        "Ireland",
        "Israel",                                      "India",
        "British Indian Ocean Territory",              "Iraq",
        "Iran, Islamic Republic of",                   "Iceland",
        "Italy",
        "Jamaica",                                     "Jordan",
        "Japan",
        "Kenya",                                       "Kyrgyzstan",
        "Cambodia",                                    "Kiribati",
        "Comoros",                                     "Saint Kitts and Nevis",
        "Korea, Democratic People's Republic of",      "Korea, Republic of",
        "Kuwait",
        "Cayman Islands",
        "Kazakhstan",
        "Lao People's Democratic Republic",
        "Lebanon",                                     "Saint Lucia",
        "Liechtenstein",                               "Sri Lanka",
        "Liberia",
        "Lesotho",                                     "Lithuania",
        "Luxembourg",
        "Latvia",                                      "Libya",
        "Morocco",                                     "Monaco",
        "Moldova, Republic of",                        "Madagascar",
        "Marshall Islands",                            "Macedonia",
        "Mali",
        "Myanmar",
        "Mongolia",                                    "Macau",
        "Northern Mariana Islands",                    "Martinique",
        "Mauritania",                                  "Montserrat",
        "Malta",
        "Mauritius",                                   "Maldives",
        "Malawi",
        "Mexico",                                      "Malaysia",
        "Mozambique",                                  "Namibia",
        "New Caledonia",                               "Niger",
        "Norfolk Island",
        "Nigeria",                                     "Nicaragua",
        "Netherlands",
        "Norway",                                      "Nepal",
        "Nauru",                                       "Niue",
        "New Zealand",                                 "Oman",
        "Panama",
        "Peru",                                        "French Polynesia",
        "Papua New Guinea",
        "Philippines",                                 "Pakistan",
        "Poland",                                      "Saint Pierre and Miquelon",
        "Pitcairn Islands",                            "Puerto Rico",
        "Palestinian Territory",                       "Portugal",
        "Palau",
        "Paraguay",
        "Qatar",                                       "Reunion",
        "Romania",                                     "Russian Federation",
        "Rwanda",                                      "Saudi Arabia",
        "Solomon Islands",                             "Seychelles",
        "Sudan",
        "Sweden",
        "Singapore",                                   "Saint Helena",
        "Slovenia",                                    "Svalbard and Jan Mayen",
        "Slovakia",                                    "Sierra Leone",
        "San Marino",
        "Senegal",                                     "Somalia",
        "Suriname",
        "Sao Tome and Principe",
        "El Salvador",                                 "Syrian Arab Republic",
        "Swaziland",                                   "Turks and Caicos Islands",
        "Chad",                                        "French Southern Territories",
        "Togo",
        "Thailand",
        "Tajikistan",
        "Tokelau",                                     "Turkmenistan",
        "Tunisia",                                     "Tonga",
        "Timor-Leste",                                 "Turkey",
        "Trinidad and Tobago",                         "Tuvalu",
        "Taiwan",
        "Tanzania, United Republic of",
        "Ukraine",                                     "Uganda",
        "United States Minor Outlying Islands",        "United States",
        "Uruguay",                                     "Uzbekistan",
        "Holy See (Vatican City State)",
        "Saint Vincent and the Grenadines",
        "Venezuela",
        "Virgin Islands, British",
        "Virgin Islands, U.S.",                        "Vietnam",
        "Vanuatu",                                     "Wallis and Futuna",
        "Samoa",                                       "Yemen",
        "Mayotte",
        "Serbia",                                      "South Africa",
        "Zambia",
        "Montenegro",                                  "Zimbabwe",
        "Anonymous Proxy",                             "Satellite Provider",
        "Other",                                       "Aland Islands",
        "Guernsey",
        "Isle of Man",                                 "Jersey",
        "Saint Barthelemy",
        "Saint Martin",
        "Bonaire, Saint Eustatius and Saba",
        "South Sudan",                                 "Other" 
    };  
    
    public plugin_init(){
        register_plugin(
            .plugin_name = "Connect Plugin based on Web API",
            .version = "1.0",
            .author = "Spawner/Freezo")
       
    }
    
    public client_connect(id)
    {
        if(!is_user_bot(id))
            loadSocket(id)
    }
    
    public loadSocket(id){
       
        new errorReturned;
       
        gSocket = socket_open(hostConnect, 80, SOCKET_TCP, errorReturned)
       
       
        if(errorReturned == 0){
           
            new sBuffer[128], IP[24] 
           
            get_user_ip(
                                id,
                                IP,
                                charsmax(IP),
                                .without_port= 1
            )
           
            format(sBuffer, 127, "GET /json/%s HTTP/1.1^nHost:%s^r^n^r^n", IP, hostConnect)
           
            if(socket_send(gSocket, sBuffer, 127)){
               
                set_task(1.0, "answerSocket", id, _, _, "a", REPEAT_TIME)
            }
           
        }
       
        return PLUGIN_HANDLED
    }
    
    public answerSocket(id){
       
        if(count != REPEAT_TIME){
            if(socket_change(gSocket)){
                new socketData[1024]
               
                socket_recv(gSocket, socketData, charsmax(socketData))
                new szName[32]
                get_user_name(id, szName, charsmax(szName))
               
                new szSteam[32]
                get_user_authid(id, szSteam, charsmax(szSteam))
               
                new num, error[128]
                new pattern[] = "city.+(?=zip)"
                new Regex:regexref = regex_match(socketData,pattern,num,error,127)
                new matchString[64]
               
                if(regexref >= REGEX_OK)
                {
                    for(new i=0; i<num; i++)
                    {
                        regex_substr(regexref, i, matchString, charsmax(matchString))
    
                        new const replaceC[][] = { "^"" , "city", ",", ":"}
    
                        for(new i = 0; i < sizeof(replaceC); i++)
                            replace_all(matchString, charsmax(matchString), replaceC[i], "")
                       
                    }
                    if(equal(matchString,""))    matchString = errorReturn;
                   
                    regex_free(regexref)
                }
               
               
               
                for(new i = 0; i< sizeof ( GeoIP_country_name ); i++){
                    if(containi(socketData, GeoIP_country_name[i]) != -1 ){
                        ChatColor(0, "!g[NG] !yPlayer !g%s!t[!g%s!t] !yhas connected from !t[!g%s!t]!y->!t[!g%s!t]",szName, szSteam,  GeoIP_country_name[i], matchString )
                       
                        break;
                    }
                }
               
               
    
                socket_close(gSocket)
                remove_task(id)
            }
        }
        else{
            socket_close(gSocket)
            remove_task(id)
        }
        count++
       
        return PLUGIN_HANDLED
    }  
    stock ChatColor(const id, const input[], any:...)
    {
        new count2 = 1, players[32]
        static msg[191]
        vformat(msg, 190, input, 3)
       
        replace_all(msg, 190, "!g", "^4") // Green Color
        replace_all(msg, 190, "!y", "^1") // Default Color
        replace_all(msg, 190, "!t", "^3") // Team Color
        replace_all(msg, 190, "!t2", "^0") // Team2 Color
       
        if (id) players[0] = id; else get_players(players, count2, "ch")
        {
        for (new i = 0; i < count2; i++)
        {
            if (is_user_connected(players[i]))
            {
                message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, players[i])
                write_byte(players[i]);
                write_string(msg);
                message_end();
            }
        }
    }
    }
    
    
    By the way, it returns
    Код:
    [NG] Player with name Uchiha~ Spawner[STEAM_0:0:1352912**] has connected from [Morocco]->[**REMOVED**]
    
     
  2. neygomon Его Темнейшество :j Команда форума

    Регистрация:
    26.02.15
    Сообщения:
    7.594
    Симпатии:
    4.169
    Пол:
    Мужской
    socket_change тут вроде надо время указывать
    чтобы натива зафейлила сокет а не ждала если нет ответа

    а вообще, лучше sypex geo module использовать
     
  3. Spawner10 Разработчик

    Регистрация:
    14.07.15
    Сообщения:
    188
    Симпатии:
    95
    [SypexGeo] SxGeo didn't loaded

    do you mean something like that in socket_change:
    Код:
    public loadSocket(id){
       
        new errorReturned;
       
        gSocket = socket_open(hostConnect, 80, SOCKET_TCP, errorReturned)
       
       
        if(errorReturned == 0){
           
            new sBuffer[128], IP[24] 
           
            get_user_ip(
            id,
            IP,
            charsmax(IP),
            .without_port= 1
            )
    
            // to test in local we gonna set a valid ip not a local one
            format(sBuffer, 127, "GET /json/105.157.208.19 HTTP/1.1^nHost:%s^r^n^r^n", hostConnect)
            //format(sBuffer, 127, "GET /json/%s HTTP/1.1^nHost:%s^r^n^r^n", IP, hostConnect)
           
            socket_send( gSocket , sBuffer , strlen(sBuffer) );
           
            if( !task_exists( id ) )
                set_task( 1.0 , "answerSocket" , id , _, _, "b" );
           
           
        }
       
        return PLUGIN_HANDLED
    }
    
    public answerSocket(id){
       
       
        if(socket_change(gSocket)){
           
            new socketData[504]
           
            socket_recv(gSocket, socketData, charsmax(socketData))
           
            if ( strlen( socketData ) )    {
                new szName[32]
                get_user_name(id, szName, charsmax(szName))
               
                new szSteam[32]
                get_user_authid(id, szSteam, charsmax(szSteam))
               
               
                new num, error[128]
                new pattern[] = "city.+(?=zip)"
                new Regex:regexref = regex_match(socketData,pattern,num,error,127)
                new matchString[64]
               
                if(regexref >= REGEX_OK)
                {
                    for(new i=0; i<num; i++)
                    {
                        regex_substr(regexref, i, matchString, charsmax(matchString))
                       
                        new const replaceC[][] = { "^"" , "city", ",", ":"}
                       
                        for(new i = 0; i < sizeof(replaceC); i++)
                            replace_all(matchString, charsmax(matchString), replaceC[i], "")
                       
                    }
                    if(equal(matchString,""))    matchString = errorReturn;
                   
                    regex_free(regexref)
                }
               
               
               
                for(new i = 0; i< sizeof ( GeoIP_country_name ); i++){
                    if(containi(socketData, GeoIP_country_name[i]) != -1 ){
                        ChatColor(0, "!g[NG] !yPlayer !g%s!t[!g%s!t] !yhas connected from !t[!g%s!t]!y->!t[!g%s!t]",szName, szSteam,  GeoIP_country_name[i], matchString )
                       
                        socket_close(gSocket)
                        remove_task(id)
                        break;
                    }
                }
     
            }
        }
    
        return PLUGIN_HANDLED
    }  
    
     
  4. PRoSToC0der Разработчик

    Регистрация:
    22.02.16
    Сообщения:
    307
    Симпатии:
    143
    Пол:
    Мужской
    @Spawner10 socket_change(..., 1) or socket_change(..., 0)
    If zero timeout works, then use it.
     
  5. Spawner10 Разработчик

    Регистрация:
    14.07.15
    Сообщения:
    188
    Симпатии:
    95
    Now it's much worst, a huge lag.
    i don't know why changing timeout is helpful, i've already did a set_task with 15 repeats if there is no response or returning data then close the sockets.