Script Configuration ​

Adding Scripts ​

WARNING

Script Configuration,EnableSequential,。 ConfigurationModifyNeedEnable,(Configuration Disable Enable ,Can 🔥 )。

Adding Scripts

Script Instructions ​

  • The script language is javascript, using boa_engine as the executor.
  • The program entry point is the main function, which accepts a params parameter (parameter name is not restricted) and returns the modified parameter.
  • params is of type object, where property values are object objects corresponding to the yaml configuration file content converted to JSON.
javascript
function main(params) {
  return params;
}

Supported APIs ​

The following APIs are supported in the scope. Network IO and file IO operations are not supported.

API TypeAPI Scope
ConstantsInfinity、NaN、undefined
Basic Functions and ObjectsFunction、Object、Math、JSON、Array、Proxy
Data Types and ArraysArrayBuffer、SharedArrayBuffer、BigInt、Boolean、Date、DataView、Map、TypedArray、Int8Array、Uint8Array、Uint8ClampedArray、Int16Array、Uint16Array、Int32Array、Uint32Array、BigInt64Array、BigUint64Array、Float32Array、Float64Array
Strings, Regular Expressions, and SymbolsString、RegExp、Symbol
Error ObjectsError、RangeError、ReferenceError、TypeError、SyntaxError、EvalError、URIError、AggregateError
Reflection and AsyncReflect、Promise
Encoding and DecodingencodeURI、encodeURIComponent、decodeURI、decodeURIComponent
Weak ReferencesWeakRef、WeakMap、WeakSet
Atomic OperationsAtomics
Console Outputconsole

Script Examples ​

Loyalsoldier Rules Script Script Examples()。

INFO

You can add your own rules under // Custom Rules in the script. For rule configuration, please refer to the Rule Configuration Documentation.

javascript
// Domestic DNS Servers
const domesticNameservers = [
  "https://dns.alidns.com/dns-query", // Alibaba Cloud Public DNS
  "https://doh.pub/dns-query", // Tencent DNSPod
  "https://doh.360.cn/dns-query", // 360 Secure DNS
];
// Foreign DNS Servers
const foreignNameservers = [
  "https://1.1.1.1/dns-query", // Cloudflare (Primary)
  "https://1.0.0.1/dns-query", // Cloudflare (Backup)
  "https://208.67.222.222/dns-query", // OpenDNS (Primary)
  "https://208.67.220.220/dns-query", // OpenDNS (Backup)
  "https://194.242.2.2/dns-query", // Mullvad (Primary)
  "https://194.242.2.3/dns-query", // Mullvad (Backup)
];
// DNS Configuration
const dnsConfig = {
  dns: true,
  listen: 1053,
  ipv6: true,
  "use-hosts": true,
  "cache-algorithm": "arc",
  "enhanced-mode": "fake-ip",
  "fake-ip-range": "198.18.0.1/16",
  "fake-ip-filter": [
    "+.lan",
    "+.local",
    "+.msftconnecttest.com",
    "+.msftncsi.com",
  ],
  "default-nameserver": ["223.5.5.5", "114.114.114.114", "1.1.1.1", "8.8.8.8"],
  nameserver: [...domesticNameservers, ...foreignNameservers],
  "proxy-server-nameserver": [...domesticNameservers, ...foreignNameservers],
  "nameserver-policy": {
    "geosite:private,cn,geolocation-cn": domesticNameservers,
    "geosite:google,youtube,telegram,gfw,geolocation-!cn": foreignNameservers,
  },
};
// Rule Provider Common Configuration
const ruleProviderCommon = {
  type: "http",
  format: "yaml",
  interval: 86400,
};
// Rule Provider Configuration
const ruleProviders = {
  reject: {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt",
    path: "./rulesets/loyalsoldier/reject.yaml",
  },
  icloud: {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/icloud.txt",
    path: "./rulesets/loyalsoldier/icloud.yaml",
  },
  apple: {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/apple.txt",
    path: "./rulesets/loyalsoldier/apple.yaml",
  },
  google: {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/google.txt",
    path: "./rulesets/loyalsoldier/google.yaml",
  },
  proxy: {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt",
    path: "./rulesets/loyalsoldier/proxy.yaml",
  },
  direct: {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/direct.txt",
    path: "./rulesets/loyalsoldier/direct.yaml",
  },
  private: {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/private.txt",
    path: "./rulesets/loyalsoldier/private.yaml",
  },
  gfw: {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/gfw.txt",
    path: "./rulesets/loyalsoldier/gfw.yaml",
  },
  "tld-not-cn": {
    ...ruleProviderCommon,
    behavior: "domain",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/tld-not-cn.txt",
    path: "./rulesets/loyalsoldier/tld-not-cn.yaml",
  },
  telegramcidr: {
    ...ruleProviderCommon,
    behavior: "ipcidr",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/telegramcidr.txt",
    path: "./rulesets/loyalsoldier/telegramcidr.yaml",
  },
  cncidr: {
    ...ruleProviderCommon,
    behavior: "ipcidr",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/cncidr.txt",
    path: "./rulesets/loyalsoldier/cncidr.yaml",
  },
  lancidr: {
    ...ruleProviderCommon,
    behavior: "ipcidr",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/lancidr.txt",
    path: "./rulesets/loyalsoldier/lancidr.yaml",
  },
  applications: {
    ...ruleProviderCommon,
    behavior: "classical",
    url: "https://fastly.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/applications.txt",
    path: "./rulesets/loyalsoldier/applications.yaml",
  },
};
// Rules
const rules = [
  // CustomRules
  "DOMAIN-SUFFIX,googleapis.cn,Proxy Select", // Google Services
  "DOMAIN-SUFFIX,gstatic.com,Proxy Select", // Google Static Resources
  "DOMAIN-SUFFIX,xn--ngstr-lra8j.com,Proxy Select", // Google Play Download Service
  "DOMAIN-SUFFIX,github.io,Proxy Select", // GitHub Pages
  "DOMAIN,v2rayse.com,Proxy Select", // V2rayse Node Tool
  // Loyalsoldier Rules
  "RULE-SET,reject,Ad Filter",
  "RULE-SET,icloud,Microsoft Services",
  "RULE-SET,apple,Apple Services",
  "RULE-SET,google,Google Services",
  "RULE-SET,proxy,Proxy Select",
  "RULE-SET,gfw,Proxy Select",
  "RULE-SET,tld-not-cn,Proxy Select",
  "RULE-SET,applications,Global Direct",
  "RULE-SET,private,Global Direct",
  "RULE-SET,direct,Global Direct",
  "RULE-SET,lancidr,Global Direct,no-resolve",
  "RULE-SET,cncidr,Global Direct,no-resolve",
  "RULE-SET,telegramcidr,Telegram Messages,no-resolve",
  // Rules
  "GEOIP,LAN,Global Direct,no-resolve",
  "GEOIP,CN,Global Direct,no-resolve",
  "MATCH,Final Match",
];
// Proxy Group Common Configuration
const groupBaseOption = {
  interval: 300,
  timeout: 3000,
  url: "https://www.google.com/generate_204",
  lazy: true,
  "max-failed-times": 3,
  hidden: false,
};

// Program Entry Point
function main(config) {
  const proxyCount = config?.proxies?.length ?? 0;
  const proxyProviderCount =
    typeof config?.["proxy-providers"] === "object"
      ? Object.keys(config["proxy-providers"]).length
      : 0;
  if (proxyCount === 0 && proxyProviderCount === 0) {
    throw new Error("No proxies found in configuration file");
  }

  // ConfigurationDNS Configuration
  config["dns"] = dnsConfig;

  // Override proxy groups in original config
  config["proxy-groups"] = [
    {
      ...groupBaseOption,
      name: "Proxy Select",
      type: "select",
      proxies: ["URL Test", "Fallback", "Load Balance (Hash)", "Load Balance (Round Robin)"],
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/adjust.svg",
    },
    {
      ...groupBaseOption,
      name: "URL Test",
      type: "url-test",
      tolerance: 100,
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/speed.svg",
    },
    {
      ...groupBaseOption,
      name: "Fallback",
      type: "fallback",
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/ambulance.svg",
    },
    {
      ...groupBaseOption,
      name: "Load Balance (Hash)",
      type: "load-balance",
      strategy: "consistent-hashing",
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/merry_go.svg",
    },
    {
      ...groupBaseOption,
      name: "Load Balance (Round Robin)",
      type: "load-balance",
      strategy: "round-robin",
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/balance.svg",
    },
    {
      ...groupBaseOption,
      name: "Google Services",
      type: "select",
      proxies: [
        "Proxy Select",
        "URL Test",
        "Fallback",
        "Load Balance (Hash)",
        "Load Balance (Round Robin)",
        "Global Direct",
      ],
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/google.svg",
    },
    {
      ...groupBaseOption,
      name: "",
      type: "select",
      proxies: [
        "Proxy Select",
        "URL Test",
        "Fallback",
        "Load Balance (Hash)",
        "Load Balance (Round Robin)",
        "Global Direct",
      ],
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/youtube.svg",
    },
    {
      ...groupBaseOption,
      name: "Telegram Messages",
      type: "select",
      proxies: [
        "Proxy Select",
        "URL Test",
        "Fallback",
        "Load Balance (Hash)",
        "Load Balance (Round Robin)",
        "Global Direct",
      ],
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/telegram.svg",
    },
    {
      ...groupBaseOption,
      name: "Microsoft Services",
      type: "select",
      proxies: [
        "Global Direct",
        "Proxy Select",
        "URL Test",
        "Fallback",
        "Load Balance (Hash)",
        "Load Balance (Round Robin)",
      ],
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/microsoft.svg",
    },
    {
      ...groupBaseOption,
      name: "Apple Services",
      type: "select",
      proxies: [
        "Proxy Select",
        "URL Test",
        "Fallback",
        "Load Balance (Hash)",
        "Load Balance (Round Robin)",
        "Global Direct",
      ],
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/apple.svg",
    },
    {
      ...groupBaseOption,
      name: "Ad Filter",
      type: "select",
      proxies: ["REJECT", "DIRECT"],
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/bug.svg",
    },
    {
      ...groupBaseOption,
      name: "Global Direct",
      type: "select",
      proxies: [
        "DIRECT",
        "Proxy Select",
        "URL Test",
        "Fallback",
        "Load Balance (Hash)",
        "Load Balance (Round Robin)",
      ],
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/link.svg",
    },
    {
      ...groupBaseOption,
      name: "Global",
      type: "select",
      proxies: ["REJECT", "DIRECT"],
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/block.svg",
    },
    {
      ...groupBaseOption,
      name: "Final Match",
      type: "select",
      proxies: [
        "Proxy Select",
        "URL Test",
        "Fallback",
        "Load Balance (Hash)",
        "Load Balance (Round Robin)",
        "Global Direct",
      ],
      "include-all": true,
      icon: "https://fastly.jsdelivr.net/gh/clash-verge-rev/clash-verge-rev.github.io@main/docs/assets/icons/fish.svg",
    },
  ];

  // ConfigurationRules
  config["rule-providers"] = ruleProviders;
  config["rules"] = rules;

  // BackModifyConfiguration
  return config;
}