PtokaX forum

Development Section => HOW-TO's => Topic started by: Mogli on 21 June, 2005, 00:51:43

Title: IP-Ranges
Post by: Mogli on 21 June, 2005, 00:51:43
The search for the UserIP, is mostly done by normal search
e.g. for i,v in table do .... , well that is a very slow method,
when you have tables, bigger, than 20 entries or so.

So I made a script to prove it.
it uses the GeoIP database from Maxmind, cause that list
has a lot of ranges, it will load the ranges into a TSDB
Table String DB to save memory, the lookup is still very fast,
but only half of the memory is needed.

these are my stats

--Added: 74161 Ranges in 126.1560 seconds, average = 0.0017 secs.
--DB Saved in 1.1560 seconds.
--DB Loaded in 0.5930 seconds.
--Binary Checked 1000 IP's in 4.4220 seconds, average 0.0044
--Normaly Checked 3 IP's in 3.0310 seconds, average 1.0103
-- Normal is 100 times slower, proof here, who uses it ( I think various IP scripts, hehe ), is really slow


This is just a script to play around, haven't got
any real use for it till now

GeoIP (http://lua.uknnet.com/attachment.php?attachmentid=119&sid=)
Title:
Post by: NightLitch on 25 June, 2005, 02:46:52
impressive, going to check the code some more, there where not much new that is strange for me in my own code, but some things you have done is intressting.

I have started to look more into the CIDR ranges and make them work better and effective.

Good work // NL
Title:
Post by: Mogli on 27 June, 2005, 08:26:50
Hi NightLitch,
thx, well the code is the same as the GeoIP from
VidFamne it just loads all ranges into memory,
thats why it is faster :).
What are CIDR ranges actually?

So I did some more testing :),
But his time, with the time needed
to handle one NewUserConnected()
cause that is what counts, how fast can your script
handle the NewUserConnect(), and then let the hub
work again, I think the results are quite cool.

-- Connect Handlingtime of a user
-- This GeoIP-0.3: Tested 100 IP's in 0.109999 secs. avg: 0.001099  , Found 52 IP's of 100
-- GeoIP: Tested 100 IP's in 12.32800 secs. avg: 0.1232800

Seems like this bot is 100 times faster than the other,
but I would like to have your results too,  so here is the testing code,

for GeoIP-0.2 add this into  Main()  but at the end

-- create a user
local user = {
sName = "Tester",
sIP = "",
}
function user:SendData()
end
function user:Disconnect()
end
local ifound = 0
local inum = 100
t1 = os.clock()
for _ = 1, inum do
local sIP = math.random(1,255).."."..math.random(1,255).."."..math.random(1,255).."."..math.random(1,255)
user.sIP = sIP
if GeoIP:NewUserConnected( user ) then
ifound = ifound + 1
end
end
local diff = os.clock() - t1
SendToAll( "GeoIP-0.3: Tested "..inum.." IP's in "..diff.." secs. avg: "..(diff/inum) )
--SendToAll( "Found "..ifound.." IP's of "..inum )


and for the GeoIP from Vidfamne add this into Main()

-- create a user
local user = {
sName = "Tester",
sIP = "",
}
function user:SendData()
end
function user:Disconnect()
end
local inum = 100
t1 = os.clock()
for _ = 1, inum do
local sIP = math.random(1,255).."."..math.random(1,255).."."..math.random(1,255).."."..math.random(1,255)
user.sIP = sIP
f.Newcon(user)
end
local diff = os.clock() - t1
SendToAll( "GeoIP: Tested "..inum.." IP's in "..diff.." secs. avg: "..(diff/inum) )

also change this code in the fFunc.lua file

tSearch = function (num)
  local tGeoIpRangefile = ""
  local num = tonumber(num)
  local tTables = { [1] = { [1] = "Files/GeoIPtables/ct01.lua", [2] = 1079422463 },
                [2] = { [1] = "Files/GeoIPtables/ct02.lua", [2] = 1347067903 },
                [3] = { [1] = "Files/GeoIPtables/ct03.lua", [2] = 2810380287 },
                [4] = { [1] = "Files/GeoIPtables/ct04.lua", [2] = 3255486719 },
                [5] = { [1] = "Files/GeoIPtables/ct05.lua", [2] = 3274963783 },
                [6] = { [1] = "Files/GeoIPtables/ct06.lua", [2] = 3285852159 },
                [7] = { [1] = "Files/GeoIPtables/ct07.lua", [2] = 3453066615 },
                [8] = { [1] = "Files/GeoIPtables/ct08.lua", [2] = 3557916671 },
                [9] = { [1] = "Files/GeoIPtables/ct09.lua", [2] = 3582571979 },
                [10] = { [1] = "Files/GeoIPtables/ct10.lua", [2] = 3741319167 }
      }
        for i = 1, 10 do
            if  num <= tTables[i][2] then
            tGeoIpRangefile = tTables[i][1]
return tGeoIpRangefile
            end
        end
end,

tSearchCountry = function(num)
tGeoIpRange = { n = 0 }
    local tGeoIpRangefile = f.tSearch(num)
if tGeoIpRangefile then
dofile(tGeoIpRangefile)
end
    local C = ""
    local CC = ""
    local low=1
    local mid = 0

    local high=tGeoIpRange.n

    while low <= high  do
        mid = math.floor((low+high)/2)
            if  tGeoIpRange[mid][1] <= num and num <= tGeoIpRange[mid][2]  then
                C = tGeoIpRange[mid][4]
                CC = tGeoIpRange[mid][3]
            break end
                if tGeoIpRange[mid][1] > num then
                high = mid-1
                else low = mid+1
                end
    end
    tGeoIpRange = nil
    collectgarbage()
    return C,CC
end,

Newcon = function(User)
    local compIp = f.ComputeIP(User.sIP)
    local C, CC = f.tSearchCountry(compIp)
        if (CC and CountryCode[CC])  or CountryCode["ALL"] or User.bOperator then else
            --User:SendData("Sorry but this hub only allow users from "..f.tCC(CountryCode))
            User:Disconnect()
        end;
--    User:SendData(" Hello "..User.sName.."  from "..C.." Welcome to "..frmHub:GetHubName()..
--    ". \r\nSending you this hubs normal user-command. Right-click on hub tab or userlist.|"..
--    "$UserCommand 0 2|$UserCommand 2 2 Get User's Country$<%[mynick]> +Country %[nick]||"..
--    "$UserCommand 1 1 Country by IP$<%[mynick]> +Country %[line:IP?]||"..
--    "$UserCommand 1 1 My IP$<%[mynick]> +myip||")
end,