operator_template.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Sub-Store operator (production-friendly)
  3. - Pull dynamic value from current_domain.json / current_ip.json
  4. - Replace vmess server field for matched nodes
  5. */
  6. const VALUE_JSON_URL = "https://git.dewofaurora.de/aurora/vmess-domain-rotator/raw/runtime-state/runtime/current_domain.json";
  7. const NODE_NAME_REGEX = /(argo|cf|vm|优选)/i;
  8. const CACHE_KEY = "vmess-domain-rotator:current";
  9. const CACHE_TTL_MS = 5 * 60 * 1000;
  10. const JSON_VALUE_KEYS = ["domain", "ip"];
  11. async function fetchValueViaSubStore() {
  12. const $ = $substore;
  13. const { body, statusCode } = await $.http.get({
  14. url: VALUE_JSON_URL,
  15. headers: {
  16. Accept: "application/json",
  17. "Cache-Control": "no-cache"
  18. },
  19. timeout: 5000
  20. });
  21. if (statusCode < 200 || statusCode >= 300) {
  22. throw new Error(`http status ${statusCode}`);
  23. }
  24. const obj = JSON.parse(body || "{}");
  25. let value = "";
  26. for (const key of JSON_VALUE_KEYS) {
  27. value = String(obj[key] || "").trim().toLowerCase();
  28. if (value) break;
  29. }
  30. if (!value) {
  31. throw new Error(`empty value field, expected one of: ${JSON_VALUE_KEYS.join(", ")}`);
  32. }
  33. return value;
  34. }
  35. async function operator(proxies = [], targetPlatform, context) {
  36. const cache = scriptResourceCache;
  37. let value = cache.get(CACHE_KEY);
  38. if (!value) {
  39. try {
  40. value = await fetchValueViaSubStore();
  41. cache.set(CACHE_KEY, value, CACHE_TTL_MS);
  42. } catch (e) {
  43. console.log(`[vmess-domain-rotator] fetch failed: ${e.message}`);
  44. return proxies;
  45. }
  46. }
  47. let updated = 0;
  48. for (const p of proxies) {
  49. if (!p || p.type !== "vmess") continue;
  50. if (!NODE_NAME_REGEX.test(p.name || "")) continue;
  51. if (p.server !== value) {
  52. p.server = value;
  53. updated += 1;
  54. }
  55. }
  56. console.log(`[vmess-domain-rotator] value=${value}, updated=${updated}, total=${proxies.length}, target=${targetPlatform}`);
  57. return proxies;
  58. }