#!/bin/sh checkArchitecture(){ # https://stackoverflow.com/questions/48678152/how-to-detect-386-amd64-arm-or-arm64-os-architecture-via-shell-bash case $(uname -m) in i386) osArchitecture="386" ;; i686) osArchitecture="386" ;; x86_64) osArchitecture="amd64" ;; arm) dpkg --print-architecture | grep -q "arm64" && osArchitecture="arm64" || osArchitecture="arm" ;; aarch64) dpkg --print-architecture | grep -q "arm64" && osArchitecture="arm64" || osArchitecture="arm" ;; * ) osArchitecture="arm" ;; esac } getLinuxOSRelease(){ checkArchitecture # NAME="OpenWrt" # VERSION="SNAPSHOT" # ID="openwrt" if [ -f /etc/os-release ]; then . /etc/os-release osInfo=$NAME osID=$ID osReleaseVersionNo=$VERSION_ID fi echo "OS: ${osInfo}, ${ID}, ${VERSION_ID} CPU: $osArchitecture" } getGithubLatestReleaseVersion(){ # https://github.com/p4gefau1t/trojan-go/issues/63 wget --no-check-certificate -qO- https://api.github.com/repos/$1/tags | grep 'name' | cut -d\" -f4 | head -1 | cut -b 1- } mosdnsDownloadPath="/tmp" mosdnsLogFilePath="/tmp/mosdns.log" mosdnsEtcPath="/etc/mosdns" getIPDKdownloadFilename(){ # mosdnsIPK_array=($(wget -qO- https://op.supes.top/packages/x86_64/ | grep -E "mosdns|v2ray" | awk -F' "${mosdnsEtcPath}/cus_config.yaml" <<-EOF log: level: info file: "${mosdnsLogFilePath}" data_providers: - tag: geosite file: ./geosite.dat auto_reload: true - tag: geoip file: ./geoip.dat auto_reload: true plugins: # 缓存 - tag: cache type: cache args: size: 2048 lazy_cache_ttl: 3600 cache_everything: true # hosts map # - tag: map_hosts # type: hosts # args: # hosts: # - 'google.com 0.0.0.0' # - 'api.miwifi.com 127.0.0.1' # - 'www.baidu.com 0.0.0.0' # 转发至本地服务器的插件 - tag: forward_local type: fast_forward args: upstream: - addr: "udp://223.5.5.5" trusted: true - addr: "udp://119.29.29.29" trusted: false # 转发至远程服务器的插件 - tag: forward_remote type: fast_forward args: upstream: ${addNewDNSServerIPText} ${addNewDNSServerDomainText} - addr: "udp://208.67.222.222" trusted: true - addr: "udp://1.0.0.1" trusted: true - addr: "https://dns.cloudflare.com/dns-query" idle_timeout: 400 trusted: true - addr: "udp://5.2.75.231" idle_timeout: 400 trusted: true - addr: "udp://185.121.177.177" idle_timeout: 400 trusted: true - addr: "udp://94.130.180.225" idle_timeout: 400 trusted: true - addr: "udp://78.47.64.161" idle_timeout: 400 trusted: true - addr: "udp://51.38.83.141" - addr: "udp://176.9.93.198" - addr: "udp://176.9.1.117" - addr: "udp://88.198.92.222" # 匹配本地域名的插件 - tag: query_is_local_domain type: query_matcher args: domain: - 'provider:geosite:cn' - tag: query_is_gfw_domain type: query_matcher args: domain: - 'provider:geosite:gfw' # 匹配非本地域名的插件 - tag: query_is_non_local_domain type: query_matcher args: domain: - 'provider:geosite:geolocation-!cn' # 匹配广告域名的插件 - tag: query_is_ad_domain type: query_matcher args: domain: - 'provider:geosite:category-ads-all' # 匹配本地 IP 的插件 - tag: response_has_local_ip type: response_matcher args: ip: - 'provider:geoip:cn' # 主要的运行逻辑插件 # sequence 插件中调用的插件 tag 必须在 sequence 前定义, # 否则 sequence 找不到对应插件。 - tag: main_sequence type: sequence args: exec: # hosts map # - map_hosts # 缓存 - cache # 屏蔽广告域名 ad block - if: query_is_ad_domain exec: - _new_nxdomain_response - _return # 已知的本地域名用本地服务器解析 - if: query_is_local_domain exec: - forward_local - _return - if: query_is_gfw_domain exec: - forward_remote - _return # 已知的非本地域名用远程服务器解析 - if: query_is_non_local_domain exec: - _prefer_ipv4 - forward_remote - _return # 剩下的未知域名用 IP 分流。 # primary 从本地服务器获取应答,丢弃非本地 IP 的结果。 - primary: - forward_local - if: "(! response_has_local_ip) && [_response_valid_answer]" exec: - _drop_response secondary: - _prefer_ipv4 - forward_remote fast_fallback: 200 always_standby: true servers: - exec: main_sequence listeners: - protocol: udp addr: ":${mosDNSServerPort}" - protocol: tcp addr: ":${mosDNSServerPort}" EOF echo echo " ================================================== " echo " Install mosdns success! 安装 mosdns 成功!" echo " mosdns running at port ${mosDNSServerPort}! 运行端口: ${mosDNSServerPort}!" echo " 查看访问日志: cat ${mosdnsLogFilePath}" echo " 请进入OpenWRT管理菜单: 服务-> MosDNS -> MosDNS 配置文件选择 下拉框选择 自定义配置 !" echo " 然后勾选 启用 复选框后, 点击 保存&应用 按钮 启动 MosDNS !" echo " ================================================== " echo } removeMosdns(){ echo echo " ==================================================" echo " 准备卸载 Mosdns on OpenWRT" echo " ==================================================" echo opkg remove luci-app-mosdns opkg remove mosdns rm -f "${mosdnsLogFilePath}" rm -rf "${mosdnsEtcPath}" rm -f /etc/config/mosdns rm -f /etc/config/mosdns-opkg echo echo " ================================================== " echo " Mosdns 卸载完毕 !" echo " ================================================== " } main(){ if [ -z "$1" ]; then installMosdns else removeMosdns fi } main $1