functionMagicHttp(c,l){conste="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";constt="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";letr;if(c.isNode){constS=require("axios");r=S.create()}classs{constructor(e=true){this.handlers=[];this.isRequest=e}use(e,t,r){this.handlers.push({fulfilled:e,rejected:t,synchronous:r?r.synchronous:false,runWhen:r?r.runWhen:null});returnthis.handlers.length-1}eject(e){if(this.handlers[e]){this.handlers[e]=null}}forEach(t){this.handlers.forEach(e=>{if(e!==null){t(e)}})}}functionn(e){letr={...e};if(!!r.params){if(!c.isNode){lete=Object.keys(r.params).map(e=>{constt=encodeURIComponent(e);r.url=r.url.replace(newRegExp(`${e}=[^&]*`,"ig"),"");r.url=r.url.replace(newRegExp(`${t}=[^&]*`,"ig"),"");return`${t}=${encodeURIComponent(r.params[e])}`}).join("&");if(r.url.indexOf("?")<0)r.url+="?";if(!/(&|\?)$/g.test(r.url)){r.url+="&"}r.url+=e;deleter.params;l.debug(`Params to QueryString: ${r.url}`)}}returnr}constd=(e,t)=>{letr=typeoft==="object"?{headers:{},...t}:{url:t,headers:{}};if(!r.method){r["method"]=e}r=n(r);if(r["rewrite"]===true){if(c.isSurge){r.headers["X-Surge-Skip-Scripting"]=false;deleter["rewrite"]}elseif(c.isQuanX){r["hints"]=false;deleter["rewrite"]}}if(c.isSurge){if(r["method"]!=="GET"&&r.headers["Content-Type"].indexOf("application/json")>=0&&r.bodyinstanceofArray){r.body=JSON.stringify(r.body);l.debug(`Convert Array object to String: ${r.body}`)}}elseif(c.isQuanX){if(r.hasOwnProperty("body")&&typeofr["body"]!=="string")r["body"]=JSON.stringify(r["body"]);r["method"]=e}elseif(c.isNode){if(e==="POST"||e==="PUT"||e==="PATCH"||e==="DELETE"){r.data=r.data||r.body}elseif(e==="GET"){r.params=r.params||r.body}deleter.body}returnr};constf=(t,r=null)=>{if(t){lete={...t,config:t.config||r,status:t.statusCode||t.status,body:t.body||t.data,headers:t.headers||t.header};if(typeofe.body==="string"){try{e.body=JSON.parse(e.body)}catch{}}deletet.data;returne}else{returnt}};consto=r=>{returnObject.keys(r).reduce((e,t)=>{e[t.toLowerCase()]=r[t];returne},{})};consti=s=>{returnObject.keys(s).reduce((e,t)=>{constr=t.split("-").map(e=>e[0].toUpperCase()+e.slice(1)).join("-");e[r]=s[t];returne},{})};consth=(t,r=null)=>{if(!!t&&t.status>=400){l.debug(`Raise exception when status code is ${t.status}`);lete={name:"RequestException",message:`Request failed with status code ${t.status}`,config:r||t.config,response:t};returne}};consta={request:news,response:news(false)};letp=[];lety=[];letg=true;functionm(e){e=n(e);l.debug(`HTTP ${e["method"].toUpperCase()}:\n${JSON.stringify(e)}`);returne}functionb(e){try{e=!!e?f(e):e;l.sniffer(`HTTP ${e.config["method"].toUpperCase()}:\n${JSON.stringify(e.config)}\nSTATUS CODE:\n${e.status}\nRESPONSE:\n${typeofe.body==="object"?JSON.stringify(e.body):e.body}`);constt=h(e);if(!!t){returnPromise.reject(t)}returne}catch(t){l.error(t);returne}}constT=t=>{try{p=[];y=[];a.request.forEach(e=>{if(typeofe.runWhen==="function"&&e.runWhen(t)===false){return}g=g&&e.synchronous;p.unshift(e.fulfilled,e.rejected)});a.response.forEach(e=>{y.push(e.fulfilled,e.rejected)})}catch(e){l.error(`Failed to register interceptors: ${e}.`)}};constu=(e,s)=>{letn;constt=e.toUpperCase();s=d(t,s);if(c.isNode){n=r}else{if(c.isSurgeLike){n=o=>{returnnewPromise((s,n)=>{$httpClient[e.toLowerCase()](o,(t,r,e)=>{if(t){lete={name:t.name||t,message:t.message||t,stack:t.stack||t,config:o,response:f(r)};n(e)}else{r.config=o;r.body=e;s(r)}})})}}else{n=n=>{returnnewPromise((r,s)=>{$task.fetch(n).then(e=>{e=f(e,n);constt=h(e,n);if(t){returnPromise.reject(t)}r(e)}).catch(e=>{lett={name:e.message||e.error,message:e.message||e.error,stack:e.error,config:n,response:!!e.response?f(e.response):null};s(t)})})}}}leto;T(s);consti=[m,undefined];consta=[b,undefined];if(!g){l.debug("Interceptors are executed in asynchronous mode.");letr=[n,undefined];Array.prototype.unshift.apply(r
functionMagicData(i,u){letf={fs:undefined,data:{}};if(i.isNode){f.fs=require("fs");try{f.fs.accessSync("./magic.json",f.fs.constants.R_OK|f.fs.constants.W_OK)}catch(e){f.fs.writeFileSync("./magic.json","{}",{encoding:"utf8"})}f.data=require("./magic.json")}consto=(e,t)=>{if(typeoft==="object"){returnfalse}else{returne===t}};consta=e=>{if(e==="true"){returntrue}elseif(e==="false"){returnfalse}elseif(typeofe==="undefined"){returnnull}else{returne}};constc=(e,t,s,n)=>{if(s){try{if(typeofe==="string")e=JSON.parse(e);if(e["magic_session"]===true){e=e[s]}else{e=null}}catch{e=null}}if(typeofe==="string"&&e!=="null"){try{e=JSON.parse(e)}catch{}}if(n===false&&!!e&&e["magic_session"]===true){e=null}if((e===null||typeofe==="undefined")&&t!==null&&typeoft!=="undefined"){e=t}e=a(e);returne};constl=t=>{if(typeoft==="string"){lete={};try{e=JSON.parse(t);consts=typeofe;if(s!=="object"||einstanceofArray||s==="bool"||e===null){e={}}}catch{}returne}elseif(tinstanceofArray||t===null||typeoft==="undefined"||t!==t||typeoft==="boolean"){return{}}else{returnt}};consty=(e,t=null,s="",n=false,r=null)=>{letl=r||f.data;if(!!l&&typeofl[e]!=="undefined"&&l[e]!==null){val=l[e]}else{val=!!s?{}:null}val=c(val,t,s,n);returnval};constd=(e,t=null,s="",n=false,r=null)=>{letl="";if(r||i.isNode){l=y(e,t,s,n,r)}else{if(i.isSurgeLike){l=$persistentStore.read(e)}elseif(i.isQuanX){l=$prefs.valueForKey(e)}l=c(l,t,s,n)}u.debug(`READ DATA [${e}]${!!s?`[${s}]`:""} <${typeofl}>\n${JSON.stringify(l)}`);returnl};constp=(t,s,n="",e=null)=>{letr=e||f.data;r=l(r);if(!!n){lete=l(r[t]);e["magic_session"]=true;e[n]=s;r[t]=e}else{r[t]=s}if(e!==null){e=r}returnr};constS=(e,t,s="",n=null)=>{if(typeoft==="undefined"||t!==t){returnfalse}if(!i.isNode&&(typeoft==="boolean"||typeoft==="number")){t=String(t)}letr="";if(n||i.isNode){r=p(e,t,s,n)}else{if(!s){r=t}else{if(i.isSurgeLike){r=!!$persistentStore.read(e)?$persistentStore.read(e):r}elseif(i.isQuanX){r=!!$prefs.valueForKey(e)?$prefs.valueForKey(e):r}r=l(r);r["magic_session"]=true;r[s]=t}}if(!!r&&typeofr==="object"){r=JSON.stringify(r,null,4)}u.debug(`WRITE DATA [${e}]${s?`[${s}]`:""} <${typeoft}>\n${JSON.stringify(t)}`);if(!n){if(i.isSurgeLike){return$persistentStore.write(r,e)}elseif(i.isQuanX){return$prefs.setValueForKey(r,e)}elseif(i.isNode){try{f.fs.writeFileSync("./magic.json",r);returntrue}catch(e){u.error(e);returnfalse}}}returntrue};conste=(t,s,n,r=o,l=null)=>{s=a(s);conste=d(t,null,n,false,l);if(r(e,s)===true){returnfalse}else{consti=S(t,s,n,l);lete=d(t,null,n,false,l);if(r===o&&typeofe==="object"){returni}returnr(s,e)}};constg=(e,t,s)=>{letn=s||f.data;n=l(n);if(!!t){obj=l(n[e]);deleteobj[t];n[e]=obj}else{deleten[e]}if(!!s){s=n}returnn};constt=(e,t="",s=null)=>{letn={};if(s||i.isNode){n=g(e,t,s);if(!s){f.fs.writeFileSync("./magic.json",JSON.stringify(n,null,4))}else{s=n}}else{if(!t){if(i.isStorm){return$persistentStore.remove(e)}elseif(i.isSurgeLike){return$persistentStore.write(null,e)}elseif(i.isQuanX){return$prefs.removeValueForKey(e)}}else{if(i.isSurgeLike){n=$persistentStore.read(e)}elseif(i.isQuanX){n=$prefs.valueForKey(e)}n=l(n);deleten[t];constr=JSON.stringify(n,null,4);S(e,r)}}u.debug(`DELETE KEY [${e}]${!!t?`[${t}]`:""}`)};consts=(e,t=null)=>{lets=[];letn=d(e,null,null,true,t);n=l(n);if(n["magic_session"]!==true){s=[]}else{s=Object.keys(n).filter(e=>e!=="magic_session")}u.debug(`READ ALL SESSIONS [${e}] <${typeofs}>\n${JSON.stringify(s,null,4)}`);returns};constn=(e,t=null)=>{lets={};letn=d(e,null,null,true,t);n=l(n);if(n["magic_session"]===true){s={...n};deletes["magic_session"]}u.debug(`READ ALL SESSIONS [${e}] <${typeofs}>\n${JSON.stringify(s,null,4)}`);returns};return{read:d,write:S,del:t,update:e,allSessions:n,allSessionNames:s,defaultValueComparator:o,convertToObject:l}}
functionMagicNotification(r,f,l){lets=null;letu=null;constc=typeofMagicHttp==="function"?MagicHttp(f,l):undefined;conste=t=>{try{lete=t.replace(/\/+$/g,"");s=`${/^https?:\/\/([^/]*)/.exec(e)[0]}/push`;u=/\/([^\/]+)\/?$/.exec(e)[1]}catch(e){l.error(`Bark url error: ${e}.`)}};functiont(e=r,t="",i="",o=""){constn=i=>{try{lett={};if(typeofi==="string"){if(f.isLoon)t={openUrl:i};elseif(f.isQuanX)t={"open-url":i};elseif(f.isSurge)t={url:i}}elseif(typeofi==="object"){if(f.isLoon){t["openUrl"]=!!i["open-url"]?i["open-url"]:"";t["mediaUrl"]=!!i["media-url"]?i["media-url"]:""}elseif(f.isQuanX){t=!!i["open-url"]||!!i["media-url"]?i:{}}elseif(f.isSurge){lete=i["open-url"]||i["openUrl"];t=e?{url:e}:{}}}returnt}catch(e){l.error(`Failed to convert notification option, ${e}`)}returni};o=n(o);if(arguments.length==1){e=r;t="",i=arguments[0]}l.notify(`title:${e}\nsubTitle:${t}\nbody:${i}\noptions:${typeofo==="object"?JSON.stringify(o):o}`);if(f.isSurge){$notification.post(e,t,i,o)}elseif(f.isLoon){if(!!o)$notification.post(e,t,i,o);else$notification.post(e,t,i)}elseif(f.isQuanX){$notify(e,t,i,o)}if(s&&u&&typeofc!=="undefined"){p(e,t,i)}}functioni(e=r,t="",i="",o=""){if(l.level==="DEBUG"){if(arguments.length==1){e=r;t="",i=arguments[0]}this.notify(e,t,i,o)}}functionp(e=r,t="",i="",o=""){if(typeofc==="undefined"||typeofc.post==="undefined"){throw"Bark notification needs to import MagicHttp module."}letn={url:s,headers:{"Content-Type":"application/json; charset=utf-8"},body:{title:e,body:t?`${t}\n${i}`:i,device_key:u}};c.post(n).catch(e=>{l.error(`Bark notify error: ${e}`)})}return{post:t,debug:i,bark:p,setBark:e}}
functionMagicUtils(r,h){conste=(o,i=5,l=0,a=null)=>{return(...e)=>{returnnewPromise((s,r)=>{functionn(...t){Promise.resolve().then(()=>o.apply(this,t)).then(e=>{if(typeofa==="function"){Promise.resolve().then(()=>a(e)).then(()=>{s(e)}).catch(e=>{if(i>=1){if(l>0)setTimeout(()=>n.apply(this,t),l);elsen.apply(this,t)}else{r(e)}i--})}else{s(e)}}).catch(e=>{h.error(e);if(i>=1&&l>0){setTimeout(()=>n.apply(this,t),l)}elseif(i>=1){n.apply(this,t)}else{r(e)}i--})}n.apply(this,e)})}};constt=(e,t="yyyy-MM-dd hh:mm:ss")=>{lets={"M+":e.getMonth()+1,"d+":e.getDate(),"h+":e.getHours(),"m+":e.getMinutes(),"s+":e.getSeconds(),"q+":Math.floor((e.getMonth()+3)/3),S:e.getMilliseconds()};if(/(y+)/.test(t))t=t.replace(RegExp.$1,(e.getFullYear()+"").substr(4-RegExp.$1.length));for(leteins)if(newRegExp("("+e+")").test(t))t=t.replace(RegExp.$1,RegExp.$1.length==1?s[e]:("00"+s[e]).substr((""+s[e]).length));returnt};consts=()=>{returnt(newDate,"yyyy-MM-dd hh:mm:ss")};constn=()=>{returnt(newDate,"yyyy-MM-dd")};consto=t=>{returnnewPromise(e=>setTimeout(e,t))};consti=(e,t=null)=>{if(r.isNode){consts=require("assert");if(t)s(e,t);elses(e)}else{if(e!==true){lete=`AssertionError: ${t||"The expression evaluated to a falsy value"}`;h.error(e)}}};return{retry:e,formatTime:t,now:s,today:n,sleep:o,assert:i}}
functionMagicQingLong(e,s,o){leti="";letl="";letc="";letu="";letd="";letn="";constg="magic.json";constr=3e3;constf=MagicHttp(e,o);constt=(e,n,r,t,a)=>{i=e;c=n;u=r;l=t;d=a};functiona(e){i=i||s.read("magic_qlurl");n=n||s.read("magic_qltoken");returne}functionp(e){if(!i){i=s.read("magic_qlurl")}if(e.url.indexOf(i)<0){e.url=`${i}${e.url}`}return{...e,timeout:r}}functiony(e){e.params={...e.params,t:Date.now()};returne}functionm(e){n=n||s.read("magic_qltoken");if(n){e.headers["Authorization"]=`Bearer ${n}`}returne}functionh(e){c=c||s.read("magic_qlclient");if(!!c){e.url=e.url.replace("/api/","/open/")}returne}asyncfunctionb(e){try{constn=e.message||e.error||JSON.stringify(e);if((n.indexOf("NSURLErrorDomain")>=0&&n.indexOf("-1012")>=0||!!e.response&&e.response.status===401)&&!!e.config&&e.config.refreshToken!==true){o.warning(`Qinglong Panel token has expired.`);awaitv();e.config["refreshToken"]=true;returnawaitf.request(e.config.method,e.config)}else{returnPromise.reject(e)}}catch(e){returnPromise.reject(e)}}f.interceptors.request.use(a,undefined);f.interceptors.request.use(p,undefined);f.interceptors.request.use(h,undefined,{runWhen:e=>{returne.url.indexOf("api/user/login")<0&&e.url.indexOf("open/auth/token")<0}});f.interceptors.request.use(m,undefined,{runWhen:e=>{returne.url.indexOf("api/user/login")<0&&e.url.indexOf("open/auth/token")<0}});f.interceptors.request.use(y,undefined,{runWhen:e=>{returne.url.indexOf("open/auth/token")<0&&e.url.indexOf("t=")<0}});f.interceptors.response.use(undefined,b);asyncfunctionv(){c=c||s.read("magic_qlclient");u=u||s.read("magic_qlsecrt");l=l||s.read("magic_qlname");d=d||s.read("magic_qlpwd");if(i&&c&&u){awaitf.get({url:`/open/auth/token`,headers:{"Content-Type":"application/json"},params:{client_id:c,client_secret:u}}).then(e=>{o.info("Successfully logged in to Qinglong Panel");n=e.body.data.token;s.update("magic_qltoken",n);returnn}).catch(e=>{o.error(`Error logging in to Qinglong Panel.\n${e.message}`)})}elseif(i&&l&&d){awaitf.post({url:`/api/user/login`,headers:{"Content-Type":"application/json"},body:{username:l,password:d}}).then(e=>{o.info("Successfully logged in to Qinglong Panel");n=e.body.data.token;s.update("magic_qltoken",n);returnn}).catch(e=>{o.error(`Error logging in to Qinglong Panel.\n${e.message}`)})}}asyncfunctionE(n,r,t=null){i=i||s.read("magic_qlurl");if(t===null){lete=awaitw([{name:n,value:r}]);if(!!e&&e.length===1){returne[0]}}else{f.put({url:`/api/envs`,headers:{"Content-Type":"application/json"},body:{name:n,value:r,id:t}}).then(e=>{if(e.body.code===200){o.debug(`QINGLONG UPDATE ENV ${n} <${typeofr}> (${t})\n${JSON.stringify(r)}`);returntrue}else{o.error(`Error updating environment variable from Qinglong Panel.\n${JSON.stringify(e)}`)}}).catch(e=>{o.error(`Error updating environment variable from Qinglong Panel.\n${e.message}`);returnfalse})}}asyncfunctionw(e){letn=[];awaitf.post({url:`/api/envs`,headers:{"Content-Type":"application/json"},body:e}).then(e=>{if(e.body.code===200){e.body.data.forEach(e=>{o.debug(`QINGLONG ADD ENV ${e.name} <${typeofe.value}> (${e.id})\n${JSON.stringify(e)}`);n.push(e.id)})}else{o.error(`Error adding environment variable from Qinglong Panel.\n${JSON.stringify(e)}`)}}).catch(e=>{o.error(`Error adding environment variable from Qinglong Panel.\n${e.message}`)});returnn}asyncfunctionN(n){returnawaitf.delete({url:`/api/envs`,headers:{Accept:"application/json","Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",Connection:"keep-alive","Content-Type":"application/json;charset=UTF-8","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"},body:n}).then(e=>{if(e.body.code===200){o.debug(`QINGLONG DELETE ENV IDS: ${n}`);returntrue}else{o.error(`Error deleting environment variable from Qinglong Panel.\n${JSON.stringify(e)}`);returnfalse}}).catch(e=>{o.error(`Error deleting environment variable from Qinglong Panel.\n${e.message}`)})}asyncfunctionO(t=null,a="",i=0){if(i<=3){letr=