koishi-plugin-yunzai
Version:
a yunzai adapter for koishi
131 lines (130 loc) • 4.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hoyo = void 0;
const koishi_1 = require("koishi");
const node_crypto_1 = require("node:crypto");
const Device_1 = require("./Device");
const Random_1 = require("./Random");
const Region_1 = require("./Region");
const logger = new koishi_1.Logger('hoyokit');
const config = {
cn: {
actID: 'e202009291139501',
clientType: 2,
appver: '2.38.1',
header: 'miHoYoBBS',
salt: 'PVeGWIZACpxXZ1ibMVJPi9inCY4Nd4y2'
},
os: {
actID: '',
clientType: 5,
appver: '2.9.0',
header: 'miHoYoBBSOversea',
salt: 'n0KjuIrKgLHh08LWSCYP0WXlVXaYvV64'
}
};
class Hoyo {
constructor(uid) {
let serverType = (0, Region_1.getRegionType)(uid);
if (serverType === Region_1.RegionType.CN || serverType === Region_1.RegionType.CNB) {
this.region = 'cn';
this.conf = config.cn;
}
else {
this.region = 'os';
this.conf = config.os;
}
//基于UID创建Device信息
this.device = new Device_1.DeviceInfo(uid).createDevice();
}
get act_id() {
return this.conf.actID;
}
//#region miHoyoDS
/**
* 获取新版ds内容
* @param body
* @param params
* @returns
*/
newDS(body, params) {
let t = Math.round(new Date().getTime() / 1000).toString();
let r = Random_1.Random.randint(100001, 200000).toString();
let b = body ? JSON.stringify(body) : '';
let q = params ? encodeURI(JSON.stringify(params)) : '';
let c = this.hash({ t, r, b, q });
return `${t},${r},${c}`;
}
/**
* 旧版本ds内容,主要用于签到
*/
oldDS(salt) {
let t = Math.round(new Date().getTime() / 1000).toString();
let r = Random_1.Random.sample('abcdefghijklmnopqrstuvwxyz0123456789'.split(''), 6).join('');
let c = this.hash({ t, r }, salt);
return `${t},${r},${c}`;
}
hash(value, salt = this.conf.salt) {
//默认加入salt
let temp = ['salt=' + salt];
//将object拆分为键值对array
Object.keys(value).forEach(key => {
temp.push(`${key}=${value[key]}`);
});
//序列化并md5哈希
return (0, node_crypto_1.createHash)('md5').update(temp.join('&')).digest('hex');
}
//#endregion miHoyoDS END
headers(cookie, query, body) {
return {
'x-rpc-app_version': this.conf.appver,
'x-rpc-client_type': this.conf.clientType.toString(),
'cookie': cookie,
'User-Agent': [Random_1.Random.randUA(this.device.Display), `${this.conf.header}/${this.conf.appver}`].join(' '),
'Referer': this.region === 'cn' ? `https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?bbs_auth_required=true&act_id=${this.act_id}&utm_source=bbs&utm_medium=mys&utm_campaign=icon` : `https://webstatic-sea.hoyolab.com`,
'DS': this.newDS(query, body)
};
}
signHeader(cookie) {
let headers = this.headers(cookie);
headers['DS'] = this.oldDS();
return Object.assign(headers, {
'x-rpc-device_id': (0, node_crypto_1.randomUUID)().replace(/-/g, ''),
'x-rpc-platform': 'android',
'x-rpc-device_model': this.device.Model,
'x-rpc-device_name': this.device.Display,
'x-rpc-channel': 'miyousheluodi',
'x-rpc-sys_version': this.device.Version.Release,
'X-Requested-With': this.region === 'cn' ? 'com.mihoyo.hyperion' : 'com.mihoyo.hoyolab'
});
}
/**
* format cookie to object
* @param cookie
*/
static parseCookie(cookie) {
//清除空格
cookie = cookie.replace(/\s/g, '');
//'key=value;foo=bar' map to [[key,value],[foo,bar]]
const cookieMap = cookie.split(';').map(value => {
//string=string
if (/([\S]+)=([\S]+)/.test(value)) {
let tmp = value.split('=');
if (tmp.length === 2) {
return tmp;
}
}
});
//Map[[key,value],[foo,bar]] to {key=value, foo=bar}
return Object.fromEntries(cookieMap);
}
/**
*
* @param cookie
*/
static vertifyCookie(cookie) {
const ck = this.parseCookie(cookie);
return ck.login_ticket ? true : false;
}
}
exports.Hoyo = Hoyo;