PtokaX forum

Archive => Archived 4.0 boards => Finished Lua 4 scripts => Topic started by: bastya_elvtars on 04 October, 2004, 02:18:28

Title: range f***er by bastya_elvtars
Post by: bastya_elvtars on 04 October, 2004, 02:18:28
This is just another range banner. Thx to the folks i have mentioned!
-- range fucker v1.1 by bastya_elvtars (the rock n' roll doctor)
-- some code from easyranger by tezlo
-- checks if ips are valid and if ranges are really ranges :D
-- thx go out to Herodes for IP validation, Typhoon for paying attention, and plop for finding the bug i couldn't
-- also thx to NightLitch, i always wanna make same scripts as him but much bettter ones :D
-- enjoy

--v1.0:released
--v1.1: fixed some shit ;)

-- prefixes:!+#

cmd1="banrange"

cmd2="showranges"

cmd3="unbanrange"

cmd4="rangehelp"

Bot="---===Ranger===---"

opsips=1 -- set 1 to check if a connecting op belongs 2 a banned IP range

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
data=strsub(data,1,strlen(data)-1)
local _,_,cmd=strfind(data,"%b<>%s+[%!%+%#](%S+)")
if cmd==cmd1 and user.iProfile==0 then
checknban(user,data) return 1
elseif cmd==cmd2 and user.iProfile==0 then
user:SendPM(Bot,showipranges()) return 1
elseif cmd==cmd3 and user.iProfile==0 then
unbanrange(user,data) return 1
elseif cmd==cmd4 and user.iProfile==0 then
help(user) return 1
elseif cmd==cmd1 or cmd==cmd2 or cmd==cmd3 or cmd==cmd4 and user.iProfile~=0 then
user:SendData(Bot,"You dont have suficiebt rights to use this command.")
return 1
end
end
end

function checknban(user,data)
local _,_,ip1,ip2=strfind(data,"%b<>%s+%S+%s+(%d+%.%d+%.%d+%.%d+)%-(%d+%.%d+%.%d+%.%d+)")
if ip1 and ip2 then
if ipchk(ip1)~="shit" then
if ipchk(ip2)~="shit" then
if calculate(ip1) local ipranges=loadipranges()
if ipranges[ip1]~=ip2 then
ipranges[ip1]=ip2
save(ipranges)
user:SendData(Bot,"Range "..ip1.."-"..ip2.." banned.")
else
user:SendData(Bot,"Already banned range!")
end
ipranges=nil
collectgarbage()

flush()
else
user:SendData(Bot,"Invalid range! "..ip1.."-"..ip2)
end
else
user:SendData(Bot,"Invalid IP #2! Please check and retry! ;)")
end
else
user:SendData(Bot,"Invalid IP #1! Please check and retry! ;)")
end
else
user:SendData(Bot,"Bad usage! Usage: !"..cmd1.." xxx.xxx.xxx.xxx-yyy.yyy.yyy.yyy")
end
end

function calculate(ip)
local _,_,pt1,pt2,pt3,pt4=strfind(ip,"(%d+)%.(%d+)%.(%d+)%.(%d+)")
return (tonumber(pt1)*16777216+tonumber(pt2)*65536+tonumber(pt3)*256+tonumber(pt4))
end

function loadipranges()
local tbl={}
readfrom("banned_ranges.dat")
while 1 do
local line=read()
if line == nil then
readfrom()
break
else
local _,_,a,b=strfind(line,"([^|]+)|(.+)")
tbl[a]=b
end
end
readfrom()
return tbl
end

function save(tbl)
writeto("banned_ranges.dat")
for a,b in tbl do
write(a.."|"..b.."\n")
end
writeto()
end

function NewUserConnected(user)
local ipranges=loadipranges()
for a,b in ipranges do
if calculate(user.sIP)>=calculate(a) and calculate(user.sIP)<=calculate(b) then
user:SendData(Bot,"Your IP range ("..a.."-"..b..") is banned in here...")
user:Disconnect()
return 1
end
end
ipranges=nil
collectgarbage()
flush()
end
--
--function ipchk(ip)
-- gsub(ip,"(%d+)",function(ipp)
-- if tonumber(ipp)>255 then
-- return "shit"
-- end
-- end)
--end

function ipchk(ip)
local tIP = {}
_,_,tIP.a,tIP.b,tIP.c,tIP.d= strfind(ip, "(%d+)%.(%d+)%.(%d+)%.(%d+)")
for a,b in tIP do
if tonumber(b)> 255 then
return "shit"
end
end
tIP=nil
collectgarbage()
flush()
end

function unbanrange(user,data)
local _,_,ip1,ip2=strfind(data,"%b<>%s+%S+%s+(%d+%.%d+%.%d+%.%d+)%-(%d+%.%d+%.%d+%.%d+)")
if ip1 and ip2 then
local ipranges=loadipranges()
if ipranges[ip1]==ip2 then
user:SendData(Bot,"Range "..ip1.."-"..ip2.." unbanned.")
ipranges[ip1]=nil
save(ipranges)
else
user:SendData(Bot,"Range was not banned...")
end
else
user:SendData(Bot,"Bad usage! Usage: !"..cmd3.." xxx.xxx.xxx.xxx-yyy.yyy.yyy.yyy")
end
end

function showipranges()
local msg="All banned IP ranges:\r\n=============================\r\n"
local ipranges=loadipranges()
for a,b in ipranges do
msg=msg..a.." - "..b.."\r\n"
end
msg=msg.."=============================\r\n"
return msg
end


function Main()
frmHub:RegBot(Bot)
if opsips==1 then
OpConnected=NewUserConnected
end
end

function help(user)
local msg="Bot usage:\r\n\r\n"..
cmd1.." \t\tAdd an IP range to banlist.\r\n"..
cmd2.."\t\tShow a list of currently banned IP ranges.\r\n"..
cmd3.." \tUnbans an IP range.\r\n"..
cmd4.."\t\t\tThis help screen ;)\r\n"
user:SendPM(Bot,msg)
end
Title:
Post by: nErBoS on 04 October, 2004, 13:47:16
Hi,

I have made one similar a time earlier, that can also bans IP, here you have...

--## MANUALY IP-RANGE BAN BOT 1.2
--## Requested by belea
--## Will ban ip-ranges by a command (only for Masters)
--## Added to ban single IPs as also
--## Added the help command
--## Add a ban to IP on an online nick
--## Made by nErBoS
--## Commands:
--## !banipr -- It bans a single IP, a Nick (will ban the Nick IP, if is online) or an IP-Range (the IP-Range should be like this: 1.1.1.1-1.1.1.2)
--## !unbanipr -- It unbas a single IP or an IP-Range (the IP-Range should be like this: 1.1.1.1-1.1.1.2)
--## !bannedipr -- Shows All the IP-Range and single IP banned
--## !iprhelp -- Shows all this commands

sBot = "MIRB-Bot"

arrIpRange = {}
fIpRange = "iprange.dat"

--## Configuration ##--

uLaterPtokax = 0 -- Choose 0 if you are using Ptokax Version 0.3.3.0 or higher
-- Choose 1 if you are using Ptokax Version lower then 0.3.3.0

--## END ##--


function Main()
frmHub:RegBot(sBot)
LoadFromFile(fIpRange)
end

function OnExit()
SaveToFile(fIpRange , arrIpRange , "arrIpRange")
end

function NewUserConnected(user)
if (CheckIP(user.sIP) == 1) then
user:SendData(sBot, "Your IP is banned.")
user:Disconnect()
end
end

function DataArrival(user, data)
if (strsub(data,1,1) == "<" or strsub(data,1,5+strlen(sBot)) == "$To: "..sBot) then
data = strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data, "%b<>%s+(%S+)")
if (cmd == "!banipr" and user.iProfile == 0) then
local s,e,nick,ip1,ip2,ip3 = strfind(data, "%b<>%s+%S+%s+(%S+)")
if (nick == nil) then
user:SendPM(sBot, "Syntax Error, !banipr , you must write an IP, a Nick or an IP-Range (the IP-Range should be like this: 1.1.1.1-1.1.1.2).")
return 1
else
s,e,ip1,ip2 = strfind(data, "%b<>%s+%S+%s+(%d+%.%d+%.%d+%.%d+)%-(%d+%.%d+%.%d+%.%d+)")
s,e,ip3 = strfind(data, "%b<>%s+%S+%s+(%d+%.%d+%.%d+%.%d+)")
if (ip1 == nil or ip2 == nil) then
if (ip3 == nil) then
if (GetItemByName(nick) == nil) then
user:SendPM(sBot, "The user "..nick.." is not online.")
else
ip = GetItemByName(nick).sIP
end
else
ip1 = ip3
end
else
ip1 = ip1.."-"..ip2
end
end
if (arrIpRange[ip1] ~= nil) then
user:SendPM(sBot, "The IP-Range or IP "..ip1.." is already banned.")
else
arrIpRange[ip1] = user.sName
user:SendPM(sBot, "The IP-Range or IP "..ip1.." is now banned.")
if (uLaterPtokax == 1) then
OnExit()
end
end
return 1
elseif (cmd == "!unbanipr" and user.iProfile == 0) then
local s,e,ip1,ip2 = strfind(data, "%b<>%s+%S+%s+(%d+%.%d+%.%d+%.%d+)%-(%d+%.%d+%.%d+%.%d+)")
local s,e,ip3 = strfind(data, "%b<>%s+%S+%s+(%d+%.%d+%.%d+%.%d+)")
if (ip1 == nil or ip2 == nil) then
if (ip3 == nil) then
user:SendPM(sBot, "Syntax Error, !unbanipr , you must write an IP or an IP-Range (the IP-Range should be like this: 1.1.1.1-1.1.1.2).")
return 1
else
ip1 = ip3
end
else
ip1 = ip1.."-"..ip2
end
if (arrIpRange[ip1] == nil) then
user:SendPM(sBot, "The IP-Range or IP "..ip1.." is not banned.")
else
arrIpRange[ip1] = nil
user:SendPM(sBot, "The IP-Range or IP "..ip1.." is now unbanned.")
if (uLaterPtokax == 1) then
OnExit()
end
end
return 1
elseif (cmd == "!bannedipr" and user.iProfile == 0) then
local sTmp,ipr,who = "The IP-Range and IPs banned in the HUB:\r\n\r\n"
for ipr, who in arrIpRange do
sTmp = sTmp.."IP-Range or IP: "..ipr.." Banned by: "..who.."\r\n"
end
user:SendPM(sBot, sTmp)
return 1
elseif (cmd == "!iprhelp" and user.iProfile == 0) then
local sTmp = "\t\t --## IP/IP-Range Ban Commands ##--\r\n\r\n"
sTmp = sTmp.."!banipr \t-- It bans a single IP or an IP-Range (the IP-Range should be like this: 1.1.1.1-1.1.1.2)\r\n"
sTmp = sTmp.."!unbanipr \t-- It unbas a single IP or an IP-Range (the IP-Range should be like this: 1.1.1.1-1.1.1.2)\r\n"
sTmp = sTmp.."!bannedipr\t\t-- Shows All the IP-Range and single IP banned\r\n"
sTmp = sTmp.."!iprhelp\t\n-- Shows all this commands\r\n"
user:SendPM(sBot, sTmp)
return 1
end
end
end

function CheckIP(sIP)
local s,e,a,b,c,d,ipr,aux,ip1,ip2 = strfind(sIP, "(%d+)%.(%d+)%.(%d+)%.(%d+)")
sIP = tonumber(Zero(a)..Zero(b)..Zero(c)..Zero(d))
for ipr, aux in arrIpRange do
local s,e,a,b,c,d,x,w,y,z = strfind(ipr, "(%d+)%.(%d+)%.(%d+)%.(%d+)%-(%d+)%.(%d+)%.(%d+)%.(%d+)")
if (x == nil) then
local s,e,a,b,c,d = strfind(ipr, "(%d+)%.(%d+)%.(%d+)%.(%d+)")
ip1 = tonumber(Zero(a)..Zero(b)..Zero(c)..Zero(d))
if (ip1 == sIP) then
return 1
end
else
ip1 = tonumber(Zero(a)..Zero(b)..Zero(c)..Zero(d))
ip2 = tonumber(Zero(x)..Zero(w)..Zero(y)..Zero(z))
if (sIP >= ip1 and sIP <= ip2) then
return 1
end
end
end
return 0
end

function Zero(iNum)
iNum = tonumber(iNum)
if (iNum < 10) then
return "00"..iNum
elseif(iNum < 100) then
return "0"..iNum
else
return iNum
end
end

function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");

assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");

sTab = sTab or "";
sTmp = ""

sTmp = sTmp..sTab..sTableName.." = {\n"

for key, value in tTable do
local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);

if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end

sTmp = sTmp..",\n"
end

sTmp = sTmp..sTab.."}"
return sTmp
end

function SaveToFile(file , table , tablename)
writeto(file)
write(Serialize(table, tablename))
writeto()
end

function LoadFromFile(file)
if (readfrom(file) ~= nil) then
readfrom(file)
dostring(read("*all"))
readfrom()
end
end

Best regards, nErBoS
Title:
Post by: pHaTTy on 04 October, 2004, 15:34:50
hmm not a bad idea to add to range blaster, might add command for it, nice idea. :)
Title:
Post by: nErBoS on 04 October, 2004, 22:18:09
Hi,

I have added a help command to the script above.

Best regards, nErBoS
Title:
Post by: bastya_elvtars on 04 October, 2004, 22:47:41
QuoteOriginally posted by nErBoS
Hi,

I have added a help command to the script above.

Best regards, nErBoS

me2  :D  :D  :D

bastya vs nerbos nice :P
Title:
Post by: imby on 04 October, 2004, 22:58:00
can't one do it in ptokax manually, or is this more for ops remotely?
Title:
Post by: bastya_elvtars on 04 October, 2004, 23:55:12
QuoteOriginally posted by imby
can't one do it in ptokax manually, or is this more for ops remotely?

in my version only admins can ban/unban/see/get help

edit the banned_ranges.dat file like this:

IP1|IP2

btw if you wanna use it for other purposes such as mediaforce blocking, then what you need is outside of ptokax and lua... peerguardian, protowall, blockpost for outpost etc
Title:
Post by: nErBoS on 05 October, 2004, 00:54:42
Hi,

QuoteOriginally posted by bastya_elvtars
QuoteOriginally posted by nErBoS
Hi,

I have added a help command to the script above.

Best regards, nErBoS

me2  :D  :D  :D

bastya vs nerbos nice :P

Lol :D

Best regards, nErBoS
Title:
Post by: nErBoS on 05 October, 2004, 12:46:39
Hi,

Made a little upgrade on my script by adding in the ban command the posibilaty to ban an online nick (will ban his ip, but you can only write his nick)

Best regards, nErBoS
Title:
Post by: chill on 05 October, 2004, 13:28:15
I just don't get it why you guys don't use a binary search in your scripts,
It is much faster than a linear search, else the scripts look okey, only the search routine is slow.
Title:
Post by: bastya_elvtars on 05 October, 2004, 14:11:42
QuoteOriginally posted by chill
I just don't get it why you guys don't use a binary search in your scripts,
It is much faster than a linear search, else the scripts look okey, only the search routine is slow.

as im a lua n00b please extend what you mean chill ;)

then i will do it, if i can
Title:
Post by: chill on 05 October, 2004, 21:57:18
okey I'll try to explain.

for exsample you have a table like this one

table = {
[1] = 1,
[2] = 5,
[3] = 7,
[4] = 12,
[5] = 17,
[6] = 20,
[7] = 21,
[8] = 25,
["n"] = 8,
}

n is the numbers of entries.

this is table with increasing values so in a way sorted.

okey for exsample now you would like to know
if you have the number 5 in your table as value.

the first choice would be to run through the table
and check the values if one matches.

But if you have a big tables with like 1000 entries that takes quite a time, especially when the value you are
looking for is at the end of the table, so there is a different way to find out if ther number 5 is in your
table as value, without running through the table.
The search you use is called is a binary search and only works with sorted tables.
And a IP-Range table can be sorted.
So what you do in that search is, you first check the
entrie in the midle, that is n/2 = 4

the value of table[4] is 12, now you check if that
value is bigger, smaller or equal to the value you search.
If it is bigger as in our case 12 > than 5, you know that
it can only be in the first half of your table, right.
then you divide the first half in 2 that is  4/2 = 2,
so you check table[2] if it is bigger, lower or equal, and in our case it is equal. and there you have your entrie,
with only 2 searches.
And now you should see that binary search gets fatser compared to linear search when using bigger and bigger tables.

Hope it is in a way clear.

here a exsample

function tSearch(table,comIP)

local iStart,iEnd,iMid = 1,getn(table),0

while (iStart <= iEnd) do

iMid = floor((iStart+iEnd)/2)

if (table[iMid][1] <= comIP) and (table[iMid][2] >= comIP) then

return table[iMid]

elseif (table[iMid][1] > comIP) then

iEnd = iMid - 1

elseif (table[iMid][1] < comIP) then

iStart = iMid + 1

end

end

return("NOTFOUND")

end

Title:
Post by: bastya_elvtars on 05 October, 2004, 22:09:13
so i get an onion and take it apart ;)

thx chill 4 this info, i am gonna play around with this...

but cause i do not wanna use this as a "peerguardian" script,so its good the way it is.
Title:
Post by: chill on 05 October, 2004, 22:30:01
no prob,
but you are already faster when using more than 10 ranges, so it is in any case a good option I'd say.
But sure you know best what you use your script for,
but maybe someone will use it and put more ranges in it than you, so you would have to add a line saying,
made for < 10 ranges, with binary, made for > 10 ranges the
more the better ;). You'll see anyways.
Title:
Post by: nErBoS on 06 October, 2004, 12:31:01
Hi,

Yes chill you are complety right, shame on me  :)) . I will do that later on.

Best regards, nErBoS
Title:
Post by: chill on 06 October, 2004, 14:11:12
great, I'm happy that I could help :),
and I'm eager to see what you make out of it, laterz
Title:
Post by: hyeclass600 on 06 October, 2004, 22:20:35
which file carries the blocked ips i cant seem to find it can you please tell me
Title:
Post by: nErBoS on 07 October, 2004, 00:12:42
Hi,

Both script saves the file in script folder, mine is "iprange.dat" and bastya is "banned_ranges.dat".

Best regards, nErBoS
Title:
Post by: hyeclass on 14 November, 2004, 07:38:28
i banned the whole 172 range and there seem to be some friendly people who have been banned as well
can we have a feature like allowing valid ip addresses to enter even if the range is blocked would be really nice...
Title:
Post by: nErBoS on 15 November, 2004, 01:47:09
Hi,

Has your friend have a DINAMIC IP ??? If not, you have to ban and unban his IPs for ever and ever. Maybe is better to create a profile to be untouched by this script, what do you think ??

Chill...

About making the search on this script, i spent a time think on it, and can't be made so easly like that, because of the ranges.

Best regards, nErBoS
Title:
Post by: bastya_elvtars on 15 November, 2004, 03:34:03
QuoteOriginally posted by nErBoS
Chill...

About making the search on this script, i spent a time think on it, and can't be made so easly like that, because of the ranges.

This binary search is only for tables containing pure numeric values? Seems for me. (atleast the rutine u posted.)
Thus, we should make 2 tables 1st for start ip, second for end IP of that range. The only way would be if the script banned (put into table) the decimal values of each ips of each range, which is not a bad idea, and i will make one version. (oh, no, not, then it would be easier to ban those ips in ptokax :S)
Title:
Post by: nErBoS on 15 November, 2004, 14:25:02
Hi,

Is not so easy as you said bastya, imagine that you ban this range... (127.0.0.1-129.0.0.1) and i enter with the IP 128.0.0.1 with a binary search we would miss the range banned.

Best regards, nErBoS
Title:
Post by: hyeclass on 16 November, 2004, 06:30:13
well from the 172 range i only want to allow such as the  AOL range to be a valid range, could we have a lets a say a feature that will detect and allow to validate some users by their hostnames for ex. ipt.aol.com...

would be really great!


Best Regards,

hyeclass
Title:
Post by: nErBoS on 16 November, 2004, 13:01:20
Hi,

Why don't you ban all the range instead the AOL range, probably you will banned two ranges to not ban the AOL range (split the range). About making the search for ISP it would take some resources (not recommend).

Best regards, nErBoS
Title:
Post by: hyeclass on 17 November, 2004, 06:10:55
Nerbos, that is just an idea i mean not only aol but other isps I think if somebody makes such a cool feature it will be widely used in hubs i have been talking to allot of hubowners and they think this is a very great idea!

:)
Title:
Post by: bastya_elvtars on 17 November, 2004, 09:24:26
QuoteOriginally posted by hyeclass
Nerbos, that is just an idea i mean not only aol but other isps I think if somebody makes such a cool feature it will be widely used in hubs i have been talking to allot of hubowners and they think this is a very great idea!

:)

that would not be a range banner but a range allower then, am i right?