Files
3XUI/防火墙.MD
T
2026-09-06 20:02:31 +08:00

29 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#### Debian12 封禁中国大陆 IP 访问 4000050000 端口
说明:封禁大陆 IP 段,有两种主流方案:**iptables + ipset(性能好,推荐)**、直接 iptables 批量添加规则(IP 段多,规则膨胀,不推荐)。端口范围:TCP+UDP 4000050000。
#### 方案一:ipset + iptables(推荐,Debian12
1. 安装依赖
~~~
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
~~~