Merge branch 'main' into markyfsun/main

This commit is contained in:
student_2333 2024-04-30 11:35:38 +08:00
commit 9c6abd5167
No known key found for this signature in database
GPG Key ID: B36E8DF853C33EE1
9 changed files with 74 additions and 6922 deletions

View File

@ -52,6 +52,7 @@ TG群<https://t.me/+nLZEnpne-pQ1OWFl>
- [LLAPI](https://github.com/Night-stars-1/LiteLoaderQQNT-Plugin-LLAPI)
- [chronocat](https://github.com/chrononeko/chronocat/)
- [koishi-plugin-adapter-onebot](https://github.com/koishijs/koishi-plugin-adapter-onebot)
- [silk-wasm](https://github.com/idranme/silk-wasm)
## 友链

View File

@ -44,7 +44,8 @@ let config = {
{ src: './manifest.json', dest: 'dist' },
{ src: './icon.jpg', dest: 'dist' },
{ src: './src/ntqqapi/external/crychic/crychic-win32-x64.node', dest: 'dist/main/' },
{ src: './src/ntqqapi/external/moehook/MoeHook-win32-x64.node', dest: 'dist/main/', rename: 'MoeHook.node' },
{ src: './src/ntqqapi/external/moehook/MoeHoo-win32-x64.node', dest: 'dist/main/' },
{ src: './src/ntqqapi/external/moehook/MoeHoo-linux-x64.node', dest: 'dist/main/' },
],
}),
],

6830
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,11 +15,16 @@ import {
import path from 'path'
import fs from 'fs'
import { ReceiveCmdS } from '../hook'
import { log } from '../../common/utils/log'
import { log } from '../../common/utils'
import http from 'http'
import { sleep } from '../../common/utils'
import { hookApi } from '../external/moehook/hook'
let privateImageRKey = ''
let groupImageRKey = ''
let lastGetRKeyTime = 0
const rkeyExpireTime = 1000 * 60 * 30
export class NTQQFileApi {
static async getFileType(filePath: string) {
return await callNTQQApi<{ ext: string }>({
@ -156,6 +161,7 @@ export class NTQQFileApi {
}
static async getImageUrl(msg: RawMessage) {
const isPrivateImage = msg.chatType !== ChatType.group
const msgElement = msg.elements.find((e) => !!e.picElement)
if (!msgElement) {
return ''
@ -164,21 +170,29 @@ export class NTQQFileApi {
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 {
return IMAGE_HTTP_HOST_NT + url
}
if (!hookApi.isAvailable()) {
log('hookApi is not available')
return ''
}
let rkey = hookApi.getRKey()
const saveRKey = (rkey: string) => {
if (isPrivateImage) {
privateImageRKey = rkey
} else {
groupImageRKey = rkey
}
lastGetRKeyTime = Date.now()
}
const refreshRKey = async () => {
log('正在获取图片rkey...')
log('获取图片rkey...')
NTQQFileApi.downloadMedia(
msg.msgId,
msg.chatType,
@ -190,80 +204,40 @@ export class NTQQFileApi {
)
.then()
.catch(() => {})
await sleep(300)
rkey = hookApi.getRKey()
if (rkey) {
log('图片rkey获取成功', rkey)
await sleep(1000)
const _rkey = hookApi.getRKey()
if (_rkey) {
log('图片rkey获取成功', _rkey)
saveRKey(_rkey)
return _rkey
}
}
if (!rkey) {
// 下载一次图片获取rkey
try {
await refreshRKey()
} catch (e) {
log('获取图片rkey失败', e)
const existsRKey = isPrivateImage ? privateImageRKey : groupImageRKey
if (Date.now() - lastGetRKeyTime > rkeyExpireTime || !existsRKey) {
// rkey过期
const newRKey = await refreshRKey()
if (newRKey) {
return IMAGE_HTTP_HOST_NT + url + `${newRKey}`
} else {
log('图片rkey获取失败', url)
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)
}
}
// 使用未过期的rkey
if (existsRKey) {
return IMAGE_HTTP_HOST_NT + url + `${existsRKey}`
}
} else {
imageUrl = IMAGE_HTTP_HOST + url
// 老的图片url不需要rkey
return IMAGE_HTTP_HOST + url
}
} else if (fileMd5 || md5HexStr) {
imageUrl = `${IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${(fileMd5 || md5HexStr)!.toUpperCase()}/0`
// 没有url需要自己拼接
return `${IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${(fileMd5 || md5HexStr)!.toUpperCase()}/0`
}
return imageUrl
log('图片url获取失败', msg)
return ''
}
}

View File

@ -221,6 +221,9 @@ export class SendMsgElementConstructor {
static async ptt(pttPath: string): Promise<SendPttElement> {
const { converted, path: silkPath, duration } = await encodeSilk(pttPath)
if (!silkPath) {
throw '语音转换失败, 请检查语音文件是否正常'
}
// log("生成语音", silkPath, duration);
const { md5, fileName, path, fileSize } = await NTQQFileApi.uploadFile(silkPath, ElementType.PTT)
if (fileSize === 0) {

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,5 @@
import { log } from '../../../common/utils'
import * as os from 'os'
interface MoeHook {
GetRkey: () => string // Return '&rkey=xxx'
@ -10,7 +11,9 @@ class HookApi {
constructor() {
try {
this.moeHook = require('./MoeHook.node')
const systemPlatform = os.platform()
const cpuArch = os.arch()
this.moeHook = require(`./MoeHoo-${systemPlatform}-${cpuArch}.node`)
console.log('hook rkey地址', this.moeHook!.HookRkey())
} catch (e) {
console.log('加载 moehoo 失败', e)

View File

@ -285,7 +285,7 @@ export async function sendMsg(
waitComplete = true,
) {
if (!sendElements.length) {
throw '消息体无法解析'
throw '消息体无法解析,请检查是否发送了不支持的消息类型'
}
const returnMsg = await NTQQMsgApi.sendMsg(peer, sendElements, waitComplete, 20000)
log('消息发送结果', returnMsg)