Skip to content

Commit

Permalink
♻️ 重构 http req,捕获aigis
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Jul 26, 2024
1 parent 6b18d63 commit e3e5b75
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"geetest": "^4.1.2",
"html2canvas": "^1.4.1",
"js-md5": "^0.8.3",
"jsencrypt": "^3.3.2",
"pinia": "^2.1.7",
"pinia-plugin-persistedstate": "^3.2.1",
"qrcode.vue": "^3.4.1",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 37 additions & 14 deletions src/plugins/Mys/request/doCaptchaLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
* @since Beta v0.5.1
*/

import { publicEncrypt } from "node:crypto";
import { JSEncrypt } from "jsencrypt";

import showSnackbar from "../../../components/func/snackbar.js";
import TGHttp from "../../../utils/TGHttp.js";
import { getDeviceInfo } from "../../../utils/toolFunc.js";
import TGConstant from "../../../web/constant/TGConstant.js";

const PUB_KEY = `-----BEGIN PUBLIC KEY-----
const PUB_KEY_STR = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDvekdPMHN3AYhm/vktJT+YJr7cI5DcsNKqdsx5DZX0gDuWFuIjzdwButrIYPNmRJ1G8ybDIF7oDW2eEpm5sMbL9zs
9ExXCdvqrn51qELbqj0XxtMTIpaCHFSI50PfPpTFV9Xt/hmyVwokoOXFlAEgCn+Q
CgGs52bFoYMtyi+xEQIDAQAB
-----END PUBLIC KEY-----`;
const encrypt = new JSEncrypt();
encrypt.setPublicKey(PUB_KEY_STR);

/**
* @description rsa 加密
Expand All @@ -23,8 +26,15 @@ CgGs52bFoYMtyi+xEQIDAQAB
* @returns {string} 加密后数据
*/
function rsaEncrypt(data: string): string {
const buffer = Buffer.from(data);
return publicEncrypt(PUB_KEY, buffer).toString("base64");
const res = encrypt.encrypt(data.toString());
if (res === false) {
showSnackbar({
text: "RSA 加密失败",
color: "error",
});
return "";
}
return res;
}

/**
Expand All @@ -36,14 +46,14 @@ function rsaEncrypt(data: string): string {
*/
export async function getCaptcha(
phone: string,
): Promise<TGApp.Plugins.Mys.CaptchaLogin.CaptchaData | TGApp.BBS.Response.Base> {
): Promise<TGApp.Plugins.Mys.CaptchaLogin.CaptchaData | TGApp.BBS.Response.BaseWithData> {
const url = "https://passport-api.mihoyo.com/account/ma-cn-verifier/verifier/createLoginCaptcha";
const device_fp = getDeviceInfo("device_fp");
const device_name = getDeviceInfo("device_name");
const device_id = getDeviceInfo("device_id");
const device_model = getDeviceInfo("product");
const body = { area_code: rsaEncrypt("+86"), mobile: rsaEncrypt(phone) };
const header = {
const header: Record<string, string> = {
"x-rpc-aigis": "",
"x-rpc-app_version": TGConstant.BBS.VERSION,
"x-rpc-client_type": "2",
Expand All @@ -61,13 +71,24 @@ export async function getCaptcha(
console.log("getCaptcha body: ", body);
const resp = await TGHttp<
TGApp.Plugins.Mys.CaptchaLogin.CaptchaResponse | TGApp.BBS.Response.Base
>(url, {
method: "POST",
headers: header,
body: JSON.stringify(body),
});
if (resp.retcode !== 0) return <TGApp.BBS.Response.Base>resp;
return resp.data;
>(
url,
{
method: "POST",
headers: header,
body: JSON.stringify(body),
},
true,
);
const data = await resp.data;
if (data.retcode !== 0) {
return <TGApp.BBS.Response.BaseWithData>{
retcode: data.retcode,
message: data.message,
data: resp.resp.headers.get("x-rpc-aigis"),
};
}
return <TGApp.Plugins.Mys.CaptchaLogin.CaptchaData>data.data;
}

/**
Expand All @@ -76,12 +97,14 @@ export async function getCaptcha(
* @param {string} phone - 手机号
* @param {string} captcha - 验证码
* @param {string} action_type - 操作类型
* @param {string} [aigis] - 验证数据
* @returns {Promise<TGApp.Plugins.Mys.CaptchaLogin.LoginData | TGApp.BBS.Response.Base>}
*/
export async function doCaptchaLogin(
phone: string,
captcha: string,
action_type: string,
aigis?: string,
): Promise<TGApp.Plugins.Mys.CaptchaLogin.LoginData | TGApp.BBS.Response.Base> {
const url = "https://passport-api.mihoyo.com/account/ma-cn-passport/app/loginByMobileCaptcha";
const device_fp = getDeviceInfo("device_fp");
Expand All @@ -95,7 +118,7 @@ export async function doCaptchaLogin(
captcha,
};
const header = {
"x-rpc-aigis": "",
"x-rpc-aigis": aigis || "",
"x-rpc-app_version": TGConstant.BBS.VERSION,
"x-rpc-client_type": "2",
"x-rpc-app_id": TGConstant.BBS.APP_ID,
Expand Down
27 changes: 20 additions & 7 deletions src/utils/TGHttp.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* @file utils/TGHttp.ts
* @description 封装HTTP请求
* @since Beta v0.5.0
* @since Beta v0.5.1
*/

import { fetch } from "@tauri-apps/plugin-http";

/**
* @description 请求参数
* @since Beta v0.5.0
* @since Beta v0.5.1
* @property {"GET"|"POST"} method 请求方法
* @property {Record<string,string>} headers 请求头
* @property {Record<string,string>} query 请求参数
Expand All @@ -26,13 +26,23 @@ type TGHttpParams = {

/**
* @description 发送请求
* @since Beta v0.5.0
* @since Beta v0.5.1
* @template T
* @param {string} url 请求地址
* @param {TGHttpParams} options 请求参数
* @returns {Promise<T>}
* @returns {Promise<T>} 请求结果
*/
async function TGHttp<T>(url: string, options: TGHttpParams): Promise<T> {
async function TGHttp<T>(url: string, options: TGHttpParams): Promise<T>;
async function TGHttp<T>(
url: string,
options: TGHttpParams,
fullResponse: true,
): Promise<{ data: Promise<T>; resp: Response }>;
async function TGHttp<T>(
url: string,
options: TGHttpParams,
fullResponse?: true,
): Promise<T | { data: Promise<T>; resp: Response }> {
const httpHeaders = new Headers();
if (options.headers) {
for (const key in options.headers) {
Expand All @@ -55,8 +65,11 @@ async function TGHttp<T>(url: string, options: TGHttpParams): Promise<T> {
return await fetch(url, fetchOptions)
.then((res) => {
if (res.ok) {
if (options.isBlob) return res.arrayBuffer();
return res.json();
const data = options.isBlob ? res.blob() : res.json();
if (fullResponse) {
return { data, resp: res };
}
return data;
}
throw new Error(`HTTP error! status: ${res.status}`);
})
Expand Down

0 comments on commit e3e5b75

Please sign in to comment.