mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2c6a6ba440 | ||
![]() |
4592bf7817 | ||
![]() |
afd6d450a0 | ||
![]() |
b134849dcf | ||
![]() |
e7d0f6d6da | ||
![]() |
16a29b0127 | ||
![]() |
1f5596ef16 | ||
![]() |
bef05432d0 | ||
![]() |
67533d7743 | ||
![]() |
0cc86c6348 | ||
![]() |
607dd68620 | ||
![]() |
7c8cbc0799 | ||
![]() |
ec0c2e8c33 | ||
![]() |
7f3dbe0552 | ||
![]() |
0e9044e0c8 | ||
![]() |
3171640193 | ||
![]() |
a56cee3485 | ||
![]() |
c8ee371982 | ||
![]() |
5778daeb60 | ||
![]() |
f51f3b9861 | ||
![]() |
44dd1a0b02 | ||
![]() |
61a00ffcbf | ||
![]() |
4b0a0f0a32 | ||
![]() |
a3088fb8bc | ||
![]() |
88fd1f9eb1 | ||
![]() |
15156bac1e | ||
![]() |
a898d2e7be | ||
![]() |
95b003802c | ||
![]() |
95c9eae4ed |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,14 +1,12 @@
|
||||
# Develop
|
||||
node_modules/
|
||||
package-lock.json
|
||||
pnpm-lock.yaml
|
||||
out/
|
||||
dist/
|
||||
/src/core.lib/common/
|
||||
/localdebug/
|
||||
|
||||
# Editor
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea/*
|
||||
|
||||
|
9
.vscode/settings.json
vendored
Normal file
9
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"explorer.fileNesting.enabled": true,
|
||||
"explorer.fileNesting.expand": false,
|
||||
"explorer.fileNesting.patterns": {
|
||||
".env.universal": ".env.*",
|
||||
"tsconfig.json": "tsconfig.*.json, env.d.ts, vite.config.ts",
|
||||
"package.json": "package-lock.json, eslint*, .prettier*, .editorconfig, manifest.json, logo.png, .gitignore, LICENSE"
|
||||
}
|
||||
}
|
@@ -32,12 +32,12 @@ NapCatQQ 是现代化的基于 NTQQ 的 Bot 协议端实现
|
||||
|
||||
[Server.Other](https://docs.napcat.cyou/)
|
||||
|
||||
[Qbot.News](https://neko.qbot.news)
|
||||
[NapCat.Wiki](https://www.napcat.wiki)
|
||||
|
||||
## 回家旅途
|
||||
[QQ Group#1](https://qm.qq.com/q/I6LU87a0Yq)
|
||||
|
||||
[QQ Group#2](https://qm.qq.com/q/uqh4I87KoM)
|
||||
[QQ Group#2](https://qm.qq.com/q/HaRcfrHpUk)
|
||||
|
||||
[Telegram](https://t.me/MelodicMoonlight)
|
||||
|
||||
@@ -46,7 +46,7 @@ NapCatQQ 是现代化的基于 NTQQ 的 Bot 协议端实现
|
||||
## 性能设计/协议标准
|
||||
NapCat 已实现90%+的 OneBot / GoCQ 标准接口,并提供兼容性保留接口,其设计理念遵守 无数据库/异步优先/后台刷新 的性能思想。
|
||||
|
||||
由此设计带来一系列好处,在开发中,获取群员列表通常小于50Ms,单条文本消息发送在320Ms以内,在1k+的群聊流畅运行,同时带来一些副作用,上报数据中大量使用Magic生成字段,消息Id无法持久,无法上报撤回消息原始内容。
|
||||
由此设计带来一系列好处,在开发中,获取群员列表通常小于50Ms,单条文本消息发送在320Ms以内,在1k+的群聊流畅运行,同时带来一些副作用,消息Id无法持久,无法上报撤回消息原始内容。
|
||||
|
||||
NapCat 在设计理念下遵守 OneBot 规范大多数要求并且积极改进,任何合理的标准化 Issue 与 Pr 将被接收。
|
||||
|
||||
|
@@ -4,7 +4,7 @@
|
||||
"name": "NapCatQQ",
|
||||
"slug": "NapCat.Framework",
|
||||
"description": "高性能的 OneBot 11 协议实现",
|
||||
"version": "4.2.68",
|
||||
"version": "4.3.6",
|
||||
"icon": "./logo.png",
|
||||
"authors": [
|
||||
{
|
||||
|
@@ -2,7 +2,7 @@
|
||||
"name": "napcat",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"version": "4.2.68",
|
||||
"version": "4.3.6",
|
||||
"scripts": {
|
||||
"build:universal": "npm run build:webui && vite build --mode universal || exit 1",
|
||||
"build:framework": "npm run build:webui && vite build --mode framework || exit 1",
|
||||
|
@@ -20,7 +20,7 @@ export class Fallback<T> {
|
||||
for (const handler of this.handlers) {
|
||||
try {
|
||||
const result = await handler();
|
||||
let data = await this.checker(result);
|
||||
const data = await this.checker(result);
|
||||
if (data) {
|
||||
return data;
|
||||
}
|
||||
|
121
src/common/file-uuid.ts
Normal file
121
src/common/file-uuid.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { Peer } from '@/core';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
class TimeBasedCache<K, V> {
|
||||
private cache = new Map<K, { value: V, timestamp: number, frequency: number }>();
|
||||
private keyList = new Set<K>();
|
||||
private operationCount = 0;
|
||||
|
||||
constructor(private maxCapacity: number, private ttl: number = 30 * 1000 * 60, private cleanupCount: number = 10) {}
|
||||
|
||||
public put(key: K, value: V): void {
|
||||
const timestamp = Date.now();
|
||||
const cacheEntry = { value, timestamp, frequency: 1 };
|
||||
this.cache.set(key, cacheEntry);
|
||||
this.keyList.add(key);
|
||||
this.operationCount++;
|
||||
if (this.keyList.size > this.maxCapacity) this.evict();
|
||||
if (this.operationCount >= this.cleanupCount) this.cleanup(this.cleanupCount);
|
||||
}
|
||||
|
||||
public get(key: K): V | undefined {
|
||||
const entry = this.cache.get(key);
|
||||
if (entry && Date.now() - entry.timestamp < this.ttl) {
|
||||
entry.timestamp = Date.now();
|
||||
entry.frequency++;
|
||||
this.operationCount++;
|
||||
if (this.operationCount >= this.cleanupCount) this.cleanup(this.cleanupCount);
|
||||
return entry.value;
|
||||
} else {
|
||||
this.deleteKey(key);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private cleanup(count: number): void {
|
||||
const currentTime = Date.now();
|
||||
let cleaned = 0;
|
||||
for (const key of this.keyList) {
|
||||
if (cleaned >= count) break;
|
||||
const entry = this.cache.get(key);
|
||||
if (entry && currentTime - entry.timestamp >= this.ttl) {
|
||||
this.deleteKey(key);
|
||||
cleaned++;
|
||||
}
|
||||
}
|
||||
this.operationCount = 0; // 重置操作计数器
|
||||
}
|
||||
|
||||
private deleteKey(key: K): void {
|
||||
this.cache.delete(key);
|
||||
this.keyList.delete(key);
|
||||
}
|
||||
|
||||
private evict(): void {
|
||||
while (this.keyList.size > this.maxCapacity) {
|
||||
let oldestKey: K | undefined;
|
||||
let minFrequency = Infinity;
|
||||
for (const key of this.keyList) {
|
||||
const entry = this.cache.get(key);
|
||||
if (entry && entry.frequency < minFrequency) {
|
||||
minFrequency = entry.frequency;
|
||||
oldestKey = key;
|
||||
}
|
||||
}
|
||||
if (oldestKey !== undefined) this.deleteKey(oldestKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface FileUUIDData {
|
||||
peer: Peer;
|
||||
modelId?: string;
|
||||
fileId?: string;
|
||||
msgId?: string;
|
||||
elementId?: string;
|
||||
fileUUID?: string;
|
||||
}
|
||||
|
||||
class FileUUIDManager {
|
||||
private cache: TimeBasedCache<string, FileUUIDData>;
|
||||
|
||||
constructor(ttl: number) {
|
||||
this.cache = new TimeBasedCache<string, FileUUIDData>(5000, ttl);
|
||||
}
|
||||
|
||||
public encode(data: FileUUIDData, endString: string = "", customUUID?: string): string {
|
||||
const uuid = customUUID ? customUUID : randomUUID().replace(/-/g, '') + endString;
|
||||
this.cache.put(uuid, data);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public decode(uuid: string): FileUUIDData | undefined {
|
||||
return this.cache.get(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
export class FileNapCatOneBotUUIDWrap {
|
||||
private manager: FileUUIDManager;
|
||||
|
||||
constructor(ttl: number = 86400000) {
|
||||
this.manager = new FileUUIDManager(ttl);
|
||||
}
|
||||
|
||||
public encodeModelId(peer: Peer, modelId: string, fileId: string, fileUUID: string = "", endString: string = "", customUUID?: string): string {
|
||||
return this.manager.encode({ peer, modelId, fileId, fileUUID }, endString, customUUID);
|
||||
}
|
||||
|
||||
public decodeModelId(uuid: string): FileUUIDData | undefined {
|
||||
return this.manager.decode(uuid);
|
||||
}
|
||||
|
||||
public encode(peer: Peer, msgId: string, elementId: string, fileUUID: string = "", customUUID?: string): string {
|
||||
return this.manager.encode({ peer, msgId, elementId, fileUUID }, "", customUUID);
|
||||
}
|
||||
|
||||
public decode(uuid: string): FileUUIDData | undefined {
|
||||
return this.manager.decode(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
export const FileNapCatOneBotUUID = new FileNapCatOneBotUUIDWrap();
|
@@ -1,7 +1,7 @@
|
||||
import path from 'node:path';
|
||||
import fs from 'fs';
|
||||
import os from 'node:os';
|
||||
import { Peer, QQLevel } from '@/core';
|
||||
import { QQLevel } from '@/core';
|
||||
|
||||
export async function solveProblem<T extends (...arg: any[]) => any>(func: T, ...args: Parameters<T>): Promise<ReturnType<T> | undefined> {
|
||||
return new Promise<ReturnType<T> | undefined>((resolve) => {
|
||||
@@ -24,81 +24,6 @@ export async function solveAsyncProblem<T extends (...args: any[]) => Promise<an
|
||||
});
|
||||
}
|
||||
|
||||
export class FileNapCatOneBotUUID {
|
||||
static encodeModelId(peer: Peer, modelId: string, fileId: string, fileUUID: string = "", endString: string = ""): string {
|
||||
const data = `NapCatOneBot|ModelIdFile|${peer.chatType}|${peer.peerUid}|${modelId}|${fileId}|${fileUUID}`;
|
||||
//前四个字节塞data长度
|
||||
const length = Buffer.alloc(4 + data.length);
|
||||
length.writeUInt32BE(data.length * 2, 0);//储存data的hex长度
|
||||
length.write(data, 4);
|
||||
return length.toString('hex') + endString;
|
||||
}
|
||||
|
||||
static decodeModelId(uuid: string): undefined | {
|
||||
peer: Peer,
|
||||
modelId: string,
|
||||
fileId: string,
|
||||
fileUUID?: string
|
||||
} {
|
||||
//前四个字节是data长度
|
||||
const length = Buffer.from(uuid.slice(0, 8), 'hex').readUInt32BE(0);
|
||||
//根据length计算需要读取的长度
|
||||
const dataId = uuid.slice(8, 8 + length);
|
||||
//hex还原为string
|
||||
const realData = Buffer.from(dataId, 'hex').toString();
|
||||
if (!realData.startsWith('NapCatOneBot|ModelIdFile|')) return undefined;
|
||||
const data = realData.split('|');
|
||||
if (data.length < 6) return undefined; // compatibility requirement
|
||||
const [, , chatType, peerUid, modelId, fileId, fileUUID = undefined] = data;
|
||||
return {
|
||||
peer: {
|
||||
chatType: +chatType,
|
||||
peerUid: peerUid,
|
||||
},
|
||||
modelId,
|
||||
fileId,
|
||||
fileUUID
|
||||
};
|
||||
}
|
||||
|
||||
static encode(peer: Peer, msgId: string, elementId: string, fileUUID: string = "", endString: string = ""): string {
|
||||
const data = `NapCatOneBot|MsgFile|${peer.chatType}|${peer.peerUid}|${msgId}|${elementId}|${fileUUID}`;
|
||||
//前四个字节塞data长度
|
||||
//一个字节8位 一个ascii字符1字节 一个hex字符4位 表示一个ascii字符需要两个hex字符
|
||||
const length = Buffer.alloc(4 + data.length);
|
||||
length.writeUInt32BE(data.length * 2, 0);
|
||||
length.write(data, 4);
|
||||
return length.toString('hex') + endString;
|
||||
}
|
||||
|
||||
static decode(uuid: string): undefined | {
|
||||
peer: Peer,
|
||||
msgId: string,
|
||||
elementId: string,
|
||||
fileUUID?: string
|
||||
} {
|
||||
//前四个字节是data长度
|
||||
const length = Buffer.from(uuid.slice(0, 8), 'hex').readUInt32BE(0);
|
||||
//根据length计算需要读取的长度
|
||||
const dataId = uuid.slice(8, 8 + length);
|
||||
//hex还原为string
|
||||
const realData = Buffer.from(dataId, 'hex').toString();
|
||||
if (!realData.startsWith('NapCatOneBot|MsgFile|')) return undefined;
|
||||
const data = realData.split('|');
|
||||
if (data.length < 6) return undefined; // compatibility requirement
|
||||
const [, , chatType, peerUid, msgId, elementId, fileUUID = undefined] = data;
|
||||
return {
|
||||
peer: {
|
||||
chatType: +chatType,
|
||||
peerUid: peerUid,
|
||||
},
|
||||
msgId,
|
||||
elementId,
|
||||
fileUUID
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
@@ -78,7 +78,7 @@ class MessageUniqueWrapper {
|
||||
private readonly msgDataMap: LimitedHashTable<string, number>;
|
||||
private readonly msgIdMap: LimitedHashTable<string, number>;
|
||||
|
||||
constructor(maxMap: number = 1000) {
|
||||
constructor(maxMap: number = 5000) {
|
||||
this.msgIdMap = new LimitedHashTable<string, number>(maxMap);
|
||||
this.msgDataMap = new LimitedHashTable<string, number>(maxMap);
|
||||
}
|
||||
|
@@ -1,165 +0,0 @@
|
||||
import https from 'node:https';
|
||||
import { napCatVersion } from './version';
|
||||
import os from 'node:os';
|
||||
|
||||
export class UmamiTraceCore {
|
||||
napcatVersion = napCatVersion;
|
||||
qqversion = '1.0.0';
|
||||
guid = 'default-user';
|
||||
heartbeatInterval: NodeJS.Timeout | null = null;
|
||||
website: string = '596cbbb2-1740-4373-a807-cf3d0637bfa7';
|
||||
referrer: string = 'https://trace.napneko.icu/';
|
||||
hostname: string = 'trace.napneko.icu';
|
||||
ua: string = '';
|
||||
workname: string = 'default';
|
||||
bootTime = Date.now();
|
||||
cache: string = '';
|
||||
platform = process.platform;
|
||||
|
||||
init(qqversion: string, guid: string, workname: string) {
|
||||
this.qqversion = qqversion;
|
||||
this.workname = workname;
|
||||
const UaList = {
|
||||
linux: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Ubuntu/11.10 Chromium/27.0.1453.93 Chrome/27.0.1453.93 Safari/537.36',
|
||||
win32: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.2128.93 Safari/537.36',
|
||||
darwin: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
|
||||
};
|
||||
|
||||
try {
|
||||
if (this.platform === 'win32') {
|
||||
const ntVersion = os.release();
|
||||
UaList.win32 = `Mozilla/5.0 (Windows NT ${ntVersion}; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.2128.93 Safari/537.36`;
|
||||
} else if (this.platform === 'darwin') {
|
||||
const macVersion = os.release();
|
||||
UaList.darwin = `Mozilla/5.0 (Macintosh; Intel Mac OS X ${macVersion}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36`;
|
||||
}
|
||||
} catch (error) {
|
||||
this.ua = UaList.win32;
|
||||
}
|
||||
|
||||
this.ua = UaList[this.platform as keyof typeof UaList] || UaList.win32;
|
||||
|
||||
this.identifyUser(guid);
|
||||
this.startHeartbeat();
|
||||
}
|
||||
|
||||
identifyUser(guid: string) {
|
||||
this.guid = guid;
|
||||
const data = {
|
||||
napcat_version: this.napcatVersion,
|
||||
qq_version: this.qqversion,
|
||||
napcat_working: this.workname,
|
||||
device_guid: this.guid,
|
||||
device_platform: this.platform,
|
||||
device_arch: os.arch(),
|
||||
boot_time: new Date(this.bootTime + 8 * 60 * 60 * 1000).toISOString().replace('T', ' ').substring(0, 19),
|
||||
sys_time: new Date(Date.now() - os.uptime() * 1000 + 8 * 60 * 60 * 1000).toISOString().replace('T', ' ').substring(0, 19),
|
||||
};
|
||||
this.sendEvent(
|
||||
{
|
||||
website: this.website,
|
||||
hostname: this.hostname,
|
||||
referrer: this.referrer,
|
||||
title: 'NapCat ' + this.napcatVersion,
|
||||
url: `/${this.qqversion}/${this.napcatVersion}/${this.workname}/identify`,
|
||||
},
|
||||
data,
|
||||
'identify'
|
||||
);
|
||||
}
|
||||
|
||||
sendEvent(event: string | object, data?: object, type = 'event') {
|
||||
const env = process.env;
|
||||
const language = env.LANG || env.LANGUAGE || env.LC_ALL || env.LC_MESSAGES;
|
||||
const payload = {
|
||||
...(typeof event === 'string' ? { event } : event),
|
||||
hostname: this.hostname,
|
||||
referrer: this.referrer,
|
||||
website: this.website,
|
||||
language: language || 'en-US',
|
||||
screen: '1920x1080',
|
||||
data: {
|
||||
...data,
|
||||
},
|
||||
};
|
||||
this.sendRequest(payload, type);
|
||||
}
|
||||
|
||||
sendTrace(eventName: string, data: string = '') {
|
||||
const payload = {
|
||||
website: this.website,
|
||||
hostname: this.hostname,
|
||||
title: 'NapCat ' + this.napcatVersion,
|
||||
url: `/${this.qqversion}/${this.napcatVersion}/${this.workname}/${eventName}` + (data ? `/${data}` : ''),
|
||||
referrer: this.referrer,
|
||||
};
|
||||
this.sendRequest(payload);
|
||||
}
|
||||
|
||||
sendRequest(payload: object, type = 'event') {
|
||||
const options = {
|
||||
hostname: '104.19.42.72', // 固定 IP 或者从 hostUrl 获取
|
||||
port: 443,
|
||||
path: '/api/send',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Host": "umami.napneko.icu",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": this.ua,
|
||||
...(this.cache ? { 'x-umami-cache': this.filterInvalidChars(this.cache) } : {})
|
||||
}
|
||||
};
|
||||
try {
|
||||
const request = https.request(options, (res) => {
|
||||
let responseData = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
responseData += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
if (!this.cache) {
|
||||
this.cache = responseData;
|
||||
console.log('Umami cache:', this.cache);
|
||||
}
|
||||
});
|
||||
|
||||
res.on('error', (error) => {
|
||||
});
|
||||
});
|
||||
|
||||
request.on('error', (error) => {
|
||||
});
|
||||
|
||||
request.write(JSON.stringify({ type, payload }));
|
||||
request.end();
|
||||
} catch (error) {
|
||||
}
|
||||
}
|
||||
|
||||
filterInvalidChars(value: string): string {
|
||||
return value.replace(/[^\x00-\x7F]/g, '');
|
||||
}
|
||||
|
||||
startHeartbeat() {
|
||||
if (this.heartbeatInterval) {
|
||||
clearInterval(this.heartbeatInterval);
|
||||
}
|
||||
this.heartbeatInterval = setInterval(() => {
|
||||
this.sendEvent({
|
||||
name: 'heartbeat',
|
||||
title: 'NapCat ' + this.napcatVersion,
|
||||
url: `/${this.qqversion}/${this.napcatVersion}/${this.workname}/heartbeat`,
|
||||
});
|
||||
}, 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
stopHeartbeat() {
|
||||
if (this.heartbeatInterval) {
|
||||
clearInterval(this.heartbeatInterval);
|
||||
this.heartbeatInterval = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const UmamiTrace = new UmamiTraceCore();
|
@@ -1 +1 @@
|
||||
export const napCatVersion = '4.2.68';
|
||||
export const napCatVersion = '4.3.6';
|
||||
|
@@ -462,7 +462,7 @@ export class NTQQFileApi {
|
||||
rkeyData.private_rkey = tempRkeyData.private_rkey;
|
||||
rkeyData.online_rkey = tempRkeyData.expired_time > Date.now() / 1000;
|
||||
} catch (e) {
|
||||
this.context.logger.logError('获取rkey失败 Fallback Old Mode', e);
|
||||
this.context.logger.logDebug('获取rkey失败 Fallback Old Mode', e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -18,7 +18,6 @@ export class NTQQGroupApi {
|
||||
context: InstanceContext;
|
||||
core: NapCatCore;
|
||||
groupMemberCache: Map<string, Map<string, GroupMember>> = new Map<string, Map<string, GroupMember>>();
|
||||
groupMemberCacheEvent: Map<string, boolean> = new Map<string, boolean>();
|
||||
essenceLRU = new LimitedHashTable<number, string>(1000);
|
||||
|
||||
constructor(context: InstanceContext, core: NapCatCore) {
|
||||
@@ -128,15 +127,12 @@ export class NTQQGroupApi {
|
||||
}
|
||||
|
||||
async refreshGroupMemberCache(groupCode: string, isWait = true) {
|
||||
this.groupMemberCacheEvent.set(groupCode, true);
|
||||
const updateCache = async () => {
|
||||
try {
|
||||
const members = await this.getGroupMemberAll(groupCode, true);
|
||||
this.groupMemberCache.set(groupCode, members.result.infos);
|
||||
} catch (e) {
|
||||
this.context.logger.logError(`刷新群成员缓存失败, 群号: ${groupCode}, 错误: ${e}`);
|
||||
} finally {
|
||||
this.groupMemberCacheEvent.set(groupCode, false);
|
||||
}
|
||||
};
|
||||
|
||||
|
16
src/core/external/appid.json
vendored
16
src/core/external/appid.json
vendored
@@ -162,5 +162,21 @@
|
||||
"9.9.17-31219": {
|
||||
"appid": 537266450,
|
||||
"qua": "V1_WIN_NQ_9.9.17_31219_GW_B"
|
||||
},
|
||||
"9.9.17-31245": {
|
||||
"appid": 537266450,
|
||||
"qua": "V1_WIN_NQ_9.9.17_31245_GW_B"
|
||||
},
|
||||
"3.2.15-31245": {
|
||||
"appid": 537266485,
|
||||
"qua": "V1_LNX_NQ_3.2.15_31245_GW_B"
|
||||
},
|
||||
"6.9.63-31245": {
|
||||
"appid": 537266474,
|
||||
"qua": "V1_MAC_NQ_6.9.63_31245_GW_B"
|
||||
},
|
||||
"9.9.17-31363": {
|
||||
"appid": 537266500,
|
||||
"qua": "V1_WIN_NQ_9.9.17_31363_GW_B"
|
||||
}
|
||||
}
|
24
src/core/external/offset.json
vendored
24
src/core/external/offset.json
vendored
@@ -206,5 +206,29 @@
|
||||
"9.9.17-31219-x64": {
|
||||
"send": "39C1350",
|
||||
"recv": "39C5784"
|
||||
},
|
||||
"9.9.17-31245-x64": {
|
||||
"send": "39C1350",
|
||||
"recv": "39C5784"
|
||||
},
|
||||
"6.9.63.31245-x64": {
|
||||
"send": "4720A40",
|
||||
"recv": "47232AC"
|
||||
},
|
||||
"6.9.63-31245-arm64": {
|
||||
"send": "41DCBD8",
|
||||
"recv": "422D4E8"
|
||||
},
|
||||
"3.2.15-31245-x64": {
|
||||
"send": "A550F80",
|
||||
"recv": "A554880"
|
||||
},
|
||||
"3.2.15-31245-arm64": {
|
||||
"send": "71BEBB8",
|
||||
"recv": "71C23F0"
|
||||
},
|
||||
"9.9.17-31363-x64": {
|
||||
"send": "39C1910",
|
||||
"recv": "39C5d44"
|
||||
}
|
||||
}
|
@@ -15,6 +15,10 @@ export class RkeyManager {
|
||||
private_rkey: '',
|
||||
expired_time: 0,
|
||||
};
|
||||
private failureCount: number = 0;
|
||||
private lastFailureTimestamp: number = 0;
|
||||
private readonly FAILURE_LIMIT: number = 8;
|
||||
private readonly ONE_DAY: number = 24 * 60 * 60 * 1000;
|
||||
|
||||
constructor(serverUrl: string[], logger: LogWrapper) {
|
||||
this.logger = logger;
|
||||
@@ -22,11 +26,21 @@ export class RkeyManager {
|
||||
}
|
||||
|
||||
async getRkey() {
|
||||
const now = new Date().getTime();
|
||||
if (now - this.lastFailureTimestamp > this.ONE_DAY) {
|
||||
this.failureCount = 0; // 重置失败计数器
|
||||
}
|
||||
|
||||
if (this.failureCount >= this.FAILURE_LIMIT) {
|
||||
this.logger.logError(`[Rkey] 服务存在异常, 图片使用FallBack机制`);
|
||||
throw new Error('获取rkey失败次数过多,请稍后再试');
|
||||
}
|
||||
|
||||
if (this.isExpired()) {
|
||||
try {
|
||||
await this.refreshRkey();
|
||||
} catch (e) {
|
||||
throw new Error(`获取rkey失败: ${e}`);//外抛
|
||||
throw new Error(`${e}`);//外抛
|
||||
}
|
||||
}
|
||||
return this.rkeyData;
|
||||
@@ -34,7 +48,6 @@ export class RkeyManager {
|
||||
|
||||
isExpired(): boolean {
|
||||
const now = new Date().getTime() / 1000;
|
||||
// console.log(`now: ${now}, expired_time: ${this.rkeyData.expired_time}`);
|
||||
return now > this.rkeyData.expired_time;
|
||||
}
|
||||
|
||||
@@ -48,14 +61,17 @@ export class RkeyManager {
|
||||
private_rkey: temp.private_rkey.slice(6),
|
||||
expired_time: temp.expired_time
|
||||
};
|
||||
this.failureCount = 0;
|
||||
return;
|
||||
} catch (e) {
|
||||
this.logger.logError(`[Rkey] Get Rkey ${url} Error `, e);
|
||||
this.logger.logError(`[Rkey] 异常服务 ${url} 异常 / `, e);
|
||||
this.failureCount++;
|
||||
this.lastFailureTimestamp = new Date().getTime();
|
||||
//是否为最后一个url
|
||||
if (url === this.serverUrl[this.serverUrl.length - 1]) {
|
||||
throw new Error(`获取rkey失败: ${e}`);//外抛
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,8 +1,9 @@
|
||||
import { LRUCache } from "@/common/lru-cache";
|
||||
import crypto, { createHash } from "crypto";
|
||||
import { PacketContext } from "@/core/packet/context/packetContext";
|
||||
import { OidbPacket, PacketHexStr } from "@/core/packet/transformer/base";
|
||||
import { LogStack } from "@/core/packet/context/clientContext";
|
||||
import { NapCoreContext } from "@/core/packet/context/napCoreContext";
|
||||
import { PacketLogger } from "@/core/packet/context/loggerContext";
|
||||
|
||||
export interface RecvPacket {
|
||||
type: string, // 仅recv
|
||||
@@ -27,13 +28,15 @@ function randText(len: number): string {
|
||||
|
||||
|
||||
export abstract class IPacketClient {
|
||||
protected readonly context: PacketContext;
|
||||
protected readonly napcore: NapCoreContext;
|
||||
protected readonly logger: PacketLogger;
|
||||
protected readonly cb = new LRUCache<string, (json: RecvPacketData) => Promise<void>>(500); // trace_id-type callback
|
||||
logStack: LogStack;
|
||||
available: boolean = false;
|
||||
|
||||
protected constructor(context: PacketContext, logStack: LogStack) {
|
||||
this.context = context;
|
||||
protected constructor(napCore: NapCoreContext, logger: PacketLogger, logStack: LogStack) {
|
||||
this.napcore = napCore;
|
||||
this.logger = logger;
|
||||
this.logStack = logStack;
|
||||
}
|
||||
|
||||
@@ -81,7 +84,7 @@ export abstract class IPacketClient {
|
||||
const md5 = crypto.createHash('md5').update(data).digest('hex');
|
||||
const trace_id = (randText(4) + md5 + data).slice(0, data.length / 2);
|
||||
return this.sendCommand(cmd, data, trace_id, rsp, 20000, async () => {
|
||||
await this.context.napcore.sendSsoCmdReqByContend(cmd, trace_id);
|
||||
await this.napcore.sendSsoCmdReqByContend(cmd, trace_id);
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -5,8 +5,9 @@ import fs from "fs";
|
||||
import { IPacketClient } from "@/core/packet/client/baseClient";
|
||||
import { constants } from "node:os";
|
||||
import { LRUCache } from "@/common/lru-cache";
|
||||
import { PacketContext } from "@/core/packet/context/packetContext";
|
||||
import { LogStack } from "@/core/packet/context/clientContext";
|
||||
import { NapCoreContext } from "@/core/packet/context/napCoreContext";
|
||||
import { PacketLogger } from "@/core/packet/context/loggerContext";
|
||||
|
||||
// 0 send 1 recv
|
||||
export interface NativePacketExportType {
|
||||
@@ -19,8 +20,8 @@ export class NativePacketClient extends IPacketClient {
|
||||
private readonly MoeHooExport: { exports: NativePacketExportType } = { exports: {} };
|
||||
private readonly sendEvent = new LRUCache<number, string>(500); // seq->trace_id
|
||||
|
||||
constructor(context: PacketContext, logStack: LogStack) {
|
||||
super(context, logStack);
|
||||
constructor(napCore: NapCoreContext, logger: PacketLogger, logStack: LogStack) {
|
||||
super(napCore, logger, logStack);
|
||||
}
|
||||
|
||||
check(): boolean {
|
||||
|
@@ -1,7 +1,8 @@
|
||||
import { Data, WebSocket, ErrorEvent } from "ws";
|
||||
import { IPacketClient, RecvPacket } from "@/core/packet/client/baseClient";
|
||||
import { PacketContext } from "@/core/packet/context/packetContext";
|
||||
import { LogStack } from "@/core/packet/context/clientContext";
|
||||
import { NapCoreContext } from "@/core/packet/context/napCoreContext";
|
||||
import { PacketLogger } from "@/core/packet/context/loggerContext";
|
||||
|
||||
export class WsPacketClient extends IPacketClient {
|
||||
private websocket: WebSocket | null = null;
|
||||
@@ -13,15 +14,15 @@ export class WsPacketClient extends IPacketClient {
|
||||
private isInitialized: boolean = false;
|
||||
private initPayload: { pid: number, recv: string, send: string } | null = null;
|
||||
|
||||
constructor(context: PacketContext, logStack: LogStack) {
|
||||
super(context, logStack);
|
||||
this.clientUrl = this.context.napcore.config.packetServer
|
||||
? this.clientUrlWrap(this.context.napcore.config.packetServer)
|
||||
constructor(napCore: NapCoreContext, logger: PacketLogger, logStack: LogStack) {
|
||||
super(napCore, logger, logStack);
|
||||
this.clientUrl = this.napcore.config.packetServer
|
||||
? this.clientUrlWrap(this.napcore.config.packetServer)
|
||||
: this.clientUrlWrap('127.0.0.1:8083');
|
||||
}
|
||||
|
||||
check(): boolean {
|
||||
if (!this.context.napcore.config.packetServer) {
|
||||
if (!this.napcore.config.packetServer) {
|
||||
this.logStack.pushLogWarn(`wsPacketClient 未配置服务器地址`);
|
||||
return false;
|
||||
}
|
||||
@@ -67,7 +68,7 @@ export class WsPacketClient extends IPacketClient {
|
||||
this.websocket.onopen = () => {
|
||||
this.available = true;
|
||||
this.reconnectAttempts = 0;
|
||||
this.context.logger.info(`wsPacketClient 已连接到 ${this.clientUrl}`);
|
||||
this.logger.info(`wsPacketClient 已连接到 ${this.clientUrl}`);
|
||||
if (!this.isInitialized && this.initPayload) {
|
||||
this.websocket!.send(JSON.stringify({
|
||||
action: 'init',
|
||||
@@ -79,15 +80,15 @@ export class WsPacketClient extends IPacketClient {
|
||||
};
|
||||
this.websocket.onclose = () => {
|
||||
this.available = false;
|
||||
this.context.logger.warn(`WebSocket 连接关闭,尝试重连...`);
|
||||
this.logger.warn(`WebSocket 连接关闭,尝试重连...`);
|
||||
reject(new Error('WebSocket 连接关闭'));
|
||||
};
|
||||
this.websocket.onmessage = (event) => this.handleMessage(event.data).catch(err => {
|
||||
this.context.logger.error(`处理消息时出错: ${err}`);
|
||||
this.logger.error(`处理消息时出错: ${err}`);
|
||||
});
|
||||
this.websocket.onerror = (event: ErrorEvent) => {
|
||||
this.available = false;
|
||||
this.context.logger.error(`WebSocket 出错: ${event.message}`);
|
||||
this.logger.error(`WebSocket 出错: ${event.message}`);
|
||||
this.websocket?.close();
|
||||
reject(new Error(`WebSocket 出错: ${event.message}`));
|
||||
};
|
||||
@@ -106,7 +107,7 @@ export class WsPacketClient extends IPacketClient {
|
||||
const event = this.cb.get(`${trace_id_md5}${action}`);
|
||||
if (event) await event(json.data);
|
||||
} catch (error) {
|
||||
this.context.logger.error(`解析ws消息时出错: ${(error as Error).message}`);
|
||||
this.logger.error(`解析ws消息时出错: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -23,9 +23,7 @@ export class PacketClientSession {
|
||||
get operation() {
|
||||
return this.context.operation;
|
||||
}
|
||||
get client() {
|
||||
return this.context.client;
|
||||
}
|
||||
|
||||
// TODO: global message element adapter (?
|
||||
get msgConverter() {
|
||||
return this.context.msgConverter;
|
||||
|
@@ -1,17 +1,17 @@
|
||||
import { PacketContext } from "@/core/packet/context/packetContext";
|
||||
import { IPacketClient } from "@/core/packet/client/baseClient";
|
||||
import { NativePacketClient } from "@/core/packet/client/nativeClient";
|
||||
import { WsPacketClient } from "@/core/packet/client/wsClient";
|
||||
import { OidbPacket } from "@/core/packet/transformer/base";
|
||||
import { PacketLogger } from "@/core/packet/context/loggerContext";
|
||||
import { NapCoreContext } from "@/core/packet/context/napCoreContext";
|
||||
|
||||
type clientPriority = {
|
||||
[key: number]: (context: PacketContext, logStack: LogStack) => IPacketClient;
|
||||
[key: number]: (napCore: NapCoreContext, logger: PacketLogger, logStack: LogStack) => IPacketClient;
|
||||
}
|
||||
|
||||
const clientPriority: clientPriority = {
|
||||
10: (context: PacketContext, logStack: LogStack) => new NativePacketClient(context, logStack),
|
||||
1: (context: PacketContext, logStack: LogStack) => new WsPacketClient(context, logStack),
|
||||
10: (napCore: NapCoreContext, logger: PacketLogger, logStack: LogStack) => new NativePacketClient(napCore, logger, logStack),
|
||||
1: (napCore: NapCoreContext, logger: PacketLogger, logStack: LogStack) => new WsPacketClient(napCore, logger, logStack),
|
||||
};
|
||||
|
||||
export class LogStack {
|
||||
@@ -51,13 +51,15 @@ export class LogStack {
|
||||
}
|
||||
|
||||
export class PacketClientContext {
|
||||
private readonly context: PacketContext;
|
||||
private readonly napCore: NapCoreContext;
|
||||
private readonly logger: PacketLogger;
|
||||
private readonly logStack: LogStack;
|
||||
private readonly _client: IPacketClient;
|
||||
|
||||
constructor(context: PacketContext) {
|
||||
this.context = context;
|
||||
this.logStack = new LogStack(context.logger);
|
||||
constructor(napCore: NapCoreContext, logger: PacketLogger) {
|
||||
this.napCore = napCore;
|
||||
this.logger = logger;
|
||||
this.logStack = new LogStack(logger);
|
||||
this._client = this.newClient();
|
||||
}
|
||||
|
||||
@@ -79,23 +81,23 @@ export class PacketClientContext {
|
||||
}
|
||||
|
||||
private newClient(): IPacketClient {
|
||||
const prefer = this.context.napcore.config.packetBackend;
|
||||
const prefer = this.napCore.config.packetBackend;
|
||||
let client: IPacketClient | null;
|
||||
switch (prefer) {
|
||||
case "native":
|
||||
this.context.logger.info("使用指定的 NativePacketClient 作为后端");
|
||||
client = new NativePacketClient(this.context, this.logStack);
|
||||
this.logger.info("使用指定的 NativePacketClient 作为后端");
|
||||
client = new NativePacketClient(this.napCore, this.logger, this.logStack);
|
||||
break;
|
||||
case "frida":
|
||||
this.context.logger.info("[Core] [Packet] 使用指定的 FridaPacketClient 作为后端");
|
||||
client = new WsPacketClient(this.context, this.logStack);
|
||||
this.logger.info("[Core] [Packet] 使用指定的 FridaPacketClient 作为后端");
|
||||
client = new WsPacketClient(this.napCore, this.logger, this.logStack);
|
||||
break;
|
||||
case "auto":
|
||||
case undefined:
|
||||
client = this.judgeClient();
|
||||
break;
|
||||
default:
|
||||
this.context.logger.error(`未知的PacketBackend ${prefer},请检查配置文件!`);
|
||||
this.logger.error(`未知的PacketBackend ${prefer},请检查配置文件!`);
|
||||
client = null;
|
||||
}
|
||||
if (!client?.check()) {
|
||||
@@ -110,7 +112,7 @@ export class PacketClientContext {
|
||||
private judgeClient(): IPacketClient {
|
||||
const sortedClients = Object.entries(clientPriority)
|
||||
.map(([priority, clientFactory]) => {
|
||||
const client = clientFactory(this.context, this.logStack);
|
||||
const client = clientFactory(this.napCore, this.logger, this.logStack);
|
||||
const score = +priority * +client.check();
|
||||
return { client, score };
|
||||
})
|
||||
@@ -120,7 +122,7 @@ export class PacketClientContext {
|
||||
if (!selectedClient) {
|
||||
throw new Error("[Core] [Packet] 无可用的后端,NapCat.Packet将不会加载!");
|
||||
}
|
||||
this.context.logger.info(`自动选择 ${selectedClient.constructor.name} 作为后端`);
|
||||
this.logger.info(`自动选择 ${selectedClient.constructor.name} 作为后端`);
|
||||
return selectedClient;
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,12 @@
|
||||
import { LogLevel, LogWrapper } from "@/common/log";
|
||||
import { PacketContext } from "@/core/packet/context/packetContext";
|
||||
import { NapCoreContext } from "@/core/packet/context/napCoreContext";
|
||||
|
||||
// TODO: check bind?
|
||||
export class PacketLogger {
|
||||
private readonly napLogger: LogWrapper;
|
||||
|
||||
constructor(context: PacketContext) {
|
||||
this.napLogger = context.napcore.logger;
|
||||
constructor(napcore: NapCoreContext) {
|
||||
this.napLogger = napcore.logger;
|
||||
}
|
||||
|
||||
private _log(level: LogLevel, ...msg: any[]): void {
|
||||
|
@@ -13,13 +13,20 @@ import { MiniAppRawData, MiniAppReqParams } from "@/core/packet/entities/miniApp
|
||||
import { AIVoiceChatType } from "@/core/packet/entities/aiChat";
|
||||
import { NapProtoDecodeStructType, NapProtoEncodeStructType } from "@napneko/nap-proto-core";
|
||||
import { IndexNode, MsgInfo } from "@/core/packet/transformer/proto";
|
||||
import { OidbPacket } from "@/core/packet/transformer/base";
|
||||
import { ImageOcrResult } from "@/core/packet/entities/ocrResult";
|
||||
|
||||
export class PacketOperationContext {
|
||||
private readonly context: PacketContext;
|
||||
|
||||
constructor(context: PacketContext) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
async sendPacket<T extends boolean = false>(pkt: OidbPacket, rsp?: T): Promise<T extends true ? Buffer : void> {
|
||||
return await this.context.client.sendOidbPacket(pkt, rsp);
|
||||
}
|
||||
|
||||
async GroupPoke(groupUin: number, uin: number) {
|
||||
const req = trans.SendPoke.build(uin, groupUin);
|
||||
await this.context.client.sendOidbPacket(req);
|
||||
@@ -90,6 +97,46 @@ export class PacketOperationContext {
|
||||
});
|
||||
}
|
||||
|
||||
async UploadImage(img: PacketMsgPicElement) {
|
||||
await this.context.highway.uploadImage({
|
||||
chatType: ChatType.KCHATTYPEC2C,
|
||||
peerUid: this.context.napcore.basicInfo.uid
|
||||
}, img);
|
||||
const index = img.msgInfo?.msgInfoBody?.at(0)?.index;
|
||||
if (!index) {
|
||||
throw new Error('img.msgInfo?.msgInfoBody![0].index! is undefined');
|
||||
}
|
||||
return await this.GetImageUrl(this.context.napcore.basicInfo.uid, index);
|
||||
}
|
||||
|
||||
async GetImageUrl(selfUid: string, node: NapProtoEncodeStructType<typeof IndexNode>) {
|
||||
const req = trans.DownloadImage.build(selfUid, node);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const res = trans.DownloadImage.parse(resp);
|
||||
return `https://${res.download.info.domain}${res.download.info.urlPath}${res.download.rKeyParam}`;
|
||||
}
|
||||
|
||||
async ImageOCR(imgUrl: string) {
|
||||
const req = trans.ImageOCR.build(imgUrl);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const res = trans.ImageOCR.parse(resp);
|
||||
return {
|
||||
texts: res.ocrRspBody.textDetections.map((item) => {
|
||||
return {
|
||||
text: item.detectedText,
|
||||
confidence: item.confidence,
|
||||
coordinates: item.polygon.coordinates.map((c) => {
|
||||
return {
|
||||
x: c.x,
|
||||
y: c.y
|
||||
};
|
||||
}),
|
||||
};
|
||||
}),
|
||||
language: res.ocrRspBody.language
|
||||
} as ImageOcrResult;
|
||||
}
|
||||
|
||||
async UploadForwardMsg(msg: PacketMsg[], groupUin: number = 0) {
|
||||
await this.UploadResources(msg, groupUin);
|
||||
const req = trans.UploadForwardMsg.build(this.context.napcore.basicInfo.uid, msg, groupUin);
|
||||
|
@@ -7,19 +7,19 @@ import { PacketOperationContext } from "@/core/packet/context/operationContext";
|
||||
import { PacketMsgConverter } from "@/core/packet/message/converter";
|
||||
|
||||
export class PacketContext {
|
||||
readonly msgConverter: PacketMsgConverter;
|
||||
readonly napcore: NapCoreContext;
|
||||
readonly logger: PacketLogger;
|
||||
readonly client: PacketClientContext;
|
||||
readonly highway: PacketHighwayContext;
|
||||
readonly msgConverter: PacketMsgConverter;
|
||||
readonly operation: PacketOperationContext;
|
||||
|
||||
constructor(core: NapCatCore) {
|
||||
this.napcore = new NapCoreContext(core);
|
||||
this.logger = new PacketLogger(this);
|
||||
this.client = new PacketClientContext(this);
|
||||
this.highway = new PacketHighwayContext(this);
|
||||
this.msgConverter = new PacketMsgConverter();
|
||||
this.napcore = new NapCoreContext(core);
|
||||
this.logger = new PacketLogger(this.napcore);
|
||||
this.client = new PacketClientContext(this.napcore, this.logger);
|
||||
this.highway = new PacketHighwayContext(this.napcore, this.logger, this.client);
|
||||
this.operation = new PacketOperationContext(this);
|
||||
}
|
||||
}
|
||||
|
15
src/core/packet/entities/ocrResult.ts
Normal file
15
src/core/packet/entities/ocrResult.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export interface ImageOcrResult {
|
||||
texts: TextDetection[];
|
||||
language: string;
|
||||
}
|
||||
|
||||
export interface TextDetection {
|
||||
text: string;
|
||||
confidence: number;
|
||||
coordinates: Coordinate[];
|
||||
}
|
||||
|
||||
export interface Coordinate {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
@@ -1,5 +1,4 @@
|
||||
import { PacketHighwayClient } from "@/core/packet/highway/client";
|
||||
import { PacketContext } from "@/core/packet/context/packetContext";
|
||||
import { PacketLogger } from "@/core/packet/context/loggerContext";
|
||||
import FetchSessionKey from "@/core/packet/transformer/highway/FetchSessionKey";
|
||||
import { int32ip2str, oidbIpv4s2HighwayIpv4s } from "@/core/packet/highway/utils";
|
||||
@@ -16,6 +15,8 @@ import { NapProtoMsg } from "@napneko/nap-proto-core";
|
||||
import * as proto from "@/core/packet/transformer/proto";
|
||||
import * as trans from "@/core/packet/transformer";
|
||||
import fs from "fs";
|
||||
import { NapCoreContext } from "@/core/packet/context/napCoreContext";
|
||||
import { PacketClientContext } from "@/core/packet/context/clientContext";
|
||||
|
||||
export const BlockSize = 1024 * 1024;
|
||||
|
||||
@@ -33,23 +34,25 @@ export interface PacketHighwaySig {
|
||||
}
|
||||
|
||||
export class PacketHighwayContext {
|
||||
private readonly context: PacketContext;
|
||||
private readonly napcore: NapCoreContext;
|
||||
private readonly client: PacketClientContext;
|
||||
protected sig: PacketHighwaySig;
|
||||
protected logger: PacketLogger;
|
||||
protected hwClient: PacketHighwayClient;
|
||||
private cachedPrepareReq: Promise<void> | null = null;
|
||||
|
||||
constructor(context: PacketContext) {
|
||||
this.context = context;
|
||||
constructor(napcore: NapCoreContext, logger: PacketLogger, client: PacketClientContext) {
|
||||
this.napcore = napcore;
|
||||
this.client = client;
|
||||
this.sig = {
|
||||
uin: String(context.napcore.basicInfo.uin),
|
||||
uid: context.napcore.basicInfo.uid,
|
||||
uin: String(this.napcore.basicInfo.uin),
|
||||
uid: this.napcore.basicInfo.uid,
|
||||
sigSession: null,
|
||||
sessionKey: null,
|
||||
serverAddr: [],
|
||||
};
|
||||
this.logger = context.logger;
|
||||
this.hwClient = new PacketHighwayClient(this.sig, context.logger);
|
||||
this.logger = logger;
|
||||
this.hwClient = new PacketHighwayClient(this.sig, this.logger);
|
||||
}
|
||||
|
||||
private async checkAvailable() {
|
||||
@@ -66,7 +69,7 @@ export class PacketHighwayContext {
|
||||
private async prepareUpload(): Promise<void> {
|
||||
this.logger.debug('[Highway] on prepareUpload!');
|
||||
const packet = FetchSessionKey.build();
|
||||
const req = await this.context.client.sendOidbPacket(packet, true);
|
||||
const req = await this.client.sendOidbPacket(packet, true);
|
||||
const rsp = FetchSessionKey.parse(req);
|
||||
this.sig.sigSession = rsp.httpConn.sigSession;
|
||||
this.sig.sessionKey = rsp.httpConn.sessionKey;
|
||||
@@ -136,7 +139,7 @@ export class PacketHighwayContext {
|
||||
private async uploadGroupImage(groupUin: number, img: PacketMsgPicElement): Promise<void> {
|
||||
img.sha1 = Buffer.from(await calculateSha1(img.path)).toString('hex');
|
||||
const req = UploadGroupImage.build(groupUin, img);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const resp = await this.client.sendOidbPacket(req, true);
|
||||
const preRespData = UploadGroupImage.parse(resp);
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != "") {
|
||||
@@ -173,7 +176,7 @@ export class PacketHighwayContext {
|
||||
private async uploadC2CImage(peerUid: string, img: PacketMsgPicElement): Promise<void> {
|
||||
img.sha1 = Buffer.from(await calculateSha1(img.path)).toString('hex');
|
||||
const req = trans.UploadPrivateImage.build(peerUid, img);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const resp = await this.client.sendOidbPacket(req, true);
|
||||
const preRespData = trans.UploadPrivateImage.parse(resp);
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != "") {
|
||||
@@ -211,7 +214,7 @@ export class PacketHighwayContext {
|
||||
video.fileSha1 = Buffer.from(await calculateSha1(video.filePath)).toString('hex');
|
||||
video.thumbSha1 = Buffer.from(await calculateSha1(video.thumbPath)).toString('hex');
|
||||
const req = trans.UploadGroupVideo.build(groupUin, video);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const resp = await this.client.sendOidbPacket(req, true);
|
||||
const preRespData = trans.UploadGroupVideo.parse(resp);
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != "") {
|
||||
@@ -276,7 +279,7 @@ export class PacketHighwayContext {
|
||||
video.fileSha1 = Buffer.from(await calculateSha1(video.filePath)).toString('hex');
|
||||
video.thumbSha1 = Buffer.from(await calculateSha1(video.thumbPath)).toString('hex');
|
||||
const req = trans.UploadPrivateVideo.build(peerUid, video);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const resp = await this.client.sendOidbPacket(req, true);
|
||||
const preRespData = trans.UploadPrivateVideo.parse(resp);
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != "") {
|
||||
@@ -339,7 +342,7 @@ export class PacketHighwayContext {
|
||||
private async uploadGroupPtt(groupUin: number, ptt: PacketMsgPttElement): Promise<void> {
|
||||
ptt.fileSha1 = Buffer.from(await calculateSha1(ptt.filePath)).toString('hex');
|
||||
const req = trans.UploadGroupPtt.build(groupUin, ptt);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const resp = await this.client.sendOidbPacket(req, true);
|
||||
const preRespData = trans.UploadGroupPtt.parse(resp);
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != "") {
|
||||
@@ -375,7 +378,7 @@ export class PacketHighwayContext {
|
||||
private async uploadC2CPtt(peerUid: string, ptt: PacketMsgPttElement): Promise<void> {
|
||||
ptt.fileSha1 = Buffer.from(await calculateSha1(ptt.filePath)).toString('hex');
|
||||
const req = trans.UploadPrivatePtt.build(peerUid, ptt);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const resp = await this.client.sendOidbPacket(req, true);
|
||||
const preRespData = trans.UploadPrivatePtt.parse(resp);
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != "") {
|
||||
@@ -413,7 +416,7 @@ export class PacketHighwayContext {
|
||||
file.fileMd5 = await computeMd5AndLengthWithLimit(file.filePath);
|
||||
file.fileSha1 = await calculateSha1(file.filePath);
|
||||
const req = trans.UploadGroupFile.build(groupUin, file);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const resp = await this.client.sendOidbPacket(req, true);
|
||||
const preRespData = trans.UploadGroupFile.parse(resp);
|
||||
if (!preRespData?.upload?.boolFileExist) {
|
||||
this.logger.debug(`[Highway] uploadGroupFileReq file not exist, need upload!`);
|
||||
@@ -476,7 +479,7 @@ export class PacketHighwayContext {
|
||||
file.fileMd5 = await computeMd5AndLengthWithLimit(file.filePath);
|
||||
file.fileSha1 = await calculateSha1(file.filePath);
|
||||
const req = await trans.UploadPrivateFile.build(this.sig.uid, peerUid, file);
|
||||
const res = await this.context.client.sendOidbPacket(req, true);
|
||||
const res = await this.client.sendOidbPacket(req, true);
|
||||
const preRespData = trans.UploadPrivateFile.parse(res);
|
||||
if (!preRespData.upload?.boolFileExist) {
|
||||
this.logger.debug(`[Highway] uploadC2CFileReq file not exist, need upload!`);
|
||||
@@ -531,7 +534,7 @@ export class PacketHighwayContext {
|
||||
file.fileUuid = preRespData.upload?.uuid;
|
||||
file.fileHash = preRespData.upload?.fileAddon;
|
||||
const fileExistReq = trans.DownloadOfflineFile.build(file.fileUuid!, file.fileHash!, this.sig.uid, peerUid);
|
||||
const fileExistRes = await this.context.client.sendOidbPacket(fileExistReq, true);
|
||||
const fileExistRes = await this.client.sendOidbPacket(fileExistReq, true);
|
||||
file._e37_800_rsp = trans.DownloadOfflineFile.parse(fileExistRes);
|
||||
file._private_send_uid = this.sig.uid;
|
||||
file._private_recv_uid = peerUid;
|
||||
|
@@ -256,6 +256,8 @@ export class PacketMsgPicElement extends IPacketMsgElement<SendPicElement> {
|
||||
width: number;
|
||||
height: number;
|
||||
picType: PicType;
|
||||
picSubType: number;
|
||||
summary: string;
|
||||
sha1: string | null = null;
|
||||
msgInfo: NapProtoEncodeStructType<typeof MsgInfo> | null = null;
|
||||
groupPicExt: NapProtoEncodeStructType<typeof CustomFace> | null = null;
|
||||
@@ -270,6 +272,10 @@ export class PacketMsgPicElement extends IPacketMsgElement<SendPicElement> {
|
||||
this.width = element.picElement.picWidth;
|
||||
this.height = element.picElement.picHeight;
|
||||
this.picType = element.picElement.picType;
|
||||
this.picSubType = element.picElement.picSubType ?? 0;
|
||||
this.summary = element.picElement.summary === '' ? (
|
||||
element.picElement.picSubType === 0 ? '[图片]' : '[动画表情]'
|
||||
) : element.picElement.summary;
|
||||
}
|
||||
|
||||
get valid(): boolean {
|
||||
@@ -288,7 +294,7 @@ export class PacketMsgPicElement extends IPacketMsgElement<SendPicElement> {
|
||||
}
|
||||
|
||||
toPreview(): string {
|
||||
return "[图片]";
|
||||
return this.summary;
|
||||
}
|
||||
}
|
||||
|
||||
|
37
src/core/packet/transformer/action/ImageOCR.ts
Normal file
37
src/core/packet/transformer/action/ImageOCR.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import * as proto from "@/core/packet/transformer/proto";
|
||||
import { NapProtoMsg } from "@napneko/nap-proto-core";
|
||||
import { OidbPacket, PacketTransformer } from "@/core/packet/transformer/base";
|
||||
import OidbBase from "@/core/packet/transformer/oidb/oidbBase";
|
||||
|
||||
class ImageOCR extends PacketTransformer<typeof proto.OidbSvcTrpcTcp0xE07_0_Response> {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
build(url: string): OidbPacket {
|
||||
const body = new NapProtoMsg(proto.OidbSvcTrpcTcp0xE07_0).encode(
|
||||
{
|
||||
version: 1,
|
||||
client: 0,
|
||||
entrance: 1,
|
||||
ocrReqBody: {
|
||||
imageUrl: url,
|
||||
originMd5: "",
|
||||
afterCompressMd5: "",
|
||||
afterCompressFileSize: "",
|
||||
afterCompressWeight: "",
|
||||
afterCompressHeight: "",
|
||||
isCut: false,
|
||||
}
|
||||
}
|
||||
);
|
||||
return OidbBase.build(0XEB7, 1, body, false, false);
|
||||
}
|
||||
|
||||
parse(data: Buffer) {
|
||||
const base = OidbBase.parse(data);
|
||||
return new NapProtoMsg(proto.OidbSvcTrpcTcp0xE07_0_Response).decode(base.body);
|
||||
}
|
||||
}
|
||||
|
||||
export default new ImageOCR();
|
@@ -5,3 +5,4 @@ export { default as GroupSign } from './GroupSign';
|
||||
export { default as GetStrangerInfo } from './GetStrangerInfo';
|
||||
export { default as SendPoke } from './SendPoke';
|
||||
export { default as SetSpecialTitle } from './SetSpecialTitle';
|
||||
export { default as ImageOCR } from './ImageOCR';
|
||||
|
51
src/core/packet/transformer/highway/DownloadImage.ts
Normal file
51
src/core/packet/transformer/highway/DownloadImage.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import * as proto from "@/core/packet/transformer/proto";
|
||||
import { NapProtoEncodeStructType, NapProtoMsg } from "@napneko/nap-proto-core";
|
||||
import { OidbPacket, PacketTransformer } from "@/core/packet/transformer/base";
|
||||
import OidbBase from "@/core/packet/transformer/oidb/oidbBase";
|
||||
import { IndexNode } from "@/core/packet/transformer/proto";
|
||||
|
||||
class DownloadImage extends PacketTransformer<typeof proto.NTV2RichMediaResp> {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
build(selfUid: string, node: NapProtoEncodeStructType<typeof IndexNode>): OidbPacket {
|
||||
const body = new NapProtoMsg(proto.NTV2RichMediaReq).encode({
|
||||
reqHead: {
|
||||
common: {
|
||||
requestId: 1,
|
||||
command: 100
|
||||
},
|
||||
scene: {
|
||||
requestType: 2,
|
||||
businessType: 1,
|
||||
sceneType: 1,
|
||||
c2C: {
|
||||
accountType: 2,
|
||||
targetUid: selfUid
|
||||
},
|
||||
},
|
||||
client: {
|
||||
agentType: 2,
|
||||
}
|
||||
},
|
||||
download: {
|
||||
node: node,
|
||||
download: {
|
||||
video: {
|
||||
busiType: 0,
|
||||
sceneType: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return OidbBase.build(0x11C5, 200, body, true, false);
|
||||
}
|
||||
|
||||
parse(data: Buffer) {
|
||||
const oidbBody = OidbBase.parse(data).body;
|
||||
return new NapProtoMsg(proto.NTV2RichMediaResp).decode(oidbBody);
|
||||
}
|
||||
}
|
||||
|
||||
export default new DownloadImage();
|
@@ -58,8 +58,11 @@ class UploadGroupImage extends PacketTransformer<typeof proto.NTV2RichMediaResp>
|
||||
compatQMsgSceneType: 2,
|
||||
extBizInfo: {
|
||||
pic: {
|
||||
bytesPbReserveTroop: Buffer.from("0800180020004200500062009201009a0100a2010c080012001800200028003a00", 'hex'),
|
||||
textSummary: "Nya~", // TODO:
|
||||
bizType: img.picSubType,
|
||||
bytesPbReserveTroop: {
|
||||
subType: img.picSubType,
|
||||
},
|
||||
textSummary: img.summary,
|
||||
},
|
||||
video: {
|
||||
bytesPbReserve: Buffer.alloc(0),
|
||||
|
@@ -58,8 +58,11 @@ class UploadPrivateImage extends PacketTransformer<typeof proto.NTV2RichMediaRes
|
||||
compatQMsgSceneType: 1,
|
||||
extBizInfo: {
|
||||
pic: {
|
||||
bytesPbReserveTroop: Buffer.from("0800180020004200500062009201009a0100a2010c080012001800200028003a00", 'hex'),
|
||||
textSummary: "Nya~", // TODO:
|
||||
bizType: img.picSubType,
|
||||
bytesPbReserveC2C: {
|
||||
subType: img.picSubType,
|
||||
},
|
||||
textSummary: img.summary,
|
||||
},
|
||||
video: {
|
||||
bytesPbReserve: Buffer.alloc(0),
|
||||
|
@@ -11,3 +11,4 @@ export { default as UploadPrivateFile } from './UploadPrivateFile';
|
||||
export { default as UploadPrivateImage } from './UploadPrivateImage';
|
||||
export { default as UploadPrivatePtt } from './UploadPrivatePtt';
|
||||
export { default as UploadPrivateVideo } from './UploadPrivateVideo';
|
||||
export { default as DownloadImage } from './DownloadImage';
|
||||
|
@@ -29,3 +29,4 @@ export * from "./oidb/Oidb.0xEB7";
|
||||
export * from "./oidb/Oidb.0xED3_1";
|
||||
export * from "./oidb/Oidb.0XFE1_2";
|
||||
export * from "./oidb/OidbBase";
|
||||
export * from "./oidb/Oidb.0xE07";
|
||||
|
59
src/core/packet/transformer/proto/oidb/Oidb.0xE07.ts
Normal file
59
src/core/packet/transformer/proto/oidb/Oidb.0xE07.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { ProtoField, ScalarType } from "@napneko/nap-proto-core";
|
||||
|
||||
export const OidbSvcTrpcTcp0xE07_0 = {
|
||||
version: ProtoField(1, ScalarType.UINT32),
|
||||
client: ProtoField(2, ScalarType.UINT32),
|
||||
entrance: ProtoField(3, ScalarType.UINT32),
|
||||
ocrReqBody: ProtoField(10, () => OcrReqBody, true),
|
||||
};
|
||||
|
||||
export const OcrReqBody = {
|
||||
imageUrl: ProtoField(1, ScalarType.STRING),
|
||||
languageType: ProtoField(2, ScalarType.UINT32),
|
||||
scene: ProtoField(3, ScalarType.UINT32),
|
||||
originMd5: ProtoField(10, ScalarType.STRING),
|
||||
afterCompressMd5: ProtoField(11, ScalarType.STRING),
|
||||
afterCompressFileSize: ProtoField(12, ScalarType.STRING),
|
||||
afterCompressWeight: ProtoField(13, ScalarType.STRING),
|
||||
afterCompressHeight: ProtoField(14, ScalarType.STRING),
|
||||
isCut: ProtoField(15, ScalarType.BOOL),
|
||||
};
|
||||
|
||||
export const OidbSvcTrpcTcp0xE07_0_Response = {
|
||||
retCode: ProtoField(1, ScalarType.INT32),
|
||||
errMsg: ProtoField(2, ScalarType.STRING),
|
||||
wording: ProtoField(3, ScalarType.STRING),
|
||||
ocrRspBody: ProtoField(10, () => OcrRspBody),
|
||||
};
|
||||
|
||||
export const OcrRspBody = {
|
||||
textDetections: ProtoField(1, () => TextDetection, false, true),
|
||||
language: ProtoField(2, ScalarType.STRING),
|
||||
requestId: ProtoField(3, ScalarType.STRING),
|
||||
ocrLanguageList: ProtoField(101, ScalarType.STRING, false, true),
|
||||
dstTranslateLanguageList: ProtoField(102, ScalarType.STRING, false, true),
|
||||
languageList: ProtoField(103, () => Language, false, true),
|
||||
afterCompressWeight: ProtoField(111, ScalarType.UINT32),
|
||||
afterCompressHeight: ProtoField(112, ScalarType.UINT32),
|
||||
};
|
||||
|
||||
export const TextDetection = {
|
||||
detectedText: ProtoField(1, ScalarType.STRING),
|
||||
confidence: ProtoField(2, ScalarType.UINT32),
|
||||
polygon: ProtoField(3, () => Polygon),
|
||||
advancedInfo: ProtoField(4, ScalarType.STRING),
|
||||
};
|
||||
|
||||
export const Polygon = {
|
||||
coordinates: ProtoField(1, () => Coordinate, false, true),
|
||||
};
|
||||
|
||||
export const Coordinate = {
|
||||
x: ProtoField(1, ScalarType.INT32),
|
||||
y: ProtoField(2, ScalarType.INT32),
|
||||
};
|
||||
|
||||
export const Language = {
|
||||
languageCode: ProtoField(1, ScalarType.STRING),
|
||||
languageDesc: ProtoField(2, ScalarType.STRING),
|
||||
};
|
@@ -189,8 +189,8 @@ export const VideoExtBizInfo = {
|
||||
export const PicExtBizInfo = {
|
||||
BizType: ProtoField(1, ScalarType.UINT32),
|
||||
TextSummary: ProtoField(2, ScalarType.STRING),
|
||||
BytesPbReserveC2c: ProtoField(11, ScalarType.BYTES),
|
||||
BytesPbReserveTroop: ProtoField(12, ScalarType.BYTES),
|
||||
BytesPbReserveC2c: ProtoField(11, () => BytesPbReserveC2c),
|
||||
BytesPbReserveTroop: ProtoField(12, () => BytesPbReserveTroop),
|
||||
FromScene: ProtoField(1001, ScalarType.UINT32),
|
||||
ToScene: ProtoField(1002, ScalarType.UINT32),
|
||||
OldFileId: ProtoField(1003, ScalarType.UINT32),
|
||||
@@ -211,3 +211,27 @@ export const UploadInfo = {
|
||||
FileInfo: ProtoField(1, () => FileInfo),
|
||||
SubFileType: ProtoField(2, ScalarType.UINT32),
|
||||
};
|
||||
|
||||
export const BytesPbReserveC2c = {
|
||||
subType: ProtoField(1, ScalarType.UINT32),
|
||||
field3: ProtoField(3, ScalarType.UINT32),
|
||||
field4: ProtoField(4, ScalarType.UINT32),
|
||||
field8: ProtoField(8, ScalarType.STRING),
|
||||
field10: ProtoField(10, ScalarType.UINT32),
|
||||
field12: ProtoField(12, ScalarType.STRING),
|
||||
field18: ProtoField(18, ScalarType.STRING),
|
||||
field19: ProtoField(19, ScalarType.STRING),
|
||||
field20: ProtoField(20, ScalarType.BYTES),
|
||||
};
|
||||
|
||||
export const BytesPbReserveTroop = {
|
||||
subType: ProtoField(1, ScalarType.UINT32),
|
||||
field3: ProtoField(3, ScalarType.UINT32),
|
||||
field4: ProtoField(4, ScalarType.UINT32),
|
||||
field9: ProtoField(9, ScalarType.STRING),
|
||||
field10: ProtoField(10, ScalarType.UINT32),
|
||||
field12: ProtoField(12, ScalarType.STRING),
|
||||
field18: ProtoField(18, ScalarType.STRING),
|
||||
field19: ProtoField(19, ScalarType.STRING),
|
||||
field21: ProtoField(21, ScalarType.BYTES),
|
||||
};
|
||||
|
@@ -18,7 +18,7 @@ export interface BuddyCategoryType {
|
||||
export interface CoreInfo {
|
||||
uid: string;
|
||||
uin: string;
|
||||
nick: string;
|
||||
nick?: string;
|
||||
remark: string;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1 @@
|
||||
import '@/universal/napcat';
|
@@ -15,7 +15,7 @@ export class SendPacket extends GetPacketStatusDepends<Payload, any> {
|
||||
actionName = ActionName.SendPacket;
|
||||
async _handle(payload: Payload) {
|
||||
const rsp = typeof payload.rsp === 'boolean' ? payload.rsp : payload.rsp === 'true';
|
||||
const data = await this.core.apis.PacketApi.pkt.client.sendOidbPacket({ cmd: payload.cmd, data: payload.data as any }, rsp);
|
||||
const data = await this.core.apis.PacketApi.pkt.operation.sendPacket({ cmd: payload.cmd, data: payload.data as any }, rsp);
|
||||
return typeof data === 'object' ? data.toString('hex') : undefined;
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
||||
import fs from 'fs/promises';
|
||||
import { FileNapCatOneBotUUID } from '@/common/helper';
|
||||
import { FileNapCatOneBotUUID } from '@/common/file-uuid';
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { OB11MessageImage, OB11MessageVideo } from '@/onebot/types';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
@@ -28,7 +28,7 @@ export class GetFileBase extends OneBotAction<GetFilePayload, GetFileResponse> {
|
||||
payload.file ||= payload.file_id || '';
|
||||
//接收消息标记模式
|
||||
const contextMsgFile = FileNapCatOneBotUUID.decode(payload.file);
|
||||
if (contextMsgFile) {
|
||||
if (contextMsgFile && contextMsgFile.msgId && contextMsgFile.elementId) {
|
||||
const { peer, msgId, elementId } = contextMsgFile;
|
||||
const downloadPath = await this.core.apis.FileApi.downloadMedia(msgId, peer.chatType, peer.peerUid, elementId, '', '');
|
||||
const rawMessage = (await this.core.apis.MsgApi.getMsgsByMsgId(peer, [msgId]))?.msgList
|
||||
@@ -68,7 +68,7 @@ export class GetFileBase extends OneBotAction<GetFilePayload, GetFileResponse> {
|
||||
|
||||
//群文件模式
|
||||
const contextModelIdFile = FileNapCatOneBotUUID.decodeModelId(payload.file);
|
||||
if (contextModelIdFile) {
|
||||
if (contextModelIdFile && contextModelIdFile.modelId) {
|
||||
const { peer, modelId } = contextModelIdFile;
|
||||
const downloadPath = await this.core.apis.FileApi.downloadFileForModelId(peer, modelId, '');
|
||||
const res: GetFileResponse = {
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { FileNapCatOneBotUUID } from "@/common/helper";
|
||||
import { FileNapCatOneBotUUID } from '@/common/file-uuid';
|
||||
import { GetPacketStatusDepends } from "@/onebot/action/packet/GetPacketStatus";
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
|
||||
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { FileNapCatOneBotUUID } from '@/common/helper';
|
||||
import { FileNapCatOneBotUUID } from '@/common/file-uuid';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
|
||||
const SchemaData = Type.Object({
|
||||
@@ -16,7 +16,7 @@ export class DeleteGroupFile extends OneBotAction<Payload, any> {
|
||||
payloadSchema = SchemaData;
|
||||
async _handle(payload: Payload) {
|
||||
const data = FileNapCatOneBotUUID.decodeModelId(payload.file_id);
|
||||
if (!data) throw new Error('Invalid file_id');
|
||||
if (!data || !data.fileId) throw new Error('Invalid file_id');
|
||||
return await this.core.apis.GroupApi.delGroupFile(payload.group_id.toString(), [data.fileId]);
|
||||
}
|
||||
}
|
||||
|
@@ -19,7 +19,6 @@ export default class GoCQHTTPGetStrangerInfo extends OneBotAction<Payload, OB11U
|
||||
const extendData = await this.core.apis.UserApi.getUserDetailInfoByUin(user_id);
|
||||
let uid = (await this.core.apis.UserApi.getUidByUinV2(user_id));
|
||||
if (!uid) uid = extendData.detail.uid;
|
||||
console.log(uid);
|
||||
const info = (await this.core.apis.UserApi.getUserDetailInfo(uid));
|
||||
return {
|
||||
...extendData.detail.simpleInfo.coreInfo,
|
||||
|
@@ -28,20 +28,14 @@ class GetGroupMemberInfo extends OneBotAction<Payload, OB11GroupMember> {
|
||||
|
||||
private async getGroupMemberInfo(payload: Payload, uid: string, isNocache: boolean) {
|
||||
const groupMemberCache = this.core.apis.GroupApi.groupMemberCache.get(payload.group_id.toString());
|
||||
let groupMember = groupMemberCache?.get(uid);
|
||||
const groupMember = groupMemberCache?.get(uid);
|
||||
|
||||
const [member, info] = await Promise.all([
|
||||
this.core.apis.GroupApi.getGroupMemberEx(payload.group_id.toString(), uid, isNocache),
|
||||
this.core.apis.UserApi.getUserDetailInfo(uid),
|
||||
]);
|
||||
|
||||
if (!member) throw new Error(`群(${payload.group_id})成员${payload.user_id}不存在`);
|
||||
|
||||
if (!groupMember && this.core.apis.GroupApi.groupMemberCacheEvent.get(payload.group_id.toString())) {
|
||||
groupMember = (await this.core.apis.GroupApi.refreshGroupMemberCache(payload.group_id.toString(), true))?.get(uid);
|
||||
}
|
||||
|
||||
if (!groupMember) throw new Error(`群(${payload.group_id})成员${payload.user_id}不存在`);
|
||||
if (!member || !groupMember) throw new Error(`群(${payload.group_id})成员${payload.user_id}不存在`);
|
||||
|
||||
return info ? { ...groupMember, ...member, ...info } : member;
|
||||
}
|
||||
|
@@ -19,7 +19,7 @@ export default class SetGroupAddRequest extends OneBotAction<Payload, null> {
|
||||
const flag = payload.flag.toString();
|
||||
const approve = payload.approve?.toString() !== 'false';
|
||||
const reason = payload.reason ?? ' ';
|
||||
let invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(flag);
|
||||
const invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(flag);
|
||||
const notify = invite_notify ?? await this.findNotify(flag);
|
||||
if (!notify) {
|
||||
throw new Error('No such request');
|
||||
|
@@ -23,7 +23,7 @@ import { OB11GroupTitleEvent } from '@/onebot/event/notice/OB11GroupTitleEvent';
|
||||
import { OB11GroupUploadNoticeEvent } from '../event/notice/OB11GroupUploadNoticeEvent';
|
||||
import { OB11GroupNameEvent } from '../event/notice/OB11GroupNameEvent';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { FileNapCatOneBotUUID } from '@/common/helper';
|
||||
import { FileNapCatOneBotUUID } from '@/common/file-uuid';
|
||||
import { OB11GroupIncreaseEvent } from '../event/notice/OB11GroupIncreaseEvent';
|
||||
|
||||
export class OneBotGroupApi {
|
||||
@@ -199,7 +199,7 @@ export class OneBotGroupApi {
|
||||
id: FileNapCatOneBotUUID.encode({
|
||||
chatType: ChatType.KCHATTYPEGROUP,
|
||||
peerUid: msg.peerUid,
|
||||
}, msg.msgId, elementWrapper.elementId, elementWrapper?.fileElement?.fileUuid, "." + element.fileName),
|
||||
}, msg.msgId, elementWrapper.elementId, elementWrapper?.fileElement?.fileUuid, element.fileName),
|
||||
url: pathToFileURL(element.filePath).href,
|
||||
name: element.fileName,
|
||||
size: parseInt(element.fileSize),
|
||||
@@ -218,12 +218,12 @@ export class OneBotGroupApi {
|
||||
element.groupName,
|
||||
);
|
||||
} else if (element.type === TipGroupElementType.KSHUTUP) {
|
||||
let event = await this.parseGroupBanEvent(msg.peerUid, elementWrapper);
|
||||
const event = await this.parseGroupBanEvent(msg.peerUid, elementWrapper);
|
||||
return event;
|
||||
} else if (element.type === TipGroupElementType.KMEMBERADD) {
|
||||
// 自己的通知 协议推送为type->85 在这里实现为了避免邀请出现问题
|
||||
if (element.memberUid == this.core.selfInfo.uid) {
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCache(msg.peerUid, false);
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCache(msg.peerUid, true);
|
||||
return new OB11GroupIncreaseEvent(
|
||||
this.core,
|
||||
parseInt(msg.peerUid),
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { FileNapCatOneBotUUID } from '@/common/helper';
|
||||
import { FileNapCatOneBotUUID } from '@/common/file-uuid';
|
||||
import { MessageUnique } from '@/common/message-unique';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import {
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
GroupNotify,
|
||||
} from '@/core';
|
||||
import faceConfig from '@/core/external/face_config.json';
|
||||
import { NapCatOneBot11Adapter, OB11Message, OB11MessageData, OB11MessageDataType, OB11MessageFileBase, OB11MessageForward, } from '@/onebot';
|
||||
import { NapCatOneBot11Adapter, OB11Message, OB11MessageData, OB11MessageDataType, OB11MessageFileBase, OB11MessageForward, OB11MessageImage, OB11MessageVideo, } from '@/onebot';
|
||||
import { OB11Construct } from '@/onebot/helper/data';
|
||||
import { EventType } from '@/onebot/event/OneBotEvent';
|
||||
import { encodeCQCode } from '@/onebot/helper/cqcode';
|
||||
@@ -116,18 +116,22 @@ export class OneBotMsgApi {
|
||||
peerUid: msg.peerUid,
|
||||
guildId: '',
|
||||
};
|
||||
const encodedFileId = FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, "." + element.fileName);
|
||||
FileNapCatOneBotUUID.encode(
|
||||
peer,
|
||||
msg.msgId,
|
||||
elementWrapper.elementId,
|
||||
element.fileUuid,
|
||||
element.fileName
|
||||
);
|
||||
return {
|
||||
type: OB11MessageDataType.image,
|
||||
data: {
|
||||
summary: element.summary,
|
||||
file: encodedFileId,
|
||||
file: element.fileName,
|
||||
sub_type: element.picSubType,
|
||||
file_id: encodedFileId,
|
||||
url: await this.core.apis.FileApi.getImageUrl(element),
|
||||
path: element.filePath,
|
||||
file_size: element.fileSize,
|
||||
file_unique: element.md5HexStr ?? element.fileName,
|
||||
},
|
||||
};
|
||||
} catch (e: any) {
|
||||
@@ -142,15 +146,15 @@ export class OneBotMsgApi {
|
||||
peerUid: msg.peerUid,
|
||||
guildId: '',
|
||||
};
|
||||
const file = FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, element.fileName);
|
||||
return {
|
||||
type: OB11MessageDataType.file,
|
||||
data: {
|
||||
file: element.fileName,
|
||||
file: file,
|
||||
path: element.filePath,
|
||||
url: pathToFileURL(element.filePath).href,
|
||||
file_id: FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, "." + element.fileName),
|
||||
file_id: file,
|
||||
file_size: element.fileSize,
|
||||
file_unique: element.fileMd5 ?? element.fileSha ?? element.fileName,
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -201,18 +205,18 @@ export class OneBotMsgApi {
|
||||
const { emojiId } = _;
|
||||
const dir = emojiId.substring(0, 2);
|
||||
const url = `https://gxh.vip.qq.com/club/item/parcel/item/${dir}/${emojiId}/raw300.gif`;
|
||||
const filename = `${dir}-${emojiId}.gif`;
|
||||
FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, "", filename);
|
||||
return {
|
||||
type: OB11MessageDataType.image,
|
||||
data: {
|
||||
summary: _.faceName, // 商城表情名称
|
||||
file: 'marketface',
|
||||
file_id: FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, "", "." + _.key + ".jpg"),
|
||||
file: filename,
|
||||
path: url,
|
||||
url: url,
|
||||
key: _.key,
|
||||
emoji_id: _.emojiId,
|
||||
emoji_package_id: _.emojiPackageId,
|
||||
file_unique: _.key
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -327,16 +331,14 @@ export class OneBotMsgApi {
|
||||
if (!videoDownUrl) {
|
||||
videoDownUrl = element.filePath;
|
||||
}
|
||||
const fileCode = FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, "", "." + element.fileName);
|
||||
const fileCode = FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, element.fileName);
|
||||
return {
|
||||
type: OB11MessageDataType.video,
|
||||
data: {
|
||||
file: fileCode,
|
||||
path: videoDownUrl,
|
||||
url: videoDownUrl ?? pathToFileURL(element.filePath).href,
|
||||
file_id: fileCode,
|
||||
file_size: element.fileSize,
|
||||
file_unique: element.videoMd5 ?? element.thumbMd5 ?? element.fileName,
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -347,16 +349,14 @@ export class OneBotMsgApi {
|
||||
peerUid: msg.peerUid,
|
||||
guildId: '',
|
||||
};
|
||||
const fileCode = FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, "", "." + element.fileName);
|
||||
const fileCode = FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, "", element.fileName);
|
||||
return {
|
||||
type: OB11MessageDataType.voice,
|
||||
data: {
|
||||
file: fileCode,
|
||||
path: element.filePath,
|
||||
url: pathToFileURL(element.filePath).href,
|
||||
file_id: fileCode,
|
||||
file_size: element.fileSize,
|
||||
file_unique: element.fileUuid
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -798,7 +798,7 @@ export class OneBotMsgApi {
|
||||
private async handlePrivateMessage(resMsg: OB11Message, msg: RawMessage) {
|
||||
resMsg.sub_type = 'friend';
|
||||
if (await this.core.apis.FriendApi.isBuddy(msg.senderUid)) {
|
||||
let nickname = (await this.core.apis.UserApi.getCoreAndBaseInfo([msg.senderUid])).get(msg.senderUid)?.coreInfo.nick;
|
||||
const nickname = (await this.core.apis.UserApi.getCoreAndBaseInfo([msg.senderUid])).get(msg.senderUid)?.coreInfo.nick;
|
||||
if (nickname) {
|
||||
resMsg.sender.nickname = nickname;
|
||||
return;
|
||||
@@ -956,30 +956,56 @@ export class OneBotMsgApi {
|
||||
|
||||
private async handleOb11FileLikeMessage(
|
||||
{ data: inputdata }: OB11MessageFileBase,
|
||||
{ deleteAfterSentFiles }: SendMessageContext,
|
||||
{ deleteAfterSentFiles }: SendMessageContext
|
||||
) {
|
||||
const realUri = inputdata.url ?? inputdata.file ?? inputdata.path ?? '';
|
||||
if (realUri.length === 0) {
|
||||
let realUri = [inputdata.url, inputdata.file, inputdata.path].find(uri => uri && uri.trim()) ?? '';
|
||||
if (!realUri) {
|
||||
this.core.context.logger.logError('文件消息缺少参数', inputdata);
|
||||
throw Error('文件消息缺少参数');
|
||||
}
|
||||
const {
|
||||
path,
|
||||
fileName,
|
||||
errMsg,
|
||||
success,
|
||||
} = (await uriToLocalFile(this.core.NapCatTempPath, realUri));
|
||||
|
||||
if (!success) {
|
||||
this.core.context.logger.logError('文件下载失败', errMsg);
|
||||
throw Error('文件下载失败' + errMsg);
|
||||
throw new Error('文件消息缺少参数');
|
||||
}
|
||||
|
||||
deleteAfterSentFiles.push(path);
|
||||
|
||||
return { path, fileName: inputdata.name ?? fileName };
|
||||
const downloadFile = async (uri: string) => {
|
||||
const { path, fileName, errMsg, success } = await uriToLocalFile(this.core.NapCatTempPath, uri);
|
||||
if (!success) {
|
||||
this.core.context.logger.logError('文件下载失败', errMsg);
|
||||
throw new Error('文件下载失败: ' + errMsg);
|
||||
}
|
||||
return { path, fileName };
|
||||
};
|
||||
try {
|
||||
const { path, fileName } = await downloadFile(realUri);
|
||||
deleteAfterSentFiles.push(path);
|
||||
return { path, fileName: inputdata.name ?? fileName };
|
||||
} catch {
|
||||
realUri = await this.handleObfuckName(realUri);
|
||||
const { path, fileName } = await downloadFile(realUri);
|
||||
deleteAfterSentFiles.push(path);
|
||||
return { path, fileName: inputdata.name ?? fileName };
|
||||
}
|
||||
}
|
||||
async handleObfuckName(name: string) {
|
||||
const contextMsgFile = FileNapCatOneBotUUID.decode(name);
|
||||
if (contextMsgFile && contextMsgFile.msgId && contextMsgFile.elementId) {
|
||||
const { peer, msgId, elementId } = contextMsgFile;
|
||||
const rawMessage = (await this.core.apis.MsgApi.getMsgsByMsgId(peer, [msgId]))?.msgList.find(msg => msg.msgId === msgId);
|
||||
const mixElement = rawMessage?.elements.find(e => e.elementId === elementId);
|
||||
const mixElementInner = mixElement?.videoElement ?? mixElement?.fileElement ?? mixElement?.pttElement ?? mixElement?.picElement;
|
||||
if (!mixElementInner) throw new Error('element not found');
|
||||
let url = '';
|
||||
if (mixElement?.picElement && rawMessage) {
|
||||
const tempData =
|
||||
await this.obContext.apis.MsgApi.rawToOb11Converters.picElement?.(mixElement?.picElement, rawMessage, mixElement, { parseMultMsg: false }) as OB11MessageImage | undefined;
|
||||
url = tempData?.data.url ?? '';
|
||||
}
|
||||
if (mixElement?.videoElement && rawMessage) {
|
||||
const tempData =
|
||||
await this.obContext.apis.MsgApi.rawToOb11Converters.videoElement?.(mixElement?.videoElement, rawMessage, mixElement, { parseMultMsg: false }) as OB11MessageVideo | undefined;
|
||||
url = tempData?.data.url ?? '';
|
||||
}
|
||||
return url !== '' ? url : await this.core.apis.FileApi.downloadMedia(msgId, peer.chatType, peer.peerUid, elementId, '', '');
|
||||
}
|
||||
throw new Error('文件名解析失败');
|
||||
}
|
||||
|
||||
groupChangDecreseType2String(type: number): GroupDecreaseSubType {
|
||||
switch (type) {
|
||||
case 130:
|
||||
@@ -994,8 +1020,8 @@ export class OneBotMsgApi {
|
||||
}
|
||||
|
||||
async waitGroupNotify(groupUin: string, memberUid?: string, operatorUid?: string) {
|
||||
let groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupUin)?.get(this.core.selfInfo.uid.toString())?.role;
|
||||
let isAdminOrOwner = groupRole === 3 || groupRole === 4;
|
||||
const groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupUin)?.get(this.core.selfInfo.uid.toString())?.role;
|
||||
const isAdminOrOwner = groupRole === 3 || groupRole === 4;
|
||||
|
||||
if (isAdminOrOwner && !operatorUid) {
|
||||
let dataNotify: GroupNotify | undefined;
|
||||
@@ -1022,8 +1048,8 @@ export class OneBotMsgApi {
|
||||
// 邀请需要解grayTipElement
|
||||
if (SysMessage.contentHead.type == 33 && SysMessage.body?.msgContent) {
|
||||
const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent);
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCache(groupChange.groupUin.toString(), false);
|
||||
let operatorUid = await this.waitGroupNotify(
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCache(groupChange.groupUin.toString(), true);
|
||||
const operatorUid = await this.waitGroupNotify(
|
||||
groupChange.groupUin.toString(),
|
||||
groupChange.memberUid,
|
||||
groupChange.operatorInfo ? Buffer.from(groupChange.operatorInfo).toString() : ''
|
||||
@@ -1039,7 +1065,7 @@ export class OneBotMsgApi {
|
||||
} else if (SysMessage.contentHead.type == 34 && SysMessage.body?.msgContent) {
|
||||
const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent);
|
||||
// 自身被踢出时operatorInfo会是一个protobuf 否则大多数情况为一个string
|
||||
let operatorUid = await this.waitGroupNotify(
|
||||
const operatorUid = await this.waitGroupNotify(
|
||||
groupChange.groupUin.toString(),
|
||||
groupChange.memberUid,
|
||||
groupChange.decreaseType === 3 && groupChange.operatorInfo ?
|
||||
@@ -1052,7 +1078,7 @@ export class OneBotMsgApi {
|
||||
}, 5000);
|
||||
// 自己被踢了 5S后回收
|
||||
} else {
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCache(groupChange.groupUin.toString(), false);
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCache(groupChange.groupUin.toString(), true);
|
||||
}
|
||||
return new OB11GroupDecreaseEvent(
|
||||
this.core,
|
||||
@@ -1063,7 +1089,7 @@ export class OneBotMsgApi {
|
||||
);
|
||||
} else if (SysMessage.contentHead.type == 44 && SysMessage.body?.msgContent) {
|
||||
const groupAmin = new NapProtoMsg(GroupAdmin).decode(SysMessage.body.msgContent);
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCache(groupAmin.groupUin.toString(), false);
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCache(groupAmin.groupUin.toString(), true);
|
||||
let enabled = false;
|
||||
let uid = '';
|
||||
if (groupAmin.body.extraEnable != null) {
|
||||
@@ -1080,17 +1106,17 @@ export class OneBotMsgApi {
|
||||
enabled ? 'set' : 'unset'
|
||||
);
|
||||
} else if (SysMessage.contentHead.type == 87 && SysMessage.body?.msgContent) {
|
||||
let groupInvite = new NapProtoMsg(GroupInvite).decode(SysMessage.body.msgContent);
|
||||
const groupInvite = new NapProtoMsg(GroupInvite).decode(SysMessage.body.msgContent);
|
||||
let request_seq = '';
|
||||
try {
|
||||
await this.core.eventWrapper.registerListen('NodeIKernelMsgListener/onRecvMsg', (msgs) => {
|
||||
for (const msg of msgs) {
|
||||
if (msg.senderUid === groupInvite.invitorUid && msg.msgType === 11) {
|
||||
let jumpUrl = JSON.parse(msg.elements.find(e => e.elementType === 10)?.arkElement?.bytesData ?? '').meta?.news?.jumpUrl;
|
||||
let jumpUrlParams = new URLSearchParams(jumpUrl);
|
||||
let groupcode = jumpUrlParams.get('groupcode');
|
||||
let receiveruin = jumpUrlParams.get('receiveruin');
|
||||
let msgseq = jumpUrlParams.get('msgseq');
|
||||
const jumpUrl = JSON.parse(msg.elements.find(e => e.elementType === 10)?.arkElement?.bytesData ?? '').meta?.news?.jumpUrl;
|
||||
const jumpUrlParams = new URLSearchParams(jumpUrl);
|
||||
const groupcode = jumpUrlParams.get('groupcode');
|
||||
const receiveruin = jumpUrlParams.get('receiveruin');
|
||||
const msgseq = jumpUrlParams.get('msgseq');
|
||||
request_seq = msgseq ?? '';
|
||||
if (groupcode === groupInvite.groupUin.toString() && receiveruin === this.core.selfInfo.uin) {
|
||||
return true;
|
||||
@@ -1136,7 +1162,7 @@ export class OneBotMsgApi {
|
||||
waitStatus: 1,
|
||||
},
|
||||
status: 1
|
||||
})
|
||||
});
|
||||
return new OB11GroupRequestEvent(
|
||||
this.core,
|
||||
+groupInvite.groupUin,
|
||||
|
@@ -92,7 +92,7 @@ export class OneBotQuickActionApi {
|
||||
|
||||
async handleGroupRequest(request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
|
||||
|
||||
let invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(request.flag);
|
||||
const invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(request.flag);
|
||||
const notify = invite_notify ?? await this.findNotify(request.flag);
|
||||
|
||||
if (!isNull(quickAction.approve) && notify) {
|
||||
|
@@ -59,6 +59,13 @@ export const httpServerDefaultConfigs = createDefaultAdapterConfig({
|
||||
});
|
||||
export type HttpServerConfig = typeof httpServerDefaultConfigs;
|
||||
|
||||
export const httpSseServerDefaultConfigs = createDefaultAdapterConfig({
|
||||
...httpServerDefaultConfigs,
|
||||
name: 'http-sse-server',
|
||||
reportSelfMessage: false,
|
||||
});
|
||||
export type HttpSseServerConfig = typeof httpSseServerDefaultConfigs;
|
||||
|
||||
export const httpClientDefaultConfigs = createDefaultAdapterConfig({
|
||||
name: 'http-client',
|
||||
enable: false as boolean,
|
||||
@@ -99,6 +106,7 @@ export type WebsocketClientConfig = typeof websocketClientDefaultConfigs;
|
||||
|
||||
export interface NetworkConfig {
|
||||
httpServers: Array<HttpServerConfig>;
|
||||
httpSseServers: Array<HttpSseServerConfig>;
|
||||
httpClients: Array<HttpClientConfig>;
|
||||
websocketServers: Array<WebsocketServerConfig>;
|
||||
websocketClients: Array<WebsocketClientConfig>;
|
||||
@@ -120,6 +128,7 @@ const createDefaultConfig = <T>(config: T): T => config;
|
||||
export const defaultOneBotConfigs = createDefaultConfig<OneBotConfig>({
|
||||
network: {
|
||||
httpServers: [],
|
||||
httpSseServers: [],
|
||||
httpClients: [],
|
||||
websocketServers: [],
|
||||
websocketClients: [],
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { calcQQLevel, FileNapCatOneBotUUID } from '@/common/helper';
|
||||
import { calcQQLevel } from '@/common/helper';
|
||||
import { FileNapCatOneBotUUID } from '@/common/file-uuid';
|
||||
import { FriendV2, Group, GroupFileInfoUpdateParamType, GroupMember, SelfInfo, NTSex } from '@/core';
|
||||
import {
|
||||
OB11Group,
|
||||
@@ -22,7 +23,7 @@ export class OB11Construct {
|
||||
...rawFriend.baseInfo,
|
||||
...rawFriend.coreInfo,
|
||||
user_id: parseInt(rawFriend.coreInfo.uin),
|
||||
nickname: rawFriend.coreInfo.nick,
|
||||
nickname: rawFriend.coreInfo.nick ?? "",
|
||||
remark: rawFriend.coreInfo.remark ?? rawFriend.coreInfo.nick,
|
||||
sex: this.sex(rawFriend.baseInfo.sex),
|
||||
level: 0,
|
||||
@@ -90,6 +91,7 @@ export class OB11Construct {
|
||||
file_name: file.fileName,
|
||||
busid: file.busId,
|
||||
size: +file.fileSize,
|
||||
file_size: +file.fileSize,
|
||||
upload_time: file.uploadTime,
|
||||
dead_time: file.deadTime,
|
||||
modify_time: file.modifyTime,
|
||||
|
@@ -53,6 +53,7 @@ import {
|
||||
import { OB11Message } from './types';
|
||||
import { OB11PluginAdapter } from './network/plugin';
|
||||
import { IOB11NetworkAdapter } from "@/onebot/network/adapter";
|
||||
import { OB11ActiveHttpSSEAdapter } from './network/active-http-sse';
|
||||
|
||||
//OneBot实现类
|
||||
export class NapCatOneBot11Adapter {
|
||||
@@ -87,6 +88,9 @@ export class NapCatOneBot11Adapter {
|
||||
for (const key of ob11Config.network.httpServers) {
|
||||
log += `HTTP服务: ${key.host}:${key.port}, : ${key.enable ? '已启动' : '未启动'}\n`;
|
||||
}
|
||||
for (const key of ob11Config.network.httpSseServers) {
|
||||
log += `HTTP-SSE服务: ${key.host}:${key.port}, : ${key.enable ? '已启动' : '未启动'}\n`;
|
||||
}
|
||||
for (const key of ob11Config.network.httpClients) {
|
||||
log += `HTTP上报服务: ${key.url}, : ${key.enable ? '已启动' : '未启动'}\n`;
|
||||
}
|
||||
@@ -125,6 +129,13 @@ export class NapCatOneBot11Adapter {
|
||||
);
|
||||
}
|
||||
}
|
||||
for(const key of ob11Config.network.httpSseServers){
|
||||
if(key.enable) {
|
||||
this.networkManager.registerAdapter(
|
||||
new OB11ActiveHttpSSEAdapter(key.name, key, this.core, this, this.actions)
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const key of ob11Config.network.httpClients) {
|
||||
if (key.enable) {
|
||||
this.networkManager.registerAdapter(
|
||||
@@ -169,8 +180,11 @@ export class NapCatOneBot11Adapter {
|
||||
WebUiDataRuntime.setQQLoginStatus(true);
|
||||
WebUiDataRuntime.setOnOB11ConfigChanged(async (newConfig) => {
|
||||
const prev = this.configLoader.configData;
|
||||
// 保证默认配置
|
||||
newConfig = mergeOneBotConfigs(newConfig);
|
||||
|
||||
this.configLoader.save(newConfig);
|
||||
this.context.logger.log(`OneBot11 配置更改:${JSON.stringify(prev)} -> ${JSON.stringify(newConfig)}`);
|
||||
//this.context.logger.log(`OneBot11 配置更改:${JSON.stringify(prev)} -> ${JSON.stringify(newConfig)}`);
|
||||
await this.reloadNetwork(prev, newConfig);
|
||||
});
|
||||
}
|
||||
@@ -389,7 +403,7 @@ export class NapCatOneBot11Adapter {
|
||||
) {
|
||||
this.context.logger.logDebug('有加群请求');
|
||||
try {
|
||||
let requestUin = await this.core.apis.UserApi.getUinByUidV2(notify.user1.uid);
|
||||
const requestUin = await this.core.apis.UserApi.getUinByUidV2(notify.user1.uid);
|
||||
const groupRequestEvent = new OB11GroupRequestEvent(
|
||||
this.core,
|
||||
parseInt(notify.group.groupCode),
|
||||
|
34
src/onebot/network/active-http-sse.ts
Normal file
34
src/onebot/network/active-http-sse.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { OB11EmitEventContent } from './index';
|
||||
import { Request, Response } from 'express';
|
||||
import { OB11Response } from '@/onebot/action/OneBotAction';
|
||||
import { OB11PassiveHttpAdapter } from './passive-http';
|
||||
|
||||
export class OB11ActiveHttpSSEAdapter extends OB11PassiveHttpAdapter {
|
||||
private sseClients: Response[] = [];
|
||||
|
||||
async handleRequest(req: Request, res: Response): Promise<any> {
|
||||
if (req.path === '/_events') {
|
||||
return this.createSseSupport(req, res);
|
||||
} else {
|
||||
super.httpApiRequest(req, res);
|
||||
}
|
||||
}
|
||||
|
||||
private async createSseSupport(req: Request, res: Response) {
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
res.flushHeaders();
|
||||
|
||||
this.sseClients.push(res);
|
||||
req.on('close', () => {
|
||||
this.sseClients = this.sseClients.filter((client) => client !== res);
|
||||
});
|
||||
}
|
||||
|
||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||
this.sseClients.forEach((res) => {
|
||||
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
||||
});
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
import { OB11NetworkReloadType } from './index';
|
||||
import { OB11EmitEventContent, OB11NetworkReloadType } from './index';
|
||||
import express, { Express, Request, Response } from 'express';
|
||||
import http from 'http';
|
||||
import { NapCatCore } from '@/core';
|
||||
@@ -17,7 +17,7 @@ export class OB11PassiveHttpAdapter extends IOB11NetworkAdapter<HttpServerConfig
|
||||
super(name, config, core, obContext, actions);
|
||||
}
|
||||
|
||||
onEvent() {
|
||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||
// http server is passive, no need to emit event
|
||||
}
|
||||
|
||||
@@ -82,12 +82,7 @@ export class OB11PassiveHttpAdapter extends IOB11NetworkAdapter<HttpServerConfig
|
||||
}
|
||||
}
|
||||
|
||||
private async handleRequest(req: Request, res: Response) {
|
||||
if (!this.isEnable) {
|
||||
this.core.context.logger.log(`[OneBot] [HTTP Server Adapter] Server is closed`);
|
||||
return res.json(OB11Response.error('Server is closed', 200));
|
||||
}
|
||||
|
||||
async httpApiRequest(req: Request, res: Response) {
|
||||
let payload = req.body;
|
||||
if (req.method == 'get') {
|
||||
payload = req.query;
|
||||
@@ -113,6 +108,15 @@ export class OB11PassiveHttpAdapter extends IOB11NetworkAdapter<HttpServerConfig
|
||||
}
|
||||
}
|
||||
|
||||
async handleRequest(req: Request, res: Response) {
|
||||
if (!this.isEnable) {
|
||||
this.core.context.logger.log(`[OneBot] [HTTP Server Adapter] Server is closed`);
|
||||
return res.json(OB11Response.error('Server is closed', 200));
|
||||
}
|
||||
|
||||
return this.httpApiRequest(req, res);
|
||||
}
|
||||
|
||||
async reload(newConfig: HttpServerConfig) {
|
||||
const wasEnabled = this.isEnable;
|
||||
const oldPort = this.config.port;
|
||||
|
@@ -75,6 +75,7 @@ export interface OB11Sender {
|
||||
}
|
||||
|
||||
export interface OB11GroupFile {
|
||||
file_size: number; // 文件大小 GOCQHTTP 群文件Api扩展
|
||||
group_id: number; // 群ID
|
||||
file_id: string; // 文件ID
|
||||
file_name: string; // 文件名称
|
||||
|
@@ -110,7 +110,6 @@ export interface OB11MessageContext {
|
||||
// 文件消息基础接口定义
|
||||
export interface OB11MessageFileBase {
|
||||
data: {
|
||||
file_unique?: string;
|
||||
path?: string;
|
||||
thumb?: string;
|
||||
name?: string;
|
||||
|
@@ -222,7 +222,7 @@ async function handleLogin(
|
||||
logger.log(`可用于快速登录的 QQ:\n${historyLoginList
|
||||
.map((u, index) => `${index + 1}. ${u.uin} ${u.nickName}`)
|
||||
.join('\n')
|
||||
}`);
|
||||
}`);
|
||||
}
|
||||
loginService.getQRCodePicture();
|
||||
}
|
||||
|
6
src/universal/LiteLoader.d.ts
vendored
Normal file
6
src/universal/LiteLoader.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
declare global {
|
||||
namespace globalThis {
|
||||
var LiteLoader: Symbol;
|
||||
}
|
||||
}
|
||||
export {}
|
@@ -1,7 +1,6 @@
|
||||
import { NCoreInitShell } from "@/shell/base";
|
||||
|
||||
export * from "@/framework/napcat";
|
||||
export * from "@/shell/base";
|
||||
if ((global as any).LiteLoader == undefined) {
|
||||
if (global.LiteLoader == undefined) {
|
||||
NCoreInitShell();
|
||||
}
|
Reference in New Issue
Block a user