| 73 | |
| 74 | |
| 75 | function NobyDa_Tools() { |
| 76 | this.isLoon = typeof $loon !== "undefined"; |
| 77 | this.isQuanX = typeof $configuration !== 'undefined'; |
| 78 | this.isSurge = typeof $environment !== 'undefined' && $environment['surge-version']; |
| 79 | this.isNode = typeof module !== 'undefined' && !!module.exports; |
| 80 | this.http = Object.fromEntries( |
| 81 | ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"].map( |
| 82 | (m) => [m.toLowerCase(), (opts) => { |
| 83 | if (this.isQuanX) return new Promise((resolve, reject) => { |
| 84 | $task.fetch({ method: m, ...opts }) |
| 85 | .then((r) => resolve({ |
| 86 | status: r.statusCode, headers: r.headers, body: r.body, |
| 87 | }), e => reject(e.error)) |
| 88 | }); |
| 89 | if (this.isSurge || this.isLoon || this.isNode) return new Promise((resolve, reject) => { |
| 90 | const request = this.isNode ? require("request") : $httpClient; |
| 91 | request[m.toLowerCase()](opts, (e, r, b) => { |
| 92 | if (e) reject(e); |
| 93 | else resolve({ status: r.status || r.statusCode, headers: r.headers, body: b }) |
| 94 | }) |
| 95 | }); |
| 96 | }] |
| 97 | ) |
| 98 | ); |
| 99 | this.policy = () => { |
| 100 | if (this.isSurge) return new Promise((r) => { |
| 101 | $httpAPI("GET", "v1/policies", null, (v) => r({ |
| 102 | proxy: v.proxies, |
| 103 | group: v['policy-groups'] |
| 104 | })) |
| 105 | }); |
| 106 | if (this.isQuanX) return new Promise((r) => { |
| 107 | $configuration.sendMessage({ |
| 108 | action: "get_customized_policy" |
| 109 | }).then(b => r({ |
| 110 | proxy: Object.keys(b.ret) |
| 111 | .reduce((t, i) => [...new Set([...t, ...b.ret[i].candidates || []])], []) |
| 112 | .filter((v) => !b.ret[v] && !['direct', 'proxy', 'reject'].includes(v)), |
| 113 | group: Object.keys(b.ret) |
| 114 | }), () => r({})); |
| 115 | }); |
| 116 | if (this.isLoon) return new Promise(async (r1) => { |
| 117 | const config = JSON.parse($config.getConfig()); |
| 118 | const groupData = await Promise.all(config['all_policy_groups'].map((i) => new Promise((r2) => { |
| 119 | $config.getSubPolicies(i, (b) => { r2(JSON.parse(b || '[]')) }) |
| 120 | }))); |
| 121 | r1({ |
| 122 | proxy: groupData.reduce((t, i) => [...new Set([...t, ...i.filter((v) => { |
| 123 | return v.type == 'node' && !config['all_buildin_nodes'].includes(v.name) |
| 124 | }).map((n) => n.name)])], []), |
| 125 | group: config['all_policy_groups'] |
| 126 | }) |
| 127 | }); |
| 128 | }; |
| 129 | this.data = Object.fromEntries(['read', 'write'].map( |
| 130 | (i) => [i, (v1, v2) => { |
| 131 | if (i === 'write') { |
| 132 | if (this.isSurge || this.isLoon) return $persistentStore.write(v1, v2); |