PtokaX forum

Archive => Archived 4.0 boards => Finished Lua 4 scripts => Topic started by: TheSig on 15 December, 2003, 14:14:11

Title: IP Bot
Post by: TheSig on 15 December, 2003, 14:14:11
Realy Worked script.
file "data/local.net" contains allowed networks data like this (one record per line):
  10.10.10.10/24
-- ********************************************
-- * IPBot (allowed IP checking script) v0.1  *
-- * (c) 2003 by TheSig   *
-- ********************************************
Bot = "IPBot"
IPList = "data/local.net"
oAllow={}
oMasks={}
sbns=0

function LongToBin(L)
  if (L == 0) then
    b = "0"
  else
    b = ""
    tl = L
    for i=1,32 do
      r0 = mod(tl,2)
      if (r0 == 0) then
        b = "0"..b
        tl = tl / 2
      else
        b = "1"..b
        tl = (tl - r0) / 2
      end
    end
  end
  return b
end

function GetHex(L)
  if tonumber(L,10) <= 15 then
    a = "0"..format("%x",L)
  else
    a = format("%x",L)
  end
  return a  
end

function GetIPLong(IP)
  r,g,a,b,c,d = strfind(IP, "(%d*).(%d*).(%d*).(%d*)")
  a1= GetHex(a)
  b1= GetHex(b)
  c1= GetHex(c)
  d1= GetHex(d)
  s = a1..b1..c1..d1
  e = tonumber(s, 16)
  return e
end

function GetIPSubnet(IP)
  r,g,a,b,c,d,m = strfind(IP, "(%d*).(%d*).(%d*).(%d*)/(%d*)")
  ipl = GetIPLong(a.."."..b.."."..c.."."..d)
  m = tonumber(m,10)
  return ipl,m
end

function LoadSubnets()
  sbns=0
  readfrom(IPList)
  repeat
    s = read("*l")
    if (s) then
      if (strsub(s,1,1) ~= "#") then
        sbns=sbns+1
        oAllow[sbns],oMasks[sbns]=GetIPSubnet(s)
      end
    end
  until (s == nil)
  readfrom()
end

function TestInSubnet(UserIP,SubnetIP,MaskLen)
  UIP = strsub(LongToBin(UserIP),1,MaskLen)
  SIP = strsub(LongToBin(SubnetIP),1,MaskLen)
  if (UIP == SIP) then
    sny = 1
  else
    sny = 0
  end
  return sny
end

function TestIP(TestIP)
  allowip = 0
  for i=1,sbns do
    if (TestInSubnet(GetIPLong(TestIP),oAllow[i],oMasks[i]) == 1) then
      allowip = 1
    end
  end
  return allowip
end

function Main()
  frmHub:RegBot(Bot)
  LoadSubnets()
end

function NewUserConnected(curUser)
  theIP=curUser.sIP
  if (TestIP(theIP) ~= 1) then
    curUser:SendPM(Bot,"Your IP isn't from [LV]zone .... If it is not correct please contact admin thesig@fastpage.org")
    curUser:SendData("$ForceMove signest.fastpage.org:4112|")
  end
end

Title:
Post by: c h i l l a on 15 December, 2003, 14:47:24
thats a cool script.. but I think the Compute Fuktion is  faster than yours, when it comes to many connection at a time.
Title:
Post by: IceCoder on 15 December, 2003, 16:28:36
Good i guess