├── test.lua ├── README.md └── lib └── GeoIP.lua /test.lua: -------------------------------------------------------------------------------- 1 | require 'lib.GeoIP' 2 | 3 | --GeoIP.filenameIPv4 = "/usr/share/GeoIP/GeoIP.dat" 4 | --GeoIP.filenameIPv6 = "/usr/share/GeoIP/GeoIPv6.dat" 5 | 6 | local ok, err =GeoIP.init() 7 | if not ok then 8 | print("GeoIP init error: "..err) 9 | exit() 10 | end 11 | 12 | 13 | print ( GeoIP.getCountry("8.8.8.8") ) -- US 14 | print ( GeoIP.getCountry("2a00:1450:4001:812::2003") ) -- DE 15 | print ( GeoIP.getCountry("2a00:2450:4001:812::2003") ) -- 0 16 | print ( GeoIP.getCountry("test") ) -- 0 17 | 18 | 19 | GeoIP.close() 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lua-geoip 2 | example for lua-geoip package 3 | 4 | ### Requiments 5 | ``` 6 | - geoip-database 7 | - libgeoip-dev 8 | - lua-geoip 9 | ``` 10 | 11 | ### Example 12 | ``` 13 | require 'lib.GeoIP' 14 | 15 | --GeoIP.filenameIPv4 = "/usr/share/GeoIP/GeoIP.dat" 16 | --GeoIP.filenameIPv6 = "/usr/share/GeoIP/GeoIPv6.dat" 17 | 18 | local ok, err =GeoIP.init() 19 | if not ok then 20 | print("GeoIP init error: "..err) 21 | exit() 22 | end 23 | 24 | 25 | print ( GeoIP.getCountry("8.8.8.8") ) -- US 26 | print ( GeoIP.getCountry("2a00:1450:4001:812::2003") ) -- DE 27 | print ( GeoIP.getCountry("2a00:2450:4001:812::2003") ) -- 0 28 | print ( GeoIP.getCountry("test") ) -- 0 29 | 30 | 31 | GeoIP.close() 32 | ``` 33 | 34 | -------------------------------------------------------------------------------- /lib/GeoIP.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Geoip package parser 3 | 4 | 5 | check if is opened by Nginx lsof /usr/share/GeoIP/GeoIP.dat 6 | ]] 7 | 8 | GeoIP = { 9 | filenameIPv4 = "/usr/share/GeoIP/GeoIP.dat", 10 | filenameIPv6 = "/usr/share/GeoIP/GeoIPv6.dat", 11 | country = require 'geoip.country', 12 | ipv4 = nil, 13 | ipv6 = nil, 14 | } 15 | 16 | 17 | function GeoIP.getCountryipv4(ip) 18 | return GeoIP.ipv4:query_by_addr(ip).code 19 | end 20 | 21 | 22 | function GeoIP.getCountryipv6(ip) 23 | return GeoIP.ipv6:query_by_addr6(ip).code 24 | end 25 | 26 | 27 | function GeoIP.getCountry(ip) 28 | if GeoIP.is_ipv4_fast(ip) then 29 | return GeoIP.getCountryipv4(ip) 30 | end 31 | return GeoIP.getCountryipv6(ip) 32 | end 33 | 34 | 35 | --[[ 36 | Fast check ipv4 37 | Note if not ip the querry_by_addr will return '--' 38 | ]] 39 | function GeoIP.is_ipv4_fast(str) 40 | if (string.byte(str,2,2) == 46) then return true end 41 | 42 | if (string.byte(str, 3, 3) == 46) then return true end 43 | 44 | if (string.byte(str, 4,4) == 46) then return true end 45 | return false 46 | end 47 | 48 | 49 | function GeoIP.init() 50 | local ipv4, err = GeoIP.country.open(GeoIP.filenameIPv4) 51 | if err then 52 | return nil, err 53 | end 54 | GeoIP.ipv4 = ipv4 55 | 56 | local ipv6, err = GeoIP.country.open(GeoIP.filenameIPv6) 57 | if err then 58 | return nil, err 59 | end 60 | GeoIP.ipv6 = ipv6 61 | 62 | return true,nil 63 | end 64 | 65 | 66 | function GeoIP.close() 67 | if GeoIP.ipv4 then 68 | GeoIP.ipv4:close() 69 | GeoIP.ipv4 = nil 70 | end 71 | if GeoIP.ipv6 then 72 | GeoIP.ipv6:close() 73 | GeoIP.ipv6 = nil 74 | end 75 | end 76 | --------------------------------------------------------------------------------