MagicJS3.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  1. function MagicJS(scriptName = "MagicJS", logLevel = "INFO") {
  2. const MagicEnvironment = () => {
  3. const isLoon = typeof $loon !== "undefined";
  4. const isQuanX = typeof $task !== "undefined";
  5. const isNode = typeof module !== "undefined";
  6. const isSurge = typeof $httpClient !== "undefined" && !isLoon;
  7. const isStorm = typeof $storm !== "undefined";
  8. const isStash = typeof $environment !== "undefined" && typeof $environment["stash-build"] !== "undefined";
  9. const isSurgeLike = isSurge || isLoon || isStorm || isStash;
  10. const isScriptable = typeof importModule !== "undefined";
  11. return {
  12. isLoon: isLoon,
  13. isQuanX: isQuanX,
  14. isNode: isNode,
  15. isSurge: isSurge,
  16. isStorm: isStorm,
  17. isStash: isStash,
  18. isSurgeLike: isSurgeLike,
  19. isScriptable: isScriptable,
  20. get name() {
  21. if (isLoon) {
  22. return "Loon";
  23. } else if (isQuanX) {
  24. return "QuantumultX";
  25. } else if (isNode) {
  26. return "NodeJS";
  27. } else if (isSurge) {
  28. return "Surge";
  29. } else if (isScriptable) {
  30. return "Scriptable";
  31. } else {
  32. return "unknown";
  33. }
  34. },
  35. get build() {
  36. if (isSurge) {
  37. return $environment["surge-build"];
  38. } else if (isStash) {
  39. return $environment["stash-build"];
  40. } else if (isStorm) {
  41. return $storm.buildVersion;
  42. }
  43. },
  44. get language() {
  45. if (isSurge || isStash) {
  46. return $environment["language"];
  47. }
  48. },
  49. get version() {
  50. if (isSurge) {
  51. return $environment["surge-version"];
  52. } else if (isStash) {
  53. return $environment["stash-version"];
  54. } else if (isStorm) {
  55. return $storm.appVersion;
  56. } else if (isNode) {
  57. return process.version;
  58. }
  59. },
  60. get system() {
  61. if (isSurge) {
  62. return $environment["system"];
  63. } else if (isNode) {
  64. return process.platform;
  65. }
  66. },
  67. get systemVersion() {
  68. if (isStorm) {
  69. return $storm.systemVersion;
  70. }
  71. },
  72. get deviceName() {
  73. if (isStorm) {
  74. return $storm.deviceName;
  75. }
  76. }
  77. };
  78. };
  79. const MagicLogger = (scriptName, logLevel = "INFO") => {
  80. let _level = logLevel;
  81. let logSeparator = '\n';
  82. const logLevels = {
  83. SNIFFER: 6,
  84. DEBUG: 5,
  85. INFO: 4,
  86. NOTIFY: 3,
  87. WARNING: 2,
  88. ERROR: 1,
  89. CRITICAL: 0,
  90. NONE: -1
  91. };
  92. const logEmoji = {
  93. SNIFFER: "",
  94. DEBUG: "",
  95. INFO: "",
  96. NOTIFY: "",
  97. WARNING: "❗ ",
  98. ERROR: "❌ ",
  99. CRITICAL: "❌ ",
  100. NONE: ""
  101. };
  102. const _log = (msg, level = "INFO") => {
  103. // let level = "INFO";
  104. // if (logs.length > 1) {
  105. // let lastParam = logs[logs.length-1];
  106. // let leveArr = ["SNIFFER","DEBUG","INFO","NOTIFY","WARNING","ERROR","RETRY"];
  107. // if(typeof(lastParam) == 'string' && leveArr.indexOf(lastParam) > 0){
  108. // level = lastParam;
  109. // logs.length = logs.length-1;
  110. // }
  111. // }
  112. // let msg = logs.join('\n');
  113. if (!(logLevels[_level] < logLevels[level.toUpperCase()])) console.log(`██[${scriptName}][${level}]` + '' + `${logEmoji[level.toUpperCase()]}${msg}` + '\n' + ``);
  114. };
  115. const setLevel = logLevel => {
  116. _level = logLevel;
  117. };
  118. return {
  119. getLevel: () => {
  120. return _level;
  121. },
  122. setLevel: setLevel,
  123. sniffer: (...logs) => {
  124. let msg = logs.join(logSeparator);
  125. _log(msg, "SNIFFER");
  126. },
  127. log: (...logs) => {
  128. let msg = logs.join(logSeparator);
  129. console.log(`██[${scriptName}]` + '' + `${msg}` + '\n' + ``);
  130. },
  131. debug: (...logs) => {
  132. let msg = logs.join(logSeparator);
  133. _log(msg, "DEBUG");
  134. },
  135. info: (...logs) => {
  136. let msg = logs.join(logSeparator);
  137. _log(msg, "INFO");
  138. },
  139. notify: (...logs) => {
  140. let msg = logs.join(logSeparator);
  141. _log(msg, "NOTIFY");
  142. },
  143. warning: (...logs) => {
  144. let msg = logs.join(logSeparator);
  145. _log(msg, "WARNING");
  146. },
  147. error: (...logs) => {
  148. let msg = logs.join(logSeparator);
  149. _log(msg, "ERROR");
  150. },
  151. retry: (...logs) => {
  152. let msg = logs.join(logSeparator);
  153. _log(msg, "RETRY");
  154. }
  155. };
  156. };
  157. return new class {
  158. constructor(scriptName, logLevel) {
  159. this._startTime = Date.now();
  160. this.version = "3.0.0";
  161. this.scriptName = scriptName;
  162. this.env = MagicEnvironment();
  163. this.logger = MagicLogger(scriptName, logLevel);
  164. this.http = typeof MagicHttp === "function" ? MagicHttp(this.env, this.logger) : undefined;
  165. this.data = typeof MagicData === "function" ? MagicData(this.env, this.logger) : undefined;
  166. this.notification = typeof MagicNotification === "function" ? MagicNotification(this.scriptName, this.env, this.logger, this.http) : undefined;
  167. this.utils = typeof MagicUtils === "function" ? MagicUtils(this.env, this.logger) : undefined;
  168. this.qinglong = typeof MagicQingLong === "function" ? MagicQingLong(this.env, this.data, this.logger) : undefined;
  169. if (typeof this.data !== "undefined") {
  170. let magicLoglevel = this.data.read("magic_loglevel");
  171. const barkUrl = this.data.read("magic_bark_url");
  172. if (magicLoglevel) {
  173. this.logger.setLevel(magicLoglevel.toUpperCase());
  174. }
  175. if (barkUrl) {
  176. this.notification.setBark(barkUrl);
  177. }
  178. }
  179. this.logger.info(`${scriptName}, 开始执行!`);
  180. // this.checkRecordRequestBody();
  181. }
  182. get isRequest() {
  183. return typeof $request !== "undefined";
  184. }
  185. get isStrictRequest() {
  186. return typeof $request !== "undefined" && typeof $response === "undefined";
  187. }
  188. get isResponse() {
  189. return typeof $response !== "undefined";
  190. }
  191. get isDebug() {
  192. return this.logger.level === "DEBUG";
  193. }
  194. get request() {
  195. return typeof $request !== "undefined" ? $request : undefined;
  196. }
  197. get response() {
  198. if (typeof $response !== "undefined") {
  199. if ($response.hasOwnProperty("status")) $response["statusCode"] = $response["status"];
  200. if ($response.hasOwnProperty("statusCode")) $response["status"] = $response["statusCode"];
  201. return $response;
  202. } else {
  203. return undefined;
  204. }
  205. }
  206. log(...logs) {
  207. this.logger.log(logs);
  208. }
  209. toStr(data, defVal = null) {
  210. try {
  211. return JSON.stringify(data);
  212. } catch {
  213. return defVal;
  214. }
  215. }
  216. toObj(str, defVal = null) {
  217. try {
  218. return JSON.parse(str);
  219. } catch {
  220. return defVal;
  221. }
  222. }
  223. checkRecordRequestBody() {
  224. if (!this.isRequest) {
  225. return;
  226. }
  227. const reqBody = $request.body;
  228. if (!reqBody) {
  229. return;
  230. }
  231. const env = this.env;
  232. const path = $request.path;
  233. let cacheKey = this.scriptName + "#" + path.replace("/", "_");
  234. cacheKey = cacheKey.replace("?", "#");
  235. if (env.isQuanX) $prefs.setValueForKey(reqBody, cacheKey);
  236. if (env.isLoon || env.isSurge) $persistentStore.write(reqBody, cacheKey);
  237. if (env.isNode) {
  238. const node_fs = require("fs");
  239. node_fs.writeFileSync(
  240. `${cacheKey}.json`,
  241. reqBody,
  242. {
  243. flag: "w"
  244. },
  245. (err) => console.log(err)
  246. );
  247. }
  248. }
  249. getRequestBody() {
  250. const env = this.env;
  251. const path = $request.path;
  252. let cacheKey = this.scriptName + "#" + path.replace("/", "_");
  253. cacheKey = cacheKey.replace("?", "#");
  254. if (env.isSurge || env.isLoon) {
  255. return $persistentStore.read(cacheKey);
  256. }
  257. if (env.isQuanX) {
  258. return $prefs.valueForKey(cacheKey);
  259. }
  260. if (env.isNode) {
  261. const fpath = `${cacheKey}.json`;
  262. const node_fs = require("fs");
  263. if (!node_fs.existsSync(fpath)) {
  264. return JSON.parse(
  265. node_fs.readFileSync(fpath)
  266. );
  267. }
  268. }
  269. }
  270. getResponseBody() {
  271. if ($response) {
  272. return $response.body;
  273. }
  274. }
  275. // 解析cookie字符串的函数
  276. parseCookies(cookieString, decodeURI = false) {
  277. let dict = {};
  278. if (decodeURI) {
  279. cookieString && cookieString.split(';').forEach(function (cookie) {
  280. let parts = cookie.split('=');
  281. dict[parts.shift().trim()] = decodeURIComponent(parts.join('='));
  282. });
  283. } else {
  284. cookieString && cookieString.split(';').forEach(function (cookie) {
  285. let parts = cookie.split('=');
  286. dict[parts.shift().trim()] = parts.join('=');
  287. });
  288. }
  289. return dict;
  290. }
  291. // 系列化为cookie字符串
  292. serializeCookies(cookieData, encodeURI = false) {
  293. const parts = [];
  294. if (encodeURI) {
  295. for (let key in cookieData) {
  296. let value = cookieData[key];
  297. let cookiePart = `${key}=${encodeURIComponent(value)}`;
  298. parts.push(cookiePart);
  299. }
  300. } else {
  301. for (let key in cookieData) {
  302. let value = cookieData[key];
  303. let cookiePart = `${key}=${value}`;
  304. parts.push(cookiePart);
  305. }
  306. }
  307. return parts.join('; ');
  308. }
  309. parseSetCookies(setCookieStr) {
  310. let tmpArr = setCookieStr.split(/,\s*/).map(cookieStr => {
  311. const cookieParts = cookieStr.trim().split(/;\s*(?=[^=]+=[^;]*)/); // 使用正则表达式分割
  312. const nameValue = cookieParts[0].split('=').map(part => part.trim());
  313. const cookieObject = { name: nameValue[0], value: nameValue[1] };
  314. // 解析其他属性
  315. cookieParts.slice(1).forEach(attr => {
  316. const [key, val] = attr.split('=').map(part => part.trim());
  317. cookieObject[key] = val || true; // 如果没有值,则设置为 true
  318. });
  319. return cookieObject;
  320. });
  321. let retData = [];
  322. let i = 0;
  323. while (i < tmpArr.length) {
  324. const curItem = tmpArr[i];
  325. retData.push(curItem);
  326. if (curItem.Expires) {
  327. const nextItem = tmpArr[i + 1];
  328. if (nextItem) {
  329. curItem.Expires = `${curItem.Expires},${nextItem.name}`;
  330. }
  331. i += 2;
  332. } else {
  333. i += 1;
  334. }
  335. }
  336. return retData;
  337. }
  338. objToQueryStr(obj, encode) {
  339. let str = '';
  340. for (const key in obj) {
  341. let value = obj[key];
  342. if (value != null && value !== '') {
  343. if (typeof value === 'object') {
  344. value = JSON.stringify(value);
  345. } else if (encode) {
  346. value = encodeURIComponent(value);
  347. }
  348. str += `${key}=${value}&`;
  349. }
  350. }
  351. str = str.substring(0, str.length - 1);
  352. return str;
  353. }
  354. parseQueryStr(str) {
  355. let obj = {};
  356. if (str.indexOf("?") > -1) {
  357. str = str.split("?")[1];
  358. }
  359. let arr = str.split("&");
  360. for (let i = 0; i < arr.length; i++) {
  361. let kv = arr[i].split("=");
  362. obj[kv[0]] = kv[1];
  363. }
  364. return obj;
  365. }
  366. deepClone(obj, newObj) {
  367. newObj = newObj || {};
  368. for (let key in obj) {
  369. if (typeof obj[key] == 'object') {
  370. newObj[key] = (obj[key].constructor === Array) ? [] : {};
  371. this.deepClone(obj[key], newObj[key]);
  372. } else {
  373. newObj[key] = obj[key];
  374. }
  375. }
  376. return newObj;
  377. }
  378. /**
  379. * formatDate y:年 M:月 d:日 q:季 H:时 m:分 s:秒 S:毫秒
  380. */
  381. formatDate(date, format) {
  382. let o = {
  383. 'M+': date.getMonth() + 1,
  384. 'd+': date.getDate(),
  385. 'H+': date.getHours(),
  386. 'm+': date.getMinutes(),
  387. 's+': date.getSeconds(),
  388. 'q+': Math.floor((date.getMonth() + 3) / 3),
  389. 'S': date.getMilliseconds()
  390. }
  391. if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  392. for (let k in o)
  393. if (new RegExp('(' + k + ')').test(format))
  394. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  395. return format
  396. }
  397. /**
  398. * parseDate 字符串格式,默认'yyyy-MM-dd',支持如下:y、M、d、H、m、s、S,不支持w和q
  399. */
  400. parseDate(str, format) {
  401. format = format || 'yyyy-MM-dd';
  402. let obj = { y: 0, M: 1, d: 0, H: 0, h: 0, m: 0, s: 0, S: 0 };
  403. format.replace(/([^yMdHmsS]*?)(([yMdHmsS])\3*)([^yMdHmsS]*?)/g, function (m, $1, $2, $3, $4, idx, old) {
  404. str = str.replace(new RegExp($1 + '(\\d{' + $2.length + '})' + $4), function (_m, _$1) {
  405. obj[$3] = parseInt(_$1);
  406. return '';
  407. });
  408. return '';
  409. });
  410. obj.M--; // 月份是从0开始的,所以要减去1
  411. let date = new Date(obj.y, obj.M, obj.d, obj.H, obj.m, obj.s);
  412. if (obj.S !== 0) date.setMilliseconds(obj.S); // 如果设置了毫秒
  413. return date;
  414. }
  415. getBaseDoneHeaders(mixHeaders = {}) {
  416. return Object.assign(
  417. {
  418. 'Access-Control-Allow-Origin': '*',
  419. 'Access-Control-Allow-Methods': 'POST,GET,OPTIONS,PUT,DELETE',
  420. 'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
  421. },
  422. mixHeaders
  423. )
  424. }
  425. getHtmlDoneHeaders() {
  426. return this.getBaseDoneHeaders({
  427. 'Content-Type': 'text/html;charset=UTF-8'
  428. })
  429. }
  430. getJsonDoneHeaders() {
  431. return this.getBaseDoneHeaders({
  432. 'Content-Type': 'text/json; charset=utf-8',
  433. 'Connection': 'keep-alive'
  434. })
  435. }
  436. /**
  437. * 推送微信消息
  438. * 管理后台 https://wxpusher.zjiecode.com/admin/
  439. * 通用助手 AT_7wDWqSoT8xpJCQqJtHpshKhw7kXc0XCW
  440. * 光予助手 AT_rTc93GQYIdMU8XLRnoJaSea8WkfhSzhX
  441. * i茅台助手 AT_6b8KjFF3bYqcr7Cv7woUUES7KadNFbvR
  442. * 个人uid UID_6P4B00X6Zv8U2oKC0I2R09emxtqq
  443. */
  444. doWxpusherSend(data) {
  445. // let data = {
  446. // appToken: "AT_rTc93GQYIdMU8XLRnoJaSea8WkfhSzhX",
  447. // content: content,// 这是主体内容
  448. // summary: summary,// 该参数可选,默认为 msg 的前10个字符
  449. // contentType: 1,
  450. // topicIds: [],
  451. // uids: [
  452. // "UID_6P4B00X6Zv8U2oKC0I2R09emxtqq"
  453. // ],
  454. // url: "",
  455. // verifyPay: false
  456. // };
  457. // if (url) {
  458. // data.url = url;
  459. // }
  460. const headers = this.getJsonDoneHeaders();
  461. headers.Host = 'wxpusher.zjiecode.com';
  462. headers['Content-Type'] = 'application/json;charset=UTF-8';
  463. let options = {
  464. url: 'https://wxpusher.zjiecode.com/api/send/message',
  465. headers: headers,
  466. body: JSON.stringify(data),
  467. };
  468. return this.http.post(options);
  469. }
  470. fastWxpusherSend(content, summary = "", url = "") {
  471. let data = {
  472. appToken: "AT_7wDWqSoT8xpJCQqJtHpshKhw7kXc0XCW",
  473. content: content,// 这是主体内容
  474. summary: summary,// 该参数可选,默认为 msg 的前10个字符
  475. contentType: 1,//内容类型 1表示文字 2表示html(只发送body标签内部的数据即可,不包括body标签,推荐使用这种) 3表示markdown
  476. // topicIds: [32852],
  477. topicIds: [],
  478. uids: [
  479. "UID_6P4B00X6Zv8U2oKC0I2R09emxtqq"
  480. ],
  481. url: url,
  482. verifyPay: false,
  483. verifyPayType: 0,
  484. };
  485. return this.doWxpusherSend(data);
  486. }
  487. isEmpty(obj) {
  488. return typeof obj == "undefined" || obj == null || obj == "" || obj == "null" || obj == "undefined" || obj.length === 0
  489. }
  490. base64Encode(str) {
  491. let base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  492. let out, i, len;
  493. let c1, c2, c3;
  494. len = str.length;
  495. i = 0;
  496. out = "";
  497. while (i < len) {
  498. c1 = str.charCodeAt(i++) & 0xff;
  499. if (i == len) {
  500. out += base64EncodeChars.charAt(c1 >> 2);
  501. out += base64EncodeChars.charAt((c1 & 0x3) << 4);
  502. out += "==";
  503. break;
  504. }
  505. c2 = str.charCodeAt(i++);
  506. if (i == len) {
  507. out += base64EncodeChars.charAt(c1 >> 2);
  508. out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  509. out += base64EncodeChars.charAt((c2 & 0xF) << 2);
  510. out += "=";
  511. break;
  512. }
  513. c3 = str.charCodeAt(i++);
  514. out += base64EncodeChars.charAt(c1 >> 2);
  515. out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  516. out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  517. out += base64EncodeChars.charAt(c3 & 0x3F);
  518. }
  519. return out;
  520. }
  521. base64Decode(input) {
  522. const base64_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  523. // 确保输入是一个正确的Base64编码字符串
  524. if (/([^\s]+[^0-9a-zA-Z\+\/\=]|[^0-9a-zA-Z\+\/\=]\s+)/.test(input)) {
  525. throw new Error('Invalid base64 input');
  526. }
  527. let str = input.replace(/\s/g, '');
  528. let output = '';
  529. let chr1, chr2, chr3;
  530. let enc1, enc2, enc3, enc4;
  531. let i = 0;
  532. while (i < str.length) {
  533. enc1 = base64_chars.indexOf(str.charAt(i++));
  534. enc2 = base64_chars.indexOf(str.charAt(i++));
  535. enc3 = base64_chars.indexOf(str.charAt(i++));
  536. enc4 = base64_chars.indexOf(str.charAt(i++));
  537. chr1 = (enc1 << 2) | (enc2 >> 4);
  538. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  539. chr3 = ((enc3 & 3) << 6) | enc4;
  540. output = output + String.fromCharCode(chr1);
  541. if (enc3 !== 64) {
  542. output = output + String.fromCharCode(chr2);
  543. }
  544. if (enc4 !== 64) {
  545. output = output + String.fromCharCode(chr3);
  546. }
  547. }
  548. output = this.utf8Decode(output);
  549. return output;
  550. }
  551. utf8Decode(str_data) {
  552. let tmp_arr = [],
  553. i = 0,
  554. c1 = 0,
  555. seqlen = 0;
  556. str_data = str_data.replace(/\r\n/g, "\n");
  557. while (i < str_data.length) {
  558. c1 = str_data.charCodeAt(i) & 0xFF;
  559. seqlen = 0;
  560. // Single byte sequence (0xxxxxxx)
  561. if (c1 <= 0xBF) {
  562. c1 = (c1 & 0x7F);
  563. seqlen = 1;
  564. } else if (c1 <= 0xDF) {
  565. c1 = (c1 & 0x1F);
  566. seqlen = 2;
  567. } else if (c1 <= 0xEF) {
  568. c1 = (c1 & 0x0F);
  569. seqlen = 3;
  570. } else {
  571. c1 = (c1 & 0x07);
  572. seqlen = 4;
  573. }
  574. for (let ai = 1; ai < seqlen; ++ai) {
  575. c1 = ((c1 << 0x06) | (str_data.charCodeAt(ai + i) & 0x3F));
  576. }
  577. if (seqlen === 4) {
  578. c1 -= 0x10000;
  579. tmp_arr.push(String.fromCharCode(0xD800 | ((c1 >> 10) & 0x3FF)));
  580. tmp_arr.push(String.fromCharCode(0xDC00 | (c1 & 0x3FF)));
  581. } else {
  582. tmp_arr.push(String.fromCharCode(c1));
  583. }
  584. i += seqlen;
  585. }
  586. return tmp_arr.join("");
  587. }
  588. parseJwt(token) {
  589. try {
  590. const segments = token.split('.');
  591. const base64HeaderUrl = segments[0];
  592. const base64Header = base64HeaderUrl.replace(/-/g, '+').replace(/_/g, '/');
  593. const jsonStrHeader = this.base64Decode(base64Header).replace(/\0/g, '');
  594. const headerData = JSON.parse(jsonStrHeader);
  595. const base64PayloadUrl = segments[1];
  596. const base64Payload = base64PayloadUrl.replace(/-/g, '+').replace(/_/g, '/');
  597. const jsonStrPayload = this.base64Decode(base64Payload).replace(/\0/g, '');
  598. const payloadData = JSON.parse(jsonStrPayload);
  599. return {
  600. header: headerData,
  601. payload: payloadData,
  602. signature: segments[2],
  603. };
  604. } catch (e) {
  605. this.log(e);
  606. return null;
  607. }
  608. }
  609. costTime() {
  610. let info = `${this.scriptName}执行完毕!`
  611. // if (this.isNode && this.isExecComm) {
  612. // info = `指令【${this.comm[1]}】执行完毕!`
  613. // }
  614. this._endTime = new Date().getTime();
  615. const ms = this._endTime - this._startTime;
  616. const costTime = ms / 1000;
  617. this.logger.info(`${info}耗时【${costTime}】秒`);
  618. }
  619. done = (value = {}) => {
  620. // this._endTime = Date.now();
  621. // let span = (this._endTime - this._startTime) / 1e3;
  622. // this.logger.info(`SCRIPT COMPLETED: ${span} S.`);
  623. this.costTime();
  624. if (typeof $done !== "undefined") {
  625. $done(value);
  626. }
  627. };
  628. }(scriptName, logLevel);
  629. }
  630. function MagicHttp(env, logger) {
  631. const phoneUA = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1";
  632. const computerUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36 Edg/84.0.522.59";
  633. let axiosInstance;
  634. if (env.isNode) {
  635. const axios = require("axios");
  636. axiosInstance = axios.create();
  637. }
  638. class InterceptorManager {
  639. constructor(isRequest = true) {
  640. this.handlers = [];
  641. this.isRequest = isRequest;
  642. }
  643. use(fulfilled, rejected, options) {
  644. if (typeof fulfilled === "function") {
  645. logger.debug(`Register fulfilled ${fulfilled.name}`);
  646. }
  647. if (typeof rejected === "function") {
  648. logger.debug(`Register rejected ${rejected.name}`);
  649. }
  650. this.handlers.push({
  651. fulfilled: fulfilled,
  652. rejected: rejected,
  653. synchronous: options && typeof options.synchronous === "boolean" ? options.synchronous : false,
  654. runWhen: options ? options.runWhen : null
  655. });
  656. return this.handlers.length - 1;
  657. }
  658. eject(id) {
  659. if (this.handlers[id]) {
  660. this.handlers[id] = null;
  661. }
  662. }
  663. forEach(fn) {
  664. this.handlers.forEach(element => {
  665. if (element !== null) {
  666. fn(element);
  667. }
  668. });
  669. }
  670. }
  671. function paramsToQueryString(config) {
  672. let _config = {
  673. ...config
  674. };
  675. if (!!_config.params) {
  676. if (!env.isNode) {
  677. let qs = Object.keys(_config.params).map(key => {
  678. const encodeKey = encodeURIComponent(key);
  679. _config.url = _config.url.replace(new RegExp(`${key}=[^&]*`, "ig"), "");
  680. _config.url = _config.url.replace(new RegExp(`${encodeKey}=[^&]*`, "ig"), "");
  681. return `${encodeKey}=${encodeURIComponent(_config.params[key])}`;
  682. }).join("&");
  683. if (_config.url.indexOf("?") < 0) _config.url += "?";
  684. if (!/(&|\?)$/g.test(_config.url)) {
  685. _config.url += "&";
  686. }
  687. _config.url += qs;
  688. delete _config.params;
  689. logger.debug(`Params to QueryString: ${_config.url}`);
  690. }
  691. }
  692. return _config;
  693. }
  694. const mergeConfig = (method, configOrUrl) => {
  695. let config = typeof configOrUrl === "object" ? {
  696. headers: {},
  697. ...configOrUrl
  698. } : {
  699. url: configOrUrl,
  700. headers: {}
  701. };
  702. if (!config.method) {
  703. config["method"] = method;
  704. }
  705. config = paramsToQueryString(config);
  706. if (config["rewrite"] === true) {
  707. if (env.isSurge) {
  708. config.headers["X-Surge-Skip-Scripting"] = false;
  709. delete config["rewrite"];
  710. } else if (env.isQuanX) {
  711. config["hints"] = false;
  712. delete config["rewrite"];
  713. }
  714. }
  715. if (env.isSurgeLike) {
  716. const contentType = config.headers["content-type"] || config.headers["Content-Type"];
  717. if (config["method"] !== "GET" && contentType && contentType.indexOf("application/json") >= 0 && config.body instanceof Array) {
  718. config.body = JSON.stringify(config.body);
  719. logger.debug(`Convert Array object to String: ${config.body}`);
  720. }
  721. } else if (env.isQuanX) {
  722. if (config.hasOwnProperty("body") && typeof config["body"] !== "string") config["body"] = JSON.stringify(config["body"]);
  723. config["method"] = method;
  724. } else if (env.isNode) {
  725. if (method === "POST" || method === "PUT" || method === "PATCH" || method === "DELETE") {
  726. config.data = config.data || config.body;
  727. } else if (method === "GET") {
  728. config.params = config.params || config.body;
  729. }
  730. delete config.body;
  731. }
  732. return config;
  733. };
  734. const modifyResponse = (resp, config = null) => {
  735. if (resp) {
  736. let _resp = {
  737. ...resp,
  738. config: resp.config || config,
  739. status: resp.statusCode || resp.status,
  740. body: resp.body || resp.data,
  741. headers: resp.headers || resp.header
  742. };
  743. if (typeof _resp.body === "string") {
  744. try {
  745. _resp.body = JSON.parse(_resp.body);
  746. } catch { }
  747. }
  748. delete _resp.data;
  749. return _resp;
  750. } else {
  751. return resp;
  752. }
  753. };
  754. const convertHeadersToLowerCase = headers => {
  755. return Object.keys(headers).reduce((acc, key) => {
  756. acc[key.toLowerCase()] = headers[key];
  757. return acc;
  758. }, {});
  759. };
  760. const convertHeadersToCamelCase = headers => {
  761. return Object.keys(headers).reduce((acc, key) => {
  762. const newKey = key.split("-").map(word => word[0].toUpperCase() + word.slice(1)).join("-");
  763. acc[newKey] = headers[key];
  764. return acc;
  765. }, {});
  766. };
  767. const raiseExceptionByStatusCode = (resp, config = null) => {
  768. if (!!resp && resp.status >= 400) {
  769. logger.debug(`Raise exception when status code is ${resp.status}`);
  770. return {
  771. name: "RequestException",
  772. message: `Request failed with status code ${resp.status}`,
  773. config: config || resp.config,
  774. response: resp
  775. };
  776. }
  777. };
  778. const interceptors = {
  779. request: new InterceptorManager(),
  780. response: new InterceptorManager(false)
  781. };
  782. let requestInterceptorChain = [];
  783. let responseInterceptorChain = [];
  784. let synchronousRequestInterceptors = true;
  785. function interceptConfig(config) {
  786. config = paramsToQueryString(config);
  787. logger.debug(`HTTP ${config["method"].toUpperCase()}:` + '\n' + `${JSON.stringify(config)}`);
  788. return config;
  789. }
  790. function interceptResponse(resp) {
  791. try {
  792. resp = !!resp ? modifyResponse(resp) : resp;
  793. logger.sniffer(`HTTP ${resp.config["method"].toUpperCase()}:` + '\n' + `${JSON.stringify(resp.config)}` + '\n' + `STATUS CODE:` + '\n' + `${resp.status}` + '\n' + `RESPONSE:` + '\n' + `${typeof resp.body === "object" ? JSON.stringify(resp.body) : resp.body}`);
  794. const err = raiseExceptionByStatusCode(resp);
  795. if (!!err) {
  796. return Promise.reject(err);
  797. }
  798. return resp;
  799. } catch (err) {
  800. logger.error(err);
  801. return resp;
  802. }
  803. }
  804. const registerInterceptors = config => {
  805. try {
  806. requestInterceptorChain = [];
  807. responseInterceptorChain = [];
  808. interceptors.request.forEach(interceptor => {
  809. if (typeof interceptor.runWhen === "function" && interceptor.runWhen(config) === false) {
  810. return;
  811. }
  812. synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
  813. requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
  814. });
  815. interceptors.response.forEach(interceptor => {
  816. responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
  817. });
  818. } catch (err) {
  819. logger.error(`Failed to register interceptors: ${err}.`);
  820. }
  821. };
  822. const request = (method, config) => {
  823. let dispatchRequest;
  824. const _method = method.toUpperCase();
  825. config = mergeConfig(_method, config);
  826. if (env.isNode) {
  827. dispatchRequest = axiosInstance;
  828. } else {
  829. if (env.isSurgeLike) {
  830. dispatchRequest = config => {
  831. return new Promise((resolve, reject) => {
  832. $httpClient[method.toLowerCase()](config, (err, resp, body) => {
  833. if (err) {
  834. let newErr = {
  835. name: err.name || err,
  836. message: err.message || err,
  837. stack: err.stack || err,
  838. config: config,
  839. response: modifyResponse(resp)
  840. };
  841. reject(newErr);
  842. } else {
  843. resp.config = config;
  844. resp.body = body;
  845. resolve(resp);
  846. }
  847. });
  848. });
  849. };
  850. } else {
  851. dispatchRequest = config => {
  852. return new Promise((resolve, reject) => {
  853. $task.fetch(config).then(resp => {
  854. resp = modifyResponse(resp, config);
  855. const err = raiseExceptionByStatusCode(resp, config);
  856. if (err) {
  857. return Promise.reject(err);
  858. }
  859. resolve(resp);
  860. }).catch(err => {
  861. let newErr = {
  862. name: err.message || err.error,
  863. message: err.message || err.error,
  864. stack: err.error,
  865. config: config,
  866. response: !!err.response ? modifyResponse(err.response) : null
  867. };
  868. reject(newErr);
  869. });
  870. });
  871. };
  872. }
  873. }
  874. let promise;
  875. registerInterceptors(config);
  876. const defaultRequestInterceptors = [interceptConfig, undefined];
  877. const defaultResponseInterceptors = [interceptResponse, undefined];
  878. if (!synchronousRequestInterceptors) {
  879. logger.debug("Interceptors are executed in asynchronous mode");
  880. let chain = [dispatchRequest, undefined];
  881. Array.prototype.unshift.apply(chain, defaultRequestInterceptors);
  882. Array.prototype.unshift.apply(chain, requestInterceptorChain);
  883. chain = chain.concat(defaultResponseInterceptors);
  884. chain = chain.concat(responseInterceptorChain);
  885. promise = Promise.resolve(config);
  886. while (chain.length) {
  887. try {
  888. let onFulfilled = chain.shift();
  889. let onRejected = chain.shift();
  890. if (!env.isNode && config["timeout"] && onFulfilled === dispatchRequest) {
  891. onFulfilled = requestTimeout;
  892. }
  893. if (typeof onFulfilled === "function") {
  894. logger.debug(`Executing request fulfilled ${onFulfilled.name}`);
  895. }
  896. if (typeof onRejected === "function") {
  897. logger.debug(`Executing request rejected ${onRejected.name}`);
  898. }
  899. promise = promise.then(onFulfilled, onRejected);
  900. } catch (err) {
  901. logger.error(`request exception: ${err}`);
  902. }
  903. }
  904. return promise;
  905. } else {
  906. logger.debug("Interceptors are executed in synchronous mode");
  907. Array.prototype.unshift.apply(requestInterceptorChain, defaultRequestInterceptors);
  908. requestInterceptorChain = requestInterceptorChain.concat([interceptConfig, undefined]);
  909. while (requestInterceptorChain.length) {
  910. let onFulfilled = requestInterceptorChain.shift();
  911. let onRejected = requestInterceptorChain.shift();
  912. try {
  913. if (typeof onFulfilled === "function") {
  914. logger.debug(`Executing request fulfilled ${onFulfilled.name}`);
  915. }
  916. config = onFulfilled(config);
  917. } catch (error) {
  918. if (typeof onRejected === "function") {
  919. logger.debug(`Executing request rejected ${onRejected.name}`);
  920. }
  921. onRejected(error);
  922. break;
  923. }
  924. }
  925. try {
  926. if (!env.isNode && config["timeout"]) {
  927. promise = requestTimeout(config);
  928. } else {
  929. promise = dispatchRequest(config);
  930. }
  931. } catch (err) {
  932. return Promise.reject(err);
  933. }
  934. Array.prototype.unshift.apply(responseInterceptorChain, defaultResponseInterceptors);
  935. while (responseInterceptorChain.length) {
  936. promise = promise.then(responseInterceptorChain.shift(), responseInterceptorChain.shift());
  937. }
  938. return promise;
  939. }
  940. function requestTimeout(config) {
  941. try {
  942. const timer = new Promise((_, reject) => {
  943. setTimeout(() => {
  944. let err = {
  945. message: `timeout of ${config["timeout"]}ms exceeded.`,
  946. config: config
  947. };
  948. reject(err);
  949. }, config["timeout"]);
  950. });
  951. return Promise.race([dispatchRequest(config), timer]);
  952. } catch (err) {
  953. logger.error(`Request Timeout exception: ${err}.`);
  954. }
  955. }
  956. };
  957. return {
  958. request: request,
  959. interceptors: interceptors,
  960. convertHeadersToLowerCase: convertHeadersToLowerCase,
  961. convertHeadersToCamelCase: convertHeadersToCamelCase,
  962. modifyResponse: modifyResponse,
  963. get: configOrUrl => {
  964. return request("GET", configOrUrl);
  965. },
  966. post: configOrUrl => {
  967. return request("POST", configOrUrl);
  968. },
  969. put: configOrUrl => {
  970. return request("PUT", configOrUrl);
  971. },
  972. patch: configOrUrl => {
  973. return request("PATCH", configOrUrl);
  974. },
  975. delete: configOrUrl => {
  976. return request("DELETE", configOrUrl);
  977. },
  978. head: configOrUrl => {
  979. return request("HEAD", configOrUrl);
  980. },
  981. options: configOrUrl => {
  982. return request("OPTIONS", configOrUrl);
  983. }
  984. };
  985. }
  986. function MagicData(env, logger) {
  987. let node = {
  988. fs: undefined,
  989. data: {}
  990. };
  991. if (env.isNode) {
  992. node.fs = require("fs");
  993. try {
  994. node.fs.accessSync("./magic.json", node.fs.constants.R_OK | node.fs.constants.W_OK);
  995. } catch (err) {
  996. node.fs.writeFileSync("./magic.json", "{}", {
  997. encoding: "utf8"
  998. });
  999. }
  1000. node.data = require("./magic.json");
  1001. }
  1002. const defaultValueComparator = (oldVal, newVal) => {
  1003. if (typeof newVal === "object") {
  1004. return false;
  1005. } else {
  1006. return oldVal === newVal;
  1007. }
  1008. };
  1009. const _typeConvertor = val => {
  1010. if (val === "true") {
  1011. return true;
  1012. } else if (val === "false") {
  1013. return false;
  1014. } else if (typeof val === "undefined") {
  1015. return null;
  1016. } else {
  1017. return val;
  1018. }
  1019. };
  1020. const _valConvertor = (val, default_, session, read_no_session) => {
  1021. if (session) {
  1022. try {
  1023. if (typeof val === "string") val = JSON.parse(val);
  1024. if (val["magic_session"] === true) {
  1025. val = val[session];
  1026. } else {
  1027. val = null;
  1028. }
  1029. } catch {
  1030. val = null;
  1031. }
  1032. }
  1033. if (typeof val === "string" && val !== "null") {
  1034. try {
  1035. val = JSON.parse(val);
  1036. } catch { }
  1037. }
  1038. if (read_no_session === false && !!val && val["magic_session"] === true) {
  1039. val = null;
  1040. }
  1041. if ((val === null || typeof val === "undefined") && default_ !== null && typeof default_ !== "undefined") {
  1042. val = default_;
  1043. }
  1044. val = _typeConvertor(val);
  1045. return val;
  1046. };
  1047. const convertToObject = obj => {
  1048. if (typeof obj === "string") {
  1049. let data = {};
  1050. try {
  1051. data = JSON.parse(obj);
  1052. const type = typeof data;
  1053. if (type !== "object" || data instanceof Array || type === "bool" || data === null) {
  1054. data = {};
  1055. }
  1056. } catch { }
  1057. return data;
  1058. } else if (obj instanceof Array || obj === null || typeof obj === "undefined" || obj !== obj || typeof obj === "boolean") {
  1059. return {};
  1060. } else {
  1061. return obj;
  1062. }
  1063. };
  1064. const readForNode = (key, default_ = null, session = "", read_no_session = false, externalData = null) => {
  1065. let data = externalData || node.data;
  1066. if (!!data && typeof data[key] !== "undefined" && data[key] !== null) {
  1067. val = data[key];
  1068. } else {
  1069. val = !!session ? {} : null;
  1070. }
  1071. val = _valConvertor(val, default_, session, read_no_session);
  1072. return val;
  1073. };
  1074. const read = (key, default_ = null, session = "", read_no_session = false, externalData = null) => {
  1075. let val = "";
  1076. if (externalData || env.isNode) {
  1077. val = readForNode(key, default_, session, read_no_session, externalData);
  1078. } else {
  1079. if (env.isSurgeLike) {
  1080. val = $persistentStore.read(key);
  1081. } else if (env.isQuanX) {
  1082. val = $prefs.valueForKey(key);
  1083. }
  1084. val = _valConvertor(val, default_, session, read_no_session);
  1085. }
  1086. logger.debug(`READ DATA [${key}]${!!session ? `[${session}]` : ""} <${typeof val}>` + '\n' + `${JSON.stringify(val)}`);
  1087. return val;
  1088. };
  1089. const writeForNode = (key, val, session = "", externalData = null) => {
  1090. let data = externalData || node.data;
  1091. data = convertToObject(data);
  1092. if (!!session) {
  1093. let obj = convertToObject(data[key]);
  1094. obj["magic_session"] = true;
  1095. obj[session] = val;
  1096. data[key] = obj;
  1097. } else {
  1098. data[key] = val;
  1099. }
  1100. if (externalData !== null) {
  1101. externalData = data;
  1102. }
  1103. return data;
  1104. };
  1105. const write = (key, val, session = "", externalData = null) => {
  1106. if (typeof val === "undefined" || val !== val) {
  1107. return false;
  1108. }
  1109. if (!env.isNode && (typeof val === "boolean" || typeof val === "number")) {
  1110. val = String(val);
  1111. }
  1112. let data = "";
  1113. if (externalData || env.isNode) {
  1114. data = writeForNode(key, val, session, externalData);
  1115. } else {
  1116. if (!session) {
  1117. data = val;
  1118. } else {
  1119. if (env.isSurgeLike) {
  1120. data = !!$persistentStore.read(key) ? $persistentStore.read(key) : data;
  1121. } else if (env.isQuanX) {
  1122. data = !!$prefs.valueForKey(key) ? $prefs.valueForKey(key) : data;
  1123. }
  1124. data = convertToObject(data);
  1125. data["magic_session"] = true;
  1126. data[session] = val;
  1127. }
  1128. }
  1129. if (!!data && typeof data === "object") {
  1130. data = JSON.stringify(data, null, 4);
  1131. }
  1132. logger.debug(`WRITE DATA [${key}]${session ? `[${session}]` : ""} <${typeof val}>` + '\n' + `${JSON.stringify(val)}`);
  1133. if (!externalData) {
  1134. if (env.isSurgeLike) {
  1135. return $persistentStore.write(data, key);
  1136. } else if (env.isQuanX) {
  1137. return $prefs.setValueForKey(data, key);
  1138. } else if (env.isNode) {
  1139. try {
  1140. node.fs.writeFileSync("./magic.json", data);
  1141. return true;
  1142. } catch (err) {
  1143. logger.error(err);
  1144. return false;
  1145. }
  1146. }
  1147. }
  1148. return true;
  1149. };
  1150. const update = (key, val, session, comparator = defaultValueComparator, externalData = null) => {
  1151. val = _typeConvertor(val);
  1152. const oldValue = read(key, null, session, false, externalData);
  1153. if (comparator(oldValue, val) === true) {
  1154. return false;
  1155. } else {
  1156. const result = write(key, val, session, externalData);
  1157. let newVal = read(key, null, session, false, externalData);
  1158. if (comparator === defaultValueComparator && typeof newVal === "object") {
  1159. return result;
  1160. }
  1161. return comparator(val, newVal);
  1162. }
  1163. };
  1164. const delForNode = (key, session, externalData) => {
  1165. let data = externalData || node.data;
  1166. data = convertToObject(data);
  1167. if (!!session) {
  1168. obj = convertToObject(data[key]);
  1169. delete obj[session];
  1170. data[key] = obj;
  1171. } else {
  1172. delete data[key];
  1173. }
  1174. if (!!externalData) {
  1175. externalData = data;
  1176. }
  1177. return data;
  1178. };
  1179. const del = (key, session = "", externalData = null) => {
  1180. let data = {};
  1181. if (externalData || env.isNode) {
  1182. data = delForNode(key, session, externalData);
  1183. if (!externalData) {
  1184. node.fs.writeFileSync("./magic.json", JSON.stringify(data, null, 4));
  1185. } else {
  1186. externalData = data;
  1187. }
  1188. } else {
  1189. if (!session) {
  1190. if (env.isStorm) {
  1191. return $persistentStore.remove(key);
  1192. } else if (env.isSurgeLike) {
  1193. return $persistentStore.write(null, key);
  1194. } else if (env.isQuanX) {
  1195. return $prefs.removeValueForKey(key);
  1196. }
  1197. } else {
  1198. if (env.isSurgeLike) {
  1199. data = $persistentStore.read(key);
  1200. } else if (env.isQuanX) {
  1201. data = $prefs.valueForKey(key);
  1202. }
  1203. data = convertToObject(data);
  1204. delete data[session];
  1205. const json = JSON.stringify(data, null, 4);
  1206. write(key, json);
  1207. }
  1208. }
  1209. logger.debug(`DELETE KEY [${key}]${!!session ? `[${session}]` : ""}`);
  1210. };
  1211. const allSessionNames = (key, externalData = null) => {
  1212. let _sessions = [];
  1213. let data = read(key, null, null, true, externalData);
  1214. data = convertToObject(data);
  1215. if (data["magic_session"] !== true) {
  1216. _sessions = [];
  1217. } else {
  1218. _sessions = Object.keys(data).filter(key => key !== "magic_session");
  1219. }
  1220. logger.debug(`READ ALL SESSIONS [${key}] <${typeof _sessions}>` + '\n' + `${JSON.stringify(_sessions, null, 4)}`);
  1221. return _sessions;
  1222. };
  1223. const allSessions = (key, externalData = null) => {
  1224. let _sessions = {};
  1225. let data = read(key, null, null, true, externalData);
  1226. data = convertToObject(data);
  1227. if (data["magic_session"] === true) {
  1228. _sessions = {
  1229. ...data
  1230. };
  1231. delete _sessions["magic_session"];
  1232. }
  1233. logger.debug(`READ ALL SESSIONS [${key}] <${typeof _sessions}>` + '\n' + `${JSON.stringify(_sessions, null, 4)}`);
  1234. return _sessions;
  1235. };
  1236. return {
  1237. read: read,
  1238. write: write,
  1239. del: del,
  1240. update: update,
  1241. allSessions: allSessions,
  1242. allSessionNames: allSessionNames,
  1243. defaultValueComparator: defaultValueComparator,
  1244. convertToObject: convertToObject
  1245. };
  1246. }
  1247. function MagicNotification(scriptName, env, logger, http) {
  1248. let _barkUrl = null;
  1249. let _barkKey = null;
  1250. let notifyInfo = []
  1251. const setBark = url => {
  1252. try {
  1253. let _url = url.replace(/\/+$/g, "");
  1254. _barkUrl = `${/^https?:\/\/([^/]*)/.exec(_url)[0]}/push`;
  1255. _barkKey = /\/([^\/]+)\/?$/.exec(_url)[1];
  1256. } catch (ex) {
  1257. logger.error(`Bark url error: ${ex}.`);
  1258. }
  1259. };
  1260. function appendNotifyInfo(info, type) {
  1261. if (type == 1) {
  1262. notifyInfo = info
  1263. } else {
  1264. notifyInfo.push(info)
  1265. }
  1266. }
  1267. function prependNotifyInfo(info) {
  1268. notifyInfo.splice(0, 0, info)
  1269. }
  1270. function msg(subtitle, message, openUrl, mediaUrl) {
  1271. let opts = {};
  1272. if (openUrl) {
  1273. opts["open-url"] = openUrl;
  1274. }
  1275. if (mediaUrl) {
  1276. opts["media-url"] = mediaUrl;
  1277. }
  1278. if (!message || message.length == 0) {
  1279. if (Array.isArray(notifyInfo)) {
  1280. message = notifyInfo.join("\n");
  1281. } else {
  1282. message = notifyInfo;
  1283. }
  1284. }
  1285. if (message && message.length > 0) {
  1286. post(scriptName, "", message, opts);
  1287. }
  1288. }
  1289. function post(title = scriptName, subTitle = "", body = "", opts = "") {
  1290. const _adaptOpts = _opts => {
  1291. try {
  1292. let newOpts = {};
  1293. if (typeof _opts === "string") {
  1294. if (_opts.length > 0) {
  1295. if (env.isLoon) {
  1296. newOpts = {
  1297. openUrl: _opts
  1298. };
  1299. } else if (env.isQuanX) {
  1300. newOpts = {
  1301. "open-url": _opts
  1302. };
  1303. } else if (env.isSurge) {
  1304. newOpts = {
  1305. url: _opts
  1306. };
  1307. }
  1308. }
  1309. } else if (typeof _opts === "object") {
  1310. if (env.isLoon) {
  1311. newOpts["openUrl"] = !!_opts["open-url"] ? _opts["open-url"] : "";
  1312. newOpts["mediaUrl"] = !!_opts["media-url"] ? _opts["media-url"] : "";
  1313. } else if (env.isQuanX) {
  1314. newOpts = !!_opts["open-url"] || !!_opts["media-url"] ? _opts : {};
  1315. } else if (env.isSurge) {
  1316. let openUrl = _opts["open-url"] || _opts["openUrl"];
  1317. newOpts = openUrl ? {
  1318. url: openUrl
  1319. } : {};
  1320. }
  1321. }
  1322. return newOpts;
  1323. } catch (err) {
  1324. logger.error(`通知选项转换失败${err}`);
  1325. }
  1326. return _opts;
  1327. };
  1328. opts = _adaptOpts(opts);
  1329. if (arguments.length === 1) {
  1330. title = scriptName;
  1331. subTitle = "", body = arguments[0];
  1332. }
  1333. logger.notify('\n' + `title:${title}` + '\n' + `subTitle:${subTitle}` + '\n' + `body:${body}` + '\n' + `options:${typeof opts === "object" ? JSON.stringify(opts) : opts}`);
  1334. if (env.isSurge) {
  1335. $notification.post(title, subTitle, body, opts);
  1336. } else if (env.isLoon) {
  1337. if (!!opts) $notification.post(title, subTitle, body, opts); else $notification.post(title, subTitle, body);
  1338. } else if (env.isQuanX) {
  1339. $notify(title, subTitle, body, opts);
  1340. }
  1341. if (_barkUrl && _barkKey) {
  1342. bark(title, subTitle, body);
  1343. }
  1344. }
  1345. function debug(title = scriptName, subTitle = "", body = "", opts = "") {
  1346. if (logger.getLevel() === "DEBUG") {
  1347. if (arguments.length === 1) {
  1348. title = scriptName;
  1349. subTitle = "";
  1350. body = arguments[0];
  1351. }
  1352. this.post(title, subTitle, body, opts);
  1353. }
  1354. }
  1355. function bark(title = scriptName, subTitle = "", body = "", opts = "") {
  1356. if (typeof http === "undefined" || typeof http.post === "undefined") {
  1357. throw "Bark notification needs to import MagicHttp module.";
  1358. }
  1359. let options = {
  1360. url: _barkUrl,
  1361. headers: {
  1362. "content-type": "application/json; charset=utf-8"
  1363. },
  1364. body: {
  1365. title: title,
  1366. body: subTitle ? `${subTitle}` + '\n' + `${body}` : body,
  1367. device_key: _barkKey
  1368. }
  1369. };
  1370. http.post(options).catch(ex => {
  1371. logger.error(`Bark notify error: ${ex}`);
  1372. });
  1373. }
  1374. return {
  1375. post: post,
  1376. debug: debug,
  1377. bark: bark,
  1378. setBark: setBark,
  1379. appendNotifyInfo: appendNotifyInfo,
  1380. prependNotifyInfo: prependNotifyInfo,
  1381. msg: msg,
  1382. };
  1383. }
  1384. function MagicUtils(env, logger) {
  1385. const retry = (fn, retries = 5, interval = 0, callback = null) => {
  1386. return (...args) => {
  1387. return new Promise((resolve, reject) => {
  1388. function _retry(...args) {
  1389. Promise.resolve().then(() => fn.apply(this, args)).then(result => {
  1390. if (typeof callback === "function") {
  1391. Promise.resolve().then(() => callback(result)).then(() => {
  1392. resolve(result);
  1393. }).catch(ex => {
  1394. if (retries >= 1) {
  1395. if (interval > 0) setTimeout(() => _retry.apply(this, args), interval); else _retry.apply(this, args);
  1396. } else {
  1397. reject(ex);
  1398. }
  1399. retries--;
  1400. });
  1401. } else {
  1402. resolve(result);
  1403. }
  1404. }).catch(ex => {
  1405. logger.error(ex);
  1406. if (retries >= 1 && interval > 0) {
  1407. setTimeout(() => _retry.apply(this, args), interval);
  1408. } else if (retries >= 1) {
  1409. _retry.apply(this, args);
  1410. } else {
  1411. reject(ex);
  1412. }
  1413. retries--;
  1414. });
  1415. }
  1416. _retry.apply(this, args);
  1417. });
  1418. };
  1419. };
  1420. const formatTime = (time, fmt = "yyyy-MM-dd hh:mm:ss") => {
  1421. let o = {
  1422. "M+": time.getMonth() + 1,
  1423. "d+": time.getDate(),
  1424. "h+": time.getHours(),
  1425. "m+": time.getMinutes(),
  1426. "s+": time.getSeconds(),
  1427. "q+": Math.floor((time.getMonth() + 3) / 3),
  1428. S: time.getMilliseconds()
  1429. };
  1430. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
  1431. for (let k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  1432. return fmt;
  1433. };
  1434. const now = () => {
  1435. return formatTime(new Date(), "yyyy-MM-dd hh:mm:ss");
  1436. };
  1437. const today = () => {
  1438. return formatTime(new Date(), "yyyy-MM-dd");
  1439. };
  1440. const sleep = time => {
  1441. return new Promise(resolve => setTimeout(resolve, time));
  1442. };
  1443. const assert = (val, msg = null) => {
  1444. if (env.isNode) {
  1445. const _assert = require("assert");
  1446. if (msg) _assert(val, msg); else _assert(val);
  1447. } else {
  1448. if (val !== true) {
  1449. let err = `AssertionError: ${msg || "The expression evaluated to a falsy value."}`;
  1450. logger.error(err);
  1451. }
  1452. }
  1453. };
  1454. return {
  1455. retry: retry,
  1456. formatTime: formatTime,
  1457. now: now,
  1458. today: today,
  1459. sleep: sleep,
  1460. assert: assert
  1461. };
  1462. }
  1463. function MagicQingLong(env, data, logger) {
  1464. let qlUrl = "";
  1465. let qlName = "";
  1466. let qlClient = "";
  1467. let qlSecret = "";
  1468. let qlPwd = "";
  1469. let qlToken = "";
  1470. const magicJsonFileName = "magic.json";
  1471. const timeout = 3e3;
  1472. const http = (() => MagicHttp(env, logger))();
  1473. const init = (url, clientId, clientSecret, username, password) => {
  1474. qlUrl = url;
  1475. qlClient = clientId;
  1476. qlSecret = clientSecret;
  1477. qlName = username;
  1478. qlPwd = password;
  1479. };
  1480. function readQingLongConfig(config) {
  1481. qlUrl = qlUrl || data.read("magic_qlurl");
  1482. qlToken = qlToken || data.read("magic_qltoken");
  1483. logger.debug(`QingLong url: ${qlUrl}` + '\n' + `QingLong token: ${qlToken}`);
  1484. return config;
  1485. }
  1486. function setBaseUrlAndTimeout(config) {
  1487. if (!qlUrl) {
  1488. qlUrl = data.read("magic_qlurl");
  1489. }
  1490. if (config.url.indexOf(qlUrl) < 0) {
  1491. config.url = `${qlUrl}${config.url}`;
  1492. }
  1493. return {
  1494. ...config,
  1495. timeout: timeout
  1496. };
  1497. }
  1498. function setTimestamp(config) {
  1499. config.params = {
  1500. ...config.params,
  1501. t: Date.now()
  1502. };
  1503. return config;
  1504. }
  1505. async function setAuthorization(config) {
  1506. qlToken = qlToken || data.read("magic_qltoken", "");
  1507. if (!qlToken) {
  1508. await getToken();
  1509. }
  1510. config.headers["authorization"] = `Bearer ${qlToken}`;
  1511. return config;
  1512. }
  1513. function switchClientMode(config) {
  1514. qlClient = qlClient || data.read("magic_qlclient");
  1515. if (!!qlClient) {
  1516. config.url = config.url.replace("/api/", "/open/");
  1517. }
  1518. return config;
  1519. }
  1520. async function refreshToken(error) {
  1521. try {
  1522. const message = error.message || error.error || JSON.stringify(error);
  1523. if ((message.indexOf("NSURLErrorDomain") >= 0 && message.indexOf("-1012") >= 0 || !!error.response && error.response.status === 401) && !!error.config && error.config.refreshToken !== true) {
  1524. logger.warning(`QingLong Panel token has expired`);
  1525. logger.info("Refreshing the QingLong Panel token");
  1526. await getToken();
  1527. error.config["refreshToken"] = true;
  1528. logger.info("Call the previous method again");
  1529. return await http.request(error.config.method, error.config);
  1530. } else {
  1531. return Promise.reject(error);
  1532. }
  1533. } catch (ex) {
  1534. return Promise.reject(ex);
  1535. }
  1536. }
  1537. http.interceptors.request.use(setBaseUrlAndTimeout, undefined);
  1538. http.interceptors.request.use(switchClientMode, undefined, {
  1539. runWhen: config => {
  1540. return config.url.indexOf("api/user/login") < 0 && config.url.indexOf("open/auth/token") < 0;
  1541. }
  1542. });
  1543. http.interceptors.request.use(setAuthorization, undefined, {
  1544. runWhen: config => {
  1545. return config.url.indexOf("api/user/login") < 0 && config.url.indexOf("open/auth/token") < 0;
  1546. }
  1547. });
  1548. http.interceptors.request.use(setTimestamp, undefined, {
  1549. runWhen: config => {
  1550. return config.url.indexOf("open/auth/token") < 0;
  1551. }
  1552. });
  1553. http.interceptors.request.use(readQingLongConfig, undefined);
  1554. http.interceptors.response.use(undefined, refreshToken);
  1555. async function getToken() {
  1556. qlClient = qlClient || data.read("magic_qlclient");
  1557. qlSecret = qlSecret || data.read("magic_qlsecrt");
  1558. qlName = qlName || data.read("magic_qlname");
  1559. qlPwd = qlPwd || data.read("magic_qlpwd");
  1560. if (qlUrl && qlClient && qlSecret) {
  1561. logger.info("Get token from QingLong Panel");
  1562. await http.get({
  1563. url: `/open/auth/token`,
  1564. headers: {
  1565. "content-type": "application/json"
  1566. },
  1567. params: {
  1568. client_id: qlClient,
  1569. client_secret: qlSecret
  1570. }
  1571. }).then(resp => {
  1572. if (Object.keys(resp.body).length > 0 && resp.body.data && resp.body.data.token) {
  1573. logger.info("Successfully logged in to QingLong Panel");
  1574. qlToken = resp.body.data.token;
  1575. data.write("magic_qltoken", qlToken);
  1576. } else {
  1577. throw new Error("Get QingLong Panel token failed.");
  1578. }
  1579. }).catch(err => {
  1580. logger.error(`Error logging in to QingLong Panel.` + '\n' + `${err.message || err}`);
  1581. });
  1582. } else if (qlUrl && qlName && qlPwd) {
  1583. await http.post({
  1584. url: `/api/user/login`,
  1585. headers: {
  1586. "content-type": "application/json"
  1587. },
  1588. body: {
  1589. username: qlName,
  1590. password: qlPwd
  1591. }
  1592. }).then(resp => {
  1593. logger.info("Successfully logged in to QingLong Panel");
  1594. qlToken = resp.body.data.token;
  1595. data.write("magic_qltoken", qlToken);
  1596. }).catch(err => {
  1597. logger.error(`Error logging in to QingLong Panel.` + '\n' + `${err.message || err}`);
  1598. });
  1599. }
  1600. return qlToken;
  1601. }
  1602. async function setEnv(name, value, id = null) {
  1603. qlUrl = qlUrl || data.read("magic_qlurl");
  1604. if (id === null) {
  1605. let envIds = await setEnvs([{
  1606. name: name,
  1607. value: value
  1608. }]);
  1609. if (!!envIds && envIds.length === 1) {
  1610. return envIds[0];
  1611. }
  1612. } else {
  1613. await http.put({
  1614. url: `/api/envs`,
  1615. headers: {
  1616. "content-type": "application/json"
  1617. },
  1618. body: {
  1619. name: name,
  1620. value: value,
  1621. id: id
  1622. }
  1623. }).then(resp => {
  1624. if (resp.body.code === 200) {
  1625. logger.debug(`QINGLONG UPDATE ENV ${name} <${typeof value}> (${id})` + '\n' + `${JSON.stringify(value)}`);
  1626. return true;
  1627. } else {
  1628. logger.error(`Error adding environment variable from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1629. }
  1630. }).catch(err => {
  1631. logger.error(`Error adding environment variable from QingLong Panel.` + '\n' + `${err.message || err}`);
  1632. return false;
  1633. });
  1634. }
  1635. }
  1636. async function setEnvs(envs) {
  1637. let envIds = [];
  1638. await http.post({
  1639. url: `/api/envs`,
  1640. headers: {
  1641. "content-type": "application/json"
  1642. },
  1643. body: envs
  1644. }).then(resp => {
  1645. if (resp.body.code === 200) {
  1646. resp.body.data.forEach(element => {
  1647. logger.debug(`QINGLONG ADD ENV ${element.name} <${typeof element.value}> (${element.id})` + '\n' + `${JSON.stringify(element)}`);
  1648. envIds.push(element.id);
  1649. });
  1650. } else {
  1651. logger.error(`Error adding environments variable from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1652. }
  1653. }).catch(err => {
  1654. logger.error(`Error adding environments variable from QingLong Panel.` + '\n' + `${err.message || err}`);
  1655. });
  1656. return envIds;
  1657. }
  1658. async function delEnvs(ids) {
  1659. return await http.delete({
  1660. url: `/api/envs`,
  1661. headers: {
  1662. accept: "application/json",
  1663. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  1664. connection: "keep-alive",
  1665. "content-type": "application/json;charset=UTF-8",
  1666. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.30"
  1667. },
  1668. body: ids
  1669. }).then(resp => {
  1670. if (resp.body.code === 200) {
  1671. logger.debug(`QINGLONG DELETE ENV IDS: ${ids}`);
  1672. return true;
  1673. } else {
  1674. logger.error(`Error deleting environments variable from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1675. return false;
  1676. }
  1677. }).catch(err => {
  1678. logger.error(`Error deleting environments variable from QingLong Panel.` + '\n' + `${err.message || err}`);
  1679. });
  1680. }
  1681. async function getEnvs(name = null, searchValue = "", retired = 0) {
  1682. let envs = [];
  1683. await http.get({
  1684. url: `/api/envs`,
  1685. headers: {
  1686. "content-type": "application/json"
  1687. },
  1688. params: {
  1689. searchValue: searchValue
  1690. }
  1691. }).then(resp => {
  1692. if (resp.body.code === 200) {
  1693. const allEnvs = resp.body.data;
  1694. if (!!name) {
  1695. let _envs = [];
  1696. for (const env of allEnvs) {
  1697. if (env.name === name) {
  1698. envs.push(env);
  1699. }
  1700. }
  1701. envs = _envs;
  1702. }
  1703. envs = allEnvs;
  1704. } else {
  1705. throw new Error(`Error reading environment variable from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1706. }
  1707. }).catch(err => {
  1708. throw new Error(`Error reading environments variable from QingLong Panel.` + '\n' + `${err.message || err}`);
  1709. });
  1710. return envs;
  1711. }
  1712. async function getEnv(id) {
  1713. let env = null;
  1714. const allEnvs = await getEnvs();
  1715. for (const _env of allEnvs) {
  1716. if (_env.id === id) {
  1717. env = _env;
  1718. break;
  1719. }
  1720. }
  1721. return env;
  1722. }
  1723. async function disableEnvs(ids) {
  1724. let result = false;
  1725. await http.put({
  1726. url: `/api/envs/disable`,
  1727. headers: {
  1728. accept: "application/json",
  1729. "accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  1730. connection: "keep-alive",
  1731. "content-type": "application/json;charset=UTF-8",
  1732. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.30"
  1733. },
  1734. body: ids
  1735. }).then(resp => {
  1736. if (resp.body.code === 200) {
  1737. logger.debug(`QINGLONG DISABLED ENV IDS: ${ids}`);
  1738. result = true;
  1739. } else {
  1740. logger.error(`Error disabling environments variable from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1741. }
  1742. }).catch(err => {
  1743. logger.error(`Error disabling environments variable from QingLong Panel.` + '\n' + `${err.message || err}`);
  1744. });
  1745. return result;
  1746. }
  1747. async function enableEnvs(ids) {
  1748. let result = false;
  1749. await http.put({
  1750. url: `/api/envs/enable`,
  1751. headers: {
  1752. accept: "application/json",
  1753. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  1754. connection: "keep-alive",
  1755. "content-type": "application/json;charset=UTF-8",
  1756. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.30"
  1757. },
  1758. body: ids
  1759. }).then(resp => {
  1760. if (resp.body.code === 200) {
  1761. logger.debug(`QINGLONG ENABLED ENV IDS: ${ids}`);
  1762. result = true;
  1763. } else {
  1764. logger.error(`Error enabling environments variable from Qilong panel.` + '\n' + `${JSON.stringify(resp)}`);
  1765. }
  1766. }).catch(err => {
  1767. logger.error(`Error enabling environments variable from Qilong panel.` + '\n' + `${err.message || err}`);
  1768. });
  1769. return result;
  1770. }
  1771. async function addScript(name, path = "", content = "") {
  1772. let result = false;
  1773. await http.post({
  1774. url: `/api/scripts`,
  1775. headers: {
  1776. "content-type": "application/json"
  1777. },
  1778. body: {
  1779. filename: name,
  1780. path: path,
  1781. content: content
  1782. }
  1783. }).then(resp => {
  1784. if (resp.body.code === 200) {
  1785. result = true;
  1786. } else {
  1787. logger.error(`Error reading data from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1788. }
  1789. }).catch(err => {
  1790. logger.error(`Error reading data from QingLong Panel.` + '\n' + `${err.message || err}`);
  1791. });
  1792. return result;
  1793. }
  1794. async function getScript(name, path = "") {
  1795. let content = "";
  1796. await http.get({
  1797. url: `/api/scripts/${name}`,
  1798. params: {
  1799. path: path
  1800. }
  1801. }).then(resp => {
  1802. if (resp.body.code === 200) {
  1803. content = resp.body.data;
  1804. } else {
  1805. throw new Error(`Error reading data from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1806. }
  1807. }).catch(err => {
  1808. throw new Error(`Error reading data from QingLong Panel.` + '\n' + `${err.message || err}`);
  1809. });
  1810. return content;
  1811. }
  1812. async function editScript(name, path = "", content = "") {
  1813. let result = false;
  1814. await http.put({
  1815. url: `/api/scripts`,
  1816. headers: {
  1817. "content-type": "application/json"
  1818. },
  1819. body: {
  1820. filename: name,
  1821. path: path,
  1822. content: content
  1823. }
  1824. }).then(resp => {
  1825. if (resp.body.code === 200) {
  1826. result = true;
  1827. } else {
  1828. logger.error(`Error reading data from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1829. }
  1830. }).catch(err => {
  1831. logger.error(`Error reading data from QingLong Panel.` + '\n' + `${err.message || err}`);
  1832. });
  1833. return result;
  1834. }
  1835. async function delScript(name, path = "") {
  1836. let result = false;
  1837. await http.delete({
  1838. url: `/api/scripts`,
  1839. headers: {
  1840. "content-type": "application/json"
  1841. },
  1842. body: {
  1843. filename: name,
  1844. path: path
  1845. }
  1846. }).then(resp => {
  1847. if (resp.body.code === 200) {
  1848. result = true;
  1849. } else {
  1850. logger.error(`Error reading data from QingLong Panel.` + '\n' + `${JSON.stringify(resp)}`);
  1851. }
  1852. }).catch(err => {
  1853. logger.error(`Error reading data from QingLong Panel.` + '\n' + `${err.message || err}`);
  1854. });
  1855. return result;
  1856. }
  1857. async function write(key, val, session = "") {
  1858. let qlContent = await getScript(magicJsonFileName, "");
  1859. let qlData = data.convertToObject(qlContent);
  1860. let writeResult = data.write(key, val, session, qlData);
  1861. qlContent = JSON.stringify(qlData, null, 4);
  1862. let editResult = await editScript(magicJsonFileName, "", qlContent);
  1863. return editResult && writeResult;
  1864. }
  1865. async function batchWrite(...args) {
  1866. let qlContent = await getScript(magicJsonFileName, "");
  1867. let qlData = data.convertToObject(qlContent);
  1868. for (let arg of args) {
  1869. data.write(arg[0], arg[1], typeof arg[2] !== "undefined" ? arg[2] : "", qlData);
  1870. }
  1871. qlContent = JSON.stringify(qlData, null, 4);
  1872. return await editScript(magicJsonFileName, "", qlContent);
  1873. }
  1874. async function update(key, val, session, comparator = data.defaultValueComparator) {
  1875. let qlContent = await getScript(magicJsonFileName, "");
  1876. let qlData = data.convertToObject(qlContent);
  1877. const updateResult = data.update(key, val, session, comparator, qlData);
  1878. let editScriptResult = false;
  1879. if (updateResult === true) {
  1880. qlContent = JSON.stringify(qlData, null, 4);
  1881. editScriptResult = await editScript(magicJsonFileName, "", qlContent);
  1882. }
  1883. return updateResult && editScriptResult;
  1884. }
  1885. async function batchUpdate(...args) {
  1886. let qlContent = await getScript(magicJsonFileName, "");
  1887. let qlData = data.convertToObject(qlContent);
  1888. for (let arg of args) {
  1889. data.update(arg[0], arg[1], typeof arg[2] !== "undefined" ? arg[2] : "", typeof arg[3] !== "undefined" ? arg["comparator"] : data.defaultValueComparator, qlData);
  1890. }
  1891. qlContent = JSON.stringify(qlData, null, 4);
  1892. return await editScript(magicJsonFileName, "", qlContent);
  1893. }
  1894. async function read(key, val, session = "", read_no_session = false) {
  1895. let qlContent = await getScript(magicJsonFileName, "");
  1896. let qlData = data.convertToObject(qlContent);
  1897. return data.read(key, val, session, read_no_session, qlData);
  1898. }
  1899. async function batchRead(...args) {
  1900. let qlContent = await getScript(magicJsonFileName, "");
  1901. let qlData = data.convertToObject(qlContent);
  1902. let results = [];
  1903. for (let arg of args) {
  1904. const result = data.read(arg[0], arg[1], typeof arg[2] !== "undefined" ? arg[2] : "", typeof arg[3] === "boolean" ? arg[3] : false, qlData);
  1905. results.push(result);
  1906. }
  1907. return results;
  1908. }
  1909. async function del(key, session = "") {
  1910. let qlContent = await getScript(magicJsonFileName, "");
  1911. let qlData = data.convertToObject(qlContent);
  1912. const delResult = data.del(key, session, qlData);
  1913. qlContent = JSON.stringify(qlData, null, 4);
  1914. const editResult = await editScript(magicJsonFileName, "", qlContent);
  1915. return delResult && editResult;
  1916. }
  1917. async function batchDel(...args) {
  1918. let qlContent = await getScript(magicJsonFileName, "");
  1919. let qlData = data.convertToObject(qlContent);
  1920. for (let arg of args) {
  1921. data.del(arg[0], typeof arg[1] !== "undefined" ? arg[1] : "", qlData);
  1922. }
  1923. qlContent = JSON.stringify(qlData, null, 4);
  1924. return await editScript(magicJsonFileName, "", qlContent);
  1925. }
  1926. async function allSessionNames(key) {
  1927. let qlContent = await getScript(magicJsonFileName, "");
  1928. let qlData = data.convertToObject(qlContent);
  1929. return data.allSessionNames(key, qlData);
  1930. }
  1931. async function allSessions(key) {
  1932. let qlContent = await getScript(magicJsonFileName, "");
  1933. let qlData = data.convertToObject(qlContent);
  1934. return data.allSessions(key, qlData);
  1935. }
  1936. return {
  1937. url: qlUrl || data.read("magic_qlurl"),
  1938. init: init,
  1939. getToken: getToken,
  1940. setEnv: setEnv,
  1941. setEnvs: setEnvs,
  1942. getEnv: getEnv,
  1943. getEnvs: getEnvs,
  1944. delEnvs: delEnvs,
  1945. disableEnvs: disableEnvs,
  1946. enableEnvs: enableEnvs,
  1947. addScript: addScript,
  1948. getScript: getScript,
  1949. editScript: editScript,
  1950. delScript: delScript,
  1951. write: write,
  1952. read: read,
  1953. del: del,
  1954. update: update,
  1955. batchWrite: batchWrite,
  1956. batchRead: batchRead,
  1957. batchUpdate: batchUpdate,
  1958. batchDel: batchDel,
  1959. allSessions: allSessions,
  1960. allSessionNames: allSessionNames
  1961. };
  1962. }