Raised This Month: $ Target: $400
 0% 

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


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 



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 14:22.


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