test pskey

This commit is contained in:
linyuchen
2024-03-20 11:06:00 +08:00
parent 235328e4fe
commit 103bf94170
5 changed files with 137 additions and 27 deletions

View File

@@ -75,7 +75,7 @@ export class NTQQGroupApi{
}
static async getGroupIgnoreNotifies() {
await NTQQGroupApi.getGroupNotifies();
return await NTQQWindowApi.openWindow(NTQQWindows.GroupNotifyFilterWindow, ReceiveCmdS.GROUP_NOTIFY);
return await NTQQWindowApi.openWindow(NTQQWindows.GroupNotifyFilterWindow,[], ReceiveCmdS.GROUP_NOTIFY);
}
static async handleGroupRequest(seq: string, operateType: GroupRequestOperateTypes, reason?: string) {
const notify: GroupNotify = await dbUtil.getGroupNotify(seq)

84
src/ntqqapi/api/webapi.ts Normal file
View File

@@ -0,0 +1,84 @@
import {net, session} from "electron";
import {NTQQApi} from "../ntcall";
import {groups} from "../../common/data";
import {log} from "../../common/utils";
export class WebApi{
private static bkn: string;
private static skey: string;
private static pskey: string;
private static cookie: string
private defaultHeaders: Record<string,string> = {
"User-Agent": "QQ/8.9.28.635 CFNetwork/1312 Darwin/21.0.0"
}
constructor() {
}
public async addGroupDigest(groupCode: string, msgSeq: string){
const url = `https://qun.qq.com/cgi-bin/group_digest/cancel_digest?random=665&X-CROSS-ORIGIN=fetch&group_code=${groupCode}&msg_seq=${msgSeq}&msg_random=444021292`
const res = await this.request(url)
return await res.json()
}
public async getGroupDigest(groupCode: string){
const url = `https://qun.qq.com/cgi-bin/group_digest/digest_list?random=665&X-CROSS-ORIGIN=fetch&group_code=${groupCode}&page_start=0&page_limit=20`
const res = await this.request(url)
return await res.json()
}
private genBkn(sKey: string){
sKey = sKey || "";
let hash = 5381;
for (let i = 0; i < sKey.length; i++) {
const code = sKey.charCodeAt(i);
hash = hash + (hash << 5) + code;
}
return (hash & 0x7FFFFFFF).toString();
}
private async init(){
if (!WebApi.bkn) {
const group = groups[0];
WebApi.skey = (await NTQQApi.getSkey(group.groupName, group.groupCode)).data;
WebApi.bkn = this.genBkn(WebApi.skey);
let cookie = await NTQQApi.getPSkey();
const pskeyRegex = /p_skey=([^;]+)/;
const match = cookie.match(pskeyRegex);
const pskeyValue = match ? match[1] : null;
WebApi.pskey = pskeyValue;
cookie = cookie.replace(/skey=.*?/, `skey=${WebApi.skey}`);
WebApi.cookie = cookie;
// for(const kv of WebApi.cookie.split(";")){
// const [key, value] = kv.split("=");
// }
// log("set cookie", key, value)
// await session.defaultSession.cookies.set({
// url: 'https://qun.qq.com', // 你要请求的域名
// name: key.trim(),
// value: value.trim(),
// expirationDate: Date.now() / 1000 + 300000, // Cookie 过期时间例如设置为当前时间之后的300秒
// });
// }
}
}
private async request(url: string, method: "GET" | "POST" = "GET", headers: Record<string, string> = {}){
await this.init();
url += "&bkn=" + WebApi.bkn;
let _headers: Record<string, string> = {
...this.defaultHeaders, ...headers,
"Cookie": WebApi.cookie,
// credentials: 'include'
}
log("request", url, _headers)
const options = {
method: method,
headers: _headers
}
return fetch(url, options)
}
}

View File

@@ -2,7 +2,7 @@ import {callNTQQApi, GeneralCallResult, NTQQApiClass, NTQQApiMethod} from "../nt
import {ReceiveCmd} from "../hook";
import {BrowserWindow} from "electron";
interface NTQQWindow{
export interface NTQQWindow{
windowName: string,
windowUrlHash: string,
}
@@ -16,13 +16,17 @@ export class NTQQWindows{
windowName: "GroupNotifyFilterWindow",
windowUrlHash: "#/group-notify-filter"
}
static GroupEssenceWindow: NTQQWindow = {
windowName: "GroupEssenceWindow",
windowUrlHash: "#/group-essence"
}
}
export class NTQQWindowApi{
// 打开窗口并获取对应的下发事件
static async openWindow(ntQQWindow: NTQQWindow, args: any[], cbCmd: ReceiveCmd=null, autoCloseSeconds: number=2){
const result = await callNTQQApi<GeneralCallResult>({
static async openWindow<R=GeneralCallResult>(ntQQWindow: NTQQWindow, args: any[], cbCmd: ReceiveCmd=null, autoCloseSeconds: number=2){
const result = await callNTQQApi<R>({
className: NTQQApiClass.WINDOW_API,
methodName: NTQQApiMethod.OPEN_EXTRA_WINDOW,
cbCmd,

View File

@@ -61,12 +61,18 @@ let receiveHooks: Array<{
export function hookNTQQApiReceive(window: BrowserWindow) {
const originalSend = window.webContents.send;
const patchSend = (channel: string, ...args: NTQQApiReturnData) => {
let isLogger = false
try {
if (!args[0]?.eventName?.startsWith("ns-LoggerApi")) {
HOOK_LOG && log(`received ntqq api message: ${channel}`, JSON.stringify(args))
}
isLogger = args[0]?.eventName?.startsWith("ns-LoggerApi")
} catch (e) {
}
if (!isLogger) {
try {
HOOK_LOG && log(`received ntqq api message: ${channel}`, JSON.stringify(args))
}catch (e) {
}
}
if (args?.[1] instanceof Array) {
for (let receiveData of args?.[1]) {
@@ -111,12 +117,18 @@ export function hookNTQQApiCall(window: BrowserWindow) {
const proxyIpcMsg = new Proxy(ipc_message_proxy, {
apply(target, thisArg, args) {
let isLogger = false
try {
if (args[3][1][0] !== "info") {
HOOK_LOG && log("call NTQQ api", thisArg, args);
}
isLogger = args[3][0].eventName.startsWith("ns-LoggerApi")
} catch (e) {
}
if (!isLogger) {
try{
HOOK_LOG && log("call NTQQ api", thisArg, args);
}catch (e) {
}
}
return target.apply(thisArg, args);
},

View File

@@ -3,7 +3,9 @@ import {hookApiCallbacks, ReceiveCmd, ReceiveCmdS, registerReceiveHook, removeRe
import {v4 as uuidv4} from "uuid"
import {log} from "../common/utils/log";
import {NTQQWindowApi, NTQQWindows} from "./api/window";
import {NTQQWindow, NTQQWindowApi, NTQQWindows} from "./api/window";
import {WebApi} from "./api/webapi";
import {HOOK_LOG} from "../common/config";
export enum NTQQApiClass {
NT_API = "ns-ntApi",
@@ -14,7 +16,8 @@ export enum NTQQApiClass {
BUSINESS_API = "ns-BusinessApi",
GLOBAL_DATA = "ns-GlobalDataApi",
SKEY_API = "ns-SkeyApi",
HOME_WORK = "ns-GroupHomeWork"
GROUP_HOME_WORK = "ns-GroupHomeWork",
GROUP_ESSENCE = "ns-GroupEssence",
}
export enum NTQQApiMethod {
@@ -103,7 +106,7 @@ export function callNTQQApi<ReturnType>(params: NTQQApiParams) {
timeout = timeout ?? 5;
afterFirstCmd = afterFirstCmd ?? true;
const uuid = uuidv4();
// log("callNTQQApi", channel, className, methodName, args, uuid)
HOOK_LOG && log("callNTQQApi", channel, className, methodName, args, uuid)
return new Promise((resolve: (data: ReturnType) => void, reject) => {
// log("callNTQQApiPromise", channel, className, methodName, args, uuid)
const _timeout = timeout * 1000
@@ -183,21 +186,21 @@ export class NTQQApi {
})
}
static async getSkey(groupCode: string, groupName: string) {
await NTQQWindowApi.openWindow(NTQQWindows.GroupHomeWorkWindow, [{
static async getSkey(groupName: string, groupCode: string): Promise<{data: string}> {
return await NTQQWindowApi.openWindow<{data: string}>(NTQQWindows.GroupHomeWorkWindow, [{
groupName,
groupCode,
"source": "funcbar"
}], ReceiveCmdS.SKEY_UPDATE, 1);
return await callNTQQApi<GeneralCallResult>({
className: NTQQApiClass.HOME_WORK,
methodName: NTQQApiMethod.UPDATE_SKEY,
args: [
{
domain: "qun.qq.com"
}
]
})
// return await callNTQQApi<string>({
// className: NTQQApiClass.GROUP_HOME_WORK,
// methodName: NTQQApiMethod.UPDATE_SKEY,
// args: [
// {
// domain: "qun.qq.com"
// }
// ]
// })
// return await callNTQQApi<GeneralCallResult>({
// methodName: NTQQApiMethod.GET_SKEY,
// args: [
@@ -218,9 +221,9 @@ export class NTQQApi {
// })
}
static async updateSkey() {
return await callNTQQApi<GeneralCallResult>({
className: NTQQApiClass.HOME_WORK,
static async getPSkey() {
return await callNTQQApi<string>({
className: NTQQApiClass.GROUP_HOME_WORK,
methodName: NTQQApiMethod.UPDATE_SKEY,
args: [
{
@@ -230,4 +233,11 @@ export class NTQQApi {
})
}
static async addGroupDigest(groupCode: string, msgSeq: string) {
return await new WebApi().addGroupDigest(groupCode, msgSeq);
}
static async getGroupDigest(groupCode: string) {
return await new WebApi().getGroupDigest(groupCode);
}
}