/*
原作者:Sunert

*****************************
中青任务安卓版 包含浏览赚&看看赚&&搜索赚
json版,请把配置文件放到同目录下,默认zqrw1.json
在环境变量export zqjson="zqrw1.json&zqrw2.json",指定脚本执行的json文件,多账户请用&分割,不指定默认zqrw1.json
json格式如下: //用户名自己写,searchdatas:搜索赚body,startdatas:浏览赚body,lookdatas:看看赚body,每条一行
{
    "user":"夏天",
    "kkzmodel":"json",
    "searchdatas":[
        "",
        "",
        ""
    ],
    "startdatas":[
        "",
        "",
        ""		
    ],
    "lookdatas": [
        "",
        "",
        ""
    ],
    "device":{ //设备信息
    "carrier":"中国联通",
    "device_brand":"honor",//设备品牌
    "device_id":"56122847", 
    "device_model":"FRD-AL10",//信号
    "device_platform":"android",//设备品牌
    "device_type":"android",//设备品牌
    "dpi":240,//设备品牌
    "inner_version":"202108181034",//软件的数据,不调整
    "openudid":"872e5e6fba572f6c",//安卓id  android Id
    "os_version":"FRD-AL10C00B396",//build id
    "sm_device_id":"20211025130601d56f524bdb808de8936db94569c28b340110d2680e89602a",
    "rom_version":"FRD-AL10C00B396"
  }
}
改写:jammy 
改写主程序,改写支持安卓版,整合少林功夫猴搜索赚
增加宝箱检测,未开启则自动开启
增加未完成任务检测通知
20211121 全部改写
2021-12-1 增加看看赚模式选择,如果有xxx_kkz.txt文件,优先选择读取文本模式,如用json模式请定义kkzmodel为json
*/

const jsname = '中青看点-看看赚任务ALL'
const $ = Env(jsname)
const notifyFlag = 1; 	//0为关闭通知,1为打开通知,默认为1
const logDebug = 0

const notify = $.isNode() ? require('./sendNotify') : '';

let notifyStr = ''
let rndtime = "" 		//毫秒
let httpResult 			//global buffer

var fs = require("fs");
const CryptoJS = require('./crypto-js')

const HttpsProxyAgent = require("https-proxy-agent")
const HttpProxyAgent = require("http-proxy-agent")

//使用参数 青龙task不支持参数
//const args = process.argv.slice(2);
//let filename = (args.length>0?args[0]:undefined)|| "./zq_yilia.json";

// .task desi # 指定账号执行 task zqkkzrw.js desi zqjson 1 使用'zqjson'的第一个参数  多个可以写1-3
// debug
// process.env.zqjson = 'zq_7627'
// let filename = 'e:\\Works\\Git\\github\\autotask\\othenautojob\\zqjson\\zq_7627.json';;//`./${process.env.zqjson || "./zq_yilia"}.json`;
// let kkzTaskIds = 'e:\\Works\\Git\\github\\autotask\\othenautojob\\zqjson\\zq_kkzids.txt';// filename.replace(/.json/, "_kkz.txt");

let filename = `./${process.env.zqjson || "./zq_yilia"}.json`;
let kkzTaskIds ="./zq_kkzids.txt";// filename.replace(/.json/, "_kkz.txt");
console.log(filename);
let zqbody

let startArr = [], lookArr = [], searchArr = [], jsonArr = [];
let gainscore = 0, lookscore = 0, searchscore = 0, boxscore = 0;
let StartBody = [], LookBody = [], searchBody = [];

let usernmae = "", zqjsons = "", gainbody = "", lookbody = "", zqcookie = "", kkzmodel = "",zqck="";
let useProxy = undefined;

//判断使用代理的账号
if (process.env.zqproxy && process.env.zqproxy.indexOf(process.env.zqjson)>-1) {
    console.log("使用代理服务");
    useProxy = process.env.zqjson;
}

///////////////////////////////////////////////////////////////////

!(async () => {

    if (typeof $request !== "undefined") {
        $.msg(jsname + ': 此脚本不做重写,请检查重写设置')
    }
    else {
        if (!(await checkEnvJson())) {
            return
        }

        notifyStr += username + "本次任务记录:\n";
        await taskall()
        if (zqcookie) {
            console.log(`--------开始检测宝箱奖励状态`);
            await $.wait(5000);
            $.index = 0;
            await getBoxRewardConf()
            console.log(`收取${$.index}次宝箱,获得${boxscore}金币\n`)
            if (boxscore > 0) {
                notifyStr += `收取${$.index}次宝箱,获得${boxscore}金币\n`;
            }
            console.log(`--------开始检测未完成任务`);
            await $.wait(1000);
            await checktasklist(0, '看看赚');
            await $.wait(1000);
            await checktasklist(2, '浏览赚');
        }
        await showmsg()
    }

})()
    .catch((e) => $.logErr(e))
    .finally(() => $.done())

//通知
async function showmsg () {

    notifyBody = "///////////////////////\n" + jsname + "运行通知\n\n" + notifyStr

    if (notifyFlag != 1) {
        console.log(notifyBody);
    }

    if (notifyFlag == 1) {
        console.log(notifyBody);
        if ($.isNode()) { await notify.sendNotify($.name, notifyBody); }
    }
}


//json读取
async function checkEnvJson () {

    console.log(`您使用的是json文件模式\n`)
    if (!check_file_exist(filename)) {
        return false
    }
    zqbody = require(filename);
    username = zqbody.user;
    zqcookie = zqbody.zqcookie;
    searchArr = zqbody.searchdatas;
    zqck = getCk(zqbody.zqcookie);

    //看看赚默认模式json格式,文件模式请设置为file
    kkzmodel = zqbody.kkzmodel ? zqbody.kkzmodel : 'file'
    if (check_file_exist(kkzTaskIds) && kkzmodel != 'json') {
        console.log(`找到看看赚body文本,选择读取文件模式\n`)
        let kkzbodys = ''
        kkzbody = fs.readFileSync(kkzTaskIds, "utf8");
        if (kkzbody !== `undefined`) {
            kkzbodys = kkzbody.split("\n");
        }
        Object.keys(kkzbodys).forEach((item) => {
            if (kkzbodys[item] && !kkzbodys[item].startsWith("#")) {
                lookArr.push(kkzbodys[item])
            }
        })
    } else {
        lookArr = zqbody.lookdatas;
        startArr = zqbody.startdatas;
    }
    if (!lookArr) {
        console.log('没有找到看看赚body,请先抓取')
        //return false
    }
    if (!startArr) {
        console.log('没有找到浏览赚body,请先抓取')
    }
    if (!searchArr) {
        console.log('没有找到搜素赚body,请先抓取')
    }
    if (!zqcookie) {
        console.log('没有找到中青ck,不能自动开宝箱')
    }
    zqcookie = replaceCookie(zqcookie)

    return true
}

function getCk(zqcookie)
{
    let params = UrlParamHash(zqcookie);
    let zquid = params["uid"];
    let zqkey = params["zqkey"];
    let zqkey_id = params["zqkey_id"];
    return 'uid=' + zquid + '&zqkey=' + zqkey + '&zqkey_id=' + zqkey_id
}

function UrlParamHash (url) {
    var params = [], h;
    //var hash = url.slice(url.indexOf("?") + 1).split('&');

    var hash = url.split('&');
    for (var i = 0; i < hash.length; i++) {
        h = hash[i].split("=");
        params.push(h[0]);
        params[h[0]] = h[1];
    }
    return params;
}

//处理ck格式
function replaceCookie (zqcookieItem) {
    if (zqcookieItem.indexOf('cookie=') == -1 && zqcookieItem.indexOf('zqkey=') > -1) {
        zqcookieItem = zqcookieItem.replace(/zqkey=/, "cookie=")
    }
    if (zqcookieItem.indexOf('cookie_id=') == -1 && zqcookieItem.indexOf('zqkey_id=') > -1) {
        zqcookieItem = zqcookieItem.replace(/zqkey_id=/, "cookie_id=")
    }
    return zqcookieItem
}

async function taskall () {

    console.log(`${username} 共提供了${startArr.length}次浏览赚任务`)
    if (startArr.length !== 0) {
        $.index = 0;
        notifyStr += `浏览赚:共${startArr.length}次任务,`;
        for (let i = 0; i < startArr.length; i++) {
            if (startArr[i]) {
                gainbody = startArr[i];
                $.log(`----------\n🏠开始浏览赚第 ${k} ${$.index + 1}次任务`)
            }
            await GainStart();
        }
        console.log(`----------\n\n中青看点共完成${$.index}次任务,共计获得${gainscore}个青豆,浏览赚任务全部结束`);
        notifyStr += `浏览赚完成${$.index}次任务` + `  获得${gainscore}个青豆\n`;
    }

    console.log(`---休息一下,开始下一个任务。`);
    await $.wait(8000);


    console.log(`\n${username}共提供${lookArr.length}次看看赚任务\n`)
    if (lookArr.length !== 0) {
        $.index = 0;
        notifyStr += `看看赚:共${lookArr.length}次任务,`;
        for (let k = 0; k < lookArr.length; k++) {
            if (lookArr[k]) {
                lookbody =await encodeUserBody1(zqck,lookArr[k]);//生成看看赚的body
                $.log(`----------\n🏠开始中青看点看看赚第 ${k} ${$.index + 1}次任务`)
            }
            await lookStart();
        }
        console.log(`\n中青看点共完成${$.index}次看看赚任务,共计获得${lookscore}个青豆,看看赚任务全部结束`);
        notifyStr += `看看赚完成${$.index}次任务` + `  获得${lookscore}个青豆\n`;
    }

    console.log(`---休息一下,开始下一个任务。`);
    await $.wait(10000);

    console.log(`\n\n${username}共提供了${searchArr.length}次搜索赚任务`);
    if (searchArr.length !== 0) {
        $.index = 0;
        notifyStr += `搜索赚:共${searchArr.length}次任务,`;
        for (let k = 0; k < searchArr.length; k++) {
            zqsszbody1 = searchArr[k];
            console.log(`----------\n🏠开始搜索赚第 ${k} ${$.index + 1} 次任务`)
            await sszStart();
            await $.wait(1000);
        }
        console.log(`-------------------------\n\n中青看点共完成${$.index}次搜索赚任务,共计获得${searchscore}个青豆,搜素赚任务全部结束`);
        notifyStr += `搜素赚完成${$.index}次任务` + `  获得${searchscore}个青豆\n`;
    }

}

//看看赚宝箱状态
async function getBoxRewardConf () {
    let caller = printCaller()
    let reqCk = 'device_type=android&' + zqcookie
    let url = 'https://kd.youth.cn/WebApi/Nameless/getBoxRewardConf?' + reqCk
    let urlObject = populateGetUrl(url)
    urlObject.headers.Referer = 'https://kd.youth.cn/h5/20190527watchMoney/?' + reqCk
    await httpGet(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.status == 1) {
        if (result.data && result.data.list && Array.isArray(result.data.list)) {
            for (let i = 0; i < result.data.list.length; i++) {
                let boxItem = result.data.list[i]
                if (boxItem.status == 1) {
                    console.log(`🏠宝箱${i + 1}:${boxItem.score}青豆未领取,尝试领取宝箱${i + 1}`);
                    await $.wait(Math.floor(Math.random() * 2000) + 32000)
                    await getBoxReward(boxItem.id)
                }
                else if (boxItem.status == 0) console.log(`开启宝箱需完成任务:${boxItem.num} 次未达成,跳过。`)
                else if (boxItem.status == 2) console.log(`🏠宝箱${i + 1}:${boxItem.score}青豆已领取,跳过。`)
            }
        }
    } else {
        console.log(`获取看看赚宝箱状态失败:${result.message}`)
    }
}

//看看赚宝箱领取
async function getBoxReward (id) {
    let caller = printCaller()
    let reqCk = 'device_type=android&' + zqcookie
    let url = `https://kandian.wkandian.com/WebApi/Nameless/getBoxReward?id=${id}&${reqCk}`
    let urlObject = populateGetUrl(url)
    urlObject.headers.Referer = 'https://kandian.wkandian.com/h5/20190527watchMoney/?' + reqCk
    await httpGet(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.status == 1) {
        boxscore += parseInt(result.data)
        $.index++
        console.log(`打开第${id + 1}个看看赚宝箱获得:${result.data}金币`)
    } else {
        console.log(`领取看看赚宝箱失败:${result.message}`)
    }
}

//检测新增浏览赚2和看看赚0任务
async function checktasklist (id, desc) {
    let list1 = 0, list2 = 0;
    let listArr = [], lists = '';
    let rndtime = Math.floor(new Date().getTime())
    let caller = printCaller()
    let reqCk = 'device_type=android&' + zqcookie
    let url = `https://kd.youth.cn/WebApi/Nameless/getTaskBrowse?type=${id}&${reqCk}`
    let urlObject = populateGetUrl(url)
    urlObject.headers.Referer = 'https://kd.youth.cn/h5/20190527watchMoney/?' + reqCk
    urlObject.headers.Host = 'kd.youth.cn'
    await httpGet(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.status == 1) {
        listArr = result.data.list;
        for (let i = 0; i < listArr.length; i++) {
            if (listArr[i].status != '2') {
                console.log(listArr[i].title + '未完成');
                lists += listArr[i].title + ' ';
                list1 += 1;
            } else {
                list2 += 1;
            }
        }
        console.log(`${desc}已完成任务数${list2}`);
        console.log(`${desc}未完成任务数${list1}`);
        if (list1 > 0) {
            notifyStr += `${username},发现 ${desc} 更新了 ${list1} 个新的任务:\n`;
            notifyStr += `${lists}\n`;
        }
    } else {
        console.log(`${result}`);
    }
}

//浏览赚
async function GainStart () {
    let caller = printCaller()
    let url = 'http://kandian.wkandian.com/v5/task/browse_start.json'
    let urlObject = populatePostUrl(url, gainbody)
    await httpPost(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.success == false) {
        console.log(result.message + "已自动跳过")
    } else {
        comstate = result.items.comtele_state;
        if (comstate == 0) {
            console.log("任务开始," + result.items.banner_id + result.message);
            await $.wait(10000);
            await GainEnd()
        } else if (comstate == 1) {
            console.log("任务:" + result.items.banner_id + "已完成,本次跳过");
        }
    }
}
//看看赚
async function lookStart () {
    let caller = printCaller()
    let url = 'http://kandian.wkandian.com/v5/Nameless/adlickstart.json'
    let urlObject = populatePostUrl(url, lookbody)
    await httpPost(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.success == false) {
        console.log(result.message + "已自动跳过")
    } else {
        comstate = result.items.comtele_state;
        if (comstate == 0) {
            console.log("任务开始," + result.items.banner_id + result.message);
            for (let j = 0; j < result.items.see_num - result.items.read_num; j++) {
                console.log("任务执行第" + parseInt(j + 1) + "次")
                await $.wait(8000);
                await lookstatus()
            }
            await $.wait(10000);
            await lookEnd()
        } else if (comstate == 1) {
            console.log("任务:" + result.items.banner_id + "已完成,本次跳过");
        }
    }
}

async function GainEnd () {
    let caller = printCaller()
    let url = 'http://kandian.wkandian.com/v5/task/browse_end.json'
    let urlObject = populatePostUrl(url, gainbody)
    await httpPost(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.success == true) {
        console.log("任务" + result.items.banner_id + result.message + ",恭喜获得" + result.items.score + "个青豆");
        gainscore += parseInt(result.items.score);
        $.index += 1;//获得奖励,次数加1
    } else {
        console.log(result.message)
    }
}

async function lookstatus () {
    let caller = printCaller()
    let url = 'http://kandian.wkandian.com/v5/Nameless/bannerstatus.json'
    let urlObject = populatePostUrl(url, lookbody)
    await httpPost(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.success == true) {
        console.log("任务" + result.items.banner_id + result.message);
    } else {
        console.log(result.message)
    }
}

async function lookEnd () {
    let caller = printCaller()
    let url = 'http://kandian.wkandian.com/v5/Nameless/adlickend.json'
    let urlObject = populatePostUrl(url, lookbody)
    await httpPost(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.success == true) {
        console.log("任务" + result.items.banner_id + result.message + "," + result.items.desc)
        lookscore += parseInt(result.items.score);
        $.index += 1;//获得奖励,次数加1
    } else {
        console.log(result.message)
    }
}

//搜索赚激活
async function sszStart (timeout = 0) {
    let caller = printCaller()
    let url = 'http://kandian.wkandian.com/v5/Sousuo/playStart.json'
    let urlObject = populatePostUrl(url, zqsszbody1)
    await httpPost(urlObject, caller)
    let result = httpResult;
    if (!result) return

    if (result.success === true) {
        console.log('激活搜索赚任务成功')
        comstate = result.items.comtele_state
        if (comstate === 1) {
            console.log('任务: ' + result.items.task_id + '已完成,跳过')
        } else {
            console.log("任务开始," + result.items.task_id + result.message);
            for (let i = 0; i < 4; i++) {
                await $.wait(2000);
                await look()
                await $.wait(10000);
                await look()
                await $.wait(10000);
                await end()
            }
        }
    }
    else {
        console.log('激活搜索赚任务失败')
        console.log("该搜索赚任务已自动跳过")
    }
}

async function look (timeout = 0) {
    let caller = printCaller()
    let url = 'http://kandian.wkandian.com/v5/Sousuo/playStatus.json'
    let urlObject = populatePostUrl(url, zqsszbody1)
    await httpPost(urlObject, caller)
    let result = httpResult;
    if (!result) return
    console.log(result)
}
//搜索赚奖励
async function end (timeout = 0) {
    let caller = printCaller()
    let url = 'http://kandian.wkandian.com/v5/Sousuo/playEnd.json'
    let urlObject = populatePostUrl(url, zqsszbody1)
    await httpPost(urlObject, caller)
    let result = httpResult;
    if (!result) return
    if (result.items.score !== "undefined") {
        console.log('搜索赚获得:' + result.items.score + '金币');
        searchscore += parseInt(result.items.score);
        $.index += 1;//获得奖励,次数加1
    } else {
        console.log('领取奖励失败')
    }
}

async function encodeUserBody1 (cookie, p1) {
    let rndtime = Math.floor(new Date().getTime() / 1000)
    preBody = 'p=ycTMBiVxDAfc%3D'
    bodyTemplate = 'access=WIFI&app-version=3.5.5&app_name=zqkd_app&app_version=3.5.5&'
    //bodyTemplate='access=WIFI&app-version=3.5.5&app_name=zqkd_app&app_version=3.5.5&channel=c1002&device_brand='+params["device_brand"]+'&device_id='+params["device_id"]+'&device_model='+params["device_model"]+'&device_platform=android&device_type=android&dpi='+params["dpi"]+'&inner_version='+params["inner_version"]+'&language=zh-CN&memory=3&mi=0&mobile_type=1&net_type=1&network_type=WIFI&openudid='+params["openudid"]+'&os_api='+params["os_api"]+'&os_version='+params["os_version"]+'&request_time='+rndtime+'&resolution='+params["resolution"]+'&rom_version='+params["rom_version"]+'&s_ad='+params["s_ad"]+'&s_im='+params["s_im"]+'&sm_device_id='+params["sm_device_id"]+'&storage='+params["storage"]+'&'
    //console.log(bodyTemplate)
    fakeSign = '&sign=573f59b726a9143335983661ca504101'
    key = '6HPjSZFH'

    replacedStr = bodyTemplate + cookie + '&task_id=' + p1 + fakeSign
    encodeBody = myFuncB1(replacedStr)
    hexBody = CryptoJS.enc.Hex.parse(encodeBody)
    let base64Body = CryptoJS.enc.Base64.stringify(hexBody)
    replaceBody3 = base64Body.replace(/\+/g, '-')
    replaceBody4 = replaceBody3.replace(/\//g, '_')
    //finalBody = preBody + replaceBody4 
    finalBody = preBody + encodeURIComponent(replaceBody4) + '=='
    return finalBody;
}

function myFuncB1 (data) {
    var a19 = CryptoJS.enc.Utf8.parse(key),
        b68 = CryptoJS.enc.Utf8.parse(key),
        b69 = CryptoJS.enc.Utf8.parse(data);
    return encrypted = CryptoJS.DES.encrypt(b69, a19, {
        'iv': b68,
        'mode': CryptoJS.mode.CBC,
        'padding': CryptoJS.pad.Pkcs7
    }), encrypted.ciphertext.toString();
}

////////////////////////////////////////////////////////////////////
function populatePostUrl (url, reqBody) {
    let rndtime = Math.floor(new Date().getTime() / 1000)
    let urlObject = {
        url: url,
        headers: {
            'User-Agent': 'okhttp/3.12.2',
            'Host': 'kandian.wkandian.com',
            'device-platform': 'android',
            'Connection': 'keep-alive',
        },
        body: reqBody
    }
    return urlObject;
}

function populateGetUrl (url) {
    let rndtime = Math.floor(new Date().getTime() / 1000)
    let urlObject = {
        url: url,
        headers: {
            'User-Agent': 'okhttp/3.12.2',
            'Host': 'kandian.wkandian.com',
            'device-platform': 'android',
            'Connection': 'keep-alive',
        }
    }
    return urlObject;
}

function getProxy (url,session) {

    //"http://lum-customer-hl_7e7d94d3-zone-data_center-ip-158.46.169.208:k5v04d5knlys@zproxy.lum-superproxy.io:22225"
    var httpProxy = new HttpProxyAgent(
        "http://lum-customer-hl_7e7d94d3-zone-data_center-session-"+session+":k5v04d5knlys@zproxy.lum-superproxy.io:22225"
        // `https://${process.env.PS_PROXY_USERNAME}:${process.env.PS_PROXY_PASSWORD}@${process.env.PS_PROXY_HOST}:${process.env.PS_PROXY_PORT}`
    );
    var httpsProxy = new HttpsProxyAgent(
        "https://lum-customer-hl_7e7d94d3-zone-data_center-session-"+session+":k5v04d5knlys@zproxy.lum-superproxy.io:22225"
        // `https://${process.env.PS_PROXY_USERNAME}:${process.env.PS_PROXY_PASSWORD}@${process.env.PS_PROXY_HOST}:${process.env.PS_PROXY_PORT}`
    );

    let agent = { http: httpProxy }
    if (url.indexOf("https") > -1) {
        agent.https = httpsProxy;
    }
    return agent;
}

async function httpPost (url, caller) {
    httpResult = null
    if (useProxy) {
        url.agent = getProxy(url.url,useProxy);
    }
    return new Promise((resolve) => {
        $.post(url, async (err, resp, data) => {
            try {
                if (err) {
                    console.log(caller + ": post请求失败");
                    console.log(JSON.stringify(err));
                    $.logErr(err);
                } else {
                    if (safeGet(data)) {
                        httpResult = JSON.parse(data, caller);
                        if (logDebug) console.log(httpResult);
                    }
                }
            } catch (e) {
                $.logErr(e, resp);
            } finally {
                resolve();
            }
        });
    });
}

async function httpGet (url, caller) {
    httpResult = null
    if (useProxy) {
        url.agent = getProxy(url.url,useProxy);
    }
    return new Promise((resolve) => {
        $.get(url, async (err, resp, data) => {
            try {
                if (err) {
                    console.log(caller + ": get请求失败");
                    console.log(JSON.stringify(err));
                    $.logErr(err);
                } else {
                    if (safeGet(data, caller)) {
                        httpResult = JSON.parse(data);
                        if (logDebug) console.log(httpResult);
                    }
                }
            } catch (e) {
                $.logErr(e, resp);
            } finally {
                resolve();
            }
        });
    });
}

function safeGet (data, caller) {
    try {
        if (typeof JSON.parse(data) == "object") {
            return true;
        } else {
            console.log(`Function ${caller}: 未知错误`);
            console.log(data)
        }
    } catch (e) {
        console.log(e);
        console.log(`Function ${caller}: 服务器访问数据为空,请检查自身设备网络情况`);
        return false;
    }
}

function printCaller () {
    return (new Error()).stack.split("\n")[2].trim().split(" ")[1]
}
function randomNum (minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            break;
        default:
            return 0;
            break;
    }
}
//检查文件是否存在
function check_file_exist (filename) {
    try {
        fs.accessSync(filename, fs.constants.R_OK);
        return true;
    } catch (err) {
        console.error(`检测文件${filename}不存在`);
        return false;
    }
}

function Env (t, e) { class s { constructor(t) { this.env = t } send (t, e = "GET") { t = "string" == typeof t ? { url: t } : t; let s = this.get; return "POST" === e && (s = this.post), new Promise((e, i) => { s.call(this, t, (t, s, r) => { t ? i(t) : e(s) }) }) } get (t) { return this.send.call(this.env, t) } post (t) { return this.send.call(this.env, t, "POST") } } return new class { constructor(t, e) { this.name = t, this.http = new s(this), this.data = null, this.dataFile = "box.dat", this.logs = [], this.isMute = !1, this.isNeedRewrite = !1, this.logSeparator = "\n", this.startTime = (new Date).getTime(), Object.assign(this, e), this.log("", `\ud83d\udd14${this.name}, \u5f00\u59cb!`) } isNode () { return "undefined" != typeof module && !!module.exports } isQuanX () { return "undefined" != typeof $task } isSurge () { return "undefined" != typeof $httpClient && "undefined" == typeof $loon } isLoon () { return "undefined" != typeof $loon } toObj (t, e = null) { try { return JSON.parse(t) } catch { return e } } toStr (t, e = null) { try { return JSON.stringify(t) } catch { return e } } getjson (t, e) { let s = e; const i = this.getdata(t); if (i) try { s = JSON.parse(this.getdata(t)) } catch { } return s } setjson (t, e) { try { return this.setdata(JSON.stringify(t), e) } catch { return !1 } } getScript (t) { return new Promise(e => { this.get({ url: t }, (t, s, i) => e(i)) }) } runScript (t, e) { return new Promise(s => { let i = this.getdata("@chavy_boxjs_userCfgs.httpapi"); i = i ? i.replace(/\n/g, "").trim() : i; let r = this.getdata("@chavy_boxjs_userCfgs.httpapi_timeout"); r = r ? 1 * r : 20, r = e && e.timeout ? e.timeout : r; const [o, h] = i.split("@"), a = { url: `http://${h}/v1/scripting/evaluate`, body: { script_text: t, mock_type: "cron", timeout: r }, headers: { "X-Key": o, Accept: "*/*" } }; this.post(a, (t, e, i) => s(i)) }).catch(t => this.logErr(t)) } loaddata () { if (!this.isNode()) return {}; { this.fs = this.fs ? this.fs : require("fs"), this.path = this.path ? this.path : require("path"); const t = this.path.resolve(this.dataFile), e = this.path.resolve(process.cwd(), this.dataFile), s = this.fs.existsSync(t), i = !s && this.fs.existsSync(e); if (!s && !i) return {}; { const i = s ? t : e; try { return JSON.parse(this.fs.readFileSync(i)) } catch (t) { return {} } } } } writedata () { if (this.isNode()) { this.fs = this.fs ? this.fs : require("fs"), this.path = this.path ? this.path : require("path"); const t = this.path.resolve(this.dataFile), e = this.path.resolve(process.cwd(), this.dataFile), s = this.fs.existsSync(t), i = !s && this.fs.existsSync(e), r = JSON.stringify(this.data); s ? this.fs.writeFileSync(t, r) : i ? this.fs.writeFileSync(e, r) : this.fs.writeFileSync(t, r) } } lodash_get (t, e, s) { const i = e.replace(/\[(\d+)\]/g, ".$1").split("."); let r = t; for (const t of i) if (r = Object(r)[t], void 0 === r) return s; return r } lodash_set (t, e, s) { return Object(t) !== t ? t : (Array.isArray(e) || (e = e.toString().match(/[^.[\]]+/g) || []), e.slice(0, -1).reduce((t, s, i) => Object(t[s]) === t[s] ? t[s] : t[s] = Math.abs(e[i + 1]) >> 0 == +e[i + 1] ? [] : {}, t)[e[e.length - 1]] = s, t) } getdata (t) { let e = this.getval(t); if (/^@/.test(t)) { const [, s, i] = /^@(.*?)\.(.*?)$/.exec(t), r = s ? this.getval(s) : ""; if (r) try { const t = JSON.parse(r); e = t ? this.lodash_get(t, i, "") : e } catch (t) { e = "" } } return e } setdata (t, e) { let s = !1; if (/^@/.test(e)) { const [, i, r] = /^@(.*?)\.(.*?)$/.exec(e), o = this.getval(i), h = i ? "null" === o ? null : o || "{}" : "{}"; try { const e = JSON.parse(h); this.lodash_set(e, r, t), s = this.setval(JSON.stringify(e), i) } catch (e) { const o = {}; this.lodash_set(o, r, t), s = this.setval(JSON.stringify(o), i) } } else s = this.setval(t, e); return s } getval (t) { return this.isSurge() || this.isLoon() ? $persistentStore.read(t) : this.isQuanX() ? $prefs.valueForKey(t) : this.isNode() ? (this.data = this.loaddata(), this.data[t]) : this.data && this.data[t] || null } setval (t, e) { return this.isSurge() || this.isLoon() ? $persistentStore.write(t, e) : this.isQuanX() ? $prefs.setValueForKey(t, e) : this.isNode() ? (this.data = this.loaddata(), this.data[e] = t, this.writedata(), !0) : this.data && this.data[e] || null } initGotEnv (t) { this.got = this.got ? this.got : require("got"), this.cktough = this.cktough ? this.cktough : require("tough-cookie"), this.ckjar = this.ckjar ? this.ckjar : new this.cktough.CookieJar, t && (t.headers = t.headers ? t.headers : {}, void 0 === t.headers.Cookie && void 0 === t.cookieJar && (t.cookieJar = this.ckjar)) } get (t, e = (() => { })) { t.headers && (delete t.headers["Content-Type"], delete t.headers["Content-Length"]), this.isSurge() || this.isLoon() ? (this.isSurge() && this.isNeedRewrite && (t.headers = t.headers || {}, Object.assign(t.headers, { "X-Surge-Skip-Scripting": !1 })), $httpClient.get(t, (t, s, i) => { !t && s && (s.body = i, s.statusCode = s.status), e(t, s, i) })) : this.isQuanX() ? (this.isNeedRewrite && (t.opts = t.opts || {}, Object.assign(t.opts, { hints: !1 })), $task.fetch(t).then(t => { const { statusCode: s, statusCode: i, headers: r, body: o } = t; e(null, { status: s, statusCode: i, headers: r, body: o }, o) }, t => e(t))) : this.isNode() && (this.initGotEnv(t), this.got(t).on("redirect", (t, e) => { try { if (t.headers["set-cookie"]) { const s = t.headers["set-cookie"].map(this.cktough.Cookie.parse).toString(); this.ckjar.setCookieSync(s, null), e.cookieJar = this.ckjar } } catch (t) { this.logErr(t) } }).then(t => { const { statusCode: s, statusCode: i, headers: r, body: o } = t; e(null, { status: s, statusCode: i, headers: r, body: o }, o) }, t => { const { message: s, response: i } = t; e(s, i, i && i.body) })) } post (t, e = (() => { })) { if (t.body && t.headers && !t.headers["Content-Type"] && (t.headers["Content-Type"] = "application/x-www-form-urlencoded"), t.headers && delete t.headers["Content-Length"], this.isSurge() || this.isLoon()) this.isSurge() && this.isNeedRewrite && (t.headers = t.headers || {}, Object.assign(t.headers, { "X-Surge-Skip-Scripting": !1 })), $httpClient.post(t, (t, s, i) => { !t && s && (s.body = i, s.statusCode = s.status), e(t, s, i) }); else if (this.isQuanX()) t.method = "POST", this.isNeedRewrite && (t.opts = t.opts || {}, Object.assign(t.opts, { hints: !1 })), $task.fetch(t).then(t => { const { statusCode: s, statusCode: i, headers: r, body: o } = t; e(null, { status: s, statusCode: i, headers: r, body: o }, o) }, t => e(t)); else if (this.isNode()) { this.initGotEnv(t); const { url: s, ...i } = t; this.got.post(s, i).then(t => { const { statusCode: s, statusCode: i, headers: r, body: o } = t; e(null, { status: s, statusCode: i, headers: r, body: o }, o) }, t => { const { message: s, response: i } = t; e(s, i, i && i.body) }) } } time (t) { let e = { "M+": (new Date).getMonth() + 1, "d+": (new Date).getDate(), "H+": (new Date).getHours(), "m+": (new Date).getMinutes(), "s+": (new Date).getSeconds(), "q+": Math.floor(((new Date).getMonth() + 3) / 3), S: (new Date).getMilliseconds() }; /(y+)/.test(t) && (t = t.replace(RegExp.$1, ((new Date).getFullYear() + "").substr(4 - RegExp.$1.length))); for (let s in e) new RegExp("(" + s + ")").test(t) && (t = t.replace(RegExp.$1, 1 == RegExp.$1.length ? e[s] : ("00" + e[s]).substr(("" + e[s]).length))); return t } msg (e = t, s = "", i = "", r) { const o = t => { if (!t) return t; if ("string" == typeof t) return this.isLoon() ? t : this.isQuanX() ? { "open-url": t } : this.isSurge() ? { url: t } : void 0; if ("object" == typeof t) { if (this.isLoon()) { let e = t.openUrl || t.url || t["open-url"], s = t.mediaUrl || t["media-url"]; return { openUrl: e, mediaUrl: s } } if (this.isQuanX()) { let e = t["open-url"] || t.url || t.openUrl, s = t["media-url"] || t.mediaUrl; return { "open-url": e, "media-url": s } } if (this.isSurge()) { let e = t.url || t.openUrl || t["open-url"]; return { url: e } } } }; this.isMute || (this.isSurge() || this.isLoon() ? $notification.post(e, s, i, o(r)) : this.isQuanX() && $notify(e, s, i, o(r))); let h = ["", "==============\ud83d\udce3\u7cfb\u7edf\u901a\u77e5\ud83d\udce3=============="]; h.push(e), s && h.push(s), i && h.push(i), console.log(h.join("\n")), this.logs = this.logs.concat(h) } log (...t) { t.length > 0 && (this.logs = [...this.logs, ...t]), console.log(t.join(this.logSeparator)) } logErr (t, e) { const s = !this.isSurge() && !this.isQuanX() && !this.isLoon(); s ? this.log("", `\u2757\ufe0f${this.name}, \u9519\u8bef!`, t.stack) : this.log("", `\u2757\ufe0f${this.name}, \u9519\u8bef!`, t) } wait (t) { return new Promise(e => setTimeout(e, t)) } done (t = {}) { const e = (new Date).getTime(), s = (e - this.startTime) / 1e3; this.log("", `\ud83d\udd14${this.name}, \u7ed3\u675f! \ud83d\udd5b ${s} \u79d2`), this.log(), (this.isSurge() || this.isQuanX() || this.isLoon()) && $done(t) } }(t, e) }