更新 防火墙.MD

This commit is contained in:
2026-09-06 20:01:54 +08:00
parent e4a47e8a21
commit 5b104e1a64
+20
View File
@@ -7,3 +7,23 @@
apt update
apt install ipset curl -y
~~~
2. 创建 ipset 集合存储中国大陆 IP 段, 创建集合,hash:net 存放网段
~~~
ipset create cn_ip hash:net family inet hashsize 1024 maxelem 65536
~~~
3. 获取中国大陆 IP 网段并导入 ipset
~~~
##### 清空旧集合
ipset flush cn_ip
#### 下载cn网段列表写入ipset
curl -s https://ftp.apnic.net/stats/apnic/delegated-apnic-latest | awk -F'|' '$2=="CN"&&$3=="ipv4"{printf("add cn_ip %s/%d\n",$4,32-log($5)/log(2))}' | ipset restore
~~~
⚠️网络不好时 curl 会失败,可以把文件下载到本地再导入。
4. iptables 规则:拒绝 cn_ip 集合访问本机 4000050000 TCP/UDP
~~~
#### TCP
iptables -A INPUT -m set --match-set cn_ip src -p tcp --dport 40000:50000 -j DROP
##### UDP
iptables -A INPUT -m set --match-set cn_ip src -p udp --dport 40000:50000 -j DROP
~~~