傻瓜级两种 OpenWrt 能简单实现无污染 DNS 的方式

2023-02-25 05:52:00 +08:00
 strp

第一种是通过自带的 DNSMASQ:

  1. 网络
  2. DHCP 和 DNS
  3. DNS 转发
  4. 填入 101.6.6.6**#**5353 注意是#而不是 @
  5. 保存并应用,然后就可以了,默认 OpenWrt 都会劫持局域网内所有 UDP/TCP 53 端口,所以你的客户端不需要额外做些什么。

第二种是从 OpenWrt 抄的防火墙规则,新的 OpenWrt 应该默认都有不过是全局劫持而且是转发到本地的 DNSMASQ:

只需要稍加修改只劫持指定的 IP 就可以正常使用,你可以按需修改。 你要先删除自带的两条全局劫持 53 端口的规则。 这样做的作用是劫持客户端到 8.8.8.8/8.8.4.4/1.1.1.1/1.0.0.1 这些 IP 地址的 53 端口到 101.6.6.6#5353 所以你要去手动设置 DHCP Client 的 DNS 为以上 IP ,你也可以劫持其它 IP ,比如 8.9.6.4,但是这样就会太 Confuse 了,不推荐,你也可以让 OpenWrt 设置 DHCP Client 获取到的 DNS 为一下 IP ,下面会讲到。

iptables -t nat -A PREROUTING -p udp -d 8.8.8.8 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p udp -d 8.8.4.4 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p udp -d 1.1.1.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p udp -d 1.0.0.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp -d 8.8.8.8 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp -d 8.8.4.4 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp -d 1.1.1.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp -d 1.0.0.1--dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353

以上内容加到:

  1. 网络
  2. 防火墙
  3. 自定义规则

如果你想把 TCP 的 53 也劫持了,那你可以用下面的:

iptables -t nat -A PREROUTING -p udp -d 8.8.8.8 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p udp -d 8.8.4.4 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p udp -d 1.1.1.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p udp -d 1.0.0.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp -d 8.8.8.8 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp -d 8.8.4.4 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp -d 1.1.1.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp -d 1.0.0.1--dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 53
iptables -t nat -A PREROUTING -p tcp -d 8.8.8.8 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p tcp -d 8.8.4.4 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p tcp -d 1.1.1.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
iptables -t nat -A PREROUTING -p tcp -d 1.0.0.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p tcp -d 8.8.8.8 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p tcp -d 8.8.4.4 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p tcp -d 1.1.1.1 --dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 5353
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p tcp -d 1.0.0.1--dport 53 -j REDIRECT --to-destination 101.6.6.6 --to-ports 53

让 DHCP Client 自动获取不同的 DNS 服务器:

  1. Network
  2. LAN Edit
  3. DHCP Section
  4. Advanced Options
  5. DHCP Options
  6. 填入 6,8.8.8.8,8.4.4.4
  7. 保存并应用

弊端:

你国内访问的所有带 CDN 的网站估计都会获取到教育网内的服务器,高峰期可能会导致访问不稳定,并且有人反馈虎牙等直播平台只允许教育网网内用户访问教育网网内服务器资源,所以这个方法不能说天衣无缝,顶顶用还是可以的。如果你实在没有办法在局域网内搭建另一个比如 Overture 这样具有分流功能的 DNS Server ,而且也不会写 init.d/Overture 配置文件也不想折腾其他东西,虽然说不会写 init.d 可以 screen这是一个不错的解决方案

3070 次点击
所在节点    OpenWrt
3 条回复
Cooky
2023-02-25 18:00:55 +08:00
打破零回复,但是只是 mark
wangweitung
2023-02-25 23:03:15 +08:00
折腾过 adgurdhome ,用来做 dns 过滤,但总会有各种各样的问题。
但最后还是放弃了。用回默认。。。
strp
322 天前
An alternative way forwarding local port 5335 to 198.18.0.1:53, replace 198.18.0.1 as your destination and replace 198.18.0.2 as your gateway IP (in this case your OpenWrt)
5335 is used by default in ShadowsocksR Plus+, only way to route the UDP DNS requests without force using TCP or send to proxy.

iptables -t nat -A PREROUTING -p tcp --dport 5335 -j DNAT --to-destination 198.18.0.1:53
iptables -t nat -A POSTROUTING -p tcp -d 198.18.0.1 --dport 53 -j SNAT --to-source 198.18.0.2
iptables -t nat -A PREROUTING -p udp --dport 5335 -j DNAT --to-destination 198.18.0.1:53
iptables -t nat -A POSTROUTING -p udp -d 198.18.0.1 --dport 53 -j SNAT --to-source 198.18.0.2

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/919044

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX