frps服务器通常就是针对国内家庭用户没有公网IP的一个工具,用于测试web程序等之用,所以国外IP连接frp服务器也没有什么意义,所以利用iptables禁止国外ip连接frps服务器,仅允许国内ip连接frp服务器,这样对frp内网穿透工具没有任何影响,同时对别有用心的用户倒是制造了写麻烦。
一、利用iptables禁止国外IP连接frps服务器,仅允许国内连接的实现方法
下载iptables规则的sh文件:http://down.xxorg.com/frp/frps_iptables.sh ,然后导入到frps服务器的iptables里
wget http://down.xxorg.com/frp/frps_iptables.sh chmod +x frps_iptables.sh ./frps_iptables.sh
然后保存iptables
service iptables save
到这里就已经结束了,下面记录一些上面用到的一些相关知识,如果不感兴趣的话就可以不用往下看了。
二、分享几个关于国内外IP段和iptables的小知识
1、最新国内外IP地址段
可以从http://www.ipdeny.com/ipblocks/data/countries/cn.zone获取到最新的中国的IP地址段,也可以直接到Apanic获取,地址为http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
2、iptables常用的命令
永久性生效
开启:chkconfig iptables on 关闭:chkconfig iptables off
即时生效,重启后失效
开启:service iptables start 关闭:service iptables stop
清空iptables
iptables -F
查看iptables规则
iptables -L -n
使用iptables来允许一个IP段的话,我们可以这样子
iptables -A INPUT -s 121.10.139.0/24 -p TCP --dport 7000 -j ACCEPT iptables -A OUTPUT -d 121.10.139.0/24 -p TCP --sport 7000 -j ACCEPT
这样子就可以允许一个IP段 访问服务器端的7000端口了
禁止其他所有IP访问服务器的7000端口
iptables -A INPUT -p TCP --dport 7000 -j DROP
三、网上找到了一个相似的脚本,修改了一下,可以自动读取国内IP并添加到iptables
以下代码没有做测试,仅仅是做个记录,慎用
#!/bin/bash COUNTRY="cn" IPTABLES=/sbin/iptables EGREP=/bin/egrep if [ "$(id -u)" != "0" ]; then echo "you must be root" 1>&2 exit 1 fi wget http://www.ipdeny.com/ipblocks/data/countries/cn.zone resetrules() { $IPTABLES -F $IPTABLES -t nat -F $IPTABLES -t mangle -F $IPTABLES -X } resetrules for c in $COUNTRY do country_file=$c.zone IPS=$($EGREP -v "^#|^$" $country_file) for ip in $IPS do echo "blocking $ip" $IPTABLES -A INPUT -s $ip -p TCP --dport 7000 -j ACCEPT done done iptables -A INPUT -p TCP --dport 7000 -j DROP exit 0