PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: Fangs404 on 17 April, 2005, 04:57:55

Title: IP Range Checker
Post by: Fangs404 on 17 April, 2005, 04:57:55
i just finished my first LUA script!  i needed a script that would allow only certain IP ranges because i only want my hub accessible only to students on campus.  i couldn't find anything that suited my needs, so i spent some time and wrote my own.  it's fully compatible with LUA 5 and the latest version of ptokax (0.3.3.0 build 16.09 at the time of this writing).  i hope some of you make use of this.  don't forget to change the sample ranges given in allowedRanges.  you may add or remove elements from allowedRanges as you see fit.

a side note: this can very easily be changed so that instead of it allowing the ranges in the table allowedRanges, it can block the given ranges.  if anyone has any questions or comments, please tell me.

--IP Range Checker v1.0
--by Fangs404
--last modified 4/16/05
--tested with PtokaX 0.3.3.0 build 16.09

allowedRanges = {[1]="1.2.0.0-1.2.255.255", [2]="69.1.0.0-69.1.255.255",
[3]="111.16.0.0-111.45.255.255", [4]="120.19.0.0-140.2.255.255"}

function NewUserConnected(curUser)
local allowed = checkIP(curUser.sIP)
if allowed==0 then
curUser:Disconnect()
end
end

function checkIP(IP)
local _,_,a,b,c,d = string.find(IP, "(%d*).(%d*).(%d*).(%d*)")
if (tonumber(a) and tonumber(b) and tonumber(c) and tonumber(d)) then
local userIP = computeIP(IP)
if userIP then
for index,allowedIP in allowedRanges do
local _,_,startRange,endRange = string.find(allowedIP, "(.*)-(.*)")
startRange = computeIP(startRange)
endRange = computeIP(endRange)
if userIP>=startRange and userIP<=endRange then
return 1
end
end
end
end
return 0
end

function computeIP(IP)
local _,_,a,b,c,d = string.find(IP, "(%d+).(%d+).(%d+).(%d+)")
return a*16777216 + b*65536 + c*256 + d
end
Title:
Post by: plop on 17 April, 2005, 21:44:29
you mailed me this script and i couldn't resist optimizing it.
but still, great job for a 1st script.
go on like this and i hope you learn some from the changes i done.
-----------------------------------------
-- IP Range Checker v1.0
-- by Fangs404
-- last modified 4/16/05
-- tested with PtokaX 0.3.3.0 build 16.09
-----------------------------------------
-- IP Range Checker v1.1
-- changed: allowedRanges table to be indexed by start of the range
-- instead of by number, the vallue is the end of the range.
-- added: precomputing allowedRanges on startup.
-- removed: the double string.find on the ip (checkIP and computeIP).
-- changed: the table loop from checkIP into the new lua 5 loop (pairs).
-- by plop.
-----------------------------------------

sBot = "ip_ranger"
allowedRanges = {
["128.62.0.0"] = "128.62.255.255",
["128.83.0.0"] = "128.83.255.255",
["129.116.0.0"] = "129.116.255.255",
["146.6.0.0"] = "146.6.255.255",
}

function NewUserConnected(user)
if checkIP(user.sIP) then
user:SendData(sBot, "Sorry your ip is not welcome here!")
user:Disconnect()
end
end

function checkIP(IP)
local userIP = computeIP(IP)
if userIP then
for startRange,endRange in pairs(computedRanges) do
if userIP >= startRange and userIP <= endRange then
return
end
end
end
return 1
end

function computeIP(IP)
local _,_,a,b,c,d = string.find(IP, "(%d+).(%d+).(%d+).(%d+)")
return a*16777216 + b*65536 + c*256 + d
end

function computeAllowedRanges()
local tTemp = {}
for a,b in pairs(allowedRanges) do
tTemp[(computeIP(a))] = computeIP(b)
end
return tTemp
end

computedRanges = computeAllowedRanges()

plop
Title:
Post by: Fangs404 on 17 April, 2005, 22:14:26
oh, that's beautiful.  thanks a lot.  :)
Title:
Post by: Fangs404 on 17 April, 2005, 23:05:35
i reworked it even a little more.  i changed some variable names.  i like your naming scheme.

-- IP Range Checker v1.1
-- by Fangs404 and plop
-- last modified 4/17/05
-- tested with PtokaX 0.3.3.0 build 17.03

sBot = frmHub:GetHubBotName()
tAllowedRanges = {["128.62.0.0"] = "128.62.255.255", ["128.83.0.0"] = "128.83.255.255",
 ["129.116.0.0"] = "129.116.255.255", ["146.6.0.0"] = "146.6.255.255"}

function NewUserConnected(user)
if checkIP(user.sIP) then
user:SendData(sBot, "We don't serve your IP here.")
user:Disconnect()
end
end

function checkIP(sIP)
local iIP = computeIP(sIP)
if iIP then
for startRange,endRange in pairs(tAllowedRanges) do
if iIP >= startRange and iIP <= endRange then
return
end
end
end
return 1
end

function computeIP(sIP)
local _,_,a,b,c,d = string.find(sIP, "(%d+).(%d+).(%d+).(%d+)")
return a*16777216 + b*65536 + c*256 + d
end

function computeAllowedRanges()
local tTemp = {}
for a,b in pairs(tAllowedRanges) do
tTemp[(computeIP(a))] = computeIP(b)
end
return tTemp
end

tAllowedRanges = computeAllowedRanges()
Title:
Post by: plop on 17 April, 2005, 23:19:16
QuoteOriginally posted by Fangs404
i reworked it even a little more.  i changed some variable names.  i like your naming scheme.
these are the unwriten rules of coding/scripting.
it's not a must but it makes life a lot easier, specialy if your working on a script which isn't made by yourself.
go on like this, you seem 2 be learning fast.

plop
Title:
Post by: Fangs404 on 17 April, 2005, 23:40:33
QuoteOriginally posted by plop
QuoteOriginally posted by Fangs404
i reworked it even a little more.  i changed some variable names.  i like your naming scheme.
these are the unwriten rules of coding/scripting.
it's not a must but it makes life a lot easier, specialy if your working on a script which isn't made by yourself.
go on like this, you seem 2 be learning fast.

plop

i have a lot of experience with java/c/c++, so this isn't too hard to all.  it's just new syntax and such.  and i've been meaning to change some of my bad coding habits (variable names is one) for a while, so i figure why not start now?  :D
Title:
Post by: TTB on 17 April, 2005, 23:42:13
another (new) lua coder  8)

Keep up your good work!!  :D
Title:
Post by: Fangs404 on 17 April, 2005, 23:45:21
QuoteOriginally posted by TTB
another (new) lua coder  8)

Keep up your good work!!  :D

and a new ptokax user.  i'm no longer bound by the unexpandable world of ynhub.  long live scripting!  thanks for the welcome.  i definitely plan to continue coding.  i'm a huge believer in open-source, so whatever i write, you guys will receive.  :D