Linux屏蔽国外IP

查看: 372|回复: 3
crll 发表于 2023-2-26 00:54:44
1.安装ipset。
  1. sudo apt-get install ipset
复制代码

2.将以下脚本保存至文件allcn.sh。
  1. mmode=$1

  2. #下面语句可以单独执行,不需要每次执行都获取网段表
  3. #wget -q --timeout=60 -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > china_ssr.txt

  4. CNIP="china_ssr.txt"

  5. gen_iplist() {
  6.         cat <<-EOF
  7.         $(cat ${CNIP:=/dev/null} 2>/dev/null)
  8.         EOF
  9. }

  10. flush_r() {
  11.         iptables -F ALLCNRULE 2>/dev/null
  12.         iptables -D INPUT -p tcp -j ALLCNRULE 2>/dev/null
  13.         iptables -X ALLCNRULE 2>/dev/null
  14.         ipset -X allcn 2>/dev/null
  15. }

  16. mstart() {
  17.         ipset create allcn hash:net 2>/dev/null
  18.         ipset -! -R <<-EOF
  19.         $(gen_iplist | sed -e "s/^/add allcn /")
  20.         EOF
  21.        
  22.         iptables -N ALLCNRULE
  23.         iptables -I INPUT -p tcp -j ALLCNRULE
  24.         iptables -A ALLCNRULE -s 127.0.0.0/8 -j RETURN
  25.         iptables -A ALLCNRULE -s 169.254.0.0/16 -j RETURN
  26.         iptables -A ALLCNRULE -s 224.0.0.0/4 -j RETURN
  27.         iptables -A ALLCNRULE -s 255.255.255.255 -j RETURN
  28.         #可在此增加你的公网网段,避免调试ipset时出现自己无法访问的情况
  29.         iptables -A ALLCNRULE -m set --match-set allcn src -j RETURN
  30.         iptables -A ALLCNRULE -p tcp -j DROP
  31. }

  32. if [ "$mmode" == "stop" ] ;then
  33.         flush_r
  34.         exit 0
  35. fi

  36. flush_r
  37. sleep 1
  38. mstart
复制代码

3.下载国内网段表。如上述脚本未注释以下语句,此步骤可不执行。
  1. mmode=$1

  2. #下面语句可以单独执行,不需要每次执行都获取网段表
  3. #wget -q --timeout=60 -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > china_ssr.txt

  4. CNIP="china_ssr.txt"

  5. gen_iplist() {
  6.         cat <<-EOF
  7.         $(cat ${CNIP:=/dev/null} 2>/dev/null)
  8.         EOF
  9. }

  10. flush_r() {
  11.         iptables -F ALLCNRULE 2>/dev/null
  12.         iptables -D INPUT -p tcp -j ALLCNRULE 2>/dev/null
  13.         iptables -X ALLCNRULE 2>/dev/null
  14.         ipset -X allcn 2>/dev/null
  15. }

  16. mstart() {
  17.         ipset create allcn hash:net 2>/dev/null
  18.         ipset -! -R <<-EOF
  19.         $(gen_iplist | sed -e "s/^/add allcn /")
  20.         EOF
  21.        
  22.         iptables -N ALLCNRULE
  23.         iptables -I INPUT -p tcp -j ALLCNRULE
  24.         iptables -A ALLCNRULE -s 127.0.0.0/8 -j RETURN
  25.         iptables -A ALLCNRULE -s 169.254.0.0/16 -j RETURN
  26.         iptables -A ALLCNRULE -s 224.0.0.0/4 -j RETURN
  27.         iptables -A ALLCNRULE -s 255.255.255.255 -j RETURN
  28.         #可在此增加你的公网网段,避免调试ipset时出现自己无法访问的情况
  29.         iptables -A ALLCNRULE -m set --match-set allcn src -j RETURN
  30.         iptables -A ALLCNRULE -p tcp -j DROP
  31. }

  32. if [ "$mmode" == "stop" ] ;then
  33.         flush_r
  34.         exit 0
  35. fi

  36. flush_r
  37. sleep 1
  38. mstart
复制代码

4.屏蔽国外IP。
  1. sudo bash allcn.sh
复制代码

5.解除屏蔽国外IP。
  1. sudo bash allcn.sh stop
复制代码

6.查看iptables,检查屏蔽或解除屏蔽的结果。
  1. iptables -L
复制代码
博主论坛 bzlt.net
打败大狗熊 发表于 2023-3-3 15:40:11
建议把里面的地址改成github的,避免失效哈哈
 楼主| crll 发表于 2023-3-3 16:27:47
打败大狗熊 发表于 2023-3-3 15:40
建议把里面的地址改成github的,避免失效哈哈

  好的~
博主论坛 bzlt.net
叶开 发表于 2023-3-3 18:25:00
我都是宝塔面板  屏蔽国外 IP, 或者是CDN服务商那边设置禁止国外IP。

我偷懒!哈哈
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于本站联系我们FAQ友情链接免责声明生存法则

Build with for "make" Copyright © 2020-2022. Powered by Discuz! GMT+8, 2024-3-29 16:58

快速回复 返回顶部 返回列表