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

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

  •  1
     
  •   efsg 2024-06-05 19:35:36 +08:00 2888 次点击
    这是一个创建于 564 天前的主题,其中的信息可能已经有所发展或是发生改变。

    教程和经验总结:

    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 条附言    2024-06-06 23:28:34 +08:00
    如果无法使用游戏加速器:
    OpenClash - 插件设置 - 模式设置,取消勾选 UDP 流量转发
    第 2 条附言    2024-06-08 15:52:06 +08:00
    更正自定义防火墙规则:

    #劫持内网所有 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 条附言    2024-06-08 15:56:16 +08:00
    补充:如果使用上面的 iptables 防火墙规则,需要手动取消 ADG 插件的 DNS 重定向

    AdGuardHome - 6060 重定向 - 无
    第 4 条附言    2024-06-08 22:24:46 +08:00
    继续补充:5333 是 AdGuardHome 的 DNS 监听端口,两个端口必须一致
    第 5 条附言    2024-06-10 10:58:40 +08:00
    继续补充:
    查询日志文件 /usr/bin/AdGuardHome/data/querylog.json
    会把路由器闪存写满,解决方法:ADG - 常规设置 - 日志配置,取消勾选启用日志,或者将查询日志保留时间改为 6 小时,保存
    第 6 条附言    2024-07-26 15:46:37 +08:00
    新帖子,修改了配置,应该是没有其它问题了 t/1060338
    3 条回复    2024-07-03 20:55:42 +08:00
    sxiaojian
        1
    sxiaojian  
       2024-06-29 10:13:00 +08:00
    看到代码就劝退了,出了问题根本不知道怎么调试
    efsg
        2
    efsg  
    OP
       2024-07-03 09:59:32 +08:00 via Android
    @sxiaojian 这套配置我觉得还是挺简单的,自己有特殊需求也可以花点时间改一下 ipset 脚本和 mosdns 的配置,按我这套流程配置下来应该都是能用的,我把能踩的坑都尽可能帮你们踩过了
    Debug1998
        3
    Debug1998  
       2024-07-03 20:55:42 +08:00
    请问您使用的是旁路由吗
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2740 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 13:13 PVG 21:13 LAX 05:13 JFK 08:13
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86