PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: shipiz on 14 January, 2004, 21:00:54

Title: Ban IP writen in txt file.
Post by: shipiz on 14 January, 2004, 21:00:54
Hy!

Im looking for a small script wich bans users listed in txt file. I need this script so I can ban users in my hub from web page...

I have php vich adds to txt file a tex.
TXT looks like this:

127.0.0.1
81.81.81.81

And i need a script wich bans IP writen in txt file. After ban IP have to be deleted from txt file.

Please make this script for me.

/shipis (servaks)
Title:
Post by: plop on 14 January, 2004, 23:31:42
QuoteOriginally posted by shipiz
Hy!

Im looking for a small script wich bans users listed in txt file. I need this script so I can ban users in my hub from web page...

I have php vich adds to txt file a tex.
TXT looks like this:

127.0.0.1
81.81.81.81

And i need a script wich bans IP writen in txt file. After ban IP have to be deleted from txt file.

Please make this script for me.

/shipis (servaks)

IP's can't be banned by the scripts, only users.

plop
Title:
Post by: NightLitch on 15 January, 2004, 00:00:29
Well there is a way but really bad idea I think...

Create a table from the file with the stored ips...

Log every user to a table when logging in and then a check
to see if the ip number is in the list, IF then banned...

but user need to connect...

It's working like a ip-bot, in my eyes..
Title:
Post by: NightLitch on 15 January, 2004, 00:01:22
just though of it... why not creating a ip-table of it and the
file is like a DenyIP Ranges... that will work...
Title:
Post by: NightLitch on 15 January, 2004, 00:24:49
try this one, gonna fix it a little more tomorrow...

-- Ip-Ban-Bot-Tester --
-- By: NightLitch

BotName = "BanIP-Bot"

BanFile = "banned.txt"
BanIP = {}

sec = 1000
min = 60 * sec

function Main()
LoadIPs(BanIP,BanFile)
SetTimer( 5 * min)
StartTimer()
end

function OnTimer()
LoadIPs(BanIP,BanFile)
end

function NewUserConnected(curUser)
uIP = curUser.sIP
local _,_,a,b,c,d = strfind(uIP, "(%d*).(%d*).(%d*).(%d*)")
if a ~= nil then
if BanIP[uIP] then
curUser:SendData(BotName, " Your IP is Banned on this Hub !!")
curUser:Ban()
return 1
end
end
end

function LoadIPs(table,file)
local handle = openfile(file, "r")
if (handle) then
    local line = read(handle)
    while line do
s,e,ips = strfind( line, "(.*)")
table[ips] = 1
line = read(handle)
    end
  closefile(handle)
end
end
Title:
Post by: plop on 15 January, 2004, 00:33:21
good start NightLitch.

plop
Title:
Post by: shipiz on 15 January, 2004, 08:34:50
Nice...

But I need to see a template of "banned.txt".

/shipis (servaks)
Title:
Post by: NightLitch on 15 January, 2004, 12:32:56
banned.txt are supposed to be replaced with your file where you have your users... I asume you are viewing
from there to Template ?? or...

Anyway gonna try finish it now... But one thing...

The Timer... it is updating the table every 5min now...

Depending how big hub you have now... it can perhaps
get laggy... but I have plop or anyone else answer that..

But my instinct tells me it will do that if many users...

you now may not have the "template" file where you store
those ips in the same folder as PtokaX so here is what you should do:

BanFile = "c:/wwwserver/template/file.ext"

You will hear from me soon...

And ThX for the credit Plop.
Title:
Post by: NightLitch on 15 January, 2004, 13:14:46
ok here it is. This Should do exact what you want.
Only Put the right location in BanFile and that it...
Simple like it should be... :-)

----------------------------------------
-- BanIP-Bot
-- By: NightLitch 2004
-- Remember the BanFile so the location is right...
----------------------------------------

-- Set Botname
BotName = "Hub-Security"
-- Set Filename ex. if it's another driver and folder like this:
-- Ex: ( c:/Server/www/templates/banned.tmp )
BanFile = "banned.txt"
-- Minutes between File/Table loads
Min = 5

----------------------------------------
BanIP = {}

sec = 1000
min = 60 * sec
---------------[ Main ]-------------------------------------
function Main()
LOADiP(BanIP,BanFile)
SetTimer( Min * min)
StartTimer()
end
---------------[ On Timer ]------------------------------
function OnTimer()
LOADiP(BanIP,BanFile)
end
---------------[ New User Connected ]-----------
function NewUserConnected(curUser)
uIP = curUser.sIP
local _,_,a,b,c,d = strfind(uIP, "(%d*).(%d*).(%d*).(%d*)")
if a ~= nil then
if BanIP[uIP] then
curUser:SendData(BotName, " Your ASS is banned!")
DELiP(BanIP,BanFile,uIP)
curUser:Ban()
return 1
end
end
end
-----------------[ Load IP from file ]-----------------
function LOADiP(table,file)
local handle = openfile(file, "r")
if (handle) then
    local line = read(handle)
    while line do
s,e,ips = strfind( line, "(.*)")
table[ips] = 1
line = read(handle)
    end
  closefile(handle)
end
end
---------------[ Delete IP from File ]----------------
function DELiP(table,file,ip)
if table[ip] then
table[ip]=nil
local handle = openfile(file, "w")
for i,v in table do
write(handle,i.."\r\n")
end
closefile(handle)
else
end
end
-- By: NightLitch

You shouldn't have any problem with this...
User is banned thru txt-file and when trying to connect user
get's ban message and ip removed from txt file and then Banned in HubSoft instead...
Title:
Post by: plop on 15 January, 2004, 13:17:59
yw boy.

i suggest you start using 2 files.
1 from the webserver with the new ip's, just check on the timer if it excists.
if yes you push them all into the 2de file, when done you delete the 1st file.
results that the timer won't do anything if the 1st file is not there.
the 2de file is only loaded on bot startup and saved if a user enters with a 2 be banned ip.
this should lower the resources 2 the bare minumum.

plop
Title:
Post by: NightLitch on 15 January, 2004, 13:21:25
k, gonna try that... np..
Title:
Post by: NightLitch on 15 January, 2004, 13:47:42
But the file do exist there... otherwise it will not load or script will create file but be emty...

cause tried you hint but get to advanced for me for the moment... Am newly awake dude... :-D

but can try later...

did a fix until then...
----------------------------------------
-- BanIP-Bot
-- By: NightLitch 2004
-- Remember the BanFile so the location is right...
-- Fixed if file not exist file created
----------------------------------------

-- Set Botname
BotName = "Hub-Security"
-- Set Filename ex. if it's another driver and folder like this:
-- Ex: ( c:/Server/www/templates/banned.tmp )
BanFile = "banned.txt"
-- Minutes between File/Table loads
Min = 5

----------------------------------------
BanIP = {}
Temp = {}

sec = 1000
min = 60 * sec
---------------[ Main ]-------------------------------------
function Main()
LOADiP(BanIP,BanFile)
SetTimer( Min * min)
StartTimer()
end
---------------[ On Timer ]------------------------------
function OnTimer()
LOADiP(BanIP,BanFile)
end
---------------[ New User Connected ]-----------
function NewUserConnected(curUser)
uIP = curUser.sIP
local _,_,a,b,c,d = strfind(uIP, "(%d*).(%d*).(%d*).(%d*)")
if a ~= nil then
if BanIP[uIP] then
curUser:SendData(BotName, " Your ASS is banned!")
DELiP(BanIP,BanFile,uIP)
curUser:Ban()
return 1
end
end
end
-----------------[ Load IP from file ]-----------------
function LOADiP(table,file)
local handle = openfile(file, "r")
if (handle) then
    local line = read(handle)
    while line do
s,e,ips = strfind( line, "(.*)")
table[ips] = 1
line = read(handle)
    end
  closefile(handle)
elseif (handle) == nil then
local f = openfile(file, "w")
write(f, "")
closefile(f)
end
end
---------------[ Delete IP from File ]----------------
function DELiP(table,file,ip)
if table[ip] then
table[ip]=nil
local handle = openfile(file, "w")
for i,v in table do
write(handle,i.."\r\n")
end
closefile(handle)
else
end
end
-- By: NightLitch

this is simpler I think... but your idea is maybe less rescource taking Plop.
Title:
Post by: NightLitch on 16 January, 2004, 12:53:13
well shipiz.. have you tried it... wanna know if I need to rewrite something...

From my point of view it looks 100% ok and finish...
exept for maybe a small mods that I got from plop.