-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbaiduwp.js
1 lines (1 loc) · 8.26 KB
/
baiduwp.js
1
(()=>{var t={781:()=>{},843:t=>{"use strict";t.exports={}}},e={};function s(i){var o=e[i];if(void 0!==o)return o.exports;var n=e[i]={exports:{}};return t[i](n,n.exports,s),n.exports}(()=>{"use strict";var t,e,i;!function(t){t.Surge="Surge",t.Loon="Loon",t.QuanX="QuanX",t.Shadowrocket="Shadowrocket",t.Node="Node"}(t||(t={})),function(t){t.Response="http-response",t.Request="http-request",t.Script="run-script"}(e||(e={})),function(t){t[t.BASE=0]="BASE",t[t.HTTP=1]="HTTP",t[t.SYS=2]="SYS",t[t.OTHER=3]="OTHER"}(i||(i={}));class o extends Error{constructor(t,e=i.BASE){super(t),this.name="baseErr",this.code=e,this.stack=(new Error).stack,Object.setPrototypeOf(this,o.prototype)}}class n{constructor(t,e){this.isMute=!1,this.logList=[],this.logSeparator="\n\n",this.appName=t,this.namespace=e,this.startTime=(new Date).getTime(),this.log(`🔔${this.appName}, 开始!`),this.initAction(),this.initEnv();let i=this.getStore("mute");this.isMute="true"==i,s(843)}initAction(){"undefined"!=typeof $response?this.action=e.Response:"undefined"!=typeof $request?this.action=e.Request:this.action=e.Script,this.log("脚本类型为:"+this.action)}async doAction(){switch(this.action){case e.Request:this.result=await this.doRequestAction($request);break;case e.Response:this.result=await this.doResponseAction($request,$response);break;case e.Script:this.result=await this.doScriptAction();break;default:this.log(this.appName,"Unknow Action","未知的脚本类型"),this.result=!1}!1===this.result&&this.msg(this.appName,"不合法的脚本","请检查脚本配置信息")}run(){this.doAction().catch((t=>{t instanceof o?(this.log(""+t.code),t.code==i.BASE?this.msg(this.appName,t.message,""):t.code==i.HTTP?Math.random()>.8?this.msg(this.appName,"网络异常:"+t.message,""):this.log(this.appName,"网络异常Log:"+t.message,""):this.log(t.message,"")):this.log(t),this.result=this.ajaxFailResult(t.message||t)})).finally((()=>{this.done()}))}transParams(t){return Object.keys(t).map((e=>`${e}=${encodeURIComponent(t[e])}`)).join("&")}httpResponseResult(e,s={}){const i={status:this.env===t.QuanX?"HTTP/1.1 200":200,body:"string"==typeof e?e:JSON.stringify(e),headers:{"Content-Type":"application/json; charset=utf-8","Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"POST,GET","Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept",...s}};return this.env===t.QuanX?i:{response:i}}randomString(t){for(var e="",s=0;s<t;s++)e+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".charAt(Math.floor(62*Math.random()));return e}handelLogHttp(){this.log(`运行 》 ${this.appName}系统运行日志http服务器`);let t=this.getStore(n.APP_LOG_KEY)||"";const e=new RegExp(this.logSeparator,"g");return t=t.replace(e,"<br>"),this.httpResponseResult(t,{"Content-Type":"text/html;charset=utf-8"})}send(t){return new Promise(((e,s)=>{this.doRequest(t,((t,n,r)=>{t?s(new o(t,i.HTTP)):e(n)}))}))}async post(t){return t.method="post",await this.send(t)}async get(t){return t.method="get",await this.send(t)}doRequest(e,s=((t,e,s)=>{})){const i=e.method?e.method.toLocaleLowerCase():"post";e.body&&e.headers&&!e.headers["Content-Type"]&&(e.headers["Content-Type"]="application/x-www-form-urlencoded"),e.headers&&delete e.headers["Content-Length"],this.env==t.Surge||this.env==t.Shadowrocket||this.env==t.Loon?(this.env==t.Surge&&this.isNeedRewrite&&(e.headers=e.headers||{},Object.assign(e.headers,{"X-Surge-Skip-Scripting":!1})),$httpClient[i](e,((t,e,i)=>{!t&&e&&(e.body=i,e.statusCode=e.status?e.status:e.statusCode,e.status=e.statusCode),s(t,e,i)}))):this.env==t.QuanX?(e.method=i,this.isNeedRewrite&&(e.opts=e.opts||{},Object.assign(e.opts,{hints:!1})),$task.fetch(e).then((t=>{const{statusCode:e,statusCode:i,headers:o,body:n}=t;s(null,{status:e,statusCode:i,headers:o,body:n},n)}),(t=>s(t&&t.error||"UndefinedError",null,null)))):this.env==t.Node&&this.print("nodejs http request",e,this.env)}sleep(t){return new Promise((e=>setTimeout(e,t)))}ajaxSuccessResult(t,e=null){let s={time:+new Date,datetime:this.date("yyyy-MM-dd HH:mm:ss"),code:1,msg:t,data:e};return this.httpResponseResult(s)}ajaxFailResult(t,e=null){let s={time:+new Date,datetime:this.date("yyyy-MM-dd HH:mm:ss"),code:0,msg:t,data:e};return this.httpResponseResult(s)}done(){const s=((new Date).getTime()-this.startTime)/1e3;if(this.action===e.Script&&this.print("运行 response: "+JSON.stringify(this.result)),this.log(`🔔${this.appName}, 结束! 🕛 ${s} 秒 ${this.logSeparator}`),this.env==t.Node)process.exit(1);else{let t=this.getStore(n.APP_LOG_KEY)||"";t=t.split(this.logSeparator).slice(0,1e4).join(this.logSeparator),t=this.logList.join("")+t,this.setStore(n.APP_LOG_KEY,t),this.print("注意本次运行日志已缓存到变量 "+this.namespace+"."+n.APP_LOG_KEY),this.result?$done(this.result):$done({})}}msg(e,s,i){this.isMute||(this.log("==============📣系统通知📣=============="+this.logSeparator+e+this.logSeparator+s+this.logSeparator+i),this.env==t.Surge||this.env==t.Shadowrocket||this.env==t.Loon?$notification.post(e,s,i):this.env==t.QuanX&&$notify(e,s,i))}print(...t){t=t.map((t=>this.date("yyyy-MM-dd HH:mm:ss")+" "+t+this.logSeparator)),console.log(t.join(this.logSeparator))}log(...t){(t=t.map((t=>this.date("yyyy-MM-dd HH:mm:ss")+" "+("string"==typeof t?t:JSON.stringify(t))+this.logSeparator))).length>0&&(this.logList=[...this.logList,...t]),console.log(t.join(this.logSeparator))}getStore(e,s=!0){return s&&(e=this.namespace+"."+e),this.env==t.Surge||this.env==t.Shadowrocket||this.env==t.Loon?$persistentStore.read(e):this.env==t.QuanX?$prefs.valueForKey(e):null}setStore(e,s,i=!0){return i&&(e=this.namespace+"."+e),this.env==t.Surge||this.env==t.Shadowrocket||this.env==t.Loon?$persistentStore.write(s,e):this.env==t.QuanX&&$prefs.setValueForKey(s,e)}initEnv(){"undefined"!=typeof $task?this.env=t.QuanX:"undefined"!=typeof $loon?this.env=t.Loon:"undefined"!=typeof $rocket?this.env=t.Shadowrocket:"undefined"!=typeof $httpClient&&"undefined"==typeof $loon?this.env=t.Surge:this.env=t.Node,this.log("当前APP为: "+this.env)}date(t,e=""){const s=e?new Date(e):new Date;let i={"M+":s.getMonth()+1,"d+":s.getDate(),"H+":s.getHours(),"m+":s.getMinutes(),"s+":s.getSeconds(),"q+":Math.floor((s.getMonth()+3)/3),S:s.getMilliseconds()};/(y+)/.test(t)&&(t=t.replace(RegExp.$1,(s.getFullYear()+"").substr(4-RegExp.$1.length)));for(let e in i){let s=i[e];new RegExp("("+e+")").test(t)&&(t=t.replace(RegExp.$1,1==RegExp.$1.length?s:("00"+s).substr((""+s).length)))}return t}getSignCount(){let t=this.getStore("sign_count"),e=this.date("yyyyMMdd");if(t){let[s,i]=t.split("_");return s==e&&i?Number(i):(this.setStore("sign_count",`${e}_0`),0)}return this.setStore("sign_count",`${e}_0`),0}incSignCount(){let t=this.getSignCount();t++;let e=this.date("yyyyMMdd");this.setStore("sign_count",`${e}_${t}`)}}n.APP_LOG_KEY="boxjs-log";const r=n;s(781),new class extends r{async doRequestAction(t){if(t.url.includes("baiduwp.log"))return this.handelLogHttp();if(t.url.includes("baidupcs.com/file")){let e=t.headers,s=this.getStore("user_agent");s||(s="LogStatistic",this.setStore("user_agent",s)),e["User-Agent"]=s;let n=this.getStore("aria2_ip"),r="true"==this.getStore("is_aria2");if(n&&r){let r=this.getStore("aria2_urls");r||(r="[]",this.setStore("aria2_urls",r));let a=JSON.parse(r);if(a.includes(t.url))throw new o("该链接已下载,请勿重复下载。。。"+t.url,i.SYS);a.push(t.url),e["User-Agent"]="";let h={id:this.randomString(10),jsonrpc:"2.0",method:"aria2.addUri",params:[[t.url],{header:["User-Agent:"+s]}]};const l="auto"==n?await this.fetchAria2Url():n;this.log("ARIA2 RPC URL:",l),this.log("ARIA2 RPC param:",h);const u=await this.post({url:l,body:JSON.stringify(h)});JSON.parse(u.body).result?(this.setStore("aria2_urls",JSON.stringify(a)),this.msg(this.appName,"下载成功!",u.body)):this.msg(this.appName,"下载失败!",u.body)}return this.log("读取到百度直链URL:"+t.url),{headers:e}}return!1}async fetchAria2Url(){const t=await this.get({url:"https://gitee.com/jsonp/jsonp/raw/master/get_cpolar_list.js"}),[,e]=/get_cpolar_list\((.*?)\)/.exec(t.body)??[],s=JSON.parse(e).find((t=>"aria2"===t.name));if(s&&s.domain)return`http://${s.domain}/jsonrpc`;throw new o("无法读取Aria2域名",i.BASE)}doResponseAction(t,e){throw new Error("Method not implemented.")}doScriptAction(){throw new Error("Method not implemented.")}}("百度网盘直链","gsonhub.baiduwp").run()})()})();