IP Range Checker
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

IP Range Checker

Started by Fangs404, 17 April, 2005, 04:57:55

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Fangs404

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

plop

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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Fangs404

#2
oh, that's beautiful.  thanks a lot.  :)

Fangs404

#3
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()

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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Fangs404

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

TTB

another (new) lua coder  8)

Keep up your good work!!  :D
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

Fangs404

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

SMF spam blocked by CleanTalk