feat: implement get cookies and CSRF and credentials

This commit is contained in:
H3CoF6
2026-05-09 05:17:18 +08:00
parent 8b313fe088
commit c8dd520d65
5 changed files with 95 additions and 6 deletions
+6
View File
@@ -75,6 +75,9 @@ import {
getGroupEssenceAll as getGroupEssenceAll_,
sendGroupNotice as sendGroupNotice_,
getGroupNotice as getGroupNotice_,
getCookiesStr as getCookiesStr_,
getCsrfToken as getCsrfToken_,
getCredentials as getCredentials_,
} from './web-actions';
import type { GroupFilesResult } from './bridge-actions';
import type { MediaIndexNode } from './bridge-actions';
@@ -496,6 +499,9 @@ export class Bridge {
async setProfile(nickname?: string, personalNote?: string): Promise<void> {
return setProfile_(this, nickname, personalNote);
}
async getCookiesStr(domain: string): Promise<string> { return getCookiesStr_(this, domain); }
async getCsrfToken(): Promise<number> { return getCsrfToken_(this); }
async getCredentials(domain: string) { return getCredentials_(this, domain); }
}
+53
View File
@@ -259,4 +259,57 @@ export async function getGroupNotice(bridge: Bridge, groupId: number) {
}
return retNotices;
}
/**
* 提取的公共算法:根据 skey/p_skey 计算 bkn (也就是 token / csrf_token)
*/
export function getBknFromSKey(skey: string): number {
let hash = 5381;
for (let i = 0; i < skey.length; i++) {
hash += (hash << 5) + skey.charCodeAt(i);
}
return hash & 2147483647;
}
/**
* 获取 Cookies (字符串格式)
*/
export async function getCookiesStr(bridge: Bridge, domain: string): Promise<string> {
const cookieObject = await getCookies(bridge, domain);
return Object.entries(cookieObject)
.map(([key, value]) => `${key}=${value}`)
.join('; ');
}
/**
* 获取 CSRF Token (即 BKN)
*/
export async function getCsrfToken(bridge: Bridge): Promise<number> {
const skey = await getSKey(bridge);
if (!skey) {
throw new Error('SKey is Empty');
}
return getBknFromSKey(skey);
}
/**
* 获取凭证 (Cookies 字符串 + Token)
*/
export async function getCredentials(bridge: Bridge, domain: string) {
const cookieObject = await getCookies(bridge, domain);
const cookiesStr = Object.entries(cookieObject)
.map(([key, value]) => `${key}=${value}`)
.join('; ');
const skey = cookieObject['p_skey'] || cookieObject['skey'] || '';
const token = skey ? getBknFromSKey(skey) : 0;
return {
cookies: cookiesStr,
token: token,
csrf_token: token
};
}
+30 -6
View File
@@ -413,18 +413,42 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
// --- Credentials ---
h.registerAction('get_cookies', async () => {
return okResponse({ cookies: '' });
h.registerAction('get_cookies', async (params) => {
const domain = asString(params.domain) || 'qun.qq.com';
if (!ctx.getCookiesStr) return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
try {
const cookies = await ctx.getCookiesStr(domain);
return okResponse({ cookies });
} catch (e) {
return failedResponse(RETCODE.ACTION_FAILED, String(e));
}
});
h.registerAction('get_csrf_token', async () => {
return okResponse({ token: 0 });
if (!ctx.getCsrfToken) return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
try {
const token = await ctx.getCsrfToken();
return okResponse({ token });
} catch (e) {
return failedResponse(RETCODE.ACTION_FAILED, String(e));
}
});
h.registerAction('get_credentials', async () => {
return okResponse({ cookies: '', csrf_token: 0 });
});
h.registerAction('get_credentials', async (params) => {
const domain = asString(params.domain) || 'qun.qq.com';
if (!ctx.getCredentials) return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
try {
const creds = await ctx.getCredentials(domain);
return okResponse(creds);
} catch (e) {
return failedResponse(RETCODE.ACTION_FAILED, String(e));
}
});
// --- Utility ---
h.registerAction('set_restart', async () => {
+3
View File
@@ -102,6 +102,9 @@ export interface ApiActionContext {
getGroupEssenceAll?: (groupId: number) => Promise<GroupEssenceMsgRet[]>;
sendGroupNotice?: (groupId: number, content: string, options?: any) => Promise<any>;
getGroupNotice?: (groupId: number) => Promise<any[]>;
getCookiesStr?: (domain: string) => Promise<string>;
getCsrfToken?: () => Promise<number>;
getCredentials?: (domain: string) => Promise<{ cookies: string; token: number; csrf_token: number }>;
}
type ActionHandler = (params: JsonObject) => Promise<import('./types').ApiResponse>;
@@ -132,6 +132,9 @@ export function buildApiContext(ref: InstanceRef): ApiActionContext {
getGroupEssenceAll: (groupId: number) => bridge.getGroupEssenceAll(groupId),
sendGroupNotice: (groupId: number, content: string, options?: any) => bridge.sendGroupNotice(groupId, content, options),
getGroupNotice: (groupId: number) => bridge.getGroupNotice(groupId),
getCookiesStr: (domain: string) => bridge.getCookiesStr(domain),
getCsrfToken: () => bridge.getCsrfToken(),
getCredentials: (domain: string) => bridge.getCredentials(domain),
};
}