♻️ 完善 ds 算法

This commit is contained in:
BTMuli
2023-09-04 14:32:59 +08:00
parent e2a1f8c862
commit 4b80de3f24
3 changed files with 22 additions and 44 deletions

View File

@@ -2,7 +2,7 @@
* @file web request getAbyss.ts
* @description 获取深渊信息
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
* @since Beta v0.3.0
*/
// tauri
@@ -10,11 +10,11 @@ import { http } from "@tauri-apps/api";
// api
import TGApi from "../api/TGApi";
// utils
import { getRequestHeader } from "../utils/getRequestHeader";
import TGUtils from "../utils/TGUtils";
/**
* @description 获取深渊信息
* @since Alpha v0.2.0
* @since Beta v0.3.0
* @param {Record<string, string>} cookie cookie
* @param {string} schedule_type 1: 本期, 2: 上期
* @param {TGApp.Sqlite.Account.Game} account 游戏账号
@@ -28,7 +28,7 @@ export async function getAbyss(
const url = TGApi.GameData.getAbyss;
const role_id = account.gameUid;
const params = { role_id, schedule_type, server: account.region };
const header = getRequestHeader(cookie, "GET", params, "common");
const header = TGUtils.User.getHeader(cookie, "GET", params, "common");
return await http
.fetch<TGApp.Game.Abyss.Response>(url, {
method: "GET",

View File

@@ -2,11 +2,11 @@
* @file web utils TGUtils.ts
* @description 应用用到的工具函数
* @author BTMuli <bt-muli@outlook.com>
* @since Alpha v0.2.1
* @since Beta v0.3.0
*/
import { getAnnoCard } from "./getAnnoCard";
import { getRequestHeader, getRequestSignHeader } from "./getRequestHeader";
import { getRequestHeader } from "./getRequestHeader";
import { parseAnnoContent } from "./parseAnno";
import { getServerByUid, transCookie } from "./tools";
@@ -17,7 +17,6 @@ const TGUtils = {
},
User: {
getHeader: getRequestHeader,
getSignHeader: getRequestSignHeader,
},
Tools: {
getServerByUid,

View File

@@ -1,8 +1,8 @@
/**
* @file web utils getRequestHeader.ts
* @description 获取请求头
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
* @author BTMuli <bt-muli@outlook.com>
* @since Beta v0.3.0
*/
// Node
@@ -13,8 +13,8 @@ import { transCookie, transParams } from "./tools";
/**
* @description 获取 salt
* @since Alpha v0.2.0
* @version 2.49.1
* @since Beta v0.3.0
* @version 2.50.1
* @param {string} saltType salt 类型
* @returns {string} salt
*/
@@ -24,6 +24,8 @@ function getSalt(saltType: string): string {
return TGConstant.Salt.Other.X4;
case "prod":
return TGConstant.Salt.Other.prod;
case "lk2":
return TGConstant.Salt.BBS.LK2;
default:
return TGConstant.Salt.Other.X4;
}
@@ -57,33 +59,35 @@ export function getRandomString(length: number): string {
/**
* @description 获取 ds
* @since Alpha v0.2.0
* @version 2.49.1
* @since Beta v0.3.0
* @version 2.50.1
* @param {string} method 请求方法
* @param {string} data 请求数据
* @param {string} saltType salt 类型
* @param {boolean} isSign 是否为签名
* @returns {string} ds
*/
function getDS(method: string, data: string, saltType: string, isSign: boolean = false): string {
function getDS(method: string, data: string, saltType: string, isSign: boolean): string {
const salt = getSalt(saltType);
const time = Math.floor(Date.now() / 1000).toString();
let random = getRandomNumber(100000, 200000).toString();
if (isSign) random = getRandomString(6);
const body = method === "GET" ? "" : data;
const query = method === "GET" ? data : "";
const hashStr = `salt=${salt}&t=${time}&r=${random}&b=${body}&q=${query}`;
let hashStr = `salt=${salt}&t=${time}&r=${random}&b=${body}&q=${query}`;
if (isSign) hashStr = `salt=${salt}&t=${time}&r=${random}`;
const md5Str = md5.update(hashStr).hex();
return `${time},${random},${md5Str}`;
}
/**
* @description 获取请求头
* @since Alpha v0.2.0
* @since Beta v0.3.0
* @param {Record<string, string>} cookie cookie
* @param {string} method 请求方法
* @param {Record<string, string|number>|string} data 请求数据
* @param {string} saltType salt 类型
* @param {boolean} isSign 是否为签名
* @returns {Record<string, string>} 请求头
*/
export function getRequestHeader(
@@ -91,12 +95,13 @@ export function getRequestHeader(
method: string,
data: Record<string, string | number> | string,
saltType: string,
isSign: boolean = false,
): Record<string, string> {
let ds;
if (typeof data === "string") {
ds = getDS(method, data, saltType);
ds = getDS(method, data, saltType, isSign);
} else {
ds = getDS(method, transParams(data), saltType);
ds = getDS(method, transParams(data), saltType, isSign);
}
return {
"User-Agent": TGConstant.BBS.USER_AGENT,
@@ -108,29 +113,3 @@ export function getRequestHeader(
Cookie: transCookie(cookie),
};
}
/**
* @description 获取签名请求头
* @since Alpha v0.2.0
* @param {Record<string, string>} cookie cookie
* @param {string} method 请求方法
* @param {Record<string, string|number>} data 请求数据
* @param {string} saltType salt 类型
* @returns {Record<string, string>} 请求头
*/
export function getRequestSignHeader(
cookie: Record<string, string>,
method: string,
data: Record<string, string | number>,
saltType: string,
): Record<string, string> {
return {
"User-Agent": TGConstant.BBS.USER_AGENT,
"x-rpc-app_version": TGConstant.BBS.VERSION,
"x-rpc-client_type": "5",
"x-requested-with": "com.mihoyo.hyperion",
Referer: "https://webstatic.mihoyo.com",
DS: getDS(method, transParams(data), saltType, true),
Cookie: transCookie(cookie),
};
}