使用nftables屏蔽國外流量
出自md5.pw
更多語言
更多操作
這是此頁面最近一次修訂;沒有已批准修訂。
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 cn3. 配置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