方永、南天紫雲

道亦有道

Linux基于域名的策略路由
2025年12月01日

最近在某台服务器上配置了基于域名和nftables的策略路由,记录一下配置过程。

用到的工具是 phantun, wireguard, nftables, chinadns-ng

这是

的姊妹篇,随着工具和技术的迭代,这是目前最新的一篇。

chinadns-ng

首先配置dns 解析服务器,可直接修改 /etc/resolv.conf 添加 nameserver 127.0.0.1 并注释掉其它的 nameserver

chinadns-ng 的配置文件:

bind-addr 0.0.0.0
bind-port 53

china-dns udp://100.100.2.136,udp://100.100.2.138
trust-dns udp://10.0.0.1

gfwlist-file gfwlist.txt
default-tag chn

add-taggfw-ip inet@fw@gfwip

cache 4096
cache-stale 86400
cache-refresh 20

nftables

table inet fw {
  set gfwip {
    typeof ip daddr
    flags interval
    auto-merge

    elements = { 10.0.0.1/24 }
  }

  chain gfwip_mark {
    type route hook output priority filter; policy accept;
    ip daddr @gfwip counter mark set 0x15;
  }
}

wireguard

[Interface]
...
Table = 21
PostUp = ip rule add fwmark 0x15 table 21
PreDown = ip rule del fwmark 0x15 table 21