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(tmp, sizeof(tmp)-1)
add(tmp, sizeof(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(fp, tmp, sizeof(tmp)-1)
strtok(tmp, tmp, sizeof(tmp)-1, tmp2, 1, ';')
trim(tmp)
if(strlen(tmp) <= 0)
continue
strbreak(tmp, tmp, sizeof(tmp)-1, tmp2, sizeof(tmp2)-1)
if(!ip_to_num(tmp, g_ip[g_ip_cnt])){
log_amx("allowed_ips.cfg: invalid ip on line %d (%s)", line, tmp)
continue
}
if(strlen(tmp2) <= 0){
log_amx("allowed_ips.cfg: missing mask on line %d", line, tmp2)
continue
}
if(!ip_to_num(tmp2, g_mask[g_ip_cnt])){
log_amx("allowed_ips.cfg: invalid mask on line %d (%s)", line, tmp2)
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_str, ip_num))
return false
for(new i=0; i<g_ip_cnt; i++){
if(ip_num&g_mask[i]==g_ip[i])
return true
}
return false
}
public ip_to_num(const ip[], &num){
new byte, dots, last_pos=-1
num = 0
for(new i=0; ip[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 < 0 || 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(id, ip, sizeof(ip)-1, 1)
if(is_ip_protected(ip))
return
new msg[128]
formatex(msg, sizeof(msg)-1, "Your IP (%s) is not allowed to connect to this server!", ip)
message_begin(MSG_ONE, SVC_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
__________________