mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
fix: get image rkey
This commit is contained in:
parent
05f0985f7f
commit
dec531c567
@ -7,12 +7,15 @@ import {
|
||||
ChatCacheList,
|
||||
ChatCacheListItemBasic,
|
||||
ChatType,
|
||||
ElementType
|
||||
ElementType, IMAGE_HTTP_HOST, IMAGE_HTTP_HOST_NT, RawMessage
|
||||
} from "../types";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import {ReceiveCmdS} from "../hook";
|
||||
import {log} from "../../common/utils/log";
|
||||
import http from "http";
|
||||
import {sleep} from "../../common/utils";
|
||||
import {hookApi} from "../external/moehook/hook";
|
||||
|
||||
export class NTQQFileApi {
|
||||
static async getFileType(filePath: string) {
|
||||
@ -86,11 +89,16 @@ export class NTQQFileApi {
|
||||
}
|
||||
}
|
||||
|
||||
static async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, isFile: boolean = false) {
|
||||
static async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, force: boolean = false) {
|
||||
// 用于下载收到的消息中的图片等
|
||||
if (sourcePath && fs.existsSync(sourcePath)) {
|
||||
if (force){
|
||||
fs.unlinkSync(sourcePath)
|
||||
}
|
||||
else{
|
||||
return sourcePath
|
||||
}
|
||||
}
|
||||
const apiParams = [
|
||||
{
|
||||
getReq: {
|
||||
@ -127,6 +135,107 @@ export class NTQQFileApi {
|
||||
})
|
||||
}
|
||||
|
||||
static async getImageUrl(msg: RawMessage) {
|
||||
const msgElement = msg.elements.find(e => !!e.picElement);
|
||||
if (!msgElement) {
|
||||
return '';
|
||||
}
|
||||
const url = msgElement.picElement.originImageUrl; // 没有域名
|
||||
const md5HexStr = msgElement.picElement.md5HexStr;
|
||||
const fileMd5 = msgElement.picElement.md5HexStr;
|
||||
const fileUuid = msgElement.picElement.fileUuid;
|
||||
let imageUrl = '';
|
||||
// let currentRKey = "CAQSKAB6JWENi5LMk0kc62l8Pm3Jn1dsLZHyRLAnNmHGoZ3y_gDZPqZt-64"
|
||||
if (url) {
|
||||
if (url.startsWith('/download')) {
|
||||
// console.log('rkey', rkey);
|
||||
if (url.includes('&rkey=')) {
|
||||
imageUrl = IMAGE_HTTP_HOST_NT + url;
|
||||
} else {
|
||||
if (!hookApi.isAvailable()) {
|
||||
log('hookApi is not available');
|
||||
return '';
|
||||
}
|
||||
let rkey = hookApi.getRKey();
|
||||
const refreshRKey = async () => {
|
||||
log('正在获取图片rkey...');
|
||||
NTQQFileApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid, msgElement.elementId, '', msgElement.picElement.sourcePath, true).then().catch(() => {
|
||||
});
|
||||
await sleep(300);
|
||||
rkey = hookApi.getRKey();
|
||||
if (rkey) {
|
||||
log('图片rkey获取成功', rkey);
|
||||
}
|
||||
};
|
||||
if (!rkey) {
|
||||
// 下载一次图片获取rkey
|
||||
try {
|
||||
await refreshRKey();
|
||||
} catch (e) {
|
||||
log('获取图片rkey失败', e);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
imageUrl = IMAGE_HTTP_HOST_NT + url + `${rkey}`;
|
||||
// 调用head请求获取图片rkey是否正常
|
||||
const checkUrl = new Promise((resolve, reject) => {
|
||||
const uri = new URL(imageUrl);
|
||||
const options = {
|
||||
method: 'GET',
|
||||
host: uri.host,
|
||||
path: uri.pathname + uri.search,
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
|
||||
'Accept': '*/*',
|
||||
'Range': 'bytes=0-0'
|
||||
}
|
||||
};
|
||||
const req = http.request(options, (res) => {
|
||||
console.log(`Check rkey status: ${res.statusCode}`);
|
||||
console.log(`Check rkey headers: ${JSON.stringify(res.headers)}`);
|
||||
if (res.statusCode == 200 || res.statusCode === 206) {
|
||||
// console.log('The image URL is accessible.');
|
||||
resolve('ok');
|
||||
} else {
|
||||
reject('The image URL is not accessible.');
|
||||
}
|
||||
});
|
||||
|
||||
req.setTimeout(3000, () => {
|
||||
req.destroy();
|
||||
reject('Check rkey request timed out');
|
||||
});
|
||||
|
||||
req.on('error', (e) => {
|
||||
console.error(`Problem with rkey request: ${e.message}`);
|
||||
reject(e.message);
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
try {
|
||||
const start = Date.now();
|
||||
await checkUrl;
|
||||
const end = Date.now();
|
||||
console.log('Check rkey request time:', end - start);
|
||||
} catch (e) {
|
||||
try {
|
||||
await refreshRKey();
|
||||
imageUrl = IMAGE_HTTP_HOST_NT + url + `${rkey}`;
|
||||
} catch (e) {
|
||||
log('获取rkey失败', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
imageUrl = IMAGE_HTTP_HOST + url;
|
||||
}
|
||||
} else if (fileMd5 || md5HexStr) {
|
||||
imageUrl = `${IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${(fileMd5 || md5HexStr)!.toUpperCase()}/0`;
|
||||
}
|
||||
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class NTQQFileCacheApi {
|
||||
|
30
src/ntqqapi/external/moehook/hook.ts
vendored
Normal file
30
src/ntqqapi/external/moehook/hook.ts
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
import {log} from "../../../common/utils";
|
||||
|
||||
interface MoeHook {
|
||||
GetRkey: () => string, // Return '&rkey=xxx'
|
||||
HookRkey: () => string
|
||||
}
|
||||
|
||||
|
||||
class HookApi {
|
||||
private readonly moeHook: MoeHook | null = null;
|
||||
|
||||
constructor() {
|
||||
try {
|
||||
this.moeHook = require('./MoeHook.node');
|
||||
console.log("hook rkey地址", this.moeHook!.HookRkey());
|
||||
} catch (e) {
|
||||
console.log('加载 moehoo 失败', e);
|
||||
}
|
||||
}
|
||||
|
||||
getRKey(): string {
|
||||
return this.moeHook?.GetRkey() || '';
|
||||
}
|
||||
|
||||
isAvailable() {
|
||||
return !!this.moeHook;
|
||||
}
|
||||
}
|
||||
|
||||
export const hookApi = new HookApi();
|
Loading…
x
Reference in New Issue
Block a user