Raised This Month: $32 Target: $400
 8% 

Find IP:PORT in string without REGEX module?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
karaulov
Senior Member
Join Date: Jul 2018
Old 11-20-2019 , 14:51   Find IP:PORT in string without REGEX module?
Reply With Quote #1

How to find IPORT in string without regex module ?((
karaulov is offline
Old 11-20-2019, 17:39
ZaX
This message has been deleted by ZaX. Reason: nvm
JocAnis
Veteran Member
Join Date: Jun 2010
Old 11-20-2019 , 18:22   Re: Find IP:PORT in string without REGEX module?
Reply With Quote #2

what is the *good* reason you wont use regex?
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-20-2019 , 20:37   Re: Find IP:PORT in string without REGEX module?
Reply With Quote #3

RegEx was designed for flexibility. If you need flexibility, use RegEx. If you want to check for a static IP and port, use a string compare.

A RegEx pattern for IP and port won't likely cause any significant performance issues. In fact, it may be more efficient than other methods if you have more than a handful of static IPs to check. Just remember to precompile the pattern.
__________________
fysiks is offline
karaulov
Senior Member
Join Date: Jul 2018
Old 11-20-2019 , 23:08   Re: Find IP:PORT in string without REGEX module?
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
RegEx was designed for flexibility. If you need flexibility, use RegEx. If you want to check for a static IP and port, use a string compare.

A RegEx pattern for IP and port won't likely cause any significant performance issues. In fact, it may be more efficient than other methods if you have more than a handful of static IPs to check. Just remember to precompile the pattern.
I got drop fps every say message, i use pattern found on this forums!

Code:
#define PATTERN                "(?:(?:2\s*5\s*[0-5]|2\s*[0-4]\s*[0-9]|1\s*[0-9]\s*[0-9]|[1-9]?\s*[0-9])\s*\.\s*){3}(?:2\s*5\s*[0-5]|2\s*[0-4]\s*[0-9]|1\s*[0-9]\s*[0-9]|[1-9]?\s*[0-9])"
Maybe this pattern is bad ? This pattern \d+\.\d+\.\d+\.\d+:\d+ can work, too?

I test join player/say message. And every player join - fps drop from 1000 to 900 (for example, 100 ms sleep), every say message, too.

Last edited by karaulov; 11-20-2019 at 23:17.
karaulov is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 11-20-2019 , 23:14   Re: Find IP:PORT in string without REGEX module?
Reply With Quote #5

Can you show full code? I have sure that is not a problem with regex itself.
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
karaulov
Senior Member
Join Date: Jul 2018
Old 11-20-2019 , 23:33   Re: Find IP:PORT in string without REGEX module?
Reply With Quote #6

Quote:
Originally Posted by ^SmileY View Post
Can you show full code? I have sure that is not a problem with regex itself.
https://pastebin.com/Fd1hEzn7
karaulov is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-21-2019 , 20:21   Re: Find IP:PORT in string without REGEX module?
Reply With Quote #7

Many spam blockers use RegEx for exactly this purpose and they don't seem to have any significant issues. E.g. Spam Blocker.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-22-2019 , 20:32   Re: Find IP:PORT in string without REGEX module?
Reply With Quote #8

I'm don't know RegEx so I can't help with that. The below will extract the IP and port from a IP : Port formatted string. Return is a bool based on success.

This will only validate the below, you can add more validation if you want.
  • A character exists before and after the ':'
  • The IP has at least 3 '.' characters
  • The port is numeric

Here is an example with good and bad formatted IP : Port strings.
PHP Code:
public Test()
{
    new const 
szTest[][] = 
    {
        
"192.168.1.1:27015",
        
"192.168.1.1:270",
        
"192.168.1.1:2",
        
"192.168.1.1:",
        
":27015",
        
"1:2",
        
"192.168.1:111",
        
"192:123",
        
"192.167:123",
        
"abc.def.ghi.jkl:mnopq"
    
};
    
    new 
szIP16 ] , szPort16 ];

    for ( new 
sizeofszTest ) ; i++ )
    {
        if ( 
Extract_IP_PortszTest] , szIP charsmaxszIP ) , szPort charsmaxszPort ) ) )
        {
            
server_print"VALID : IP=[%s] Port=[%s]" szIP szPort );
        }
        else
        {
            
server_print"INVALID : [%s]" szTest] );
        }
    }
}

bool:Extract_IP_Port( const szIPPort[] , szIP[] , iIPChars szPort[] , iPortChars )
{
    new 
iPos strfindszIPPort ":" );
    new 
iLen strlenszIPPort );
    new 
bool:bRetVal iDotCount;

    if ( ( 
iPos ) && ( ( iLen iPos ) >= ) )
    {
        
copyszIP iIPChars szIPPort );
        
szIPiPos ] = EOS;
        
copyszPort iPortChars szIPPortiPos ] );
        
        for ( new 
iIPChars i++ )
        {
            if ( 
szIP] == '.' )
            {
                
iDotCount++;
            }
        }
        
        
bRetVal bool:( ( iDotCount == ) && is_str_numszPort ) );
    }
    
    return 
bRetVal;

Code:
VALID : IP=[192.168.1.1] Port=[27015]
VALID : IP=[192.168.1.1] Port=[270]
VALID : IP=[192.168.1.1] Port=[2]
INVALID : [192.168.1.1:]
INVALID : [:27015]
INVALID : [1:2]
INVALID : [192.168.1:111]
INVALID : [192:123]
INVALID : [192.167:123]
INVALID : [abc.def.ghi.jkl:mnopq]
__________________

Last edited by Bugsy; 11-22-2019 at 20:35.
Bugsy is offline
DJEarthQuake
Veteran Member
Join Date: Jan 2014
Location: Astral planes
Old 11-23-2019 , 01:42   Re: Find IP:PORT in string without REGEX module?
Reply With Quote #9

Code:
        #include <amxmodx>     #include <regex>         #define PLUGINNAME    "ANTISPAM + PING KICKER"     #define VERSION        "0.1"     #define AUTHOR        "and karaulov"         #define PATTERN "(?:(?:2\s*5\s*[0-5]|2\s*[0-4]\s*[0-9]|1\s*[0-9]\s*[0-9]|[1-9]?\s*[0-9])\s*\.\s*){3}(?:2\s*5\s*[0-5]|2\s*[0-4]\s*[0-9]|1\s*[0-9]\s*[0-9]|[1-9]?\s*[0-9])"         #define ADMINLEVEL ADMIN_IMMUNITY         // Variables     new iResult, Regex:hPattern, iReturnValue, szError[64], szAllArgs[256], szMessage[256];         new badpingcheck[33];     new minping[33];     new maxping[33];     new lossess[33];         new const REALMAXPING = 125         public client_disconnected(id)     {     badpingcheck[id] = 0     }         public pings(id, level, cid)     {     if (get_user_flags(id) & ADMIN_IMMUNITY)     {     new players[32],iNum;     get_players(players,iNum)     for(new i = 0; i < iNum;i++)     {     new idx = players[i]     if (is_user_connected(idx))     {     new nPing, nLoss     get_user_ping(idx, nPing, nLoss)     new username[32]     get_user_name(idx,username,31)     client_print(id,print_console,"^4[PATRIOT PING]^1: Игрок ^3%s^1 потерь ^3%i^1 ping ^3%i^1 мин ^3%i^1  макс ^3%i^1 !",username,lossess[idx],nPing,minping[idx],maxping[idx] )     }     }     }     else     {     new idx = id     new nPing, nLoss     get_user_ping(idx, nPing, nLoss)     new username[32]     get_user_name(idx,username,31)     client_print(id,print_console,"^4[PATRIOT PING]^1: Игрок ^3%s^1 потерь ^3%i^1 ping ^3%i^1 мин ^3%i^1  макс ^3%i^1 !",username,lossess[idx],nPing,minping[idx],maxping[idx] )     }     }         public check_players(idhask)     {     new players[32],iNum;     get_players(players,iNum)     for(new i = 0; i < iNum;i++)     {     new id = players[i]     if (is_user_connected(id))     {     new nPing, nLoss     lossess[id] += nLoss;     get_user_ping(id, nPing, nLoss)     if (minping[id] == -1 || nPing < minping[id])     {     minping[id] = nPing;     }     if (maxping[id] < nPing)     {     maxping[id] = nPing;     }     if (nPing > REALMAXPING)     {     if (badpingcheck[id] > 10 && !(get_user_flags(id) & ADMIN_IMMUNITY))     {     new username[32]     get_user_name(id,username,31)     client_print_color(0,print_team_red,"^4[PATRIOT PING]^1: Игрок ^3%s^1 выброшен за высокий ping (^3%i^1) потерь ^3%i^1!",username,maxping[id],lossess[id])     server_cmd("kick #%i ^"ping %i^"", get_user_userid(id),maxping[id])     }     else     {        badpingcheck[id] += 1;     client_print_color(id,print_team_red,"^4[PATRIOT PING]^1: Ваш ping высокий (^3%i^1) из 140,потерь (^3%i^1). Предупреждение %i из 10!",nPing,lossess[id], badpingcheck[id])     }     }     else     {     badpingcheck[id] = badpingcheck[id] / 2;     }     }     }     }         public check_players2(idhask)     {     new players[32],iNum;     get_players(players,iNum)     for(new i = 0; i < iNum;i++)     {     new id = players[i]     if (is_user_connected(id))     {     new nPing, nLoss     get_user_ping(id, nPing, nLoss)     if (nPing < REALMAXPING)     {     badpingcheck[id]=0;     }     }     }     }         public client_putinserver(id)     {     badpingcheck[id] = 0;     lossess[id] = 0;     minping[id] = -1;         new userid = get_user_userid(id);     if (userid > 0)     {     new name[32];     get_user_name(id,name,31);     if (strlen(name) > 8)     {     new szIP[40];     get_user_ip ( id, szIP, charsmax(szIP) , 1 );         iResult = regex_match_c(name, hPattern, iReturnValue);         switch (iResult)     {     case REGEX_MATCH_FAIL:     {     log_amx("REGEX_MATCH_FAIL! %s", szError);     }     case REGEX_PATTERN_FAIL:     {     log_amx("REGEX_PATTERN_FAIL! %s", szError);     }     case REGEX_NO_MATCH:     {     }     default:{     log_amx("REGEX_MATCH! %s", name);     server_cmd("fb_ban 10 #%i ^"&#1088;еклама сервера! смени ник^"", userid)     server_cmd("addip 10 %s", szIP)     return PLUGIN_HANDLED;     }     }     }     }     return PLUGIN_CONTINUE;     }         public hook_say(id, level, cid)     {     if(!(get_user_flags(id) & ADMINLEVEL))     {     read_args(szAllArgs, charsmax(szAllArgs));         iResult = regex_match_c(szAllArgs, hPattern, iReturnValue);         switch (iResult)     {     case REGEX_MATCH_FAIL:     {     log_amx("REGEX_MATCH_FAIL! %s", szError);     return PLUGIN_CONTINUE;     }     case REGEX_PATTERN_FAIL:     {     log_amx("REGEX_PATTERN_FAIL! %s", szError);     return PLUGIN_CONTINUE;     }     case REGEX_NO_MATCH:     {     return PLUGIN_CONTINUE;     }     default:{     szMessage[0] = 0x04;     format(szMessage[1], 251, "[Blocked] %s", szAllArgs);     szMessage[192] = '^0';     client_print_color(id,print_team_red,"%s", szMessage)     return PLUGIN_HANDLED;     }     }     }     return PLUGIN_CONTINUE     }         public plugin_init()     {     register_plugin(PLUGINNAME, VERSION, AUTHOR);         register_clcmd("say", "hook_say");     register_clcmd("say_team", "hook_say");         register_clcmd("pings", "pings");     register_clcmd("say pings", "pings");     register_clcmd("say_team pings", "pings");     register_clcmd("say /pings", "pings");     register_clcmd("say_team /pings", "pings");         hPattern = regex_compile(PATTERN, iReturnValue, szError, 63, "is");         set_task(2.0,"check_players",_,_,_,"b")     }         public plugin_end() {     regex_free(hPattern);        }
__________________
DJEarthQuake is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:54.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode