-
IP range
Hi all,
I made an IP ban command, which saves the IP of the player in a file and when a player connects, his IP is checked against that list. However, for example, if your IP is 123.45.67.10, you can connect again if your IP is 123.45.67.11. Is it possible to make some kind of IP range that checks ur IP against 123.45.67.i with i from 0 till 150 for example?
EDIT:
Or can be a isSubStr(PlayerIP, bannedIP) used? No idea how to set that up though
-
I'd go with ip ranges entirely, and not just /8 /16 or /24, but anything in between.
Basically you have to convert an ip to a 32 bit integer, then compare the first /N bits to your ban. You can ban the player's entire ASN to ensure he wont be able to come back that way (ASNs might have multiple ranges, google is your friend here)
-
hi
u can split the ips with strtok and then compare it like this
PHP Code:
IsOk(testIp,ranges,index)
{
r = strtok(ranges,".");
t = strtok(testIp,".");
if(!isdefined(t[index-1])) return false;
if(int(t[index-1]) > int(r[index-1])) return false;
return true;
}
iprangeissmaller = IsOk("123.123.123.10","123.123.123.150",4);
-
Use it like this to ban ranges:
PHP Code:
init() //setup ip range bans
{
banned = [];
banned[banned.size] = "1.2.0.0/16"; //a /16 ban bans any ip that matches the first 16 bits of this address, so it effectively bans 65536 ips (1.2.0.0 - 1.2.255.255)
banned[banned.size] = "3.4.5.0/24"; //a /24 ban bans any ip that matches the first 24 bits of this address, so it effectively bans 256 ips (3.4.5.0 - 3.4.5.255)
banned[banned.size] = "6.7.8.9"; //an ip without range is the same as a /32 ban, matching the first 32 bits, which is the entire address. This only bans the ip in question (6.7.8.9)
level.banned_ranges = createbans(banned);
}
isbanned()
{
ip = self getip();
ip_int = parse_ip(ip);
for(i = 0; i < level.banned_ranges.size; i++)
{
if((ip_int>>(32 - level.banned_ranges[i].range)) == (level.banned_ranges[i].ip>>(32 - level.banned_ranges[i].range)) //bitshift ftw, compare the first N bits of the ip with the first N bits of the banned range
return true;
}
return false;
}
createbans(ip_ranges)
{
result = [];
for(i = 0; i < ip_ranges.size; i++)
{
split = strtok(ip_ranges[i], "/");
if(!split.size || split.size > 2)
return; //error occured parsing the ips
if(split.size == 1)
range = 32;
else
range = int(split[1]);
ip = parse_ip(split[0]);
result[result.size] = spawnstruct();
result[result.size - 1].ip = ip;
result[result.size - 1].range = range;
}
return result;
}
parse_ip(ip)
{
ip_parts = [];
part = 0;
ip_parts[part] = "";
prev_found = 0;
for(i = 0; i < ip.size; i++)
{
if(ip[i] != ".")
ip_parts[part] += ip[i];
else
{
part++;
ip_parts[part] = "";
}
}
multi = 1;
num = 0;
for(i = ip_parts.size - 1; i >= 0; i--)
{
num += int(ip_parts[i]) * multi;
multi *= 256;
}
return num;
}
-
How does this script know which IP's are banned? I save them in scriptdata/bans and then ip.cfg
-
Quote:
PHP Code:
init() //setup ip range bans
{
banned = [];
banned[banned.size] = "1.2.0.0/16"; //a /16 ban bans any ip that matches the first 16 bits of this address, so it effectively bans 65536 ips (1.2.0.0 - 1.2.255.255)
banned[banned.size] = "3.4.5.0/24"; //a /24 ban bans any ip that matches the first 24 bits of this address, so it effectively bans 256 ips (3.4.5.0 - 3.4.5.255)
banned[banned.size] = "6.7.8.9"; //an ip without range is the same as a /32 ban, matching the first 32 bits, which is the entire address. This only bans the ip in question (6.7.8.9)
level.banned_ranges = createbans(banned);
}
10characters
-
So i have to setup all bans manually? I cant use the ip's stored in a scriptdata folder?
-
Just read the damn scriptdata files INTO that array....
-
How can I do that? There are all seperated files with the name as IP.cfg
-
listdir() maybe?
Libcod functions...