Skip to content

Commit

Permalink
perf: 优化验证码登录流程,增加设备登录
Browse files Browse the repository at this point in the history
fix: 修复验证码登录后未移除实例

Signed-off-by: bennett <bennett_asia@outlook.com>
  • Loading branch information
BennettChina committed Sep 27, 2024
1 parent 3a9e2e5 commit eb18ce9
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.3

- perf: 优化验证码登录流程,增加设备登录选项
- fix: 修复验证码登录后未移除实例

## 2.1.2

- perf: 优化人机验证,验证完成撤回消息。
Expand Down
2 changes: 1 addition & 1 deletion achieves/captcha-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export default defineDirective( "enquire", async ( input: InputParameter ) => {

if ( /^\d{6}$/.test( data ) ) {
await captchaLogin.loginByCaptcha( data );
return true;
}

MiHoYoCaptchaLogin.removeInstance( input.messageData.user_id );
return true;
} catch ( error: any ) {
logger.error( error );
const tips = error?.message || typeof error === "string" ? error : "未知错误";
Expand Down
47 changes: 43 additions & 4 deletions module/captcha-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import UserAgent from "user-agents";
import platform from "platform";
import {
createCaptcha,
deviceLogin,
getCookieAccountInfoBySToken,
getDeviceFp,
getGameRecordCard,
getLTokenBySToken,
getValidate,
loginByCaptcha
loginByCaptcha,
saveDevice
} from "#/mihoyo-login/util/api";
import { config } from "#/mihoyo-login/init";
import { sleep } from "@/utils/async";
Expand All @@ -18,7 +20,7 @@ import { privateClass } from "#/genshin/init";
import { isPrivateMessage } from "@/modules/message";
import { ForwardElem } from "@/modules/lib";
import { Md5 } from "md5-typescript";
import { DeviceData, GameRole, MiHoYoData } from "#/mihoyo-login/util/types";
import { DeviceData, GameRole, MiHoYoData, SaveDevice } from "#/mihoyo-login/util/types";
import { encrypt } from "#/mihoyo-login/util/crypto";

export class MiHoYoCaptchaLogin {
Expand All @@ -35,6 +37,7 @@ export class MiHoYoCaptchaLogin {
private actionType: string = "login_by_mobile_captcha";
private dbKey: string = "adachi.miHoYo.data.";
private mobile: string = "";
private cookie: string = "";

private constructor( input: InputParameter ) {
this.context = input;
Expand Down Expand Up @@ -77,7 +80,6 @@ export class MiHoYoCaptchaLogin {
}

public async loginByCaptcha( captcha: string ) {
console.log( this.getAccountHeader() )
const body = {
captcha,
area_code: encrypt( "+86" ),
Expand Down Expand Up @@ -123,13 +125,30 @@ export class MiHoYoCaptchaLogin {
// 保存用户CK等数据 (数据格式不局限于原神的数据,更泛用一些)
const k = `${ userId }:${ aid }`;
this.dbKey = `${ this.dbKey }${ Md5.init( k ) }`;
this.cookie = cookie;
const userData: MiHoYoData = {
games: JSON.stringify( gameRoles ),
cookie,
uid: aid,
userId
};
await this.context.redis.setHash( this.dbKey, userData );

this.deviceLogin().catch( reason => this.context.logger.error( "[验证码登录] [登录设备]", reason ) );
}

public async deviceLogin(): Promise<void> {
const header = this.getHeader();
const body: SaveDevice = {
device_id: this.deviceId,
platform: "iOS",
device_name: header["x-rpc-device_model"],
app_version: bbs_version,
os_version: header["x-rpc-sys_version"],
registration_id: getMiHoYoRandomStr( 19 )
};
await deviceLogin( body, this.cookie, header );
await saveDevice( body, this.cookie, header );
}

private async createCaptcha( mobile: string, aigis_data?: string ): Promise<void> {
Expand Down Expand Up @@ -170,7 +189,7 @@ export class MiHoYoCaptchaLogin {
_url.searchParams.append( "new_captcha", new_captcha );
_url.searchParams.append( "success", success );
const content = _url.toString();
const id = await this.context.sendMessage( [ "触发风控,需要你处理下人机验证。\n", content ] );
const id = await this.context.sendMessage( [ "请打开地址并完成验证。\n", content ] );
const { geetest_validate, geetest_seccode, geetest_challenge } = await this.get_validate( challenge );
this.context.client.recallMessage( id ).then();

Expand Down Expand Up @@ -202,6 +221,7 @@ export class MiHoYoCaptchaLogin {
throw err;
}
if ( !logged ) {
logged = true;
this.context.logger.info( err );
}
}
Expand Down Expand Up @@ -336,6 +356,25 @@ export class MiHoYoCaptchaLogin {
};
}

private getHeader(): Record<string, string> {
const plat = platform.parse( this.userAgent.toString() );
return {
"x-rpc-verify_key": "bll8iq97cem8",
"x-rpc-device_fp": this.deviceFp,
"x-rpc-client_type": "1",
"x-rpc-device_id": this.deviceId.toUpperCase(),
"x-rpc-channel": "appstore",
"x-rpc-device_model": "iPhone10,3",
"Referer": "https://app.mihoyo.com",
"x-rpc-device_name": "iPhone",
"x-rpc-h265_supported": "1",
"x-rpc-app_version": bbs_version,
"User-Agent": "Hyperion/461 CFNetwork/1410.1 Darwin/22.6.0",
"x-rpc-sys_version": plat.os?.version || "16.7.9",
"x-rpc-csm_source": "home",
}
}

private getAccountHeader(): Record<string, string> {
const app_id = "bll8iq97cem8";

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author" : "Bennett",
"private" : true,
"license" : "MIT",
"version" : "2.1.2",
"version" : "2.1.3",
"description" : "米哈游登录插件 为 Adachi-BOT 衍生插件,用于获取米游社登录后的 Token 。",
"dependencies" : {
"qrcode" : "^1.5.3",
Expand Down
41 changes: 39 additions & 2 deletions util/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { randomStr, transformCookie } from "#/mihoyo-login/util/utils";
import bot from "ROOT";
import { config } from "#/mihoyo-login/init";
import { ds, ds2 } from "#/mihoyo-login/util/ds";
import { SaveDevice } from "#/mihoyo-login/util/types";

enum Api {
mihoyo_login_qrcode_creat = "https://passport-api.miyoushe.com/account/ma-cn-passport/web/createQRLogin",
Expand All @@ -15,7 +16,9 @@ enum Api {
getLTokenBySToken = "https://passport-api.mihoyo.com/account/auth/api/getLTokenBySToken",
getCookieAccountInfoBySToken = "https://passport-api.mihoyo.com/account/auth/api/getCookieAccountInfoBySToken",
userApiLogin = "https://bbs-api.miyoushe.com/user/api/login",
getGameRecordCard = "https://api-takumi-record.mihoyo.com/game_record/card/api/getGameRecordCard"
getGameRecordCard = "https://api-takumi-record.mihoyo.com/game_record/card/api/getGameRecordCard",
deviceLogin = "https://bbs-api.miyoushe.com/apihub/api/deviceLogin",
saveDevice = "https://bbs-api.mihoyo.com/apihub/api/saveDevice"
}

const HEADERS = {
Expand Down Expand Up @@ -233,7 +236,7 @@ export async function loginApi( cookie: string, headers: Record<string, string>
headers: {
...headers,
"Cookie": cookie,
"DS": ds2( "bbs", data )
"DS": ds2( "lk2", data )
}
} )

Expand Down Expand Up @@ -261,5 +264,39 @@ export async function getGameRecordCard( uid: string | number, cookie: string, h
return Promise.reject( response.data.message );
}

return response.data.data;
}

export async function deviceLogin( body: SaveDevice, cookie: string, headers: Record<string, string> ) {
const { stoken, stuid, mid, login_ticket } = transformCookie( cookie );
cookie = transformCookie( { stuid, stoken, mid, login_ticket } );

const response = await axios.post( Api.deviceLogin, body, {
headers: {
...headers,
Cookie: cookie,
DS: ds2( 'lk2', body )
}
} );
if ( response.data.retcode !== 0 ) {
throw new Error( response.data.message );
}
return response.data.data;
}

export async function saveDevice( body: SaveDevice, cookie: string, headers: Record<string, string> ) {
const { stoken, stuid, mid, login_ticket } = transformCookie( cookie );
cookie = transformCookie( { stuid, stoken, mid, login_ticket } );

const response = await axios.post( Api.saveDevice, body, {
headers: {
...headers,
Cookie: cookie,
DS: ds2( 'lk2', body )
}
} );
if ( response.data.retcode !== 0 ) {
throw new Error( response.data.message );
}
return response.data.data;
}
5 changes: 3 additions & 2 deletions util/ds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { Md5 } from "md5-typescript";
import { randomStr } from "#/mihoyo-login/util/utils";
import { urlParamsParse } from "@/utils/url";

type DSType = "account" | "bbs" | "game" | "sign_in";

const salt = {
account: "JwYDpKvLj6MrMqqYU6jTKF17KNO2PXoS",// PROD
bbs: "WGtruoQrwczmsjLOPXzJLnaAYycsLavx",// K2
lk2: "YaROXkMnnbaQzjOmGPDmDKvpcavlZbli",// LK2
game: "xV8v4Qu54lUKrEYFZkJhB8cuOh9Asafs",// 4X
sign_in: "t0qEgfub6cvueAPgR5m9aQWWVciEer7v"// 6X
}

type DSType = keyof typeof salt;

export const bbs_version = "2.73.1";

export function ds( type: DSType ): string {
Expand Down
9 changes: 9 additions & 0 deletions util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ export type MiHoYoData = {
uid: string | number;
cookie: string;
games: string;
}

export type SaveDevice = {
os_version: string;
device_name: string;
platform: string;
device_id: string;
registration_id: string;
app_version: string;
}

0 comments on commit eb18ce9

Please sign in to comment.