jdpro/jdCookie.js

225 lines
8.8 KiB
JavaScript
Raw Normal View History

2024-06-28 07:47:44 +00:00
/*
此文件为Node.js专用其他用户请忽略
*/
//此处填写京东账号cookie。
let CookieJDs = [
2024-08-02 03:12:05 +00:00
'',//账号一ck,例:pt_key=XXX;pt_pin=XXX;
'',//账号二ck,例:pt_key=XXX;pt_pin=XXX;如有更多,依次类推
2024-06-28 07:47:44 +00:00
]
let IP = '';
// 判断环境变量里面是否有京东ck
if (process.env.JD_COOKIE) {
2024-08-02 03:12:05 +00:00
if (process.env.JD_COOKIE.indexOf('&') > -1) {
CookieJDs = process.env.JD_COOKIE.split('&');
} else if (process.env.JD_COOKIE.indexOf('\n') > -1) {
CookieJDs = process.env.JD_COOKIE.split('\n');
} else {
CookieJDs = [process.env.JD_COOKIE];
}
2024-06-28 07:47:44 +00:00
}
if (JSON.stringify(process.env).indexOf('GITHUB') > -1) {
2024-08-02 03:12:05 +00:00
console.log(`请勿使用github action运行此脚本,无论你是从你自己的私库还是其他哪里拉取的源代码,都会导致我被封号\n`);
!(async () => {
await require('./sendNotify').sendNotify('提醒', `请勿使用github action、滥用github资源会封我仓库以及账号`)
await process.exit(0);
})()
2024-06-28 07:47:44 +00:00
}
//!(async () => {
// IP = await getIP();
// try {
// IP = IP.match(/((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/)[0];
// console.log(`\n当前公网IP: ${IP}`);
// } catch (e) { }
//})()
CookieJDs = [...new Set(CookieJDs.filter(item => !!item))]
if (process.env.JD_DEBUG && process.env.JD_DEBUG === 'false') console.log = () => { };
2024-07-19 02:58:11 +00:00
if (process.env.BANPIN) {
2024-08-02 03:12:05 +00:00
try {
let banpin = process.env.BANPIN;
if (banpin.includes('@')) {
const arr = banpin.split('&');
for (let i of arr) {
//if (process.mainModule.filename.includes(i.split('^')[0])) {
if (i.split('@')[0].split('|').filter(x => process.argv[1].includes(x)).length != 0) {
let pinarr = i.split('@')[1].split(',');
console.log(`已配置该任务不执行pin: ${JSON.stringify(pinarr)}`);
for (let j of pinarr) {
j = decodeURIComponent(j);
CookieJDs = CookieJDs.filter(x => !x.includes(encodeURIComponent(j)));
}
}
}
} else {
let pinarr = banpin.split(',');
console.log(`已配置全局不执行pin: ${JSON.stringify(pinarr)}`);
for (let i of pinarr) {
j = decodeURIComponent(j);
CookieJDs = CookieJDs.filter(x => !x.includes(encodeURIComponent(i)));
}
2024-07-19 02:58:11 +00:00
}
2024-08-02 03:12:05 +00:00
} catch { }
2024-07-19 02:58:11 +00:00
}
2024-06-28 07:47:44 +00:00
console.log(`\n====================共${CookieJDs.length}个京东账号Cookie=================`);
console.log(`===========脚本执行时间:${formatdate(new Date(new Date().getTime() + new Date().getTimezoneOffset() * 60 * 1000 + 8 * 60 * 60 * 1000))}============`);
2024-08-02 03:12:05 +00:00
console.log('>>>>>>>>>>>>6dylan6提醒您有问题先更新不行在反馈>>>>>>>>>>>>>\n');
2024-07-19 02:58:11 +00:00
2024-06-28 07:47:44 +00:00
for (let i = 0; i < CookieJDs.length; i++) {
2024-08-02 03:12:05 +00:00
if (!CookieJDs[i].match(/pt_pin=(.+?);/) || !CookieJDs[i].match(/pt_key=(.+?);/)) console.log(`\n提示:京东cookie 【${CookieJDs[i]}】填写不规范,可能会影响部分脚本正常使用。正确格式为: pt_key=xxx;pt_pin=xxx;(分号;不可少)\n`);
CookieJDs[i] = CookieJDs[i].replace(/[\u4e00-\u9fa5]/g, (str) => encodeURI(str));
const index = (i + 1 === 1) ? '' : (i + 1);
exports['CookieJD' + index] = CookieJDs[i].trim();
2024-06-28 07:47:44 +00:00
}
let permit = process.env.PERMIT_JS ? process.env.PERMIT_JS.split('&') : '';
if (process.env.DP_POOL) {
2024-08-02 03:12:05 +00:00
if (permit && permit.filter(x => process.mainModule.filename.includes(x)).length != 0) {
try {
require("global-agent/bootstrap");
global.GLOBAL_AGENT.HTTP_PROXY = process.env.DP_POOL;
if (process.env.NO_PROXY) {
global.GLOBAL_AGENT.NO_PROXY = process.env.NO_PROXY;
}}
2024-08-02 03:12:05 +00:00
console.log(`\n---------------使用代理池模式---------------\n`);
} catch {
throw new Error(`请安装global-agent依赖才能启用代理`);
2024-06-28 07:47:44 +00:00
2024-08-02 03:12:05 +00:00
}
} else {
2024-06-28 07:47:44 +00:00
2024-08-02 03:12:05 +00:00
}
2024-06-28 07:47:44 +00:00
}
function getIP() {
2024-08-02 03:12:05 +00:00
const https = require('https');
return new Promise((resolve, reject) => {
let opt = {
hostname: "www.cip.cc",
port: 443,
path: "/",
method: "GET",
headers: {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
},
timeout: 5000
}
const req = https.request(opt, (res) => {
res.setEncoding('utf-8');
let tmp = '';
res.on('error', reject);
res.on('data', d => tmp += d);
res.on('end', () => resolve(tmp));
});
req.on('error', reject);
req.end();
2024-06-28 07:47:44 +00:00
});
}
// 以下为注入互助码环境变量仅nodejs内起效的代码
function SetShareCodesEnv(nameChinese = "", nameConfig = "", envName = "") {
2024-08-02 03:12:05 +00:00
let rawCodeConfig = {}
let fs = require('fs')
// 读取互助码
let shareCodeLogPath = fs.existsSync(`${process.env.QL_DIR}/data`) ? `${process.env.QL_DIR}/data/log/.ShareCode/${nameConfig}.log` : `${process.env.QL_DIR}/log/.ShareCode/${nameConfig}.log`;
if (fs.existsSync(shareCodeLogPath)) {
// 因为faker2目前没有自带ini改用已有的dotenv来解析
// // 利用ini模块读取原始互助码和互助组信息
// let ini = require('ini')
// rawCodeConfig = ini.parse(fs.readFileSync(shareCodeLogPath, 'utf-8'))
// 使用env模块
require('dotenv').config({ path: shareCodeLogPath })
rawCodeConfig = process.env
}
// 解析每个用户的互助码
let codes = {}
Object.keys(rawCodeConfig).forEach(function (key) {
if (key.startsWith(`My${nameConfig}`)) {
codes[key] = rawCodeConfig[key]
}
});
// 解析每个用户要帮助的互助码组,将用户实际的互助码填充进去
let helpOtherCodes = {}
Object.keys(rawCodeConfig).forEach(function (key) {
if (key.startsWith(`ForOther${nameConfig}`)) {
let helpCode = rawCodeConfig[key]
for (const [codeEnv, codeVal] of Object.entries(codes)) {
helpCode = helpCode.replace("${" + codeEnv + "}", codeVal)
}
helpOtherCodes[key] = helpCode
}
});
// 按顺序用&拼凑到一起,并放入环境变量,供目标脚本使用
let shareCodes = []
let leftIndex = 1, rightIndex = Object.keys(helpOtherCodes).length
// 判断是否是ptask并行触发若是则修改实际需要设置的互助码范围
let ptaskLeft = process.env.PTASK_LEFT
let ptaskRight = process.env.PTASK_RIGHT
if (ptaskLeft && ptaskRight) {
leftIndex = Number(ptaskLeft)
rightIndex = Number(ptaskRight)
2024-06-28 07:47:44 +00:00
}
2024-08-02 03:12:05 +00:00
for (let idx = leftIndex; idx <= rightIndex; idx++) {
shareCodes.push(helpOtherCodes[`ForOther${nameConfig}${idx}`])
2024-06-28 07:47:44 +00:00
}
2024-08-02 03:12:05 +00:00
let shareCodesStr = shareCodes.join('&')
process.env[envName] = shareCodesStr
let totalCodeCount = rightIndex - leftIndex + 1
//console.info(`${nameChinese}的 互助码环境变量 ${envName},共计 ${totalCodeCount} 组互助码,总大小为 ${shareCodesStr.length} 字节`)
2024-06-28 07:47:44 +00:00
}
// 判断当前活动脚本是否在互助脚本列表中
function IsShareJsFile() {
2024-08-02 03:12:05 +00:00
// 尝试获取在task_before.sh中设置的 互助活动的脚本文件名的关键部分 列表
let rawJsNameList = process.env.ShareCodeJSNameList
if (!rawJsNameList) {
return false
}
// 转换为list
let jsNameList = process.env.ShareCodeJSNameList.split(" ")
// 判断当前
let currentActivityScriptFileName = GetCurrentActivityScriptFileName()
let isShareJsFile = false
for (let idx = 0; idx < jsNameList.length; idx++) {
if (currentActivityScriptFileName.includes(jsNameList[idx])) {
isShareJsFile = true
break
}
2024-06-28 07:47:44 +00:00
}
2024-08-02 03:12:05 +00:00
return isShareJsFile
2024-06-28 07:47:44 +00:00
}
// 获取当前活动脚本的文件名
function GetCurrentActivityScriptFileName() {
2024-08-02 03:12:05 +00:00
const path = require('path')
return path.basename(process.argv[1])
2024-06-28 07:47:44 +00:00
}
// 若在task_before.sh 中设置了要设置互助码环境变量的活动名称和环境变量名称信息则在nodejs中处理供活动使用
let nameChinese = process.env.ShareCodeConfigChineseName
let nameConfig = process.env.ShareCodeConfigName
let envName = process.env.ShareCodeEnvName
if (nameChinese && nameConfig && envName) {
2024-08-02 03:12:05 +00:00
SetShareCodesEnv(nameChinese, nameConfig, envName)
2024-06-28 07:47:44 +00:00
}
function formatdate(date) {
2024-08-02 03:12:05 +00:00
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
const hours = ('0' + date.getHours()).slice(-2);
const minutes = ('0' + date.getMinutes()).slice(-2);
const seconds = ('0' + date.getSeconds()).slice(-2);
return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
2024-06-28 07:47:44 +00:00
}