PtokaX forum

Archive => Archived 4.0 boards => Finished Lua 4 scripts => Topic started by: bliskner on 27 May, 2004, 12:01:32

Title: iprangeblocker needed
Post by: bliskner on 27 May, 2004, 12:01:32
hi i need an ip range blocker for PtokaX however the only onesi can find all expect you to add the ranges to be blocked - as i am on a university network where we get charged for traffic off the network we want a hub thet only supports local users - however the hub cannont itself be on the network. so what i need is an ip range blocker that at base level blocks all ips - yet allows me to add specific ranges which are not blocked. can this be done? i really don't want to have to go thru putting in 256 ranges in to the perma ban list on PtokaX only to have someone delte the list if they accidentally (or not as the case may be) clear the ban list.

can anyone help?
Title:
Post by: Jaras on 27 May, 2004, 12:54:58
function Is2ndIPHigher( IP1, IP2 )
local _,_,dec1 = strfind( IP1, "^(%d+)%.?" )
local _,_,dec2 = strfind( IP2, "^(%d+)%.?" )
if not( strfind( IP1, ".", 1, 1 ) ) or not( strfind( IP2, ".", 1, 1 ) ) or ( dec1 ~= dec2 ) then
return tonumber( dec1 ) <= tonumber( dec2 )
else
IP1 = strsub( IP1, strlen( dec1 ) + 2 )
IP2 = strsub( IP2, strlen( dec2 ) + 2 )
return Is2ndIPHigher( IP1, IP2 )
end
end

this function returns true if IP1 <= IP2, else it returns false, so when you do:
IPRangeFrom = "66.66.66.66"
IPRangeTo = "77.77.77.77"

function IsIPInRange( curUser )
     return ( Is2ndIPHigher( IPRangeFrom, curUser.sIP ) ) and ( Is2ndIPHigher( curUser.sIP, IPRangeTo ) )
end

IsIPInRange will return true when curUser.sIP is between 66.66.66.66 and 77.77.77.77.

edit: thought that i don't have time, but had some and here - it SHOULD work:
BotName = "IPRangeBlocker"

Message = "***Sorry but your IP isn't allowed here..."

--//Note that IPRangeFrom and IPRangeTo must've same size, so first range is 11.11.11.11 to 15.15.15.15 and so on
IPRangeFrom = {
[1]="11.11.11.11",
[2]="22.22.22.22",
[3]="33.33.33.33",
}

IPRangeTo = {
[1]="15.15.15.15",
[2]="25.25.25.25",
[3]="35.35.35.35",
}


function Is2ndIPHigher( IP1, IP2 )
local _,_,dec1 = strfind( IP1, "^(%d+)%.?" )
local _,_,dec2 = strfind( IP2, "^(%d+)%.?" )
if not( strfind( IP1, ".", 1, 1 ) ) or not( strfind( IP2, ".", 1, 1 ) ) or ( dec1 ~= dec2 ) then
return tonumber( dec1 ) <= tonumber( dec2 )
else
IP1 = strsub( IP1, strlen( dec1 ) + 2 )
IP2 = strsub( IP2, strlen( dec2 ) + 2 )
return Is2ndIPHigher( IP1, IP2 )
end
end

function IsIPInRange( userIP )
local inRange = 1
for i,v in IPRangeFrom do
inRange = ( inRange ) and ( ( Is2ndIPHigher( v, userIP ) ) and ( Is2ndIPHigher( userIP, IPRangeTo[i] ) ) )
end
return inRange
end

function NewUserConnected( curUser )
if not IsIPInRange( curUser.sIP ) then
curUser:SendData( BotName, Message )
curUser:Disconnect()
end
end

function Main()
frmHub:RegBot( BotName )
end