mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
commit
ff65a42350
@ -3,28 +3,45 @@ import http from 'node:http';
|
||||
|
||||
export class RequestUtil {
|
||||
// 适用于获取服务器下发cookies时获取,仅GET
|
||||
static async HttpsGetCookies(url: string): Promise<Map<string, string>> {
|
||||
return new Promise<Map<string, string>>((resolve, reject) => {
|
||||
const protocol = url.startsWith('https://') ? https : http;
|
||||
protocol.get(url, (res) => {
|
||||
const cookiesHeader = res.headers['set-cookie'];
|
||||
if (!cookiesHeader) {
|
||||
resolve(new Map<string, string>());
|
||||
} else {
|
||||
const cookiesMap = new Map<string, string>();
|
||||
cookiesHeader.forEach((cookieStr) => {
|
||||
cookieStr.split(';').forEach((cookiePart) => {
|
||||
const trimmedPart = cookiePart.trim();
|
||||
if (trimmedPart.includes('=')) {
|
||||
const [key, value] = trimmedPart.split('=').map(part => part.trim());
|
||||
cookiesMap.set(key, decodeURIComponent(value)); // 解码cookie值
|
||||
}
|
||||
});
|
||||
static async HttpsGetCookies(url: string): Promise<{ [key: string]: string }> {
|
||||
const client = url.startsWith('https') ? https : http;
|
||||
return new Promise((resolve, reject) => {
|
||||
client.get(url, (res) => {
|
||||
let cookies: { [key: string]: string } = {};
|
||||
const handleRedirect = (res: http.IncomingMessage) => {
|
||||
//console.log(res.headers.location);
|
||||
if (res.statusCode === 301 || res.statusCode === 302) {
|
||||
if (res.headers.location) {
|
||||
const redirectUrl = new URL(res.headers.location, url);
|
||||
RequestUtil.HttpsGetCookies(redirectUrl.href).then((redirectCookies) => {
|
||||
// 合并重定向过程中的cookies
|
||||
cookies = { ...cookies, ...redirectCookies };
|
||||
resolve(cookies);
|
||||
});
|
||||
} else {
|
||||
resolve(cookies);
|
||||
}
|
||||
} else {
|
||||
resolve(cookies);
|
||||
}
|
||||
};
|
||||
res.on('data', () => { }); // Necessary to consume the stream
|
||||
res.on('end', () => {
|
||||
handleRedirect(res);
|
||||
});
|
||||
if (res.headers['set-cookie']) {
|
||||
//console.log(res.headers['set-cookie']);
|
||||
res.headers['set-cookie'].forEach((cookie) => {
|
||||
const parts = cookie.split(';')[0].split('=');
|
||||
const key = parts[0];
|
||||
const value = parts[1];
|
||||
if (key && value && key.length > 0 && value.length > 0) {
|
||||
cookies[key] = value;
|
||||
}
|
||||
});
|
||||
resolve(cookiesMap);
|
||||
}
|
||||
}).on('error', (error) => {
|
||||
reject(error);
|
||||
}).on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import { NTQQWindowApi, NTQQWindows } from './window'
|
||||
import { cacheFunc, isQQ998, log, sleep } from '../../common/utils'
|
||||
import { wrapperApi } from '@/ntqqapi/native/wrapper'
|
||||
import * as https from 'https'
|
||||
import { RequestUtil } from '@/common/utils/request'
|
||||
|
||||
let userInfoCache: Record<string, User> = {} // uid: User
|
||||
|
||||
@ -98,7 +99,16 @@ export class NTQQUserApi {
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
static async getQzoneCookies() {
|
||||
const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + selfInfo.uin + '&clientkey=' + this.getClientKey() + '&u1=https%3A%2F%2Fuser.qzone.qq.com%2F' + selfInfo.uin + '%2Finfocenter&keyindex=19%27'
|
||||
let cookies: { [key: string]: string; } = {};
|
||||
try {
|
||||
cookies = await RequestUtil.HttpsGetCookies(requestUrl);
|
||||
} catch (e: any) {
|
||||
cookies = {}
|
||||
}
|
||||
return cookies;
|
||||
}
|
||||
static async getSkey(): Promise<string> {
|
||||
const clientKeyData = await this.getClientKey()
|
||||
if (clientKeyData.result !== 0) {
|
||||
@ -106,33 +116,19 @@ export class NTQQUserApi {
|
||||
}
|
||||
const url = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + selfInfo.uin
|
||||
+ '&clientkey=' + clientKeyData.clientKey
|
||||
+ '&u1=https%3A%2F%2Fh5.qzone.qq.com%2Fqqnt%2Fqzoneinpcqq%2Ffriend%3Frefresh%3D0%26clientuin%3D0%26darkMode%3D0&keyindex=' + clientKeyData.keyIndex
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = https.get(url, (res) => {
|
||||
const rawCookies = res.headers['set-cookie']
|
||||
const cookies = {}
|
||||
rawCookies.forEach(cookie => {
|
||||
// 使用正则表达式匹配 cookie 名称和值
|
||||
const regex = /([^=;]+)=([^;]*)/
|
||||
const match = regex.exec(cookie)
|
||||
if (match) {
|
||||
cookies[match[1].trim()] = match[2].trim()
|
||||
}
|
||||
})
|
||||
resolve(cookies['skey'])
|
||||
})
|
||||
req.on('error', e => {
|
||||
reject(e)
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
+ '&u1=https%3A%2F%2Fh5.qzone.qq.com%2Fqqnt%2Fqzoneinpcqq%2Ffriend%3Frefresh%3D0%26clientuin%3D0%26darkMode%3D0&keyindex=' + clientKeyData.keyIndex;
|
||||
return (await RequestUtil.HttpsGetCookies(url))?.skey;
|
||||
}
|
||||
|
||||
@cacheFunc(60 * 30 * 1000)
|
||||
static async getCookies(domain: string) {
|
||||
if (domain.endsWith("qzone.qq.com")) {
|
||||
let data = (await NTQQUserApi.getQzoneCookies());
|
||||
const CookieValue = 'p_skey=' + data.p_skey + '; skey=' + data.skey + '; p_uin=o' + selfInfo.uin + '; uin=o' + selfInfo.uin;
|
||||
return { bkn: NTQQUserApi.genBkn(data.p_skey), cookies: CookieValue };
|
||||
}
|
||||
const skey = await this.getSkey();
|
||||
const pskey= (await this.getPSkey([domain])).get(domain);
|
||||
const pskey = (await this.getPSkey([domain])).get(domain);
|
||||
if (!pskey || !skey) {
|
||||
throw new Error('获取Cookies失败')
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
import BaseAction from '../BaseAction'
|
||||
import { NTQQUserApi } from '../../../ntqqapi/api'
|
||||
import { groups } from '../../../common/data'
|
||||
import { ActionName } from '../types'
|
||||
|
||||
interface Payload {
|
||||
domain: string
|
||||
}
|
||||
|
||||
export class GetCookies extends BaseAction<Payload, { cookies: string; bkn: string }> {
|
||||
actionName = ActionName.GetCookies
|
||||
|
||||
protected async _handle(payload: Payload) {
|
||||
const domain = payload.domain || 'qun.qq.com'
|
||||
return NTQQUserApi.getCookies(domain);
|
||||
}
|
||||
}
|
||||
import BaseAction from '../BaseAction'
|
||||
import { NTQQUserApi, WebApi } from '../../../ntqqapi/api'
|
||||
import { groups, selfInfo } from '../../../common/data'
|
||||
import { ActionName } from '../types'
|
||||
|
||||
interface Payload {
|
||||
domain: string
|
||||
}
|
||||
|
||||
export class GetCookies extends BaseAction<Payload, { cookies: string; bkn: string }> {
|
||||
actionName = ActionName.GetCookies
|
||||
|
||||
protected async _handle(payload: Payload) {
|
||||
const domain = payload.domain || 'qun.qq.com'
|
||||
return NTQQUserApi.getCookies(domain);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user