Raised This Month: $12 Target: $400
 3% 

Block All IP's ( only Allow one range IP )


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
mohsen9010
Senior Member
Join Date: Jun 2011
Location: Iran
Old 08-10-2011 , 15:19   Block All IP's ( only Allow one range IP )
Reply With Quote #1

Hi
i don't no how can i ban or block all IPs & only allow one range ip to join my server ?
for ex. i want allow this range ip only 192.168.0.0-192.168.255.255
& only this ip can join & see my server
how can i do that ?

Last edited by mohsen9010; 08-14-2011 at 01:56.
mohsen9010 is offline
Send a message via Yahoo to mohsen9010 Send a message via Skype™ to mohsen9010
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-10-2011 , 15:24   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #2

sv_lan 1?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
mohsen9010
Senior Member
Join Date: Jun 2011
Location: Iran
Old 08-10-2011 , 15:40   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #3

Quote:
Originally Posted by Exolent[jNr] View Post
sv_lan 1?
no its not my means
192.168.0.0 only an example IP
i want only allow one range IP too see & join my server

Last edited by mohsen9010; 08-14-2011 at 01:58.
mohsen9010 is offline
Send a message via Yahoo to mohsen9010 Send a message via Skype™ to mohsen9010
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-10-2011 , 16:10   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #4

PHP Code:
#include <amxmodx>

new pcvarRange;

public 
plugin_init() {
    
register_plugin("Only IP Range""0.0.1""Exolent");
    
    
pcvarRange register_cvar("player_ip_range""*.*.*.*");
}

public 
client_putinserver(id) {
    new 
ip[32];
    
get_user_ip(idipcharsmax(ip), 1);
    
    new 
range[32];
    
get_pcvar_string(pcvarRangerangecharsmax(range));
    
    new 
ipPieces[4][4];
    
str_explode(ip'.'ipPiecessizeof(ipPieces), charsmax(ipPieces[]));
    
    new 
rangePieces[4][8];
    
str_explode(range'.'rangePiecessizeof(rangePieces), charsmax(rangePieces[]));
    
    new 
minMaxRange[2][4];
    
    for(new 
04i++) {
        if(
equal(rangePieces[i], "*")) {
            continue;
        }
        
        if(
str_explode(rangePieces[i], '-'minMaxRangesizeof(minMaxRange), charsmax(minMaxRange[])) == 2) {
            if(
str_to_num(minMaxRange[0]) <= str_to_num(ipPieces[i]) <= str_to_num(minMaxRange[1])) {
                continue;
            }
        }
        else if(
equal(rangePieces[i], ipPieces[i])) {
            continue;
        }
        
        
message_begin(MSG_ONE_UNRELIABLESVC_DISCONNECT_id);
        
write_string("Kicked: IP not in allowed range for this server.");
        
message_end();
        
        break;
    }
}

stock str_explode(const string[], delimiteroutput[][], output_sizeoutput_len)
{
    new 
iposlen strlen(string);
    
    do
    {
        
pos += (copyc(output[i], output_lenstring[pos], delimiter) + 1);
    }
    while(++
output_size && pos len);
    
    return 
i;

Cvar:
player_ip_range mask.mask.mask.mask (Default: *.*.*.*)
- Each "mask" can have different types of content:
- 1. A number set as a mask forces that value to be equal
- 2. A range (X-Y) set as a mask forces that value to be in the given range
- 3. A wildcard (*) allows any value

Example cvar value for your first post's example:
player_ip_range "192.168.0-255.0-255"
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 08-10-2011 at 21:57.
Exolent[jNr] is offline
mohsen9010
Senior Member
Join Date: Jun 2011
Location: Iran
Old 08-10-2011 , 17:52   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #5

sorry i test it but not work for me
mohsen9010 is offline
Send a message via Yahoo to mohsen9010 Send a message via Skype™ to mohsen9010
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-10-2011 , 18:19   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #6

Quote:
Originally Posted by mohsen9010 View Post
sorry i test it but not work for me
What is your IP and what is the setting for the cvar?
__________________
fysiks is offline
mohsen9010
Senior Member
Join Date: Jun 2011
Location: Iran
Old 08-10-2011 , 18:27   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #7

Quote:
Originally Posted by fysiks View Post
What is your IP and what is the setting for the cvar?
i set in cvar player_ip_range "19.16.55.5"
so another IPs must be kick
my IP address is 2.176.37.53 but I'm not kick
mohsen9010 is offline
Send a message via Yahoo to mohsen9010 Send a message via Skype™ to mohsen9010
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-10-2011 , 18:49   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #8

Quote:
Originally Posted by mohsen9010 View Post
i set in cvar player_ip_range "19.16.55.5"
so another IPs must be kick
my IP address is 2.176.37.53 but I'm not kick
Is the server hosted on your machine (LAN/Dedicated) or is it on a different machine (eg. hosted by game server company)?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 08-10-2011 , 19:35   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #9

There was request in scripting help related to ip ranges: http://forums.alliedmods.net/showthread.php?t=141741
I made plugin with configuration file instead of cvar using the same method I posted in that thread.
PHP Code:
#include <amxmodx>
#include <amxmisc>

#define MAX_IPS 100
new g_ip[MAX_IPS]
new 
g_mask[MAX_IPS]
new 
g_ip_cnt


public plugin_init(){
    
register_plugin("Allowed IPs""1.0""Sylwester")
    new 
tmp[128], tmp2[20]
    
get_configsdir(tmpsizeof(tmp)-1)
    
add(tmpsizeof(tmp)-1"/allowed_ips.cfg")
    new 
fp fopen(tmp"rt")
    if(!
fp){
        
log_amx("Could not open configuration file (%s), allowing connection from all IPs"tmp)
        return
    }
    new 
line
    
while(!feof(fp)){
        if(
g_ip_cnt >= MAX_IPS)
            break
        
line++
        
fgets(fptmpsizeof(tmp)-1)
        
strtok(tmptmpsizeof(tmp)-1tmp21';')
        
trim(tmp)
        if(
strlen(tmp) <= 0)
            continue
        
strbreak(tmptmpsizeof(tmp)-1tmp2sizeof(tmp2)-1)
        if(!
ip_to_num(tmpg_ip[g_ip_cnt])){
            
log_amx("allowed_ips.cfg: invalid ip on line %d (%s)"linetmp)
            continue
        }
        if(
strlen(tmp2) <= 0){
            
log_amx("allowed_ips.cfg: missing mask on line %d"linetmp2)
            continue
        }
        if(!
ip_to_num(tmp2g_mask[g_ip_cnt])){
            
log_amx("allowed_ips.cfg: invalid mask on line %d (%s)"linetmp2)
            continue
        }
        
g_ip[g_ip_cnt] &= g_mask[g_ip_cnt]
        
g_ip_cnt++
    }
    
fclose(fp)
    if(
g_ip_cnt<=0){
        
log_amx("Configuration file contains no valid entries, allowing connection from all IPs")
    }
}


public 
bool:is_ip_protected(const ip_str[]){
    new 
ip_num
    
if(!ip_to_num(ip_strip_num))
        return 
false
    
for(new i=0i<g_ip_cnti++){
        if(
ip_num&g_mask[i]==g_ip[i])
            return 
true 
    
}
    return 
false
}


public 
ip_to_num(const ip[], &num){
    new 
bytedotslast_pos=-1
    num 
0
    
for(new i=0ip[i]!='^0'i++){
        if(
ip[i]=='.'){
            
dots++
            if(
last_pos==i-1)
                return 
0
            last_pos 
i
            num 
= (num<<8)+byte
            byte
=0
            
continue
        }else if(
ip[i] < '0' || ip[i] > '9')
            return 
0
        byte 
byte*10+ip[i]-'0'
        
if(byte || byte 255)
            return 
0
    
}
    if(
dots != 3)
        return 
0
    num 
= (num<<8)+byte
    
return 1
}  


public 
client_connect(id){
    if(
g_ip_cnt <= 0){
        return
    }
    new 
ip[16]
    
get_user_ip(idipsizeof(ip)-11)
    if(
is_ip_protected(ip))    
        return
    new 
msg[128]
    
formatex(msgsizeof(msg)-1"Your IP (%s) is not allowed to connect to this server!"ip)
    
message_begin(MSG_ONESVC_DISCONNECT, {0,0,0}, id)
    
write_string(msg)
    
message_end()

Configuration file (addons\amxmodx\configs\allowed_ips.cfg):
Code:
; Allowed IPs configuration file
; every line must contain: ip mask
; every bit in mask set to 1 means that corrsponding bit from loaded ip must match with ip of connecting client

; examples:
; 192.168.1.2 255.255.255.255 ; this allows connections only from ip 192.168.1.2
; 192.168.1.0 255.255.255.0 ; this allows connections from ip range 192.168.1.0 - 192.168.1.255
; 192.0.1.11 255.0.255.255 ; this allows connections from ip range 192.0.1.11 - 192.255.1.11
; 192.168.1.2 255.255.255.254 ; this allows connections from ips 192.168.1.2 and 192.168.1.3

19.16.55.5 255.255.255.255
__________________
Impossible is Nothing

Last edited by Sylwester; 08-11-2011 at 01:48. Reason: fixed log_amx informing about lack of valid entries in config file
Sylwester is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-10-2011 , 20:04   Re: Ban All IPs ( only Allow one range IP )
Reply With Quote #10

@Exolent, str_explode() is only returning 1 when it finds two strings to explode with. I fixed it like this:

Code:
stock str_explode(const string[], delimiter, output[][], output_size, output_len) {     new i, pos, len = strlen(string);     do     {         pos += (copyc(output[i], output_len, string[pos], delimiter) + 1);         i++     }     while(pos < len && i < output_size);     return i; }
__________________
fysiks 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 21:04.


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