From a2ee900ed5519094ba363954bb2501afa302555e Mon Sep 17 00:00:00 2001 From: linyuchen Date: Wed, 24 Apr 2024 21:03:47 +0800 Subject: [PATCH] fix: check rkey --- package.json | 2 +- src/onebot11/constructor.ts | 91 ++++--------------------------------- src/onebot11/index.ts | 4 +- 3 files changed, 11 insertions(+), 86 deletions(-) diff --git a/package.json b/package.json index e3297bffa..78a8ee0e8 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "build:dev": "vite build --mode development", "build:prod": "vite build --mode production", "build": "npm run build:dev", - "build:core": "cd ./src/core && vite build --mode production", + "build:core": "cd ./src/core && npm run build --mode production", "watch": "npm run watch:dev", "debug-win": "powershell dist/napcat.ps1", "lint": "eslint --fix src/**/*.{js,ts}", diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index 5755f3a7c..1754b997a 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -40,7 +40,6 @@ import { OB11GroupDecreaseEvent } from './event/notice/OB11GroupDecreaseEvent'; import { ob11Config } from '@/onebot11/config'; import { deleteGroup, getFriend, getGroupMember, groupMembers, selfInfo, tempGroupCodeMap } from '@/common/data'; import { NTQQFileApi, NTQQGroupApi, NTQQUserApi } from '@/core/qqnt/apis'; -import { rkeyHook } from '@/core/qqnt/extends/rkey'; import http from 'http'; @@ -140,88 +139,8 @@ export class OB11Constructor { // message_data["data"]["file"] = element.picElement.sourcePath message_data['data']['file'] = element.picElement.fileName; // message_data["data"]["path"] = element.picElement.sourcePath - const url = element.picElement.originImageUrl; - const md5HexStr = element.picElement.md5HexStr; - const fileMd5 = element.picElement.md5HexStr; - const fileUuid = element.picElement.fileUuid; - // let currentRKey = config.imageRKey || "CAQSKAB6JWENi5LMk0kc62l8Pm3Jn1dsLZHyRLAnNmHGoZ3y_gDZPqZt-64" - if (url) { - if (url.startsWith('/download')) { - let rkey = rkeyHook.GetRkey(); - console.log('rkey', rkey); - if (url.includes('&rkey=')) { - // 正则提取rkey - // const rkey = url.match(/&rkey=([^&]+)/)[1] - // // log("图片url已有rkey", rkey) - // if (rkey != currentRKey){ - // config.imageRKey = rkey - // if (Date.now() - lastRKeyUpdateTime > 1000 * 60) { - // lastRKeyUpdateTime = Date.now() - // getConfigUtil().setConfig(config) - // } - // } - message_data['data']['url'] = IMAGE_HTTP_HOST_NT + url; - } else { - const getRkey = async () => { - await NTQQFileApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid, element.elementId, '', ''); - rkey = rkeyHook.GetRkey(); - }; - if (!rkey) { - // 下载一次图片获取rkey - try { - await getRkey(); - } catch (e) { - continue; - } - } - let imageUrl = IMAGE_HTTP_HOST_NT + url + `${rkey}`; - // 调用head请求获取图片rkey是否正常 - const checkUrl = new Promise((resolve, reject) => { - const options = { - method: 'HEAD', - host: new URL(imageUrl).host, - path: new URL(imageUrl).pathname - }; - const req = http.request(options, (res) => { - console.log(`STATUS: ${res.statusCode}`); - console.log(`HEADERS: ${JSON.stringify(res.headers)}`); - - if (res.statusCode == 200) { - console.log('The Image URL is accessible.'); - resolve('ok'); - } else { - reject('The Image URL is not accessible.'); - } - }); - - req.on('error', (e) => { - console.error(`problem with request: ${e.message}`); - reject(e.message); - }); - req.end(); - }); - try { - await checkUrl; - } catch (e) { - try { - await getRkey(); - imageUrl = IMAGE_HTTP_HOST_NT + url + `${rkey}`; - } catch (e) { - log('获取rkey失败', e); - } - } - message_data['data']['url'] = imageUrl; - } - } else { - message_data['data']['url'] = IMAGE_HTTP_HOST + url; - } - } else if (fileMd5) { - message_data['data']['url'] = `${IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${fileMd5.toUpperCase()}/0`; - } - - if (!message_data['data']['url']) { - message_data['data']['url'] = `${IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${md5HexStr!.toUpperCase()}/0`; - } + // let currentRKey = "CAQSKAB6JWENi5LMk0kc62l8Pm3Jn1dsLZHyRLAnNmHGoZ3y_gDZPqZt-64" + message_data['data']['url'] = await NTQQFileApi.getImageUrl(msg); // message_data["data"]["file_id"] = element.picElement.fileUuid message_data['data']['file_size'] = element.picElement.fileSize; dbUtil.addFileCache({ @@ -293,6 +212,12 @@ export class OB11Constructor { } else if (element.marketFaceElement) { message_data['type'] = OB11MessageDataType.mface; message_data['data']['text'] = element.marketFaceElement.faceName; + const md5 = element.marketFaceElement.emojiId; + // 取md5的前两位 + const dir = md5.substring(0, 2); + // 获取组装url + const url = `https://p.qpic.cn/CDN_STATIC/0/data/imgcache/htdocs/club/item/parcel/item/${dir}/${md5}/300x300.png?max_age=31536000`; + message_data['data']['url'] = url; } else if (element.markdownElement) { message_data['type'] = OB11MessageDataType.markdown; message_data['data']['data'] = element.markdownElement.content; diff --git a/src/onebot11/index.ts b/src/onebot11/index.ts index 0b6106cb5..c7495bddf 100644 --- a/src/onebot11/index.ts +++ b/src/onebot11/index.ts @@ -1,4 +1,4 @@ -import { rkeyHook } from '@/core/qqnt/extends/rkey'; +import { rkeyManager } from '@/core/qqnt/extends/rkey'; import { napCatCore } from '@/core'; import { MsgListener } from '@/core/qqnt/listeners'; import { NapCatOnebot11 } from '@/onebot11/main'; @@ -41,7 +41,7 @@ napCatCore.addLoginSuccessCallback(() => { console.log('login success'); try{ - console.log(rkeyHook.HookRkey()); + console.log(rkeyManager.HookRkey()); }catch (e) { console.error(); }