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

24次阅读

共计 2489 个字符,预计需要花费 7 分钟才能阅读完成。

教程和经验总结:

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 用的。
OpenWrt 套娃组合 Clash+ADG+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
OpenWrt 套娃组合 Clash+ADG+MOSDNS 代理OpenWrt 套娃组合 Clash+ADG+MOSDNS 代理
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

正文完
 0