/** * mihomo配置覆写脚本(全量版) * 作者:AIsouler * 原仓库:https://github.com/AIsouler/MyClash * 脚本链接:https://raw.githubusercontent.com/AIsouler/MyClash/main/Script/mihomoScript.js * 友情推荐,非常好用、省电且内存占用低的代理软件:https://github.com/appshubcc/Bettbox */ // --- 静态配置区域 --- /** * 分流策略组启用配置,若不需要某个策略组,请设为 false * true = 启用 * false = 禁用 */ const ruleOptionsEnable = { AI: true, // 国外AI服务 Media: true, // 国外视频平台 FCM: true, // GoogleFCM服务 Google: true, // Google服务 Microsoft: true, // Microsoft服务 Apple: true, // Apple服务 Telegram: true, // Telegram通讯软件 Steam: true, // Steam游戏平台 TikTok: true, // TikTok视频平台 Twitter: true, // Twitter社交平台 Emby: true, // Emby媒体服务 PikPak: true, // PikPak网盘服务 Spotify: true, // Spotify音乐服务 EHentai: true, // E-Hentai网站 AdBlock: true, // 广告拦截 }; /** * 全局排除高倍率节点配置 * 该配置用于启用全局排除高倍率节点功能 * true = 启用 * false = 禁用 */ const excludeHighRateProxiesEnable = false; // 预定义 rules const rules = [ // 私有网络直连 'RULE-SET,private,直连', 'RULE-SET,private_ip,直连,no-resolve', // 国内直连 'RULE-SET,games_cn,直连', // 已包含 steam 下载域名 'RULE-SET,epicgames,直连', 'RULE-SET,nvidia_cn,直连', 'RULE-SET,apple_cn,直连', 'RULE-SET,microsoft_cn,直连', 'DOMAIN,fsend.cn,直连', 'DOMAIN,international-gfe.download.nvidia.com,直连', 'DOMAIN-SUFFIX,hdslb.com,直连', // 禁用国外 QUIC 流量 'AND,((NETWORK,UDP),(DST-PORT,443),(NOT,((OR,((RULE-SET,cn_additional),(RULE-SET,cn_ip,no-resolve)))))),REJECT', 'RULE-SET,github,默认代理', ]; // 定义全局排除节点的正则表达式,用于排除非地区的信息节点 const excludeFilter = /群|返利|循环|官网|客服|网站|网址|获取|订阅|流量|到期|机场|下次|版本|官址|过期|已用|联系|邮箱|工单|贩卖|通知|倒卖|防止|国内|地址|频道|无法|说明|提示|访问|支持|教程|关注|更新|作者|加入|超时|收藏|福利|邀请|好友|失联|选择|剩余|公益|发布|DIZTNA|通路|登录|定时|渠道|牢记|永久|余额|阁下|本站|刷新|导航|建议|重置|以下|⚠️|@|expire|http|com|traffic/iu; // 直连节点 const directProxies = [ { name: '🇨🇳 直连 | 双栈', type: 'direct', }, { name: '🇨🇳 直连 | IPv4优先', type: 'direct', 'ip-version': 'ipv4-prefer', }, { name: '🇨🇳 直连 | IPv6优先', type: 'direct', 'ip-version': 'ipv6-prefer', }, ]; // 定义地区策略组 const regionDefinitions = [ { name: '香港', regex: /🇭🇰|香港|(? r.name === '高倍率节点')?.regex : null; // 过滤节点列表 const filteredProxies = (config.proxies || []).filter((proxy) => { const type = String(proxy.type ?? '').toLowerCase(); return ( type !== 'direct' && type !== 'reject' && !excludeFilter.test(proxy.name) && !highRateRegex?.test(proxy.name) ); }); // 验证节点列表是否存在代理节点 if (!filteredProxies.length) { throw new Error('配置文件中未找到任何代理节点,请使用机场提供的配置文件进行覆写'); } // --- 构建地区组和倍率组 --- // 节点分类 const regionGroups = Object.fromEntries(regionDefinitions.map((r) => [r.name, { ...r, proxies: [] }])); const otherProxies = []; for (const proxy of filteredProxies) { let matched = false; for (const region of regionDefinitions) { if (region.regex.test(proxy.name)) { regionGroups[region.name].proxies.push(proxy.name); // 如果匹配到的是地区组(非倍率组),则标记为已分类 if (region.name !== '低倍率节点' && region.name !== '高倍率节点') { matched = true; } } } // 未匹配到地区组(不包含倍率组)的归为其他节点 if (!matched) { otherProxies.push(proxy.name); } } // 构建地区策略组 const generatedRegionGroups = regionDefinitions .filter((r) => regionGroups[r.name].proxies.length > 0) .flatMap((r) => createRegionGroup(r.name, r.icon, regionGroups[r.name].proxies)); if (otherProxies.length > 0) { generatedRegionGroups.push( ...createRegionGroup( '其他节点', 'https://fastly.jsdelivr.net/gh/Koolson/Qure@master/IconSet/Color/World_Map.png', otherProxies, ), ); } // --- 构建分流策略组 --- const functionalGroups = []; const finalRules = [...rules]; const finalRuleProviders = { ...baseRuleProviders }; // 获取所有节点名称 const allProxiesNames = filteredProxies.map((p) => p.name); // 筛选类型为 select 的地区策略组 const groupNamesOfSelect = generatedRegionGroups.filter((g) => g.type === 'select').map((g) => g.name); // 生成基础策略组 functionalGroups.push( { ...selectBaseOption, name: '默认代理', proxies: [...groupNamesOfSelect, '手动选择', '自动选择', '负载均衡'], icon: 'https://fastly.jsdelivr.net/gh/Koolson/Qure@master/IconSet/Color/Proxy.png', }, { ...selectBaseOption, name: '手动选择', proxies: [...allProxiesNames], icon: 'https://fastly.jsdelivr.net/gh/Koolson/Qure@master/IconSet/Color/Static.png', }, { ...urlTestBaseOption, name: '自动选择', proxies: [...allProxiesNames], }, { ...loadBalanceBaseOption, name: '负载均衡', proxies: [...allProxiesNames], }, ); // 构建分流策略组 // 优先添加 AdBlock 规则 const adBlockConfig = serviceConfigs.find((svc) => svc.name === 'AdBlock'); if (adBlockConfig && ruleOptionsEnable[adBlockConfig.name]) { finalRules.push(...adBlockConfig.rules); Object.assign(finalRuleProviders, adBlockConfig.providers); } for (const svc of serviceConfigs) { if (!ruleOptionsEnable[svc.name]) continue; // 添加分流策略组对应的 Rule 和 Rule Providers if (svc.name !== adBlockConfig?.name) { finalRules.push(...svc.rules); Object.assign(finalRuleProviders, svc.providers); } // 添加分流策略组对应的节点列表 const groupProxies = svc.reject ? ['REJECT', 'REJECT-DROP', 'PASS'] : ['默认代理', '手动选择', '自动选择', '负载均衡', ...groupNamesOfSelect, ...(svc.direct ? ['直连'] : [])]; functionalGroups.push({ ...selectBaseOption, name: svc.name, icon: svc.icon, proxies: groupProxies, ...(svc.defaultSelected !== undefined && { 'default-selected': svc.defaultSelected, }), }); } // 添加其他策略组 functionalGroups.push( { ...selectBaseOption, name: '漏网之鱼', proxies: ['默认代理', '直连'], icon: 'https://fastly.jsdelivr.net/gh/Koolson/Qure@master/IconSet/Color/Stack.png', }, { ...selectBaseOption, name: '直连', proxies: [...directProxies.map((p) => p.name)], url: 'https://connectivitycheck.platform.hicloud.com/generate_204', icon: 'https://fastly.jsdelivr.net/gh/Koolson/Qure@master/IconSet/Color/China_Map.png', }, ); // 构建 GLOBAL 全局策略组 const globalGroup = { ...selectBaseOption, name: 'GLOBAL', proxies: [...functionalGroups.map((g) => g.name), ...generatedRegionGroups.map((g) => g.name)], icon: 'https://fastly.jsdelivr.net/gh/Koolson/Qure@master/IconSet/Color/Global.png', }; // --- 添加基础配置 --- // ---DNS配置--- // 读取订阅中的 DNS 配置,保留订阅中的私有 DNS // 用以解决部分机场使用私有 DNS 导致无法解析节点的问题 const originalDnsConfig = config.dns || {}; // 过滤常见的公共 DNS const commonDnsList = [ // IP(国内) '223.5.5.5', '223.6.6.6', '119.29.29.29', '1.12.12.12', '120.53.53.53', '114.114.114.114', '180.76.76.76', '1.2.4.8', '116.116.116.116', '101.226.4.6', '123.125.81.6', '180.184.1.1', '180.184.2.2', // IP(国外) '1.1.1.1', '1.0.0.1', '8.8.8.8', '8.8.4.4', '9.9.9.9', '149.112.112.112', '208.67.222.222', '208.67.220.220', '94.140.14.14', '94.140.15.15', '76.76.2.0', '76.76.10.0', '185.228.168.9', '185.228.169.9', '77.88.8.8', '77.88.8.1', '156.154.70.1', '156.154.71.1', // 非公共DNS,但部分机场会使用这个 '127.0.0.1', // 关键词(国内) 'alidns', 'doh.pub', 'dot.pub', 'dnspod', 'dns.baidu', // 关键词(国外) 'dns.google', 'cloudflare', 'quad9', 'opendns', 'nextdns', 'adguard', // 系统 'system', ]; const isCommonDns = (dns) => { const value = String(dns).toLowerCase(); return commonDnsList.some((keyword) => value.includes(keyword)); }; // 提取私有 DNS const privateDNS = [ ...new Set([...(originalDnsConfig['nameserver'] || []), ...(originalDnsConfig['proxy-server-nameserver'] || [])]), ].filter((dns) => !isCommonDns(dns)); // 收集所有节点域名 const proxyDomains = new Set( filteredProxies.filter((proxy) => typeof proxy.server === 'string').map((proxy) => proxy.server.toLowerCase()), ); // 提取节点域名对应的 DNS 配置 const proxyServerPolicy = {}; for (const policy of [ originalDnsConfig['nameserver-policy'] || {}, originalDnsConfig['proxy-server-nameserver-policy'] || {}, ]) { for (const [domain, dns] of Object.entries(policy)) { if (matchDomainPattern(domain, proxyDomains)) { proxyServerPolicy[domain] = dns; } } } // 国内外 DNS 定义 const chinaDNS = ['https://dns.alidns.com/dns-query#DIRECT', 'https://doh.pub/dns-query#DIRECT']; const foreignDNS = ['https://dns.cloudflare.com/dns-query#默认代理', 'https://dns.google/dns-query#默认代理']; newConfig['dns'] = { enable: true, ipv6: true, 'use-hosts': true, 'cache-algorithm': 'arc', 'use-system-hosts': true, 'enhanced-mode': 'fake-ip', 'fake-ip-range': '198.18.0.1/16', 'fake-ip-filter': ['rule-set:private', 'rule-set:fakeip_filter'], 'proxy-server-nameserver': [...chinaDNS, ...privateDNS], ...(Object.keys(proxyServerPolicy).length > 0 && { 'proxy-server-nameserver-policy': proxyServerPolicy, }), 'default-nameserver': ['223.5.5.5', '119.29.29.29'], nameserver: [...foreignDNS], 'nameserver-policy': { 'rule-set:cn': [...chinaDNS], }, 'direct-nameserver': ['system', '223.5.5.5', '119.29.29.29'], }; // ---hosts 配置--- // 提取订阅 hosts 中与节点域名对应的记录 const originalHosts = config.hosts || {}; const proxyServerHosts = {}; for (const [domain, value] of Object.entries(originalHosts)) { if (matchDomainPattern(domain, proxyDomains)) { proxyServerHosts[domain] = value; } } newConfig['hosts'] = { 'dns.alidns.com': ['223.5.5.5', '223.6.6.6'], 'doh.pub': ['1.12.12.12', '120.53.53.53'], 'dns.cloudflare.com': ['1.1.1.1', '1.0.0.1'], 'dns.google': ['8.8.8.8', '8.8.4.4'], // 解决谷歌商店无法下载的问题 'services.googleapis.cn': ['services.googleapis.com'], // 屏蔽哔哩哔哩PCDN,解决访问视频卡顿问题 '+.mcdn.bilivideo.com': ['0.0.0.0'], '+.mcdn.bilivideo.cn': ['0.0.0.0'], '+.edge.mountaintoys.cn': ['0.0.0.0'], // 保留机场用于节点解析的 hosts ...proxyServerHosts, }; newConfig['mixed-port'] = 7890; newConfig['allow-lan'] = true; newConfig['ipv6'] = true; newConfig['mode'] = 'rule'; newConfig['log-level'] = 'info'; newConfig['bind-address'] = '*'; newConfig['unified-delay'] = true; newConfig['tcp-concurrent'] = true; newConfig['keep-alive-idle'] = 600; newConfig['keep-alive-interval'] = 60; newConfig['find-process-mode'] = 'strict'; newConfig['external-controller'] = '127.0.0.1:9090'; newConfig['external-ui'] = 'ui'; newConfig['external-ui-url'] = 'https://github.com/Zephyruso/zashboard/releases/latest/download/dist.zip'; newConfig['profile'] = { 'store-selected': true, 'store-fake-ip': true, }; newConfig['ntp'] = { enable: true, 'write-to-system': false, server: 'ntp.aliyun.com', port: 123, interval: 60, }; newConfig['tun'] = { enable: true, stack: 'system', 'auto-route': true, 'strict-route': true, 'auto-redirect': true, 'auto-detect-interface': true, 'dns-hijack': ['any:53', 'tcp://any:53'], }; newConfig['proxies'] = [...filteredProxies, ...directProxies]; newConfig['proxy-groups'] = [globalGroup, ...functionalGroups, ...generatedRegionGroups]; newConfig['rule-providers'] = finalRuleProviders; newConfig['rules'] = [ ...finalRules, // 兜底规则 'RULE-SET,gfw,默认代理', 'RULE-SET,geolocation-cn,直连', 'RULE-SET,cn_ip,直连', 'MATCH,漏网之鱼', ]; return newConfig; }