feat: implement get cookies and CSRF and credentials
This commit is contained in:
1 parent
8b313fe088
commit
c8dd520d65
5 files changed
+95
-6
No files matched your search
@@ -75,6 +75,9 @@ import {
|
|||||||
getGroupEssenceAll as getGroupEssenceAll_,
|
getGroupEssenceAll as getGroupEssenceAll_,
|
||||||
sendGroupNotice as sendGroupNotice_,
|
sendGroupNotice as sendGroupNotice_,
|
||||||
getGroupNotice as getGroupNotice_,
|
getGroupNotice as getGroupNotice_,
|
||||||
|
getCookiesStr as getCookiesStr_,
|
||||||
|
getCsrfToken as getCsrfToken_,
|
||||||
|
getCredentials as getCredentials_,
|
||||||
} from './web-actions';
|
} from './web-actions';
|
||||||
import type { GroupFilesResult } from './bridge-actions';
|
import type { GroupFilesResult } from './bridge-actions';
|
||||||
import type { MediaIndexNode } from './bridge-actions';
|
import type { MediaIndexNode } from './bridge-actions';
|
||||||
@@ -496,6 +499,9 @@ export class Bridge {
|
|||||||
async setProfile(nickname?: string, personalNote?: string): Promise<void> {
|
async setProfile(nickname?: string, personalNote?: string): Promise<void> {
|
||||||
return setProfile_(this, nickname, personalNote);
|
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); }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -259,4 +259,57 @@ export async function getGroupNotice(bridge: Bridge, groupId: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return retNotices;
|
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
|
||||||
|
};
|
||||||
}
|
}
|
||||||
@@ -413,18 +413,42 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
|||||||
|
|
||||||
// --- Credentials ---
|
// --- Credentials ---
|
||||||
|
|
||||||
h.registerAction('get_cookies', async () => {
|
h.registerAction('get_cookies', async (params) => {
|
||||||
return okResponse({ cookies: '' });
|
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 () => {
|
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 () => {
|
h.registerAction('get_credentials', async (params) => {
|
||||||
return okResponse({ cookies: '', csrf_token: 0 });
|
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 ---
|
// --- Utility ---
|
||||||
|
|
||||||
h.registerAction('set_restart', async () => {
|
h.registerAction('set_restart', async () => {
|
||||||
|
|||||||
@@ -102,6 +102,9 @@ export interface ApiActionContext {
|
|||||||
getGroupEssenceAll?: (groupId: number) => Promise<GroupEssenceMsgRet[]>;
|
getGroupEssenceAll?: (groupId: number) => Promise<GroupEssenceMsgRet[]>;
|
||||||
sendGroupNotice?: (groupId: number, content: string, options?: any) => Promise<any>;
|
sendGroupNotice?: (groupId: number, content: string, options?: any) => Promise<any>;
|
||||||
getGroupNotice?: (groupId: number) => 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>;
|
type ActionHandler = (params: JsonObject) => Promise<import('./types').ApiResponse>;
|
||||||
|
|||||||
@@ -132,6 +132,9 @@ export function buildApiContext(ref: InstanceRef): ApiActionContext {
|
|||||||
getGroupEssenceAll: (groupId: number) => bridge.getGroupEssenceAll(groupId),
|
getGroupEssenceAll: (groupId: number) => bridge.getGroupEssenceAll(groupId),
|
||||||
sendGroupNotice: (groupId: number, content: string, options?: any) => bridge.sendGroupNotice(groupId, content, options),
|
sendGroupNotice: (groupId: number, content: string, options?: any) => bridge.sendGroupNotice(groupId, content, options),
|
||||||
getGroupNotice: (groupId: number) => bridge.getGroupNotice(groupId),
|
getGroupNotice: (groupId: number) => bridge.getGroupNotice(groupId),
|
||||||
|
getCookiesStr: (domain: string) => bridge.getCookiesStr(domain),
|
||||||
|
getCsrfToken: () => bridge.getCsrfToken(),
|
||||||
|
getCredentials: (domain: string) => bridge.getCredentials(domain),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user