海运的博客

Nginx模块GeoIP匹配处理IP所在国家、城市

发布时间:July 1, 2012 // 分类:Nginx // 5 Comments

Nginx可配合GeoIP模块定位IP所在物理位置并做相应处理,支持多个条件匹配:

#https://www.haiyun.me
$geoip_country_code #国家代码2位,如CN
$geoip_country_code3 #国家代码3位,如CHN
$geoip_country_name #国家完整名称,如China
$geoip_region #所在地区
$geoip_city #所在城市,如BeiJing
$geoip_postal_code #邮政编码
$geoip_city_continent_code #所在洲,如AS
$geoip_latitude #纬度
$geoip_longitude #经度

编译安装Nginx并添加GeoIP模块:

yum install geoip-devel #安装GeoIP解析库
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz 
cd nginx-1.0.15
 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_geoip_module
make 
make install

下载GeoIP城市国家数据库:

#https://www.haiyun.me
mkdir -p /usr/local/nginx/geoip
cd /usr/local/nginx/geoip
wget http://www.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoIP.dat.gz 
gunzip GeoLiteCity.dat.gz 

编辑Nginx配置文件加载GeoIP数据库:

http
    [...]
geoip_country  /usr/local/nginx/geoip/GeoIP.dat; #国家数据库
geoip_city     /usr/local/nginx/geoip/GeoLiteCity.dat; #城市数据库
[...]

如需Nginx传递变量给PHP,编辑fastcgi_params添加:

fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

应用示例,Nginx判断如果访问者IP所在国家为美国或中国,返回404错误

server
    [...]
                if ($geoip_country_code ~* (US|CN)) {
                #return 404;
                }
[...]

新建PHP程序测试GeoIP:

<html>
<head>
 <title>IP地址检测,我的IP地址是多少?</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
        $ipaddress = getenv(REMOTE_ADDR);
        echo "<br>您的代理IP地址是: ".$pipaddress. " (via $ipaddress) " ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "<br>您的IP地址是 : $ipaddress";
    }
  $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
  $geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3);
  $geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME);
  $geoip_region = getenv(GEOIP_REGION);
  $geoip_city = getenv(GEOIP_CITY);
  $geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
  $geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE);
  $geoip_latitude = getenv(GEOIP_LATITUDE);
  $geoip_longitude = getenv(GEOIP_LONGITUDE);
  echo "<br>国家 : $geoip_city_country_name ( $geoip_city_country_code3 , $geoip_city_country_code ) ";
  echo "<br>地区 :  $geoip_region";
  echo "<br>城市 :  $geoip_city ";
  echo "<br>邮政编码 :  $geoip_postal_code";
  echo "<br>所在洲 :  $geoip_city_continent_code";
  echo "<br>纬度 :  $geoip_latitude ";
  echo "<br>经度 :   $geoip_longitude ";

?>
</body>
</html>

Iptables拒绝指定国家的IP访问

发布时间:July 1, 2012 // 分类:Iptables // No Comments

有些服务器需拒绝特定国家的IP访问,可使用Iptables配合ipdeny提供的各个国家IP段为源进行过滤操作,由于数目较多会影响iptables性能,也可使用高效率Iptables geoip模块进行匹配操作。
应用示例,以拒绝美国IP为例:

#https://www.haiyun.me
#/bin/bash
wget -O /tmp/us.zone http://www.ipdeny.com/ipblocks/data/countries/us.zone
for ip in `cat /tmp/us.zone`
do
Blocking $ip
iptables -I INPUT -s $ip -j DROP
done

Centos6下用Xtables-Addons不编译安装Iptables模块Geoip

发布时间:June 29, 2012 // 分类:Iptables // No Comments

通常增加Iptables模块需重新编译内核及Iptables,通过Xtables-Addons安装其支持的模块可免编译安装。
Xtables-Addons支持模块:

build_ACCOUNT=m
build_CHAOS=m
build_CHECKSUM=
build_DELUDE=m
build_DHCPMAC=m
build_DNETMAP=m
build_ECHO=
build_IPMARK=m
build_LOGMARK=m
build_RAWNAT=m
build_STEAL=m
build_SYSRQ=m
build_TARPIT=m
build_TEE=
build_condition=m
build_fuzzy=m
build_geoip=m
build_gradm=m
build_iface=m
build_ipp2p=m
build_ipset4=m
build_ipset6=
build_ipv4options=m
build_length2=m
build_lscan=m
build_pknock=m
build_psd=m
build_quota2=m

Xtables-Addons安装要求:

iptables >= 1.4.3
kernel-source >= 2.6.29

CentOS安装支持组件:

rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/6/i386/epel-release-6-7.noarch.rpm
yum install gcc gcc-c++ make automake kernel-devel iptables-devel perl-Text-CSV_XS xz

安装Xtables-Addons:

wget http://sourceforge.net/projects/xtables-addons/files/Xtables-addons/1.42/xtables-addons-1.42.tar.xz
xz -d xtables-addons-1.42.tar.xz 
tar xvf xtables-addons-1.42.tar 
cd xtables-addons-1.42
./configure 
make 
make install
#可修改mconfig选择要安装的模块

下载Geoip数据库:

cd geoip
./xt_geoip_dl
./xt_geoip_build GeoIPCountryWhois.csv
mkdir /usr/share/xt_geoip
cp -r {BE,LE}  /usr/share/xt_geoip/

应用举例:

iptables -I INPUT -m geoip --src-cc US-j DROP
#拒绝美国IP连接 

Awstats配置GeoIP和纯真IP库显示IP地理位置信息

发布时间:June 25, 2012 // 分类:日记分析 // No Comments

1.GeoIP插件安装:

#安装Perl组件
perl -MCPAN -e 'install Geo::IP::PurePerl'
或
yum install perl-Geo-IP
#下载GeoIP国家、城市数据库、AS数据库
cd /usr/local/awstats/wwwroot/cgi-bin/plugins
wget http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://www.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://www.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
gzip -d GeoLiteCity.dat.gz
gzip -d GeoIP.dat.gz
gzip -d GeoIPASNum.dat.gz

编辑Awstats配置文件加载插件:

LoadPlugin="geoip GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/plugins/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/plugins/GeoLiteCity.dat"
LoadPlugin="geoip_org_maxmind GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/plugins/GeoIPASNum.dat"  

2.纯真IP库插件安装:

cd /usr/local/awstats/wwwroot/cgi-bin/plugins
wget https://www.haiyun.me/download/qqwry.pl
wget https://www.haiyun.me/download/qqhostinfo.pm
#下载纯真IP数据库
wget http://update.cz88.net/soft/qqwry.rar
unrar qqwry.rar

修改qqwry.pl内IP数据目录:

my $ipfile="${DIR}/plugins/QQWry.Dat"; 

修改awstats配置加载扩展:

LoadPlugin="qqhostinfo"

最终效果如下:
awstats安装GeoIP和纯真IP库.png

分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...