打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

使用nftables屏蔽国外流量

来自md5.pw
Liam留言 | 贡献2026年2月19日 (四) 08:13的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

1. 准备

确保 nftables 与 python3 已经安装

nft --version
python3 --version

若没有安装, 以Debian13为例

apt update
apt install nftables python3 unzip
systemctl enable nftables

拷贝原有的nftables配置, 到一个好找的目录里, 以 /main/nftables 为例, 再对原路径进行链接

cp /etc/nftables.conf /main/nftables/
ln -f /main/nftables/nftables.conf /etc/nftables.conf

并准备好编辑 /main/nftables/nftables.conf

2. 获取国内IP段

先获取并解压项目文件

mkdir /main/nftables/geoip
cd /main/nftables/geoip
wget https://github.com/pvxe/nftables-geoip/archive/refs/heads/master.zip
unzip master.zip
rm master.zip

执行脚本

mv nftables-geoip-master/* ./
chmod u+x nft_geoip.py
./nft_geoip.py --file-location location.csv --download
./nft_geoip.py --download -c cn

3. 配置nftables

在合适位置添加或与其它的table进行合并, 这里比如只有访问tcp 46端口的国内IP, 才能放行

table inet geoip_filter {

    include "/main/nftables/geoip/geoip-def-all.nft";
    include "/main/nftables/geoip/geoip-ipv4-interesting.nft";
	include "/main/nftables/geoip/geoip-ipv6-interesting.nft";

    chain geoip_mark_input {
		type filter hook                        prerouting priority -10;
        policy accept;


		meta mark set                           ip saddr map @geoip4;
		meta mark set                           ip6 saddr map @geoip6;
	}

    chain input {
        type filter hook input priority 0;
        policy drop;

        # 放行本地/已建立/相关的连接
        iif lo accept;
        ct state established,related        accept;
        
        tcp dport { 46 }    meta mark $CN   ct state new    accept;
    }
}

4. 完成配置

保存后并重启

systemctl restart nftables.service