V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
OpenWrt 是一个专门面向嵌入式设备的 Linux 发行版。你可以将 OpenWrt 支持的型号的嵌入式设备,比如各种路由器上的系统,换成一个有更多可能性可以折腾的 Linux 系统。
OpenWrt 官方网站
efsg
V2EX  ›  OpenWrt

OpenWrt 套娃组合 Clash+ADG+MOSDNS 代理

  •  
  •   efsg · 21 天前 · 655 次点击

    教程和经验总结:

    3 个插件,luci-app-openclash,luci-app-mosdns,luci-app-adguardhome

    插件设置 - 模式设置 - 运行模式: 切换到 Fake-IP (增强)模式

    插件设置 - DNS 设置 - 本地 DNS 劫持 选择 禁用

    插件设置 - 流量控制 - 绕过中国大陆 IP 取消勾选

    插件设置 - 流量控制 - 仅允许内网 开启

    插件设置 - IPv6 设置 这页的选项全都关闭就行了

    覆写设置 - 常规设置 这里都不用改,只需要记住 DNS 监听,后面配置 mosdns 要用

    覆写设置 - DNS 设置 - 自定义上游 DNS 服务器 勾选

    覆写设置 - DNS 设置 - 追加上游 DNS 勾选

    覆写设置 - DNS 设置 - 追加默认 DNS 勾选

    覆写设置 - DNS 设置 - Fake-IP 持久化 勾选

    覆写设置 - DNS 设置 页面下方 NameServer ,FallBack ,Default-NameServer 里的 DNS 服务器全都取消勾选,我们只用运营商提供的 DNS 服务器就够了,一般运营商 DNS 都是最快的,也是 CDN 最优化的。

    插件设置 - GEO 数据库订阅 把 GeoIP Dat 和 GeoSite 这两个库的自动更新打开,都选 Loyalsoldier 的版本,这个是用来给 mosdns 用的。 插件设置 - 开发者选项,添加防火墙规则

    # This script is called by /etc/init.d/openclash
    # Add your custom firewall rules here, they will be added after the end of the OpenClash iptables rules
    
    en_mode=$(uci -q get openclash.config.en_mode)
    proxy_port=$(uci -q get openclash.config.proxy_port)
    
    if [ "$en_mode" == "fake-ip" ]; then
    	LOG_OUT "limit route to only fake ips with proxy port $proxy_port"
    	iptables -t nat -D openclash -p tcp -j REDIRECT --to-ports $proxy_port
    	sleep 1
    
    	LOG_OUT "update telegram ipset"
    	/etc/mosdns/rule/geoip2ipset.sh /etc/openclash/GeoIP.dat telegram
    	iptables -t nat -A openclash -m set --match-set telegram dst -p tcp -j REDIRECT --to-ports $proxy_port
    	sleep 1
    
    	LOG_OUT "update netflix ipset"
    	/etc/mosdns/rule/geoip2ipset.sh /etc/openclash/GeoIP.dat netflix
    	iptables -t nat -A openclash -m set --match-set netflix dst -p tcp -j REDIRECT --to-ports $proxy_port
    	sleep 1
    fi
    
    #  停止 AdguradHome
    LOG_OUT "stop adguardhome"
    /etc/init.d/AdGuardHome stop
    sleep 1
    
    #  开启 AdguradHome
    LOG_OUT "start adguardhome"
    /etc/init.d/AdGuardHome start
    sleep 1
    
    # 停止 Mosdns
    LOG_OUT "stop mosdns"
    /etc/init.d/mosdns stop
    sleep 1
    
    # 开启 Mosdns
    LOG_OUT "start mosdns"
    /etc/init.d/mosdns start
    sleep 1
    
    # 重载 Mosdns
    LOG_OUT "reload mosdns"
    /etc/init.d/mosdns reload
    sleep 1
    
    exit 0
    

    SSH 登录路由器,添加脚本

    /etc/mosdns/rule/geoip2ipset.sh
    
    #!/bin/bash
    
    geoipfile="$1"
    tag="$2"
    tmpdir="/tmp/v2dat"
    FW4=$(command -v fw4)
    
    cd $(cd $(dirname $BASH_SOURCE) && pwd)
    
    mkdir -p "$tmpdir"
    filename=$(basename -- "$geoipfile")
    filename="${filename%.*}"
    filename="$tmpdir/${filename}_$tag.txt"
    
    if [ "$tag" == "telegram" ]; then
        wget -4 --timeout 5 -O "$filename" 'https://mirror.ghproxy.com/https://raw.githubusercontent.com/fernvenue/telegram-cidr-list/master/CIDR.txt'
        if [ "$?" != "0" ]; then
             /usr/bin/v2dat unpack geoip -o "$tmpdir" -f "$tag" "$geoipfile"
        fi
    else
        /usr/bin/v2dat unpack geoip -o "$tmpdir" -f "$tag" "$geoipfile"
    fi
    
    if test -f "$filename"; then
        if [ -n "$FW4" ]; then
            nft add set inet fw4 "$tag" { type ipv4_addr\; flags interval\;  auto-merge\; }
            nft add set inet fw4 "${tag}6" { type ipv6_addr\; flags interval\;  auto-merge\; }
            nft flush set inet fw4 "$tag"
            nft flush set inet fw4 "${tag}6"
        fi
        ipset create "$tag" hash:net -!
        ipset create "${tag}6" hash:net family inet6 -!
        ipset flush "$tag"
        ipset flush "${tag}6"
        while read p; do
            if ! grep -q ":" <<< "$p"; then
                if [ -n "$FW4" ]; then
                    nft add element inet fw4 "$tag" { "$p" }
                fi
                ipset add "$tag" "$p"
            else
                if [ -n "$FW4" ]; then
                    nft add element inet fw4 "${tag}6" { "$p" }
                fi
                ipset add "${tag}6" "$p"
            fi
        done <"$filename"
    else
        echo "$filename missing."
    fi
    
    rm -rf "$tmpdir"
    

    添加脚本执行权限

    chmod a+x /etc/mosdns/rule/geoip2ipset.sh
    

    MosDNS - 配置文件 - 自定义

    DNS 转发 - 取消勾选

    GeoData 数据导出 添加标签:

    GeoSite: cn, apple-cn, category-games@cn, geolocation-!cn, GeoIP: cn MosDNS 配置文件

    log:
      level: info
      file: "/tmp/mosdns.log"
    
    include: []
    
    plugins:
      # 国内域名
      - tag: geosite_cn
        type: domain_set
        args:
          exps:
            - "lan"
            - "local"
            - "arpa"
          files:
            - "/etc/mosdns/rule/whitelist.txt"
            - "/var/mosdns/geosite_cn.txt"
            - "/var/mosdns/geosite_apple-cn.txt"
            - "/var/mosdns/[email protected]"
    
      # 国内 IP
      - tag: geoip_cn
        type: ip_set
        args:
          files:
            - "/var/mosdns/geoip_cn.txt"
    
      # 国外域名
      - tag: geosite_no_cn
        type: domain_set
        args:
          files:
            - "/etc/mosdns/rule/greylist.txt"
            - "/var/mosdns/geosite_geolocation-!cn.txt"
    
      # hosts
      - tag: hosts
        type: hosts
        args:
          files:
            - "/etc/mosdns/rule/hosts.txt"
    
      # 国内解析
      - tag: local_sequence
        type: sequence
        args:
          - exec: forward 127.0.0.1
          - matches:
            - has_resp
            - resp_ip 192.168.1.0/24 # LAN IPCIDR 改成你路由器的网段
            exec: ttl 1800-0
    
      # 国外解析
      - tag: remote_sequence
        type: sequence
        args:
          - matches:
            - qtype 28
            exec: reject 0
          - exec: forward 127.0.0.1:7874
          - exec: ttl 1800-0
    
      # 有响应终止返回
      - tag: has_resp_sequence
        type: sequence
        args:
          - matches: has_resp
            exec: accept
    
      # fallback 用本地服务器 sequence
      # 返回非国内 ip 则 drop_resp
      - tag: fallback_local
        type: sequence
        args:
          - exec: $local_sequence
          - matches: "!resp_ip $geoip_cn"
            exec: drop_resp
    
      # fallback 用远程服务器 sequence
      - tag: fallback
        type: fallback
        args:
          primary: fallback_local
          secondary: remote_sequence
          threshold: 200
          always_standby: true
    
      # 主要的运行逻辑插件
      # sequence 插件中调用的插件 tag 必须在 sequence 前定义,
      # 否则 sequence 找不到对应插件。
      - tag: main_sequence
        type: sequence
        args:
    
          # hosts
          - exec: $hosts
          - exec: jump has_resp_sequence
    
          # drop https query type
          - matches:
            - qtype 65
            exec: reject 3
    
          # handle local ptr
          - matches:
            - qtype 12
            exec: $local_sequence
          - exec: jump has_resp_sequence
    
          - matches:
            - qname $geosite_cn
            exec: $local_sequence
          - exec: jump has_resp_sequence
    
          - matches:
            - qname $geosite_no_cn
            exec: $remote_sequence
          - exec: jump has_resp_sequence
    
          - exec: $fallback
    
      - tag: udp_server
        type: udp_server
        args:
          entry: main_sequence
          listen: ":5335"
    

    AdGuardHome - 6060 重定向 - 重定向 53 端口到 AdGuardHome

    更新并且启动 AdGuardHome ,在 Web 管理页面上,设置 - DNS 设置中,上游 DNS 服务器内只填写一个 mosdns 的地址 127.0.0.1:5335 ,私人反向 DNS 服务器写上 127.0.0.1 。DNS 缓存配置里面,缓存大小看你内存大小填写,乐观缓存勾上。

    对于不想走代理的设备,可以在设置 - 客户端设置中添加,并且把上游 DNS 服务器设置成 127.0.0.1 。

    防火墙 - 自定义规则,添加规则,重启防火墙

    iptables -t nat -A PREROUTING -d 198.18.0.0/16 -p tcp -j REDIRECT --to-ports 7892

    解决谷歌 Play 商店不能更新:

    ADG - 过滤器 - 添加 DNS 重写,services.googleapis.cn,198.18.1.50

    参考教程:

    https://blog.openwrtcn.eu.org/dnsling-wu-ran/

    https://songchenwen.com/tproxy-split-by-dns

    第 1 条附言  ·  20 天前
    如果无法使用游戏加速器:
    OpenClash - 插件设置 - 模式设置,取消勾选 UDP 流量转发
    第 2 条附言  ·  18 天前
    更正自定义防火墙规则:

    #劫持内网所有 DNS 到 ADG 端口,防止国产机内置 114DNS 导致异常
    iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5333
    iptables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 5333
    ip6tables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5333
    ip6tables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 5333

    #OpenClash 内置,无需添加
    #iptables -t nat -A PREROUTING -d 198.18.0.0/16 -p tcp -j REDIRECT --to-ports 7892
    第 3 条附言  ·  18 天前
    补充:如果使用上面的 iptables 防火墙规则,需要手动取消 ADG 插件的 DNS 重定向

    AdGuardHome - 6060 重定向 - 无
    第 4 条附言  ·  18 天前
    继续补充:5333 是 AdGuardHome 的 DNS 监听端口,两个端口必须一致
    第 5 条附言  ·  16 天前
    继续补充:
    查询日志文件 /usr/bin/AdGuardHome/data/querylog.json
    会把路由器闪存写满,解决方法:ADG - 常规设置 - 日志配置,取消勾选启用日志,或者将查询日志保留时间改为 6 小时,保存
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3225 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 00:45 · PVG 08:45 · LAX 17:45 · JFK 20:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.