feat(protocol): implement OIDB 0xcde_2 (RequestDbKey) (#69)

This commit is contained in:
H3CoF6 authored and GitHub committed 2026-05-28 15:18:06 +08:00
1 parent f2b7ef0056
commit 00ca582f1a
6 files changed
+146 -2

No files matched your search

+12
View File
@@ -9,6 +9,7 @@ import type { BridgeContext } from '../bridge-context';
import { ClickInlineKeyboardButton } from '@snowluma/protocol/oidb-services/misc/click-inline-keyboard-button';
import { SendGroupSign } from '@snowluma/protocol/oidb-services/misc/send-group-sign';
import { TranslateEnToZh } from '@snowluma/protocol/oidb-services/misc/translate-en-to-zh';
import { RequestDbKey } from '@snowluma/protocol/oidb-services/misc/request-decrypt-key';
export class MiscApi {
constructor(private readonly ctx: BridgeContext) { }
@@ -75,4 +76,15 @@ export class MiscApi {
sendGroupSign(groupId: number): Promise<void> {
return SendGroupSign.invoke(this.ctx, { groupId });
}
async getDecryptKey(dbSalt: string): Promise<string> {
const result = await RequestDbKey.invoke(this.ctx, { db_salt: dbSalt });
if (!result.success) {
throw new Error(result.errorMsg || `Failed to get decrypt key (code: ${result.errorCode})`);
}
if (!result.dbKey) {
throw new Error('Decrypt key is empty');
}
return result.dbKey;
}
}
+30
View File
@@ -1,3 +1,4 @@
import { readFile } from 'node:fs/promises';
import type { ApiActionContext, ApiHandler } from '../api-handler';
import { asBoolean, asMessage, asNumber, asString } from '../api-handler';
import type { ForwardPreviewMeta } from '../modules/message-actions';
@@ -1175,6 +1176,35 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
return failedResponse(RETCODE.ACTION_FAILED, err instanceof Error ? err.message : String(err));
}
});
h.registerAction('request_decrypt_key', async (params) => {
const dbPath = asString(params.db_path);
if (!dbPath) {
return failedResponse(RETCODE.BAD_REQUEST, 'db_path is required');
}
try {
const buffer = Buffer.alloc(128);
const fileHandle = await readFile(dbPath);
if (fileHandle.length < 0xaf) {
return failedResponse(RETCODE.ACTION_FAILED, 'Database file too short');
}
fileHandle.copy(buffer, 0, 0x2f, 0xaf);
const dbSalt = buffer.toString('utf8');
if (!/^[0-9a-fA-F]{128}$/.test(dbSalt)) {
return failedResponse(RETCODE.ACTION_FAILED, 'Invalid db_salt: not a valid 128-character hex string');
}
const dbKey = await ctx.bridge.apis.misc.getDecryptKey(dbSalt.toLowerCase());
return okResponse({ db_key: dbKey });
} catch (err) {
return failedResponse(RETCODE.ACTION_FAILED, err instanceof Error ? err.message : String(err));
}
});
}
function hexToBytes(hex: string): Uint8Array {
+19 -1
View File
@@ -657,4 +657,22 @@ export interface GroupAvatarExtra {
field3?: pb<3, GroupAvatarExtraField3>;
field5?: pb<5, uint_32>;
field6?: pb<6, uint_32>;
}
}
export interface Oidb0xcdeReqBodyInfo {
db_salt?: pb<1, string>;
}
export interface Oidb0xcdeReq {
info?: pb<2, Oidb0xcdeReqBodyInfo>;
sessionData?: pb<10, bytes>;
}
export interface Oidb0xcdeRespBodyInfo {
dbKey?: pb<1, string>;
}
export interface Oidb0xcdeResp {
info?: pb<2, Oidb0xcdeRespBodyInfo>;
}
+4
View File
@@ -107,6 +107,10 @@
"types": "./src/oidb-services/misc/send-group-sign.ts",
"default": "./src/oidb-services/misc/send-group-sign.ts"
},
"./oidb-services/misc/request-decrypt-key": {
"types": "./src/oidb-services/misc/request-decrypt-key.ts",
"default": "./src/oidb-services/misc/request-decrypt-key.ts"
},
"./oidb-services/web/force-fetch-client-key": {
"types": "./src/oidb-services/web/force-fetch-client-key.ts",
"default": "./src/oidb-services/web/force-fetch-client-key.ts"
+11 -1
View File
@@ -117,9 +117,19 @@ export async function invokeOidb<TCtx extends OidbSender, TReq, TResp, TParams,
: `OidbSvcTrpcTcp.0x${spec.command.toString(16)}_${subCommand}`;
const result = await ctx.sendRawPacket(wireName, reqBytes, timeoutMs);
if (!result.success) throw new Error(result.errorMessage || 'packet send failed');
if (!result.gotResponse) throw new Error(result.errorMessage || 'no response');
if (!result.success) {
if (result.errorCode && result.errorCode !== 0) {
throw new OidbError(
result.errorCode,
result.errorMessage || '',
spec.command,
subCommand
);
}
}
const respBytes = result.responseData ?? new Uint8Array(0);
if (respBytes.length > 0) {
const meta = protobuf_decode<OidbBaseMeta>(respBytes);
@@ -0,0 +1,70 @@
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { Oidb0xcdeReq, Oidb0xcdeResp } from '@snowluma/proto-defs/oidb-actions/base';
import { invokeOidb, type OidbSender } from '../../oidb-service';
export namespace RequestDbKey {
export const command = 0xcde;
export const subCommand = 2;
export interface Params {
db_salt: string;
sessionData?: Uint8Array;
}
export interface Result {
success: boolean;
dbKey?: string;
errorCode?: number;
errorMsg?: string;
}
export type Deps = OidbSender;
export const serialize = (_ctx: Deps, p: Params): Oidb0xcdeReq => {
const req: Oidb0xcdeReq = {
info: {
db_salt: p.db_salt,
}
};
if (p.sessionData) {
req.sessionData = p.sessionData;
}
return req;
};
export const deserialize = (_ctx: Deps, body: Oidb0xcdeResp): string => {
const dbKey = body.info?.dbKey;
if (!dbKey) {
throw new Error('Oidb 0xcde_2 payload empty (Missing dbKey)');
}
return dbKey;
};
export const encode = (env: OidbBase<Oidb0xcdeReq>): Uint8Array =>
protobuf_encode<OidbBase<Oidb0xcdeReq>>(env);
export const decode = (bytes: Uint8Array): OidbBase<Oidb0xcdeResp> =>
protobuf_decode<OidbBase<Oidb0xcdeResp>>(bytes);
export const invoke = async (deps: Deps, params: Params): Promise<Result> => {
try {
const key = await invokeOidb(deps, RequestDbKey, params);
return {
success: true,
dbKey: key,
};
} catch (error: any) {
const code = error?.code ?? -1;
return {
success: false,
errorCode: code,
errorMsg: code === 1006
? "数据库不匹配!检查数据库是否属于当前帐号"
: (error?.serverMsg || error?.message || "网络或包结构解析失败"),
};
}
};
}