Files
Sliverkiss-QuantumultX/Utils/QingLong.min.js
2025-01-03 17:31:11 +08:00

2 lines
4.6 KiB
JavaScript

function QingLong(e,t,n){const o=(e,t="GET")=>new Promise(((n,o)=>{$.http[t.toLowerCase()](e).then((e=>{var t=e.body;try{t=$.toObj(t)||t}catch(e){}n(t)})).catch((e=>o(e)))}));return new class{constructor(e,t,n){this.host=e,this.clientId=t,this.clientSecret=n,this.token="",this.envs=[]}async checkLogin(){let e;try{e=$.getjson("yuheng_ql_token")||{}}catch(e){return console.log("❌The token is invalid, please re-enter the token"),await this.getAuthToken(),!1}if(Object.keys(e).length>0){const{token:t,expiration:n}=e;(new Date).getTime()>n?($.log("❌The token has expired"),await this.getAuthToken()):(this.token=t,$.log(`✅The token is successfully obtained (${this.token}) from cache and is valid until ${$.time("yyyy-MM-dd HH:mm:ss",n)}`))}else await this.getAuthToken()}async getAuthToken(){const e={url:`${this.host}/open/auth/token`,params:{client_id:this.clientId,client_secret:this.clientSecret}};try{$.log(`传入参数: ${JSON.stringify(e)}`);const{code:t,data:n,message:s}=await o(e);if(200!==t)throw s||"Failed to obtain user token.";{const{token:e,token_type:t,expiration:o}=n;$.log(`✅The token is successfully obtained: ${e} and is valid until ${$.time("yyyy-MM-dd HH:mm:ss",1e3*o)}`),this.token=`${t} ${e}`,$.setjson({token:this.token,expiration:1e3*o},"yuheng_ql_token")}}catch(e){throw e?"object"==typeof e?JSON.stringify(e):e:"Network Error."}}async getEnvs(){const e={url:`${this.host}/open/envs`,headers:{Authorization:this.token}};try{const{code:t,data:n,message:s}=await o(e);if(200!==t)throw s||"Failed to obtain the environment variable.";this.envs=n,$.log("✅Obtaining environment variables succeeded.")}catch(e){$.log(e?"object"==typeof e?JSON.stringify(e):e:"Network Error.")}}checkEnvByName(e){return this.envs.findIndex((t=>t.name===e))}checkEnvByRemarks(e){return this.envs.findIndex((t=>t.remarks===e))}checkEnvByValue(e,t){const n=e.match(t);if(n){const t=this.envs.findIndex((e=>e.value.includes(n[0])));return t>-1?($.log(`🆗${e} Matched: ${n[0]}`),t):($.log(`${e} No Matched`),-1)}return $.log(`${e} No Matched`),-1}selectEnvByName(e){return this.envs.filter((t=>t.name===e))}selectEnvByRemarks(e){return this.envs.filter((t=>t.remarks===e))}async addEnv(e){const t={url:`${this.host}/open/envs`,headers:{Authorization:this.token,"Content-Type":"application/json;charset=UTF-8"},body:JSON.stringify(e)};try{const{code:e,message:n}=await o(t,"post");if(200!==e)throw n||"Failed to add the environment variable.";$.log("✅The environment variable was added successfully.")}catch(e){$.log(e?"object"==typeof e?JSON.stringify(e):e:"Network Error.")}}async updateEnv(e){const t={url:`${this.host}/open/envs`,method:"put",headers:{Authorization:this.token,"Content-Type":"application/json;charset=UTF-8"},body:JSON.stringify(e)};try{const{code:n,message:s}=await o(t,"post");if(200!==n)throw s||"Failed to update the environment variable.";$.log("✅The environment variable was updated successfully."),await this.enableEnv([e._id])}catch(e){$.log(e?"object"==typeof e?JSON.stringify(e):e:"Network Error.")}}async deleteEnv(e){const t={url:`${this.host}/open/envs`,method:"delete",headers:{Authorization:`${this.token}`,"Content-Type":"application/json;charset=UTF-8"},body:JSON.stringify(e)};try{const{code:e,message:n}=await o(t,"post");if(200!==e)throw n||"Failed to delete the environment variable.";$.log("✅The environment variable was deleted successfully.")}catch(e){$.log(e?"object"==typeof e?JSON.stringify(e):e:"Network Error.")}}async enableEnv(e){const t={url:`${this.host}/open/envs/enable`,method:"put",headers:{Authorization:`${this.token}`,"Content-Type":"application/json;charset=UTF-8"},body:JSON.stringify(e)};try{const{code:e,message:n}=await o(t,"post");if(200!==e)throw n||"Failed to enable the environment variable.";$.log("✅The environment variable was enabled successfully.")}catch(e){$.log(e?"object"==typeof e?JSON.stringify(e):e:"Network Error.")}}async disableEnv(e){const t={url:`${this.host}/open/envs/disable`,method:"put",headers:{Authorization:`${this.token}`,"Content-Type":"application/json;charset=UTF-8"},body:JSON.stringify(e)};try{const{code:e,message:n}=await o(t,"post");if(200!==e)throw n||"Failed to enable the environment variable.";$.log("✅The environment variable was disable successfully.")}catch(e){$.log(e?"object"==typeof e?JSON.stringify(e):e:"Network Error.")}}async getEnvById(e){const t={url:`${this.host}/open/envs/${e}`,headers:{Authorization:`${this.token}`}};try{const{code:e,data:n,message:s}=await o(t);if(200===e)return n;throw s||"Failed to get the environment variable."}catch(e){$.log(e?"object"==typeof e?JSON.stringify(e):e:"Network Error.")}}}(e,t,n)}