Clash Verge全局脚本
大约 7 分钟

全局扩展脚本:
// Define the `main` function
const proxyName = "代理模式";
function main(params) {
if (!params.proxies) return params;
overwriteRules(params);
overwriteProxyGroups(params);
overwriteDns(params);
return params;
}
//覆写规则
function overwriteRules(params) {
const customRules = [
// 在此添加自定义规则, 最高优先级。
//"DOMAIN-SUFFIX,domain," + proxyName,
"DOMAIN-SUFFIX,gstatic.com,自动选择",
"DOMAIN-SUFFIX,googleapis.com,自动选择",
];
const rules = [
...customRules,
"RULE-SET,reject,广告拦截",
"RULE-SET,direct,DIRECT",
"RULE-SET,cncidr,DIRECT",
"RULE-SET,private,DIRECT",
"RULE-SET,lancidr,DIRECT",
"GEOIP,LAN,DIRECT,no-resolve",
"GEOIP,CN,DIRECT,no-resolve",
"RULE-SET,applications,DIRECT",
"RULE-SET,openai,ChatGPT",
"RULE-SET,claude,Claude",
"RULE-SET,gemini,Gemini",
"RULE-SET,tld-not-cn," + proxyName,
"RULE-SET,google," + proxyName,
"RULE-SET,icloud," + proxyName,
"RULE-SET,apple," + proxyName,
"RULE-SET,gfw," + proxyName,
"RULE-SET,greatfire," + proxyName,
"RULE-SET,proxy," + proxyName,
"MATCH,漏网之鱼",
];
const ruleProviders = {
reject: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt",
path: "./ruleset/reject.yaml",
interval: 86400,
},
icloud: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/icloud.txt",
path: "./ruleset/icloud.yaml",
interval: 86400,
},
apple: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/apple.txt",
path: "./ruleset/apple.yaml",
interval: 86400,
},
google: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/google.txt",
path: "./ruleset/google.yaml",
interval: 86400,
},
proxy: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt",
path: "./ruleset/proxy.yaml",
interval: 86400,
},
openai: {
type: "http",
behavior: "classical",
url: "https://fastly.jsdelivr.net/gh/blackmatrix7/ios_rule_script@master/rule/Clash/OpenAI/OpenAI.yaml",
path: "./ruleset/custom/openai.yaml"
},
claude: {
type: "http",
behavior: "classical",
url: "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Clash/Claude/Claude.yaml",
path: "./ruleset/custom/Claude.yaml"
},
gemini: {
type: "http",
behavior: "classical",
url: "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Clash/Gemini/Gemini.yaml",
path: "./ruleset/custom/Gemini.yaml"
},
direct: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/direct.txt",
path: "./ruleset/direct.yaml",
interval: 86400,
},
private: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/private.txt",
path: "./ruleset/private.yaml",
interval: 86400,
},
gfw: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/gfw.txt",
path: "./ruleset/gfw.yaml",
interval: 86400,
},
greatfire: {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/greatfire.txt",
path: "./ruleset/greatfire.yaml",
interval: 86400,
},
"tld-not-cn": {
type: "http",
behavior: "domain",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/tld-not-cn.txt",
path: "./ruleset/tld-not-cn.yaml",
interval: 86400,
},
cncidr: {
type: "http",
behavior: "ipcidr",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/cncidr.txt",
path: "./ruleset/cncidr.yaml",
interval: 86400,
},
lancidr: {
type: "http",
behavior: "ipcidr",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/lancidr.txt",
path: "./ruleset/lancidr.yaml",
interval: 86400,
},
applications: {
type: "http",
behavior: "classical",
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/applications.txt",
path: "./ruleset/applications.yaml",
interval: 86400,
},
};
params["rule-providers"] = ruleProviders;
params["rules"] = rules;
}
//覆写代理组
function overwriteProxyGroups(params) {
// 添加自用代理
params.proxies.push(
// { name: '1-香港-示例', type: *, server: **, port: *, cipher: **, password: **, udp: true }
);
// 所有代理
const allProxies = params["proxies"].map((e) => e.name);
// 自动选择代理组,按地区分组选延迟最低
const autoProxyGroupRegexs = [
{name: "CN-自动选择", regex: /香港|HK|Hong|🇭🇰|台湾|TW|Taiwan|Wan|🇨🇳|🇹🇼|澳门|🇲🇴/},
{name: "KR-自动选择", regex: /韩国|South Korea|KR|🇰🇷/},
{name: "JP-自动选择", regex: /日本|JP|Japan|🇯🇵/},
{name: "SG-自动选择", regex: /新加坡|狮城|SG|Singapore|🇸🇬/},
{name: "US-自动选择", regex: /美国|US|United States|America|🇺🇸/},
{
name: "AI-自动选择",
// 理论上非中国节点都可以使用,
regex: /^(?!.*(?:剩余|到期|主页|官网|游戏|关注|订阅|台湾|TW|Taiwan|Wan|🇨🇳|🇹🇼|香港|HK|Hong|🇭🇰|澳门|🇲🇴)).*$/i,
},
];
const autoProxyGroups = autoProxyGroupRegexs
.map((item) => ({
name: item.name,
type: "url-test",
url: "http://www.gstatic.com/generate_204",
interval: 300,
tolerance: 50,
proxies: getProxiesByRegex(params, item.regex),
hidden: true,
}))
.filter((item) => item.proxies.length > 0);
//手工选择代理组
const manualProxyGroups = [
{
name: "AI-手工选择",
// 理论上非中国节点都可以使用
regex: /^(?!.*(?:剩余|到期|主页|官网|游戏|关注|订阅|澳门|台湾|TW|Taiwan|Wan|🇨🇳|🇹🇼|香港|HK|Hong|🇭🇰|澳门|🇲🇴)).*$/i,
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/chatgpt.svg"
},
];
const manualProxyGroupsConfig = manualProxyGroups
.map((item) => ({
name: item.name,
type: "select",
proxies: getManualProxiesByRegex(params, item.regex),
icon: item.icon,
hidden: false,
}))
.filter((item) => item.proxies.length > 0);
const groups = [
{
name: proxyName,
type: "select",
url: "http://www.gstatic.com/generate_204",
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/adjust.svg",
proxies: [
"自动选择",
"手动选择",
"负载均衡(散列)",
"负载均衡(轮询)",
"DIRECT",
],
},
{
name: "手动选择",
type: "select",
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/link.svg",
proxies: allProxies,
},
{
name: "自动选择",
type: "select",
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/speed.svg",
proxies: ["ALL-自动选择"],
},
{
name: "负载均衡(散列)",
type: "load-balance",
url: "http://www.gstatic.com/generate_204",
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/balance.svg",
interval: 300,
"max-failed-times": 3,
strategy: "consistent-hashing",
lazy: true,
proxies: allProxies,
},
{
name: "负载均衡(轮询)",
type: "load-balance",
url: "http://www.gstatic.com/generate_204",
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/merry_go.svg",
interval: 300,
"max-failed-times": 3,
strategy: "round-robin",
lazy: true,
proxies: allProxies,
},
{
name: "ALL-自动选择",
type: "url-test",
url: "http://www.gstatic.com/generate_204",
interval: 300,
tolerance: 50,
proxies: allProxies,
hidden: true,
},
{
name: "ChatGPT",
type: "select",
proxies: ["AI-自动选择", proxyName, "AI-手工选择"],
// "include-all": true,
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/chatgpt.svg"
},
{
name: "Claude",
type: "select",
proxies: ["AI-自动选择", proxyName, "AI-手工选择"],
// "include-all": true,
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/claude.svg"
},
{
name: "Gemini",
type: "select",
proxies: ["AI-自动选择", proxyName, "AI-手工选择"],
// "include-all": true,
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/google.svg"
},
{
name: "漏网之鱼",
type: "select",
proxies: ["DIRECT", proxyName],
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/fish.svg"
},
{
name: "广告拦截",
type: "select",
proxies: ["REJECT", "DIRECT", proxyName],
icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/block.svg"
},
];
autoProxyGroups.length &&
groups[2].proxies.unshift(...autoProxyGroups.map((item) => item.name));
groups.push(...autoProxyGroups);
groups.push(...manualProxyGroupsConfig);
params["proxy-groups"] = groups;
}
//防止dns泄露
function overwriteDns(params) {
const cnDnsList = [
"https://1.12.12.12/dns-query", // 由 DNSPod 提供的公共免费 DNS,后来 DNSPod 被腾讯 (Tencent) 收购,现在属于腾讯公司所有,稳定性和连通性也是不错的,经我在海外好友测试反馈,海外也可以使用。DNSPod 除了 IPv4,现在同时支持 IPv6 DNS 和 DoT/DoH 服务。
"https://120.53.53.53/dns-query", // 由 DNSPod 提供的公共免费 DNS,后来 DNSPod 被腾讯 (Tencent) 收购,现在属于腾讯公司所有,稳定性和连通性也是不错的,经我在海外好友测试反馈,海外也可以使用。DNSPod 除了 IPv4,现在同时支持 IPv6 DNS 和 DoT/DoH 服务。
"https://223.5.5.5/dns-query", // 这组 DNS 是由阿里巴巴提供的,国内连通性还是不错的,有我在海外好友反馈,海外部分地区连通性不是特别好,具体可以测试一下。阿里 DNS 同时提供了 IPv4/IPv6 DNS 和 DoT/DoH 服务。
"https://223.6.6.6/dns-query", // 这组 DNS 是由阿里巴巴提供的,国内连通性还是不错的,有我在海外好友反馈,海外部分地区连通性不是特别好,具体可以测试一下。阿里 DNS 同时提供了 IPv4/IPv6 DNS 和 DoT/DoH 服务。
];
const trustDnsList = [
"https://1.1.1.1/dns-query", // Cloudflare DNS 是 Cloudflare 与 APNIC 联合推出的号称全球最快的 DNS 服务。APNIC 是一个管理亚太与大洋洲地区 IP 地址分配的非盈利性组织,1.1.1.1 归 APNIC 所有,现在交给 Cloudflare 来提供 DNS 服务,同时提供 IPv4/IPv6/DoT/DoH 服务。
"https://1.0.0.1/dns-query", // Cloudflare DNS 是 Cloudflare 与 APNIC 联合推出的号称全球最快的 DNS 服务。APNIC 是一个管理亚太与大洋洲地区 IP 地址分配的非盈利性组织,1.1.1.1 归 APNIC 所有,现在交给 Cloudflare 来提供 DNS 服务,同时提供 IPv4/IPv6/DoT/DoH 服务。
"https://77.88.8.8/dns-query", // Yandex DNS 是俄国最大的搜索引擎 Yandex 提供的 DNS 服务,提供基础版 / 安全版 / 家庭版三个版本的 IPv4/IPv6/DoH/DoT 服务。其中每个版本的 IPv4 和 IPv6 第一个地址同样可用于 DoH 和 DoT,参考基础版,下面两个版本不再重复。
"https://77.88.8.1/dns-query", // Yandex DNS 是俄国最大的搜索引擎 Yandex 提供的 DNS 服务,提供基础版 / 安全版 / 家庭版三个版本的 IPv4/IPv6/DoH/DoT 服务。其中每个版本的 IPv4 和 IPv6 第一个地址同样可用于 DoH 和 DoT,参考基础版,下面两个版本不再重复。
"https://94.140.14.14/dns-query", // AdGuard 提供的公共免费 DNS 服务,默认服务器 (拦截广告和跟踪器)。
"https://94.140.15.15/dns-query", // AdGuard 提供的公共免费 DNS 服务,默认服务器 (拦截广告和跟踪器)。
"https://208.67.222.222/dns-query", // OpenDNS 是由美国老牌通信设备商 Cisco 思科 提供的公共 DNS 服务,其中基础版和家庭版提供免费的 IPv4/IPv6/DoH 服务,后改名 Cisco Umbrella。
"https://208.67.220.220/dns-query", // OpenDNS 是由美国老牌通信设备商 Cisco 思科 提供的公共 DNS 服务,其中基础版和家庭版提供免费的 IPv4/IPv6/DoH 服务,后改名 Cisco Umbrella。
];
const dnsOptions = {
enable: true,
"prefer-h3": true, // 如果DNS服务器支持DoH3会优先使用h3
"default-nameserver": cnDnsList,
nameserver: trustDnsList, // 其他网络请求都归他管
// 这个用于覆盖上面的 nameserver
"nameserver-policy": {
//[combinedUrls]: notionDns,
"geosite:cn": cnDnsList,
"geosite:geolocation-!cn": trustDnsList,
// 如果你有一些内网使用的DNS,应该定义在这里,多个域名用英文逗号分割
// '+.公司域名.com, www.4399.com, +.baidu.com': '10.0.0.1'
},
fallback: trustDnsList,
"fallback-filter": {
geoip: true,
//除了 geoip-code 配置的国家 IP, 其他的 IP 结果会被视为污染 geoip-code 配置的国家的结果会直接采用,否则将采用 fallback结果
"geoip-code": "CN",
//geosite 列表的内容被视为已污染,匹配到 geosite 的域名,将只使用 fallback解析,不去使用 nameserver
geosite: ["gfw"],
ipcidr: ["240.0.0.0/4"],
domain: ["+.google.com", "+.facebook.com", "+.youtube.com"],
},
};
// GitHub加速前缀
const githubPrefix = "https://fastgh.lainbo.com/";
// GEO数据GitHub资源原始下载地址
const rawGeoxURLs = {
geoip:
"https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip-lite.dat",
geosite:
"https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat",
mmdb: "https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country-lite.mmdb",
};
// 生成带有加速前缀的GEO数据资源对象
const accelURLs = Object.fromEntries(
Object.entries(rawGeoxURLs).map(([key, githubUrl]) => [
key,
`${githubPrefix}${githubUrl}`,
])
);
const otherOptions = {
"unified-delay": true,
"tcp-concurrent": true,
profile: {
"store-selected": true,
"store-fake-ip": true,
},
sniffer: {
enable: true,
sniff: {
TLS: {
ports: [443, 8443],
},
HTTP: {
ports: [80, "8080-8880"],
"override-destination": true,
},
},
},
"geodata-mode": true,
"geox-url": accelURLs,
};
params.dns = {...params.dns, ...dnsOptions};
Object.keys(otherOptions).forEach((key) => {
params[key] = otherOptions[key];
});
}
function getProxiesByRegex(params, regex) {
const matchedProxies = params.proxies.filter((e) => regex.test(e.name)).map((e) => e.name);
return matchedProxies.length > 0 ? matchedProxies : ["手动选择"];
}
function getManualProxiesByRegex(params, regex) {
const matchedProxies = params.proxies.filter((e) => regex.test(e.name)).map((e) => e.name);
return matchedProxies.length > 0 ? matchedProxies : ["DIRECT", "手动选择", proxyName];
}
效果:

自定义规则
- 增加自定义规则 customRules 最高优先级,方便有特殊代理需求的。