通过GeoIP2分析访问者IP获取地理位置信息

原文链接:http://blog.csdn.net/johnnycode/article/details/42028841

MaxMind GeoIP2 服务能识别互联网用户的地点位置与其他特征,应用广泛,包括个性化定制内容、诈欺检测、广告定向、网站流量分析、执行规定、地理目标定位、地理围栏定位 (geo-fencing)以及数字版权管理。目前使用 GeoIP 更多是配合Nginx或Apache服务器进行日志分析获取网站访问量地域分布状况。

GeoIP 分为商业版和免费版,免费版比商业版精度差了许多,经测试对于城市定位确实有差距,能否接受看你的精度要求!

一、免费版本介绍:

1、GeoLite 版本,网上流传较广,数据库类型为 dat 格式文件,库文件较小未进行精准度测试。

2、GeoLite2版本,目前最新版本,数据库文件为 mmdb 格式文件,有兴趣了解 mmdb 格式的点这里 。

两者数据库文件大小比对,GeoLite2 特性点击这里

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ du -sh *  
  2. 32M GeoLite2-City.mmdb  
  3. 2.3M    GeoLite2-Country.mmdb  
  4. 18M GeoLiteCity.dat  
  5. 732K    GeoLiteCountry.dat  

City 文件为包含城市信息数据库,Country 文件为国家信息数据库。

 

二、下载 GeoLite2 数据库
下载方式分为两种,第一种通过下载 gz 压缩包,第二种通过使用官方提供的下载更新程序,建议使用第二种,官方称数据库在每个月的第一个星期二更新,如果想做成计划任务每月都更新建议选择第二种!GeoIP2详细更新日志点这里

两种方式这里都啰嗦一下,本阶段只是讲如何下载数据库,调用方式需要参考第三阶段 API 调用部分!

1、第一种方式,下载 gz 文件并解压缩。

GeoLite2 只提供 City 数据库和 Country 数据库下载 查看详情点击里,数据库文件分为 Binary 和 CVS 两种,这里使用 Binary 文件。

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ sudo mkdir -p /mnt/data/geolite2 && cd $_  
  2. $ sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz  
  3. $ sudo gzip -d GeoLite2-City.mmdb.gz  
  4. $ sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz  
  5. $ sudo gzip -d GeoLite2-Country.mmdb.gz  

 

2、第二种方式,需安装官方下载更新程序 geoipupdate 。

a、到 GitHub下载地址 下载 geoipupdate,目前最新版为 2.1.0,GitHub 连接速度要有耐心,肯定可以下载滴!编译文件需要 libcurl-devel 包支持,需提前下载安装。

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ sudo yum install libcurl-devel -y  
  2. $ sudo wget https://github.com/maxmind/geoipupdate/releases/download/v2.1.0/geoipupdate-2.1.0.tar.gz  
  3. $ sudo tar xzvf geoipupdate-2.1.0.tar.gz  
  4. $ cd geoipupdate-2.1.0  
  5. $ sudo ./configure  
  6. $ sudo make  
  7. $ sudo make install  

编译完毕只需要关注两个文件

更新执行文件 /usr/local/bin/geoipupdate

账户信息文件 /usr/local/etc/GeoIP.conf

b、配置账户信息 GeoIP.conf,修改配置文件如下即可,本配置文件默认下载 mmdb 文件,若想下载 dat 文件取消注释即可!

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1.  # The following UserId and LicenseKey are required placeholders:  
  2. UserId 999999  
  3. LicenseKey 000000000000  
  4.   
  5. # Include one or more of the following ProductIds:  
  6. # * GeoLite2-City - GeoLite 2 City  
  7. # * GeoLite2-Country - GeoLite2 Country  
  8. # * 506 - GeoLite Legacy Country  
  9. # * 517 - GeoLite Legacy ASN  
  10. # * 533 - GeoLite Legacy City  
  11.   
  12. # dat 格式数据库  
  13. #ProductIds GeoLite2-City GeoLite2-Country 506 533  
  14.   
  15. # mmdb 格式数据库  
  16. ProductIds GeoLite2-City GeoLite2-Country 132 106  

c、执行更新

查看geoipupdate帮助文件,了解有哪些参数可以使用! -d 参数将文件下载到指定目录,-v 参数就是显示下载过程明细信息。

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ /usr/local/bin/geoipupdate -h  
  2. Usage: geoipupdate [-Vhv] [-f license_file] [-d custom directory]  
  3.   
  4.   -d DIR   store downloaded files in DIR  
  5.   -f FILE  use configuration found in FILE (see GeoIP.conf(5) man page)  
  6.   -h       display this help text  
  7.   -v       use verbose output  
  8.   -V       display the version and exit  

执行更新命令,下载速度看网络情况,本文将文件下载到 /mnt/data/geolite2/目录 。

$ ll /mnt/data/geolite2/ && cd $_
总用量 0
$ sudo /usr/local/bin/geoipupdate -d /mnt/data/geolite2/ -v
$ ll
总用量 34088
-rw-r--r--. 1 root root 32553611 12月 19 18:14 GeoLite2-City.mmdb
-rw-r--r--. 1 root root  2349406 12月 19 18:14 GeoLite2-Country.mmdb

如何配置计划任务定时更新 GeoLite2 数据库请自行解决。

三、安装 GeoLite2 API 调用程序

官方提供 .NET (C#)、C、Java、Perl、Python、Apache API调用。其他第三方接口也有,但官方不提供技术支持,详情点击这里

本文使用 C 语言API接口进行调用测试,为下篇文章Nginx与GeoIP2配合做铺垫。其他语言请参考官方指导自行解决!C语言API GitHub 下载地址

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ sudo wget https://github.com/maxmind/libmaxminddb/releases/download/1.0.3/libmaxminddb-1.0.3.tar.gz  
  2. $ sudo tar xzvf libmaxminddb-1.0.3.tar.gz  
  3. $ cd libmaxminddb-1.0.3  
  4. $ sudo ./configure  
  5. $ sudo make  
  6. $ sudo make install  
  7. $ sudo ldconfig  

查看帮助文档

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ /usr/local/bin/mmdblookup --help  
  2.   
  3.   mmdblookup --file /path/to/file.mmdb --ip 1.2.3.4 [path to lookup]  
  4.   
  5.   This application accepts the following options:  
  6.   
  7.       --file (-f)     The path to the MMDB file. Required.  
  8.   
  9.       --ip (-i)       The IP address to look up. Required.  
  10.   
  11.       --verbose (-v)  Turns on verbose output. Specifically, this causes this  
  12.                       application to output the database metadata.  
  13.   
  14.       --version       Print the program's version number and exit.  
  15.   
  16.       --help (-h -?)  Show usage information.  
  17.   
  18.   If an IP's data entry resolves to a map or array, you can provide  
  19.   a lookup path to only show part of that data.  
  20.   
  21.   For example, given a JSON structure like this:  
  22.   
  23.     {  
  24.         "names": {  
  25.              "en": "Germany",  
  26.              "de": "Deutschland"  
  27.         },  
  28.         "cities": [ "Berlin", "Frankfurt" ]  
  29.     }  
  30.   
  31.   You could look up just the English name by calling mmdblookup with a lookup path of:  
  32.   
  33.     mmdblookup --file ... --ip ... names en  
  34.   
  35.   Or you could look up the second city in the list with:  
  36.   
  37.     mmdblookup --file ... --ip ... cities 1  
  38.   
  39.   Array numbering begins with zero (0).  
  40.   
  41.   If you do not provide a path to lookup, all of the information for a given IP  
  42.   will be shown.  

四、测试

1、获取国家信息,国家信息是正确滴,看着像乱码的地方是显示的俄语!

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ /usr/local/bin/mmdblookup --file /mnt/data/geolite2/GeoLite2-Country.mmdb --ip 112.225.35.70  
  2.   
  3.   {  
  4.     "continent":   
  5.       {  
  6.         "code":   
  7.           "AS" <utf8_string>  
  8.         "geoname_id":   
  9.           6255147 <uint32>  
  10.         "names":   
  11.           {  
  12.             "de":   
  13.               "Asien" <utf8_string>  
  14.             "en":   
  15.               "Asia" <utf8_string>  
  16.             "es":   
  17.               "Asia" <utf8_string>  
  18.             "fr":   
  19.               "Asie" <utf8_string>  
  20.             "ja":   
  21.               "アジア" <utf8_string>  
  22.             "pt-BR":   
  23.               "Ásia" <utf8_string>  
  24.             "ru":   
  25.               "Азия" <utf8_string>  
  26.             "zh-CN":   
  27.               "亚洲" <utf8_string>  
  28.           }  
  29.       }  
  30.     "country":   
  31.       {  
  32.         "geoname_id":   
  33.           1814991 <uint32>  
  34.         "iso_code":   
  35.           "CN" <utf8_string>  
  36.         "names":   
  37.           {  
  38.             "de":   
  39.               "China" <utf8_string>  
  40.             "en":   
  41.               "China" <utf8_string>  
  42.             "es":   
  43.               "China" <utf8_string>  
  44.             "fr":   
  45.               "Chine" <utf8_string>  
  46.             "ja":   
  47.               "中国" <utf8_string>  
  48.             "pt-BR":   
  49.               "China" <utf8_string>  
  50.             "ru":   
  51.               "Китай" <utf8_string>  
  52.             "zh-CN":   
  53.               "中国" <utf8_string>  
  54.           }  
  55.       }  
  56.     "registered_country":   
  57.       {  
  58.         "geoname_id":   
  59.           1814991 <uint32>  
  60.         "iso_code":   
  61.           "CN" <utf8_string>  
  62.         "names":   
  63.           {  
  64.             "de":   
  65.               "China" <utf8_string>  
  66.             "en":   
  67.               "China" <utf8_string>  
  68.             "es":   
  69.               "China" <utf8_string>  
  70.             "fr":   
  71.               "Chine" <utf8_string>  
  72.             "ja":   
  73.               "中国" <utf8_string>  
  74.             "pt-BR":   
  75.               "China" <utf8_string>  
  76.             "ru":   
  77.               "Китай" <utf8_string>  
  78.             "zh-CN":   
  79.               "中国" <utf8_string>  
  80.           }  
  81.       }  
  82.   }  

 

2、获取城市信息,这个数据就有点纠结了,省份没有问题,城市是有问题的! 官方演示地址 非常精准,也许这就是免费和收费的差别 :)

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ /usr/local/bin/mmdblookup --file /mnt/data/geolite2/GeoLite2-City.mmdb --ip 112.225.35.70   
  2.   {  
  3.     "city":   
  4.       {  
  5.         "geoname_id":   
  6.           1805753 <uint32>  
  7.         "names":   
  8.           {  
  9.             "de":   
  10.               "Jinan" <utf8_string>  
  11.             "en":   
  12.               "Jinan" <utf8_string>  
  13.             "es":   
  14.               "Jinan" <utf8_string>  
  15.             "fr":   
  16.               "Jinan" <utf8_string>  
  17.             "ja":   
  18.               "済南市" <utf8_string>  
  19.             "pt-BR":   
  20.               "Jinan" <utf8_string>  
  21.             "ru":   
  22.               "Цзинань" <utf8_string>  
  23.             "zh-CN":   
  24.               "济南" <utf8_string>  
  25.           }  
  26.       }  
  27.     "continent":   
  28.       {  
  29.         "code":   
  30.           "AS" <utf8_string>  
  31.         "geoname_id":   
  32.           6255147 <uint32>  
  33.         "names":   
  34.           {  
  35.             "de":   
  36.               "Asien" <utf8_string>  
  37.             "en":   
  38.               "Asia" <utf8_string>  
  39.             "es":   
  40.               "Asia" <utf8_string>  
  41.             "fr":   
  42.               "Asie" <utf8_string>  
  43.             "ja":   
  44.               "アジア" <utf8_string>  
  45.             "pt-BR":   
  46.               "Ásia" <utf8_string>  
  47.             "ru":   
  48.               "Азия" <utf8_string>  
  49.             "zh-CN":   
  50.               "亚洲" <utf8_string>  
  51.           }  
  52.       }  
  53.     "country":   
  54.       {  
  55.         "geoname_id":   
  56.           1814991 <uint32>  
  57.         "iso_code":   
  58.           "CN" <utf8_string>  
  59.         "names":   
  60.           {  
  61.             "de":   
  62.               "China" <utf8_string>  
  63.             "en":   
  64.               "China" <utf8_string>  
  65.             "es":   
  66.               "China" <utf8_string>  
  67.             "fr":   
  68.               "Chine" <utf8_string>  
  69.             "ja":   
  70.               "中国" <utf8_string>  
  71.             "pt-BR":   
  72.               "China" <utf8_string>  
  73.             "ru":   
  74.               "Китай" <utf8_string>  
  75.             "zh-CN":   
  76.               "中国" <utf8_string>  
  77.           }  
  78.       }  
  79.     "location":   
  80.       {  
  81.         "latitude":   
  82.           36.668300 <double>  
  83.         "longitude":   
  84.           116.997200 <double>  
  85.         "time_zone":   
  86.           "Asia/Shanghai" <utf8_string>  
  87.       }  
  88.     "registered_country":   
  89.       {  
  90.         "geoname_id":   
  91.           1814991 <uint32>  
  92.         "iso_code":   
  93.           "CN" <utf8_string>  
  94.         "names":   
  95.           {  
  96.             "de":   
  97.               "China" <utf8_string>  
  98.             "en":   
  99.               "China" <utf8_string>  
  100.             "es":   
  101.               "China" <utf8_string>  
  102.             "fr":   
  103.               "Chine" <utf8_string>  
  104.             "ja":   
  105.               "中国" <utf8_string>  
  106.             "pt-BR":   
  107.               "China" <utf8_string>  
  108.             "ru":   
  109.               "Китай" <utf8_string>  
  110.             "zh-CN":   
  111.               "中国" <utf8_string>  
  112.           }  
  113.       }  
  114.     "subdivisions":   
  115.       [  
  116.         {  
  117.           "geoname_id":   
  118.             1796328 <uint32>  
  119.           "iso_code":   
  120.             "37" <utf8_string>  
  121.           "names":   
  122.             {  
  123.               "en":   
  124.                 "Shandong Sheng" <utf8_string>  
  125.               "zh-CN":   
  126.                 "山东省" <utf8_string>  
  127.             }  
  128.         }  
  129.       ]  
  130.   }  

 

测试IP1:112.225.35.70 山东省青岛市,定位错误。

测试IP2:115.29.113.101 浙江省杭州市,定位正确。

测试IP3:112.124.127.64 浙江省杭州市,定位正确。

测试IP4:180.153.214.152 上海市,定位正确。

因为获取的数据是 Json 格式,所以根据帮助文档提示可以对内容进行格式化输出,如输出城市数据库中 city->names->zh-CN 内容

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. $ /usr/local/bin/mmdblookup --file /mnt/data/geolite2/GeoLite2-City.mmdb --ip 112.225.35.70 city names zh-CN  
  2.   
  3.   "济南" <utf8_string>  

 

获取省份要注意一点,省份是个数组,无意中发现每个版本的获取方式还不一样,注意版本区别!

 

[plain] view plain copy
 
  1. $ /usr/local/bin/mmdblookup --file /mnt/data/geolite2/GeoLite2-City.mmdb --ip 112.225.35.70 subdivisions 0 names en  
  2.   
  3.   "Shandong Sheng" <utf8_string>  

 

 

虽然 GeoIP2 免费数据库在城市定位分析的不是很理想,但对我来说精度可以接受,聊胜于无嘛! 

posted on 2016-08-09 09:11  让编程成为一种习惯  阅读(1011)  评论(0编辑  收藏  举报