惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
U
Unit 42
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
腾讯CDC
I
InfoQ
GbyAI
GbyAI
博客园_首页

Dallas Lu

一些没有意义的事情 博客程序的一次大重构 OpenWRT 使用 udp2raw 对抗 WireGuard 阻断 OpenWRT 使用 udp2raw 对抗 WireGuard 阻断 博客程序架构的思考与展望 如何证明你是原创作者 如何证明你是原创作者 Nginx 泛域名配置的隐患与对策 Nginx 泛域名配置的隐患与对策 WISeID S/MIME 证书 WISeID S/MIME 证书 V2EX 刑满释放记 V2EX 刑满释放记 使用 Radicale 在 Ubuntu 24.04 中搭建 vCards CardDav 服务 使用 Radicale 在 Ubuntu 24.04 中搭建 vCards CardDav 服务 邮件服务的域名成功从 SURBL 黑名单移除 邮件服务的域名成功从 SURBL 黑名单移除 网站多语言的设计细节 网站多语言的设计细节 网站评论系统的目前进展和展望 网站评论系统的目前进展和展望 在公网使用 iptables 转发端口时保留客户端 IP 在公网使用 iptables 转发端口时保留客户端 IP 邮件投递平台 Postal 的使用经验 邮件投递平台 Postal 的使用经验 自建 Postal 完美替代 SendGrid 自建 Postal 完美替代 SendGrid 互联网在崩塌吗,然后呢 互联网在崩塌吗,然后呢 在 SvelteKit 应用中使用 JSON-LD
在 OpenWRT 23 中使用 nftset 配置 Shadowsocks 规则
达拉斯・卢 · 2024-03-18 · via Dallas Lu

在 OpenWRT 23 中,默认使用的防火墙是 fw4;nftables 对应的的是 nftset。本文介绍使用 dnsmasq-full/nftset/nftables 为 shadowsocks redir 创建基于 gfwlist 的规则。

nftables

编辑 /etc/nftables.d/gfwlist.nft,设置 nftset 的初始配置,加入了 Telegram 的 IP 段,以及转发规则1

set gfwlist {
	type ipv4_addr
	flags interval
	elements = {
		# telegram start
		91.105.192.0/23,
		91.108.4.0/22,
		91.108.8.0/22,
		91.108.12.0/22,
		91.108.16.0/22,
		91.108.20.0/22,
		91.108.56.0/22,
		149.154.160.0/20,
		185.76.151.0/24,
		#telegram end
	}
}

chain gfwlist-redirect {
	type nat hook prerouting priority 0; policy accept;
	ip daddr @gfwlist ip protocol tcp redirect to :1100
}

以上配置假设 ss-redir 监听的端口是 1100。重启防火墙:

service firewall restart

手动配置

临时将 IP 加入 gfwlist 或从 gfwlist 移出2

nft add element inet fw4 gfwlist { 1.2.3.4 }
nft delete element inet fw4 gfwlist { 1.2.3.4 }

Dnsmasq

切换为 dnsmasq-full

opkg remove dnsmasq
opkg install dnsmasq-full

service dnsmasq restart

创建 dnsmasq 配置文件

默认的配置目录是 /tmp/dnsmasq.d,所以我们最好将配置文件放在另外一个位置:

mkdir -p /root/gfwlist/nftset

并在启动时,自动复制配置文件:

cp -f /root/gfwlist/nftset/*.conf /tmp/dnsmasq.d

手动配置

如果我们有一个手动维护的配置文件 /root/gfwlist/nftset/dnsmasq_gfwlist_nftset_custom.conf

server=/githubusercontent.com/127.0.0.1#5353
nftset=/githubusercontent.com/4#inet#fw4#gfwlist
server=/github.com/127.0.0.1#5353
nftset=/github.com/4#inet#fw4#gfwlist

创建部署脚本 deploy-dnsmasq-conf.sh:

cp -f /root/gfwlist/nftset/*.conf /tmp/dnsmasq.d && service dnsmasq restart

gfwlist

将 gfwlist 转化为 dnsmasq 配置文件的脚本 gfwlist2dnsmasq.sh 只支持 ipset,需要进行一些编辑:

- ipset=/\1/'$IPSET_NAME'#g' > $CONF_TMP_FILE
+ nftset=/\1/4\#inet\#fw4\#'$IPSET_NAME'#g' > $CONF_TMP_FILE

将其写入到脚本文件 /root/gfwlist/nftset/gfwlist2dnsmasq-nftset.sh 中。另建立 update-gfwlist-dnsmasq-conf.sh

sh /root/gfwlist/nftset/gfwlist2dnsmasq-nftset.sh -s gfwlist -o /root/gfwlist/nftset/dnsmasq_gfwlist_nftset.conf && /root/gfwlist/nftset/deploy-dnsmasq-conf.sh

编辑 /etc/rc.local,加入:

sh /root/gfwlist/nftset/update-gfwlist-dnsmasq-conf.sh

添加 crontab 任务:

0 0 1 * * ?     sh /root/gfwlist/nftset/update-gfwlist-dnsmasq-conf.sh

结语

网上的文章多以翻墙为例,本文内容也选择了这一场景。实际上,另一个有用的场合是使用住宅 IP 访问 ChatGPT 等服务。


  1. 99010. dnsmasq-full + nftset + nftables透明代理. 恩山无线论坛. 2023. ↩

  2. 10.5. 使用 nftables 命令中的集合. Red Hat. ↩