Catfight360 Catfight360
Home
  • vuepress (opens new window)
  • vuepress-theme-vdoing (opens new window)
  • U2SB (opens new window)
  • MonoLogueChi (opens new window)
GitHub (opens new window)

羽先生

我,肥宅,程序猿。
Home
  • vuepress (opens new window)
  • vuepress-theme-vdoing (opens new window)
  • U2SB (opens new window)
  • MonoLogueChi (opens new window)
GitHub (opens new window)
  • OpenWrt DNS 网络规划

    • dnsmasq 设置
      • 其他设置
    • smartdns 设置
      • 上游服务器设置
    • 防火墙设置
    羽先生
    2023-04-25
    随笔
    目录

    OpenWrt DNS 网络规划

    警告

    本文所述内容仅在 21.02 版本有效,22.03 版本的 OpenWrt 防火墙已切换至 nftables

    目前个人在使用中觉得体验比较优秀的 DNS 方案是 dnsmasq + smartdns

    # dnsmasq 设置

    提示

    22.03 版本需要额外配置才能让 dnsmasq 获取到 /etc/dnsmasq.d 文件夹下的配置

    首先不考虑转发 dns 到 smartdns,编辑 /etc/dnsmasq.conf 文件,添加如下行:

    conf-dir=/etc/dnsmasq.d
    
    1

    然后新建文件夹 /etc/dnsmasq.d ,在文件夹中添加文件 local.conf ,编辑 local.conf ,添加如下行:

    address=/router.xxxx.cn/192.168.1.1
    
    1

    按照自己的需求来添加所有的局域网使用的域名。注意每次编辑之后都要重启 dnsmasq 才能使改动生效。

    然后在计划任务中添加一个定时下载 anti-ad 规则并重启 dnsmasq 的任务:

    0 3 * * * wget -O /etc/dnsmasq.d/anti-ad.conf https://anti-ad.net/anti-ad-for-dnsmasq.conf && /etc/init.d/dnsmasq restart >/dev/null 2>&1
    
    1

    以上是每天 3 点执行。

    # 其他设置

    注意

    不建议修改 “监听接口” 设置,设置此项将导致 dnsmasq 仅在对应的接口监听 53 端口,这会影响本机 docker 的 dns 解析,和重新获取到 ipv6-pd 前缀后的 windows 系统 dns 解析。

    排除接口设置里添加所有的 wan 口,包括 iptv 和 wan6 接口。

    # smartdns 设置

    使用 smartdns 的主要原因是为了他的双 dns 分组功能。

    主 dns 分组设置监听 6053,开启 tcp 服务器,ipv6 服务器,双栈 IP 优选,域名预加载,缓存过期服务。重定向选择作为 dnsmasq 上游服务器,缓存大小 10240

    第二 dns 服务器设置监听 5335,开启 tcp 服务器,服务器组填写 overseas,跳过测速,跳过双栈优选,停用 IPV6 地址解析。

    # 上游服务器设置

    推荐采用 DoH 方式设置上游服务器:

    config server
        option enabled '1'
        option server_group 'cn'
        option blacklist_ip '0'
        option name '阿里 DoH'
        option ip 'https://223.5.5.5/dns-query'
        option type 'https'
    
    config server
        option enabled '1'
        option server_group 'cn'
        option blacklist_ip '0'
        option ip 'https://223.6.6.6/dns-query'
        option type 'https'
        option name '阿里 DoH'
    
    config server
        option enabled '1'
        option server_group 'cn'
        option blacklist_ip '0'
        option ip 'https://2400:3200::1/dns-query'
        option type 'https'
        option name '阿里 DoH'
    
    config server
        option enabled '1'
        option server_group 'cn'
        option blacklist_ip '0'
        option ip 'https://2400:3200:baba::1/dns-query'
        option type 'https'
        option name '阿里 DoH'
    
    config server
        option enabled '1'
        option blacklist_ip '0'
        option server_group 'overseas'
        option ip 'https://8.8.8.8/dns-query'
        option type 'https'
        option name '谷歌 DoH'
    
    config server
        option enabled '1'
        option blacklist_ip '0'
        option server_group 'overseas'
        option ip 'https://8.8.4.4/dns-query'
        option type 'https'
        option name '谷歌 DoH'
    
    config server
        option enabled '1'
        option blacklist_ip '0'
        option server_group 'overseas'
        option ip 'https://1.1.1.1/dns-query'
        option type 'https'
        option name 'CF DoH'
    
    config server
        option enabled '1'
        option blacklist_ip '0'
        option server_group 'overseas'
        option ip 'https://1.0.0.1/dns-query'
        option type 'https'
        option name 'CF DoH'
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    # 防火墙设置

    提示

    也可以用同样的方法劫持 123 端口来保证局域网内所有设备的授时

    添加自定义规则:

    iptables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53
    iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53
    
    ip6tables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53
    ip6tables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53
    
    1
    2
    3
    4
    5
    #OpenWrt
    Theme by Vdoing | Copyright © 2023-2023 羽先生 | CC BY-NC-SA 4.0
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式