1. Post #1
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    [RELEASE]
    libGeoIP Lua binding - Get info about IPs!


    Download
    Binary module: http://88.191.111.120/~srcds/modules/gm13/bin/

    Uses the GeoIP database GeoLiteCity.dat
    Uncompess, rename to GeoIP.dat and put to garrysmod/data/GeoIP.dat or orangebox/GeoIP.dat

    Possible (additional / commercial) database files:
    Code:
    (garrysmod/data/)GeoIP.dat - Accepts GeoIP.dat / GeoLiteCity.dat and anything which gives out city data
    (garrysmod/data/)GeoIPCity.dat - accepts same as GeoIP.dat, appears as underscores on table
    (garrysmod/data/)GeoIPASNum.dat - free internet routing related data
    
    (garrysmod/data/)GeoIPISP.dat -- non free isp
    (garrysmod/data/)GeoIPOrg.dat -- non free organization / sip
    (garrysmod/data/)GeoIPNetSpeed.dat -- non free netspeed
    Example
    lua_run require'geoip' PrintTable( GeoIP.Get"8.8.8.8" )
    > PrintTable(GeoIP.Get("8.8.8.8"))...
    {
    	   -- GeoIP/GeoLite(City) --
           country_name = "United States",
           city = "Mountain View",
           country_code = "US",
           longitude = -122.05740356445,
           latitude = 37.419200897217,
           region = "CA",
           netmask = 24,
           postal_code = "94043",
           
    	   -- GeoIPOrg
           org = "Google Incorporated",
    	   
    	   -- GeoIPASNum
           asn = "AS15169 Google Inc.",
    
    	   -- GeoIPISP
           ISP = "Level 3 Communications",
    	   
    	   -- GeoIPNetSpeed (number from 0 to 3, 0 is unknown and 1 is dialup)
           speed = 0,
    }
    
    
    
    [/RELEASE]Changelog
    - Removed confidence
    - Updated to latest GeoIP
    - Added more database loaders
    - Disabled multiloading errors

    Source Code
    http://88.191.111.120/~srcds/modules...gmsv_geoip.cpp
    To compile you need the API library from MaxMind and link it statically (or dynamically if you want to).
    Reply With Quote Edit / Delete Reply Finland Show Events Useful x 7Lua King x 3Dumb x 2Winner x 1Late x 1 (list)

  2. Post #2
    kna_rus's Avatar
    September 2008
    121 Posts
    Linux version here:
    Binary: http://dl.dropbox.com/u/3679614/gm_geoloc_linux.dll
    Sources:
    gm_geoloc.cpp
    Code:
    #include "GMLuaModule.h"
    #ifdef _LINUX
    #include "GeoIP.h"
    #include "GeoIPCity.h"
    #else
    #include "libgeoip/GeoIP.h"
    #include "libgeoip/GeoIPCity.h"
    #endif
    namespace IPLoc {
        GeoIP * giCity;
        GeoIP * giISP;
    
        GMOD_MODULE(Init, Shutdown);
    
        LUA_FUNCTION(GetIPInfo)
        {
    	Lua()->CheckType(1, GLua::TYPE_STRING);
    	const char *FindMe = Lua()->GetString(1);
    
    	GeoIPRecord *gir;
    
    	ILuaObject *tbl = Lua()->GetNewTable();
    	if (giCity!=NULL) {
    
    	    gir = GeoIP_record_by_addr(giCity, FindMe);
    	    if (gir != NULL) {
    		tbl->SetMember("City",gir->city);
    		tbl->SetMember("Country",gir->country_name);
    		tbl->SetMember("Region",gir->region);
    
    		#ifndef _LINUX
    		tbl->SetMember("CityCode",(float)gir->postal_code); //for some reason latest linux libgeoip-dev does not have this
    		#endif
    		if (giISP!=NULL) {
    		    char* isp=GeoIP_org_by_name(giISP,FindMe);
    		    if (isp != NULL) {
    			tbl->SetMember("ISP",isp);
    		    }
    		}
    
    		GeoIPRecord_delete(gir);
    	    }
    
    	}
    	Lua()->Push(tbl);
    	tbl->UnReference();
    
    	return 1;
        }
    
    
        int Init(lua_State *L)
        {
    	giCity = GeoIP_open("garrysmod/data/GeoIP.dat", GEOIP_INDEX_CACHE);
    	giISP = GeoIP_open("garrysmod/data/GeoIPISP.dat", GEOIP_STANDARD);
    
    	ILuaObject *tbl = Lua()->GetNewTable();
    	    tbl->SetMember("Get", GetIPInfo);
    	    Lua()->SetGlobal("GeoIP", tbl);
    	tbl->UnReference();
    	return 0;
    
        }
    
        int Shutdown(lua_State *L)
        {
    	//GeoIP_delete(giCountry);
    	GeoIP_delete(giCity);
    	GeoIP_delete(giISP);
    	return 0;
        }
    }
    Makefile (64bit system, sorry, you have to get 32-bit libGeoIP.a somewhere or edit makefile):
    Code:
    INCLUDES = -I.
    
    CFLAGS = -m32 -fpermissive -Wall -DLINUX -D_LINUX -DNDEBUG $(INCLUDES)
    
    MODE = Release
    LIBDIR = output/$(MODE)
    OBJDIR = $(LIBDIR)/obj
    SOURCEDIR = .
    
    OBJECTS = $(OBJDIR)/gm_geoloc.obj
    					
    LIBS = ./libGeoIP.a
    			
    CC = gcc
    MKDIR = mkdir -p
    AR = ar
    RM = rm -f
    RMDIR = rmdir -p
    
    all: $(LIBDIR)/gm_geoloc_linux.dll
    $(LIBDIR)/gm_geoloc_linux.dll: $(OBJECTS)
    	$(MKDIR) $(LIBDIR)
    	$(CC) -shared -o $@ $(CFLAGS) $(OBJECTS) $(LIBS)
    	
    $(OBJDIR)/%.obj : $(SOURCEDIR)/%.cpp
    	$(MKDIR) $(OBJDIR)
    	$(CC) -c -o $@ $< $(CFLAGS)
    	
    clean:
    	$(RM)     $(OBJECTS)
    	$(RM)     $(LIBDIR)/gm_geoloc_linux.dll
    	$(RMDIR)  $(OBJDIR)
    Reply With Quote Edit / Delete Reply Russian Federation Show Events Useful Useful x 1Winner Winner x 1 (list)

  3. Post #3
    Cubar's Avatar
    February 2010
    1,750 Posts
    Nice module thanks.

  4. Post #4
    Dragon Dildoes
    Dennab
    April 2009
    4,432 Posts
    Yay, no more french kids on my server.

    I love you man.
    Reply With Quote Edit / Delete Reply Germany Show Events Funny Funny x 14Dumb Dumb x 2Agree Agree x 1 (list)

  5. Post #5
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Updated version to include latitude and longitude.
    Do note it is incompatible with the old version! I changed the table values to be consistent with the GeoIP C API. It'll just hurt once :)

    Now I could do this:
    Reply With Quote Edit / Delete Reply Finland Show Events Lua King Lua King x 5Funny Funny x 3Artistic Artistic x 1 (list)

  6. Post #6
    Wow, will you release that as addon?
    Reply With Quote Edit / Delete Reply Germany Show Events Dumb Dumb x 3Funny Funny x 1 (list)

  7. Post #7
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    I can post the code I used, but everyone needs to adapt it to their databases.
    Reply With Quote Edit / Delete Reply Finland Show Events Agree Agree x 2 (list)

  8. Post #8
    Gold Member
    DrogenViech's Avatar
    December 2006
    823 Posts
    Post it! :O

  9. Post #9
    RTM xBEASTx's Avatar
    September 2010
    563 Posts
    Please post that code.

    It is an amazing idea.

    Edited:

    Im a very stupid guy but to print just the country could i do.

    IP = GeoIP.Get("8.8.8.8")
    print(IP.country_name)
    

    Am i just being an idiot or would that work fine?
    Reply With Quote Edit / Delete Reply United Kingdom Show Events Dumb Dumb x 1 (list)

  10. Post #10
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    require 'geoip'
    print(GeoIP.Get("8.8.8.8").country_name) -- "United States"
    
    The return value may not always be a string so account for that.

    Here's the FUGLY(TM) code for those who want to see it:
    local files=file.Find('datacraft/*.*')
    print("Found",#files,"files")
    data={}
    local a=SysTime()
    	for _,thefile in pairs(files) do
    		local content = file.Read('datacraft/'..thefile)
    		if content then
    			local decoded = glon.decode(content)
    			if decoded then
    				data[decoded.unique_id or decoded.id]=decoded
    			end
    		end
    	end
    local b=SysTime()
    print("Decoding files took ",(b-a),"seconds")
    
    local a=SysTime()
    	require'geoip'
    	local get=GeoIP.Get
    	results={}
    	local i=0
    	for plid,usr in pairs(data) do
    		i=i+1
    		
    		local ip=usr.ip
    		local steamid=usr.id
    		local name=usr.name
    		
    		local iploc=get(ip)
    		
    		iploc.name=name or "???"
    		iploc.steamid=steamid or "???"
    		iploc.ip=ip or "???"
    		
    		results[plid]=results[plid] and debug.Trace() or iploc
    	end
    local b=SysTime()
    print("Geolocating",i,"ips took",(b-a),"seconds")
    
    Msg("Writing kml file...")
    local tbl={[[<?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <Document>
    	<name>places.kml</name>
    	<Folder>
    		<name>Server Players</name>
    		<open>1</open>
    ]]}
    
    local i=2
    for plid,info in pairs(results) do
    	if info.latitude then -- useless without
    		tbl[i]=
    [[
    	<Placemark>
    		<name>]]..info.ip..[[</name>
    		<description>]].."<![CDATA["..
              "<h1>"..info.steamid.."</h1><p>"..info.name.."</p>\n]]>"..
    [[</description>
    		<Point>
    			<coordinates>]]..info.longitude..","..info.latitude..[[,0</coordinates>
    		</Point>
    	</Placemark>
    ]]
    		i=i+1
    	end
    end
    tbl[i]=[[
    	</Folder>
    </Document>
    </kml>
    ]]
    local kml=table.concat(tbl,"\n")
    file.Write("locations.txt",kml)
    print"done!"
    
    Word of warning: it does not escape cdata yet so malicious injection may occur. You need to write your own way to get the user data. This code is generally an example how to not do things, but I did anyway since I wanted to just see the end result.

  11. Post #11
    RTM xBEASTx's Avatar
    September 2010
    563 Posts
    Yep, Thought so. Thanks.

    Edited:

    Also, Please Could you post the googlemaps thing.

  12. Post #12
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Also, you need to rename the .txt file to .kml

    Here's more hints how to use the kml: http://code.google.com/apis/kml/documentation/

    Unrelated but XML-Related: http://www.facepunch.com/threads/961...l-A-XML-parser

    Also I would love if someone posted a cleaner example of my code :p

  13. Post #13
    RTM xBEASTx's Avatar
    September 2010
    563 Posts
    ] lua_run require 'gxml'
    > require 'gxml'...
    ] lua_run PrintTable(Entity(1):Community())
    > PrintTable(Entity(1):Community())...
    [lua_run:1] attempt to call method 'Community' (a nil value)
    

    Any reason that XML thing aint working?

    Edited:

    And with GeoIP i get this.

    ] lua_run require 'geoip'
    > require 'geoip'...
    GeoIP.dat not found. GeoIP defunct.
    ] lua_run PrintTable(GeoIP.Get("8.8.8.8"))
    > PrintTable(GeoIP.Get("8.8.8.8"))...
    [lua_run:1] attempt to index global 'GeoIP' (a nil value)
    

    Why is this happening?

  14. Post #14
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    ] lua_run require 'gxml'
    > require 'gxml'...
    ] lua_run PrintTable(Entity(1):Community())
    > PrintTable(Entity(1):Community())...
    [lua_run:1] attempt to call method 'Community' (a nil value)
    

    Any reason that XML thing aint working?

    Edited:

    And with GeoIP i get this.

    ] lua_run require 'geoip'
    > require 'geoip'...
    GeoIP.dat not found. GeoIP defunct.
    ] lua_run PrintTable(GeoIP.Get("8.8.8.8"))
    > PrintTable(GeoIP.Get("8.8.8.8"))...
    [lua_run:1] attempt to index global 'GeoIP' (a nil value)
    

    Why is this happening?
    :Community() does not exist in normal Garry's Mod. It is a system on my server that contacts your profile ( http://steamcommunity.com/id/zoe?xml=true ) and then uses gxml to make a community table out of it.

    Code:
    GeoIP.dat not found. GeoIP defunct
    Like I said you need to get the GeoIP.dat ( http://geolite.maxmind.com/download/...iteCity.dat.gz ) or it won't work.

  15. Post #15
    RTM xBEASTx's Avatar
    September 2010
    563 Posts
    Oh ok, i thought the GeoIP.dat was optional.

    Also, is there any chance you could give me that community function, as i thought it was built into the module.

  16. Post #16
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Replied on the XML thread.

  17. Post #17
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,754 Posts
    Does this still work right?

  18. Post #18
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Of course?

  19. Post #19
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,754 Posts
    I saved the file as the wrong name, my bad.

  20. Post #20
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    I saved the file as the wrong name, my bad.
    Yeah it's not the original filename, easy to change but not so easy to recompile. Hope it works for you whatever you're going to do with it.
    Reply With Quote Edit / Delete Reply Finland Show Events Agree Agree x 1 (list)

  21. Post #21
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,754 Posts
    It's working great, I'm adding a feature like one I saw in CSS (when a player join it says "XXX (STEAMID) joined from COUNTRY"). I just wish there was a way to make it say "the United States" or "Norway" so it doesn't say "BANANALORD LOLXD (IDK MY OWN STEAMID) joined from United States"

  22. Post #22
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    You can always make a translator table so you could replace words the api spits out with our own if they're wrong.

  23. Post #23
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,754 Posts
    You can always make a translator table so you could replace words the api spits out with our own if they're wrong.
    Yeah, that's what I'm doing, thanks for this module though, it's awesome :buddy:

  24. Post #24
    cyw960517's Avatar
    May 2011
    281 Posts
    Where i should put API library ?

  25. Post #25
    Gold Member
    dingusnin's Avatar
    February 2010
    2,024 Posts
    I am extreemly sorry to bump an old thread, But is it possible for some to reupload the module? It seems the link is now corrupt :(

    Thanks in advance,
    Dingusnin :)

  26. Post #26
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Fixed the link to point to the server's ip as my domain expired. Wouldn't mind a new one as a christmas gift
    Reply With Quote Edit / Delete Reply Finland Show Events Optimistic Optimistic x 2Winner Winner x 1 (list)

  27. Post #27
    Gold Member
    dingusnin's Avatar
    February 2010
    2,024 Posts
    Thanks <3

  28. Post #28
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Uploaded GM13 version.

    Edited:

    Linux only D:
    Reply With Quote Edit / Delete Reply Windows 7 Finland Show Events Winner Winner x 2 (list)

  29. Post #29
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,754 Posts
    linux ftw

    thanks bby

  30. Post #30
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Uploaded win32 version!

    Edited:

    Now with utf8 support. Both windows and linux.
    Reply With Quote Edit / Delete Reply Windows 7 Finland Show Events Lua King Lua King x 1Friendly Friendly x 1 (list)

  31. Post #31
    DiXi's Avatar
    August 2006
    1 Posts
    I am able to load the module, but GeoIP returns a nil value.. any ideas what i might do wrong?.

    I am just testing what someone posted before me:

    Code:
    lua_run require 'geoip'
    > require 'geoip'...
    lua_run PrintTable(GeoIP.Get("8.8.8.8"))
    > PrintTable(GeoIP.Get("8.8.8.8"))...
    
    [ERROR] lua_run:1: attempt to index global 'GeoIP' (a nil value)
      1. unknown - lua_run:1
    
    
    [ERROR] lua_run:1: attempt to index global 'GeoIP' (a nil value)
      1. unknown - lua_run:1

    ok i figured out that im getting:
    Error Opening file garrysmod/data/GeoIP.dat
    Error Opening file GeoIP.dat
    GeoIP.dat not found. GeoIP defunct.

    no idea why though, cause the GeoIP file is there..
    I should be using the binary version, right?




    Edit:
    Nevermind.. i forgot to edit my startupscript from template/ to $PORT/ so it started the server in wrong directory.
    I have symlinked most of my serverfiles to one base directory, and while testing if everything worked i used the template.. i guess i forgot to fix it..

    anyway, it works fine now.. thanks alot :)
    Reply With Quote Edit / Delete Reply Windows 7 Norway Show Events Funny Funny x 1 (list)

  32. Post #32
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Recompiled linux version with additional database support and latest libGeoIP!
    Reply With Quote Edit / Delete Reply Windows 7 Finland Show Events Winner Winner x 1 (list)

  33. Post #33

    June 2012
    169 Posts
    Thanks man. I've been trying to kill my bitch ex girlfriend since about a year ago. But after she fled the state, I've been having trouble tracking her. This will teach that cheating whore not to play tf2. Thanks again man.
    Reply With Quote Edit / Delete Reply United States Show Events Dumb Dumb x 7Funny Funny x 5Informative Informative x 2 (list)

  34. Post #34
    Google first. ( Wiki )
    Robotboy655's Avatar
    May 2008
    7,543 Posts
    Is this thing offline? What's different from http://ipinfodb.com/ ?

  35. Post #35
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    It uses local database(s) so basically unlimited queries. You might be right a cloud service could be more fitting most often though.
    Reply With Quote Edit / Delete Reply Windows 7 Finland Show Events Informative Informative x 1 (list)

  36. Post #36
    DefaultText.ini
    Charrax's Avatar
    February 2011
    1,342 Posts
    Anybody have a working Windows download? Also I just install it to /lua/bin right?

  37. Post #37
    Dennab
    January 2008
    121 Posts
    Anybody have a working Windows download? Also I just install it to /lua/bin right?
    I compiled it myself (nighmare) as i wanted it to load the databases from lua/bin.

    Source: http://mfsinc.googlecode.com/svn/tru...geolite_win32/
    DLL: http://mfsinc.googlecode.com/svn/tru...lite_win32.dll
    Example: http://mfsinc.googlecode.com/svn/tru...se/geolite.lua

  38. Post #38
    DefaultText.ini
    Charrax's Avatar
    February 2011
    1,342 Posts
    So lua/bin it is then.

  39. Post #39
    Gold Member
    12voltsdc's Avatar
    June 2011
    1,601 Posts
    Does anybody have a working linux version of this module?

  40. Post #40
    likes men
    Python1320's Avatar
    May 2007
    1,720 Posts
    Sorry, the server got the boot quite bad, all my modules went with it. Even the ip changed.
    I'll try to get some place to have them hosted more permanently. Any suggestions? Probably googlecode or github.
    Reply With Quote Edit / Delete Reply Windows 8 Finland Show Events Friendly Friendly x 1 (list)