clash verge js脚本配置

各个机场所用的规则都不一样,使用软件自带的script混入可以修改规则.
有个问题就是会dns泄漏,不知道怎么配置才好

const mainSelect = "🚀 节点选择"
const autoSelect = "🎃 自动选择"
const otherSelect ="🐟 漏网之鱼"
const githubProxyUrl = "https://ghproxy.net/https://raw.githubusercontent.com/"

function main(params) {
  
  initDns(params)
  initGroups(params)
  initRules(params)
  delParams(params)
  // 自定义
  microsoftRules(params)
 // myRules(params)
 console.log(params)
  return params;
}

function initGroups(params) {

  const proxies = params["proxies"].map(e => e.name)

  const groupFilter = {
    "🇭🇰 香港 自动选择" : "港|HK|Hong",
    "🇯🇵 日本 自动选择" : "日|东京|JP|Japan",
    "🇸🇬 新加坡 自动选择" : "新|坡|獅|SG|Singapore"
  }

  let auto =  Object.keys(groupFilter).map(gf =>{
    return {
      name: gf,
      type: 'url-test',
      proxies: proxies.filter(p => new RegExp(groupFilter[gf]).test(p))
    }
  }).filter(e => e.proxies.length>0)

  const proxyGroups = [
    {
      name: mainSelect,
      type: 'select',
      proxies: [autoSelect,...auto.map(e=>e.name),...proxies]
    },
    {
      name: otherSelect,
      type: 'select',
      proxies: [mainSelect, 'DIRECT']
    },
    {
      name: autoSelect,
      type: 'url-test',
      proxies: proxies
    },...auto
  ]

  params["proxy-groups"] = proxyGroups
}

function initRules(params) {

  let baseUrl = githubProxyUrl + "Loyalsoldier/clash-rules/release/";
  let ruleProvidersKeys = [
    'reject',
    'icloud',
    'apple',
    // 'google',
    'proxy',
    'direct',
    'private',
    // 'gfw', proxy 包含 gfw
    'tld-not-cn',
    'telegramcidr',
    'cncidr',
    'lancidr',
    'applications']

  let ruleProviders = {}
  ruleProvidersKeys.forEach(e => {
    ruleProviders[e] = {
      type: "http",
      behavior: "domain",
      url: baseUrl + `${e}.txt`,
      path: `./ruleset/${e}.yaml`,
      interval: 86400
    }
  })

  let rules = [
    "RULE-SET,applications,DIRECT",
    "DOMAIN,clash.razord.top,DIRECT",
    "DOMAIN,yacd.haishan.me,DIRECT",
    "RULE-SET,private,DIRECT",
    "RULE-SET,reject,REJECT",  // 广告域名列表
    "RULE-SET,icloud,DIRECT",
    "RULE-SET,apple,DIRECT",
    // "RULE-SET,google," + mainSelect,  Google 在中国大陆可直连的域名列表
    "RULE-SET,direct,DIRECT",
    "RULE-SET,lancidr,DIRECT",
    "RULE-SET,cncidr,DIRECT",
    "RULE-SET,telegramcidr," + mainSelect,
    "RULE-SET,proxy," + mainSelect,
    "GEOIP,LAN,DIRECT",
    "GEOIP,CN,DIRECT",
    "MATCH,"+otherSelect
  ]

  params["rule-providers"] = ruleProviders
  params["rules"] = rules

}

function initDns(params){
  params["dns"] = {
    "enable": true,
    "ipv6": false,
    "listen":"0.0.0.0:1053",
    "default-nameserver": [
        "223.5.5.5",
        "119.29.29.29",
        "114.114.114.114"
    ],
    "enhanced-mode": "fake-ip",
    "fake-ip-range": "28.0.0.1/8",
    "use-hosts": true,
    "nameserver": [
        "tls://223.5.5.5:853",
        "tls://223.6.6.6:853",
        "https://doh.pub/dns-query",
        "https://dns.alidns.com/dns-query"
    ],
    "fallback": [
        "https://doh.dns.sb/dns-query",
        "https://dns.cloudflare.com/dns-query",
        "https://dns.twnic.tw/dns-query",
        "tls://8.8.4.4:853"
    ],
    "fallback-filter": {
        "geoip": true,
        "ipcidr": [
            "240.0.0.0/4",
            "0.0.0.0/32"
        ]
    }
}
}

function delParams(params){ 
  delete params["cfw-conn-break-strategy"]
  delete params["experimental"]
  delete params["hosts"]
  delete params["cfw-latency-timeout"]
  delete params["cfw-latency-url"]
  delete params["clash-for-android"]
  delete params["udp"]


}




function microsoftRules(params) {

  let baseUrl = githubProxyUrl + "blackmatrix7/ios_rule_script/master/rule/Clash/";
  let microsoftProviders = {
    'microsoft': {
      type: "http",
      behavior: "classical",
      url: baseUrl + "Microsoft/Microsoft.yaml",
      path: `./ruleset/microsoft.yaml`,
      interval: 86400
    },
    "bing": {
      type: "http",
      behavior: "classical",
      url: baseUrl + "Bing/Bing.yaml",
      path: `./ruleset/bing.yaml`,
      interval: 86400
    },
    "oneDrive": {
      type: "http",
      behavior: "classical",
      url: baseUrl + "OneDrive/OneDrive.yaml",
      path: `./ruleset/oneDrive.yaml`,
      interval: 86400
    },
  }

  let microsoftRules = [
    "RULE-SET,microsoft,Microsoft",
    "RULE-SET,bing,Microsoft",
    "RULE-SET,oneDrive,Microsoft"
  ]
  let microsoftGroup = {
    name: 'Microsoft',
    type: 'select',
    proxies: [ mainSelect,"DIRECT"]
  }

  params["rule-providers"] = { ...microsoftProviders, ...params["rule-providers"] }
  params["rules"] = [...microsoftRules, ...params["rules"]]
  params["proxy-groups"] = [...params["proxy-groups"], microsoftGroup]

}


function myRules(params) {

  let rules = [
    "DOMAIN-SUFFIX,x.com," + mainSelect
  ]
  
  params["rules"] = [...rules, ...params["rules"]]
}





1 个赞

搞七捻三快问快答

我不是太懂,这个文章可以参考一下

2 个赞

真是太感谢了,实用的很 :grinning:

我照着设置了,有些节点就寄了,搞不懂

有截屏吗?

一般不转换订阅的话,节点不会掉的,我没有转换订阅。我主要是研究如何设置clash verge,解决DNS泄露的问题。

我自己按照那个博客来设置clash verge,网速比以前要快了,将近 1 倍。你再检查一下步骤,或者再看看找找其它类似的文章对比。

我订阅转换了,主要是好分流,默认的分组太少了。就是有些节点开了那个js脚本就会断,连不上网,然后关了就好了

脚本有问题,他那个基本只是示意,你还要了解一下基本的含义。
我这里有一份 chatgpt 写的,你可以参考一下。大致是,从原有节点中筛选出“台湾”节点,openai 和 gemini 走台湾节点。新建一个融合,类型改为 script,编辑文件

function main(config) {
// 筛选出包含“台湾”的节点
const taiwanProxies = config.proxies.filter(proxy => proxy.name.includes(‘台湾’));

// 如果存在“台湾”的节点,则创建一个新的代理分组
if (taiwanProxies.length > 0) {
// 新建一个分组名称为 “Taiwan Proxies”
const newGroup = {
name: “Taiwan Proxies”,
type: “select”,
proxies: taiwanProxies.map(proxy => proxy.name)
};

// 将新的分组添加到代理分组列表中
config['proxy-groups'].push(newGroup);

// 假设你要为 "example.com" 使用这个新的分组,则需要添加一条规则

config.rules.unshift(`DOMAIN-SUFFIX,openai.com,Taiwan Proxies`);
config.rules.unshift(`DOMAIN-SUFFIX,gemini.googl.com,Taiwan Proxies`);
config.rules.unshift(`DOMAIN-KEYWORD,gemini,Taiwan Proxies`);

}
return config;
}

不懂的问一下 chatgpt

确实我转换后的订阅加上那个dns脚本就断很多,没转换的就正常,那我只能用js分组了?

没听懂。我的理解,那个 DNS设置,只是用纯净的 DNS 服务器解析域名和 IP 地址,和节点没有关系。我猜测,节点掉了,是因为你的分流规则里没写清楚,或者格式不对。
你可以把你的设置文件丢给 GPT,让他解释每一项的含义。

好东西,我把dns泄露的和结合你的规则重新写了个

// 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 rules = [
        "RULE-SET,reject,⛔广告拦截",
        "RULE-SET,direct,DIRECT",
        "RULE-SET,cncidr,DIRECT",
        "RULE-SET,private,DIRECT",
        "RULE-SET,lancidr,DIRECT",
        "GEOIP,LAN,DIRECT",
        "GEOIP,CN,DIRECT",
        "RULE-SET,applications,DIRECT",
        "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,telegramcidr," + 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,
        },
        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,
        },
        telegramcidr: {
            type: "http",
            behavior: "ipcidr",
            url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/telegramcidr.txt",
            path: "./ruleset/telegramcidr.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) {
    // 香港地区
    const hongKongRegex = /香港|HK|Hong|🇭🇰/;
    const hongKongProxies = getProxiesByRegex(params, hongKongRegex);
    // 台湾地区
    const taiwanRegex = /台湾|TW|Taiwan|Wan|🇨🇳|🇹🇼/;
    const taiwanProxies = getProxiesByRegex(params, taiwanRegex);
    // 狮城地区
    const singaporeRegex = /新加坡|狮城|SG|Singapore|🇸🇬/;
    const singaporeProxies = getProxiesByRegex(params, singaporeRegex);
    // 日本地区
    const japanRegex = /日本|JP|Japan|🇯🇵/;
    const japanProxies = getProxiesByRegex(params, japanRegex);
    // 美国地区
    const americaRegex = /美国|US|United States|America|🇺🇸/;
    const americaProxies = getProxiesByRegex(params, americaRegex);
    // 所有代理
    const allProxies = params["proxies"].map((e) => e.name);

    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",
            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: [
                "HK-自动选择",
                "SG-自动选择",
                "JP-自动选择",
                "US-自动选择",
                "TW-自动选择",
                "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: "HK-自动选择",
            type: "url-test",
            url: "http://www.gstatic.com/generate_204",
            interval: 300,
            tolerance: 50,
            proxies: hongKongProxies,
            hidden: true,
        },
        {
            name: "SG-自动选择",
            type: "url-test",
            url: "http://www.gstatic.com/generate_204",
            interval: 300,
            tolerance: 50,
            proxies: singaporeProxies,
            hidden: true,
        },
        {
            name: "JP-自动选择",
            type: "url-test",
            url: "http://www.gstatic.com/generate_204",
            interval: 300,
            tolerance: 50,
            proxies: japanProxies,
            hidden: true,
        },
        {
            name: "TW-自动选择",
            type: "url-test",
            url: "http://www.gstatic.com/generate_204",
            interval: 300,
            tolerance: 50,
            proxies: taiwanProxies,
            hidden: true,
        },
        {
            name: "US-自动选择",
            type: "url-test",
            url: "http://www.gstatic.com/generate_204",
            interval: 300,
            tolerance: 50,
            proxies: americaProxies,
            hidden: true,
        },
        {
            name: "ALL-自动选择",
            type: "url-test",
            url: "http://www.gstatic.com/generate_204",
            interval: 300,
            tolerance: 50,
            proxies: allProxies,
            hidden: true,
        },
        {
            name: "🐟漏网之鱼",
            type: "select",
            proxies: ["DIRECT", proxyName],
        },
        {
            name: "⛔广告拦截",
            type: "select",
            proxies: ["REJECT", "DIRECT", proxyName],
        },
    ];

    params["proxy-groups"] = groups
}
//防止dns泄露
function overwriteDns(params) {
    const cnDnsList = [
        'https://223.5.5.5/dns-query',
        'https://223.6.6.6/dns-query',
    ]
    const trustDnsList = [
        'quic://dns.cooluc.com',
        'https://doh.apad.pro/dns-query',
        'https://1.0.0.1/dns-query',
    ]
    const notionDns = 'tls://dns.jerryw.cn'
    const notionUrls = [
        'http-inputs-notion.splunkcloud.com',
        '+.notion-static.com',
        '+.notion.com',
        '+.notion.new',
        '+.notion.site',
        '+.notion.so',
    ]
    const combinedUrls = notionUrls.join(',');
    const dnsOptions = {
        'enable': true,
        'prefer-h3': true, // 如果DNS服务器支持DoH3会优先使用h3
        'default-nameserver': cnDnsList, // 用于解析其他DNS服务器、和节点的域名, 必须为IP, 可为加密DNS。注意这个只用来解析节点和其他的dns,其他网络请求不归他管
        'nameserver': trustDnsList, // 其他网络请求都归他管

        // 这个用于覆盖上面的 nameserver
        'nameserver-policy': {
            [combinedUrls]: notionDns,
            'geosite:geolocation-!cn': trustDnsList,
            // 如果你有一些内网使用的DNS,应该定义在这里,多个域名用英文逗号分割
            // '+.公司域名.com, www.4399.com, +.baidu.com': '10.0.0.1'
        },
    }

    // 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) {
    return params.proxies.filter((e) => regex.test(e.name)).map((e) => e.name);
}

4 个赞

标记下

公司内部的DNS应该如何配置规则

From 快问快答 to 开发调优