mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-12 16:00:27 +00:00
Compare commits
8 Commits
copilot/ad
...
fix-config
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9e569771c | ||
|
|
84fa9da762 | ||
|
|
2f9bcd88ba | ||
|
|
8eacaa294f | ||
|
|
5a2939f4ec | ||
|
|
d3013e32e1 | ||
|
|
5499b5fbc9 | ||
|
|
ed0b8408df |
17
README.md
17
README.md
@@ -13,15 +13,6 @@ _Modern protocol-side framework implemented based on NTQQ._
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## New Feature
|
|
||||||
在 v4.8.115+ 版本开始
|
|
||||||
|
|
||||||
1. NapCatQQ 支持 [Stream Api](https://napneko.github.io/develop/file)
|
|
||||||
2. NapCatQQ 推荐 message_id/user_id/group_id 均使用字符串类型
|
|
||||||
|
|
||||||
- [1] 解决 Docker/跨设备/大文件 的多媒体上下传问题
|
|
||||||
- [2] 采用字符串可以解决扩展到int64的问题,同时也可以解决部分语言(如JavaScript)对大整数支持不佳的问题,增加极少成本。
|
|
||||||
|
|
||||||
## Welcome
|
## Welcome
|
||||||
+ NapCatQQ is a modern implementation of the Bot protocol based on NTQQ.
|
+ NapCatQQ is a modern implementation of the Bot protocol based on NTQQ.
|
||||||
- NapCatQQ 是现代化的基于 NTQQ 的 Bot 协议端实现
|
- NapCatQQ 是现代化的基于 NTQQ 的 Bot 协议端实现
|
||||||
@@ -42,7 +33,6 @@ _Modern protocol-side framework implemented based on NTQQ._
|
|||||||
|
|
||||||
**首次使用**请务必查看如下文档看使用教程
|
**首次使用**请务必查看如下文档看使用教程
|
||||||
|
|
||||||
> 项目非盈利,对接问题/基础问题/下层框架问题 请自行搜索解决,本项目社区不提供此类解答。
|
|
||||||
## Link
|
## Link
|
||||||
|
|
||||||
| Docs | [](https://napneko.github.io/) | [](https://doc.napneko.icu/) | [](https://napcat.napneko.icu/) |
|
| Docs | [](https://napneko.github.io/) | [](https://doc.napneko.icu/) | [](https://napcat.napneko.icu/) |
|
||||||
@@ -51,17 +41,12 @@ _Modern protocol-side framework implemented based on NTQQ._
|
|||||||
| Docs | [](https://napneko.pages.dev/) | [](https://napcat.cyou/) | [](https://www.napcat.wiki) |
|
| Docs | [](https://napneko.pages.dev/) | [](https://napcat.cyou/) | [](https://www.napcat.wiki) |
|
||||||
|:-:|:-:|:-:|:-:|
|
|:-:|:-:|:-:|:-:|
|
||||||
|
|
||||||
| QQ Group | [](https://qm.qq.com/q/CMmPbGw0jA) | [](https://qm.qq.com/q/8zJMLjqy2Y) | [](https://qm.qq.com/q/CMmPbGw0jA) | [](https://qm.qq.com/q/I6LU87a0Yq) |
|
| QQ Group | [](https://qm.qq.com/q/CMmPbGw0jA) | [](https://qm.qq.com/q/8zJMLjqy2Y) | [](https://qm.qq.com/q/HaRcfrHpUk) | [](https://qm.qq.com/q/I6LU87a0Yq) |
|
||||||
|:-:|:-:|:-:|:-:|:-:|
|
|:-:|:-:|:-:|:-:|:-:|
|
||||||
|
|
||||||
| Telegram | [](https://t.me/napcatqq) |
|
| Telegram | [](https://t.me/napcatqq) |
|
||||||
|:-:|:-:|
|
|:-:|:-:|
|
||||||
|
|
||||||
| DeepWiki | [](https://deepwiki.com/NapNeko/NapCatQQ) |
|
|
||||||
|:-:|:-:|
|
|
||||||
|
|
||||||
> 请不要在其余社区提及本项目(包括其余协议端/相关应用端项目)引发争论,如有建议到达官方交流群讨论或PR。
|
|
||||||
|
|
||||||
## Thanks
|
## Thanks
|
||||||
|
|
||||||
+ [Lagrange](https://github.com/LagrangeDev/Lagrange.Core) 对本项目的大力支持 参考部分代码 已获授权
|
+ [Lagrange](https://github.com/LagrangeDev/Lagrange.Core) 对本项目的大力支持 参考部分代码 已获授权
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ set NAPCAT_LAUNCHER_PATH=%cd%\NapCatWinBootMain.exe
|
|||||||
set NAPCAT_MAIN_PATH=%cd%\napcat.mjs
|
set NAPCAT_MAIN_PATH=%cd%\napcat.mjs
|
||||||
:loop_read
|
:loop_read
|
||||||
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" /v "UninstallString"') do (
|
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" /v "UninstallString"') do (
|
||||||
set "RetString=%%~b"
|
set RetString=%%b
|
||||||
goto :napcat_boot
|
goto :napcat_boot
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ for %%a in ("%RetString%") do (
|
|||||||
set "pathWithoutUninstall=%%~dpa"
|
set "pathWithoutUninstall=%%~dpa"
|
||||||
)
|
)
|
||||||
|
|
||||||
set "QQPath=%pathWithoutUninstall%QQ.exe"
|
SET QQPath=%pathWithoutUninstall%QQ.exe
|
||||||
|
|
||||||
if not exist "%QQpath%" (
|
if not exist "%QQpath%" (
|
||||||
echo provided QQ path is invalid
|
echo provided QQ path is invalid
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ set NAPCAT_LAUNCHER_PATH=%cd%\NapCatWinBootMain.exe
|
|||||||
set NAPCAT_MAIN_PATH=%cd%\napcat.mjs
|
set NAPCAT_MAIN_PATH=%cd%\napcat.mjs
|
||||||
:loop_read
|
:loop_read
|
||||||
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" /v "UninstallString"') do (
|
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" /v "UninstallString"') do (
|
||||||
set "RetString=%%~b"
|
set RetString=%%b
|
||||||
goto :napcat_boot
|
goto :napcat_boot
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ for %%a in ("%RetString%") do (
|
|||||||
set "pathWithoutUninstall=%%~dpa"
|
set "pathWithoutUninstall=%%~dpa"
|
||||||
)
|
)
|
||||||
|
|
||||||
set "QQPath=%pathWithoutUninstall%QQ.exe"
|
SET QQPath=%pathWithoutUninstall%QQ.exe
|
||||||
|
|
||||||
if not exist "%QQpath%" (
|
if not exist "%QQpath%" (
|
||||||
echo provided QQ path is invalid
|
echo provided QQ path is invalid
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ set NAPCAT_LAUNCHER_PATH=%cd%\NapCatWinBootMain.exe
|
|||||||
set NAPCAT_MAIN_PATH=%cd%\napcat.mjs
|
set NAPCAT_MAIN_PATH=%cd%\napcat.mjs
|
||||||
:loop_read
|
:loop_read
|
||||||
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" /v "UninstallString"') do (
|
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" /v "UninstallString"') do (
|
||||||
set "RetString=%%~b"
|
set RetString=%%b
|
||||||
goto :napcat_boot
|
goto :napcat_boot
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ for %%a in ("%RetString%") do (
|
|||||||
set "pathWithoutUninstall=%%~dpa"
|
set "pathWithoutUninstall=%%~dpa"
|
||||||
)
|
)
|
||||||
|
|
||||||
set "QQPath=%pathWithoutUninstall%QQ.exe"
|
SET QQPath=%pathWithoutUninstall%QQ.exe
|
||||||
|
|
||||||
if not exist "%QQPath%" (
|
if not exist "%QQPath%" (
|
||||||
echo provided QQ path is invalid
|
echo provided QQ path is invalid
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ set NAPCAT_LAUNCHER_PATH=%cd%\NapCatWinBootMain.exe
|
|||||||
set NAPCAT_MAIN_PATH=%cd%\napcat.mjs
|
set NAPCAT_MAIN_PATH=%cd%\napcat.mjs
|
||||||
:loop_read
|
:loop_read
|
||||||
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" /v "UninstallString"') do (
|
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" /v "UninstallString"') do (
|
||||||
set "RetString=%%~b"
|
set RetString=%%b
|
||||||
goto :napcat_boot
|
goto :napcat_boot
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ for %%a in ("%RetString%") do (
|
|||||||
set "pathWithoutUninstall=%%~dpa"
|
set "pathWithoutUninstall=%%~dpa"
|
||||||
)
|
)
|
||||||
|
|
||||||
set "QQPath=%pathWithoutUninstall%QQ.exe"
|
SET QQPath=%pathWithoutUninstall%QQ.exe
|
||||||
|
|
||||||
if not exist "%QQPath%" (
|
if not exist "%QQPath%" (
|
||||||
echo provided QQ path is invalid
|
echo provided QQ path is invalid
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"name": "NapCatQQ",
|
"name": "NapCatQQ",
|
||||||
"slug": "NapCat.Framework",
|
"slug": "NapCat.Framework",
|
||||||
"description": "高性能的 OneBot 11 协议实现",
|
"description": "高性能的 OneBot 11 协议实现",
|
||||||
"version": "4.8.119",
|
"version": "4.8.109",
|
||||||
"icon": "./logo.png",
|
"icon": "./logo.png",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
|||||||
24
napcat.webui/package-lock.json
generated
24
napcat.webui/package-lock.json
generated
@@ -93,7 +93,6 @@
|
|||||||
"@types/node": "^22.12.0",
|
"@types/node": "^22.12.0",
|
||||||
"@types/path-browserify": "^1.0.3",
|
"@types/path-browserify": "^1.0.3",
|
||||||
"@types/react": "^19.0.8",
|
"@types/react": "^19.0.8",
|
||||||
"@types/react-color": "^3.0.13",
|
|
||||||
"@types/react-dom": "^19.0.3",
|
"@types/react-dom": "^19.0.3",
|
||||||
"@types/react-window": "^1.8.8",
|
"@types/react-window": "^1.8.8",
|
||||||
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
||||||
@@ -8153,19 +8152,6 @@
|
|||||||
"csstype": "^3.0.2"
|
"csstype": "^3.0.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@types/react-color": {
|
|
||||||
"version": "3.0.13",
|
|
||||||
"resolved": "https://registry.npmjs.org/@types/react-color/-/react-color-3.0.13.tgz",
|
|
||||||
"integrity": "sha512-2c/9FZ4ixC5T3JzN0LP5Cke2Mf0MKOP2Eh0NPDPWmuVH3NjPyhEjqNMQpN1Phr5m74egAy+p2lYNAFrX1z9Yrg==",
|
|
||||||
"dev": true,
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"@types/reactcss": "*"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"@types/react": "*"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@types/react-dom": {
|
"node_modules/@types/react-dom": {
|
||||||
"version": "19.1.6",
|
"version": "19.1.6",
|
||||||
"resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-19.1.6.tgz",
|
"resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-19.1.6.tgz",
|
||||||
@@ -8186,16 +8172,6 @@
|
|||||||
"@types/react": "*"
|
"@types/react": "*"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@types/reactcss": {
|
|
||||||
"version": "1.2.13",
|
|
||||||
"resolved": "https://registry.npmjs.org/@types/reactcss/-/reactcss-1.2.13.tgz",
|
|
||||||
"integrity": "sha512-gi3S+aUi6kpkF5vdhUsnkwbiSEIU/BEJyD7kBy2SudWBUuKmJk8AQKE0OVcQQeEy40Azh0lV6uynxlikYIJuwg==",
|
|
||||||
"dev": true,
|
|
||||||
"license": "MIT",
|
|
||||||
"peerDependencies": {
|
|
||||||
"@types/react": "*"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@types/unist": {
|
"node_modules/@types/unist": {
|
||||||
"version": "3.0.3",
|
"version": "3.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.3.tgz",
|
||||||
|
|||||||
@@ -95,7 +95,6 @@
|
|||||||
"@types/node": "^22.12.0",
|
"@types/node": "^22.12.0",
|
||||||
"@types/path-browserify": "^1.0.3",
|
"@types/path-browserify": "^1.0.3",
|
||||||
"@types/react": "^19.0.8",
|
"@types/react": "^19.0.8",
|
||||||
"@types/react-color": "^3.0.13",
|
|
||||||
"@types/react-dom": "^19.0.3",
|
"@types/react-dom": "^19.0.3",
|
||||||
"@types/react-window": "^1.8.8",
|
"@types/react-window": "^1.8.8",
|
||||||
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ export default function FileTable({
|
|||||||
setPreviewImages([])
|
setPreviewImages([])
|
||||||
setPreviewIndex(0)
|
setPreviewIndex(0)
|
||||||
setShowImage(false)
|
setShowImage(false)
|
||||||
setPage(1)
|
|
||||||
}, [currentPath])
|
}, [currentPath])
|
||||||
|
|
||||||
const onPreviewImage = (name: string, images: PreviewImage[]) => {
|
const onPreviewImage = (name: string, images: PreviewImage[]) => {
|
||||||
|
|||||||
@@ -171,8 +171,7 @@ const GenericForm = <T extends keyof NetworkConfigType>({
|
|||||||
|
|
||||||
export default GenericForm
|
export default GenericForm
|
||||||
export function random_token(length: number) {
|
export function random_token(length: number) {
|
||||||
const chars =
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@#$%^&*()-_=+[]{}|;:,.<>?'
|
||||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@#$%^&*()-_=+[]{}|;:,.<>?'
|
|
||||||
let result = ''
|
let result = ''
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
result += chars.charAt(Math.floor(Math.random() * chars.length))
|
result += chars.charAt(Math.floor(Math.random() * chars.length))
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import CryptoJS from 'crypto-js'
|
|
||||||
import { EventSourcePolyfill } from 'event-source-polyfill'
|
import { EventSourcePolyfill } from 'event-source-polyfill'
|
||||||
|
|
||||||
import { LogLevel } from '@/const/enum'
|
import { LogLevel } from '@/const/enum'
|
||||||
|
|
||||||
import { serverRequest } from '@/utils/request'
|
import { serverRequest } from '@/utils/request'
|
||||||
|
import CryptoJS from "crypto-js";
|
||||||
export interface Log {
|
export interface Log {
|
||||||
level: LogLevel
|
level: LogLevel
|
||||||
message: string
|
message: string
|
||||||
@@ -18,7 +17,7 @@ export default class WebUIManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static async loginWithToken(token: string) {
|
public static async loginWithToken(token: string) {
|
||||||
const sha256 = CryptoJS.SHA256(token + '.napcat').toString()
|
const sha256 = CryptoJS.SHA256(token + '.napcat').toString();
|
||||||
const { data } = await serverRequest.post<ServerResponse<AuthResponse>>(
|
const { data } = await serverRequest.post<ServerResponse<AuthResponse>>(
|
||||||
'/auth/login',
|
'/auth/login',
|
||||||
{ hash: sha256 }
|
{ hash: sha256 }
|
||||||
@@ -197,13 +196,4 @@ export default class WebUIManager {
|
|||||||
)
|
)
|
||||||
return data.data
|
return data.data
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理缓存
|
|
||||||
public static async cleanCache() {
|
|
||||||
const { data } =
|
|
||||||
await serverRequest.post<
|
|
||||||
ServerResponse<{ result: boolean; message: string }>
|
|
||||||
>('/base/CleanCache')
|
|
||||||
return data.data
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,16 @@
|
|||||||
import { Button } from '@heroui/button'
|
|
||||||
import { Input } from '@heroui/input'
|
import { Input } from '@heroui/input'
|
||||||
import { Switch } from '@heroui/switch'
|
import { Switch } from '@heroui/switch'
|
||||||
import { useRequest } from 'ahooks'
|
import { useRequest } from 'ahooks'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect } from 'react'
|
||||||
import { Controller, useForm } from 'react-hook-form'
|
import { Controller, useForm } from 'react-hook-form'
|
||||||
import toast from 'react-hot-toast'
|
import toast from 'react-hot-toast'
|
||||||
import { MdDeleteSweep } from 'react-icons/md'
|
|
||||||
|
|
||||||
import SaveButtons from '@/components/button/save_buttons'
|
import SaveButtons from '@/components/button/save_buttons'
|
||||||
import PageLoading from '@/components/page_loading'
|
import PageLoading from '@/components/page_loading'
|
||||||
|
|
||||||
import useDialog from '@/hooks/use-dialog'
|
|
||||||
|
|
||||||
import WebUIManager from '@/controllers/webui_manager'
|
import WebUIManager from '@/controllers/webui_manager'
|
||||||
|
|
||||||
const ServerConfigCard = () => {
|
const ServerConfigCard = () => {
|
||||||
const dialog = useDialog()
|
|
||||||
const [isCleaningCache, setIsCleaningCache] = useState(false)
|
|
||||||
const {
|
const {
|
||||||
data: configData,
|
data: configData,
|
||||||
loading: configLoading,
|
loading: configLoading,
|
||||||
@@ -75,42 +69,6 @@ const ServerConfigCard = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleCleanCache = () => {
|
|
||||||
dialog.confirm({
|
|
||||||
title: '清理缓存',
|
|
||||||
content: (
|
|
||||||
<div className="space-y-2">
|
|
||||||
<p>确定要清理缓存吗?此操作将清理以下内容:</p>
|
|
||||||
<ul className="list-disc list-inside text-sm text-default-600">
|
|
||||||
<li>临时文件夹中的所有文件</li>
|
|
||||||
<li>图片缓存 (Pic)</li>
|
|
||||||
<li>语音缓存 (Ptt)</li>
|
|
||||||
<li>视频缓存 (Video)</li>
|
|
||||||
<li>文件缓存 (File)</li>
|
|
||||||
<li>日志文件 (log)</li>
|
|
||||||
</ul>
|
|
||||||
<p className="text-warning text-sm">此操作不可撤销,请谨慎操作。</p>
|
|
||||||
</div>
|
|
||||||
),
|
|
||||||
onConfirm: async () => {
|
|
||||||
setIsCleaningCache(true)
|
|
||||||
try {
|
|
||||||
const result = await WebUIManager.cleanCache()
|
|
||||||
if (result.result) {
|
|
||||||
toast.success(result.message || '缓存清理成功')
|
|
||||||
} else {
|
|
||||||
toast.error(result.message || '缓存清理失败')
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
const msg = (error as Error).message
|
|
||||||
toast.error(`清理缓存失败: ${msg}`)
|
|
||||||
} finally {
|
|
||||||
setIsCleaningCache(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
reset()
|
reset()
|
||||||
}, [configData])
|
}, [configData])
|
||||||
@@ -173,30 +131,6 @@ const ServerConfigCard = () => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex flex-col gap-2">
|
|
||||||
<div className="flex-shrink-0 w-full">维护操作</div>
|
|
||||||
<div className="flex flex-col gap-2 p-4 rounded-lg bg-default-50">
|
|
||||||
<div className="flex items-start justify-between gap-2">
|
|
||||||
<div className="flex-1">
|
|
||||||
<div className="font-medium">清理缓存</div>
|
|
||||||
<div className="text-sm text-default-500">
|
|
||||||
清理临时文件、图片、语音、视频、文件缓存和日志文件
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<Button
|
|
||||||
color="danger"
|
|
||||||
variant="flat"
|
|
||||||
startContent={<MdDeleteSweep size={20} />}
|
|
||||||
onPress={handleCleanCache}
|
|
||||||
isLoading={isCleaningCache}
|
|
||||||
isDisabled={!!configError}
|
|
||||||
>
|
|
||||||
清理缓存
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
<div className="flex-shrink-0 w-full">安全配置</div>
|
<div className="flex-shrink-0 w-full">安全配置</div>
|
||||||
<Controller
|
<Controller
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "napcat",
|
"name": "napcat",
|
||||||
"version": "4.8.119",
|
"version": "4.8.98",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "napcat",
|
"name": "napcat",
|
||||||
"version": "4.8.119",
|
"version": "4.8.98",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^5.0.0",
|
"express": "^5.0.0",
|
||||||
"silk-wasm": "^3.6.1",
|
"silk-wasm": "^3.6.1",
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "napcat",
|
"name": "napcat",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "4.8.119",
|
"version": "4.8.109",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build:universal": "npm run build:webui && vite build --mode universal || exit 1",
|
"build:universal": "npm run build:webui && vite build --mode universal || exit 1",
|
||||||
"build:framework": "npm run build:webui && vite build --mode framework || exit 1",
|
"build:framework": "npm run build:webui && vite build --mode framework || exit 1",
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
export const napCatVersion = '4.8.119';
|
export const napCatVersion = '4.8.109';
|
||||||
|
|||||||
4
src/core/external/appid.json
vendored
4
src/core/external/appid.json
vendored
@@ -386,9 +386,5 @@
|
|||||||
"9.9.21-39038": {
|
"9.9.21-39038": {
|
||||||
"appid": 537313906,
|
"appid": 537313906,
|
||||||
"qua": "V1_WIN_NQ_9.9.21_39038_GW_B"
|
"qua": "V1_WIN_NQ_9.9.21_39038_GW_B"
|
||||||
},
|
|
||||||
"9.9.22-40362": {
|
|
||||||
"appid": 537314212,
|
|
||||||
"qua": "V1_WIN_NQ_9.9.22_40362_GW_B"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
6
src/core/external/offset.json
vendored
6
src/core/external/offset.json
vendored
@@ -507,12 +507,8 @@
|
|||||||
"send": "7B025C8",
|
"send": "7B025C8",
|
||||||
"recv": "7B05F58"
|
"recv": "7B05F58"
|
||||||
},
|
},
|
||||||
"9.9.21-39038-x64": {
|
"9.9.21-39038-x64": {
|
||||||
"send": "313FB58",
|
"send": "313FB58",
|
||||||
"recv": "31432FC"
|
"recv": "31432FC"
|
||||||
},
|
|
||||||
"9.9.22-40362-x64": {
|
|
||||||
"send": "31C0EB8",
|
|
||||||
"recv": "31C465C"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,10 +4,9 @@ import { NapCatCore } from '@/core';
|
|||||||
import { NapCatOneBot11Adapter, OB11Return } from '@/onebot';
|
import { NapCatOneBot11Adapter, OB11Return } from '@/onebot';
|
||||||
import { NetworkAdapterConfig } from '../config/config';
|
import { NetworkAdapterConfig } from '../config/config';
|
||||||
import { TSchema } from '@sinclair/typebox';
|
import { TSchema } from '@sinclair/typebox';
|
||||||
import { StreamPacket, StreamPacketBasic, StreamStatus } from './stream/StreamBasic';
|
|
||||||
|
|
||||||
export class OB11Response {
|
export class OB11Response {
|
||||||
private static createResponse<T>(data: T, status: string, retcode: number, message: string = '', echo: unknown = null, useStream: boolean = false): OB11Return<T> {
|
private static createResponse<T>(data: T, status: string, retcode: number, message: string = '', echo: unknown = null): OB11Return<T> {
|
||||||
return {
|
return {
|
||||||
status,
|
status,
|
||||||
retcode,
|
retcode,
|
||||||
@@ -15,32 +14,28 @@ export class OB11Response {
|
|||||||
message,
|
message,
|
||||||
wording: message,
|
wording: message,
|
||||||
echo,
|
echo,
|
||||||
stream: useStream ? 'stream-action' : 'normal-action'
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static res<T>(data: T, status: string, retcode: number, message: string = '', echo: unknown = null, useStream: boolean = false): OB11Return<T> {
|
static res<T>(data: T, status: string, retcode: number, message: string = ''): OB11Return<T> {
|
||||||
return this.createResponse(data, status, retcode, message, echo, useStream);
|
return this.createResponse(data, status, retcode, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ok<T>(data: T, echo: unknown = null, useStream: boolean = false): OB11Return<T> {
|
static ok<T>(data: T, echo: unknown = null): OB11Return<T> {
|
||||||
return this.createResponse(data, 'ok', 0, '', echo, useStream);
|
return this.createResponse(data, 'ok', 0, '', echo);
|
||||||
}
|
}
|
||||||
|
|
||||||
static error(err: string, retcode: number, echo: unknown = null, useStream: boolean = false): OB11Return<null | StreamPacketBasic> {
|
static error(err: string, retcode: number, echo: unknown = null): OB11Return<null> {
|
||||||
return this.createResponse(useStream ? { type: StreamStatus.Error, data_type: 'error' } : null, 'failed', retcode, err, echo, useStream);
|
return this.createResponse(null, 'failed', retcode, err, echo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export abstract class OneBotRequestToolkit {
|
|
||||||
abstract send<T>(packet: StreamPacket<T>): Promise<void>;
|
|
||||||
}
|
|
||||||
export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
||||||
actionName: typeof ActionName[keyof typeof ActionName] = ActionName.Unknown;
|
actionName: typeof ActionName[keyof typeof ActionName] = ActionName.Unknown;
|
||||||
core: NapCatCore;
|
core: NapCatCore;
|
||||||
private validate?: ValidateFunction<unknown> = undefined;
|
private validate?: ValidateFunction<unknown> = undefined;
|
||||||
payloadSchema?: TSchema = undefined;
|
payloadSchema?: TSchema = undefined;
|
||||||
obContext: NapCatOneBot11Adapter;
|
obContext: NapCatOneBot11Adapter;
|
||||||
useStream: boolean = false;
|
|
||||||
|
|
||||||
constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
||||||
this.obContext = obContext;
|
this.obContext = obContext;
|
||||||
@@ -62,33 +57,33 @@ export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
|||||||
return { valid: true };
|
return { valid: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
public async handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig, req: OneBotRequestToolkit = { send: async () => { } }, echo?: string): Promise<OB11Return<ReturnDataType | StreamPacketBasic | null>> {
|
public async handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig): Promise<OB11Return<ReturnDataType | null>> {
|
||||||
const result = await this.check(payload);
|
const result = await this.check(payload);
|
||||||
if (!result.valid) {
|
if (!result.valid) {
|
||||||
return OB11Response.error(result.message, 400);
|
return OB11Response.error(result.message, 400);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const resData = await this._handle(payload, adaptername, config, req);
|
const resData = await this._handle(payload, adaptername, config);
|
||||||
return OB11Response.ok(resData, echo, this.useStream);
|
return OB11Response.ok(resData);
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
this.core.context.logger.logError('发生错误', e);
|
this.core.context.logger.logError('发生错误', e);
|
||||||
return OB11Response.error((e as Error).message.toString() || (e as Error)?.stack?.toString() || '未知错误,可能操作超时', 200, echo, this.useStream);
|
return OB11Response.error((e as Error).message.toString() || (e as Error)?.stack?.toString() || '未知错误,可能操作超时', 200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async websocketHandle(payload: PayloadType, echo: unknown, adaptername: string, config: NetworkAdapterConfig, req: OneBotRequestToolkit = { send: async () => { } }): Promise<OB11Return<ReturnDataType | StreamPacketBasic | null>> {
|
public async websocketHandle(payload: PayloadType, echo: unknown, adaptername: string, config: NetworkAdapterConfig): Promise<OB11Return<ReturnDataType | null>> {
|
||||||
const result = await this.check(payload);
|
const result = await this.check(payload);
|
||||||
if (!result.valid) {
|
if (!result.valid) {
|
||||||
return OB11Response.error(result.message, 1400, echo, this.useStream);
|
return OB11Response.error(result.message, 1400, echo);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const resData = await this._handle(payload, adaptername, config, req);
|
const resData = await this._handle(payload, adaptername, config);
|
||||||
return OB11Response.ok(resData, echo, this.useStream);
|
return OB11Response.ok(resData, echo);
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
this.core.context.logger.logError('发生错误', e);
|
this.core.context.logger.logError('发生错误', e);
|
||||||
return OB11Response.error(((e as Error).message.toString() || (e as Error).stack?.toString()) ?? 'Error', 1200, echo, this.useStream);
|
return OB11Response.error(((e as Error).message.toString() || (e as Error).stack?.toString()) ?? 'Error', 1200, echo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract _handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig, req: OneBotRequestToolkit): Promise<ReturnDataType>;
|
abstract _handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig): Promise<ReturnDataType>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,12 +41,12 @@ export class GetFileBase extends OneBotAction<GetFilePayload, GetFileResponse> {
|
|||||||
let url = '';
|
let url = '';
|
||||||
if (mixElement?.picElement && rawMessage) {
|
if (mixElement?.picElement && rawMessage) {
|
||||||
const tempData =
|
const tempData =
|
||||||
await this.obContext.apis.MsgApi.rawToOb11Converters.picElement?.(mixElement?.picElement, rawMessage, mixElement, { parseMultMsg: false, disableGetUrl: false, quick_reply: true }) as OB11MessageImage | undefined;
|
await this.obContext.apis.MsgApi.rawToOb11Converters.picElement?.(mixElement?.picElement, rawMessage, mixElement, { parseMultMsg: false }) as OB11MessageImage | undefined;
|
||||||
url = tempData?.data.url ?? '';
|
url = tempData?.data.url ?? '';
|
||||||
}
|
}
|
||||||
if (mixElement?.videoElement && rawMessage) {
|
if (mixElement?.videoElement && rawMessage) {
|
||||||
const tempData =
|
const tempData =
|
||||||
await this.obContext.apis.MsgApi.rawToOb11Converters.videoElement?.(mixElement?.videoElement, rawMessage, mixElement, { parseMultMsg: false, disableGetUrl: false, quick_reply: true }) as OB11MessageVideo | undefined;
|
await this.obContext.apis.MsgApi.rawToOb11Converters.videoElement?.(mixElement?.videoElement, rawMessage, mixElement, { parseMultMsg: false }) as OB11MessageVideo | undefined;
|
||||||
url = tempData?.data.url ?? '';
|
url = tempData?.data.url ?? '';
|
||||||
}
|
}
|
||||||
const res: GetFileResponse = {
|
const res: GetFileResponse = {
|
||||||
|
|||||||
@@ -130,18 +130,10 @@ import { DoGroupAlbumComment } from './extends/DoGroupAlbumComment';
|
|||||||
import { GetGroupAlbumMediaList } from './extends/GetGroupAlbumMediaList';
|
import { GetGroupAlbumMediaList } from './extends/GetGroupAlbumMediaList';
|
||||||
import { SetGroupAlbumMediaLike } from './extends/SetGroupAlbumMediaLike';
|
import { SetGroupAlbumMediaLike } from './extends/SetGroupAlbumMediaLike';
|
||||||
import { DelGroupAlbumMedia } from './extends/DelGroupAlbumMedia';
|
import { DelGroupAlbumMedia } from './extends/DelGroupAlbumMedia';
|
||||||
import { CleanStreamTempFile } from './stream/CleanStreamTempFile';
|
|
||||||
import { DownloadFileStream } from './stream/DownloadFileStream';
|
|
||||||
import { TestDownloadStream } from './stream/TestStreamDownload';
|
|
||||||
import { UploadFileStream } from './stream/UploadFileStream';
|
|
||||||
|
|
||||||
export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
||||||
|
|
||||||
const actionHandlers = [
|
const actionHandlers = [
|
||||||
new CleanStreamTempFile(obContext, core),
|
|
||||||
new DownloadFileStream(obContext, core),
|
|
||||||
new TestDownloadStream(obContext, core),
|
|
||||||
new UploadFileStream(obContext, core),
|
|
||||||
new DelGroupAlbumMedia(obContext, core),
|
new DelGroupAlbumMedia(obContext, core),
|
||||||
new SetGroupAlbumMediaLike(obContext, core),
|
new SetGroupAlbumMediaLike(obContext, core),
|
||||||
new DoGroupAlbumComment(obContext, core),
|
new DoGroupAlbumComment(obContext, core),
|
||||||
|
|||||||
@@ -10,14 +10,6 @@ export interface InvalidCheckResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const ActionName = {
|
export const ActionName = {
|
||||||
// 所有 Normal Stream Api 表示并未流传输 表示与流传输有关
|
|
||||||
CleanStreamTempFile: 'clean_stream_temp_file',
|
|
||||||
|
|
||||||
// 所有 Upload/Download Stream Api 应当 _stream 结尾
|
|
||||||
TestDownloadStream: 'test_download_stream',
|
|
||||||
UploadFileStream: 'upload_file_stream',
|
|
||||||
DownloadFileStream: 'download_file_stream',
|
|
||||||
|
|
||||||
DelGroupAlbumMedia: 'del_group_album_media',
|
DelGroupAlbumMedia: 'del_group_album_media',
|
||||||
SetGroupAlbumMediaLike: 'set_group_album_media_like',
|
SetGroupAlbumMediaLike: 'set_group_album_media_like',
|
||||||
DoGroupAlbumComment: 'do_group_album_comment',
|
DoGroupAlbumComment: 'do_group_album_comment',
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
import { ActionName } from '@/onebot/action/router';
|
|
||||||
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
|
||||||
import { join } from 'node:path';
|
|
||||||
import { readdir, unlink } from 'node:fs/promises';
|
|
||||||
|
|
||||||
export class CleanStreamTempFile extends OneBotAction<void, void> {
|
|
||||||
override actionName = ActionName.CleanStreamTempFile;
|
|
||||||
|
|
||||||
async _handle(_payload: void): Promise<void> {
|
|
||||||
try {
|
|
||||||
// 获取临时文件夹路径
|
|
||||||
const tempPath = this.core.NapCatTempPath;
|
|
||||||
|
|
||||||
// 读取文件夹中的所有文件
|
|
||||||
const files = await readdir(tempPath);
|
|
||||||
|
|
||||||
// 删除每个文件
|
|
||||||
const deletePromises = files.map(async (file) => {
|
|
||||||
const filePath = join(tempPath, file);
|
|
||||||
try {
|
|
||||||
await unlink(filePath);
|
|
||||||
this.core.context.logger.log(`已删除文件: ${filePath}`);
|
|
||||||
} catch (err: unknown) {
|
|
||||||
this.core.context.logger.log(`删除文件 ${filePath} 失败: ${(err as Error).message}`);
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
await Promise.all(deletePromises);
|
|
||||||
} catch (err: unknown) {
|
|
||||||
this.core.context.logger.log(`清理流临时文件失败: ${(err as Error).message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
import { ActionName } from '@/onebot/action/router';
|
|
||||||
import { OneBotAction, OneBotRequestToolkit } from '@/onebot/action/OneBotAction';
|
|
||||||
import { Static, Type } from '@sinclair/typebox';
|
|
||||||
import { NetworkAdapterConfig } from '@/onebot/config/config';
|
|
||||||
import { StreamPacket, StreamStatus } from './StreamBasic';
|
|
||||||
import fs from 'fs';
|
|
||||||
import { FileNapCatOneBotUUID } from '@/common/file-uuid';
|
|
||||||
const SchemaData = Type.Object({
|
|
||||||
file: Type.Optional(Type.String()),
|
|
||||||
file_id: Type.Optional(Type.String()),
|
|
||||||
chunk_size: Type.Optional(Type.Number({ default: 64 * 1024 })) // 默认64KB分块
|
|
||||||
});
|
|
||||||
|
|
||||||
type Payload = Static<typeof SchemaData>;
|
|
||||||
|
|
||||||
// 下载结果类型
|
|
||||||
interface DownloadResult {
|
|
||||||
// 文件信息
|
|
||||||
file_name?: string;
|
|
||||||
file_size?: number;
|
|
||||||
chunk_size?: number;
|
|
||||||
|
|
||||||
// 分片数据
|
|
||||||
index?: number;
|
|
||||||
data?: string;
|
|
||||||
size?: number;
|
|
||||||
progress?: number;
|
|
||||||
base64_size?: number;
|
|
||||||
|
|
||||||
// 完成信息
|
|
||||||
total_chunks?: number;
|
|
||||||
total_bytes?: number;
|
|
||||||
message?: string;
|
|
||||||
data_type?: 'file_info' | 'file_chunk' | 'file_complete';
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DownloadFileStream extends OneBotAction<Payload, StreamPacket<DownloadResult>> {
|
|
||||||
override actionName = ActionName.DownloadFileStream;
|
|
||||||
override payloadSchema = SchemaData;
|
|
||||||
override useStream = true;
|
|
||||||
|
|
||||||
async _handle(payload: Payload, _adaptername: string, _config: NetworkAdapterConfig, req: OneBotRequestToolkit): Promise<StreamPacket<DownloadResult>> {
|
|
||||||
try {
|
|
||||||
payload.file ||= payload.file_id || '';
|
|
||||||
const chunkSize = payload.chunk_size || 64 * 1024;
|
|
||||||
let downloadPath = '';
|
|
||||||
let fileName = '';
|
|
||||||
let fileSize = 0;
|
|
||||||
|
|
||||||
//接收消息标记模式
|
|
||||||
const contextMsgFile = FileNapCatOneBotUUID.decode(payload.file);
|
|
||||||
if (contextMsgFile && contextMsgFile.msgId && contextMsgFile.elementId) {
|
|
||||||
const { peer, msgId, elementId } = contextMsgFile;
|
|
||||||
downloadPath = await this.core.apis.FileApi.downloadMedia(msgId, peer.chatType, peer.peerUid, elementId, '', '');
|
|
||||||
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');
|
|
||||||
fileSize = parseInt(mixElementInner.fileSize?.toString() ?? '0');
|
|
||||||
fileName = mixElementInner.fileName ?? '';
|
|
||||||
}
|
|
||||||
//群文件模式
|
|
||||||
else if (FileNapCatOneBotUUID.decodeModelId(payload.file)) {
|
|
||||||
const contextModelIdFile = FileNapCatOneBotUUID.decodeModelId(payload.file);
|
|
||||||
if (contextModelIdFile && contextModelIdFile.modelId) {
|
|
||||||
const { peer, modelId } = contextModelIdFile;
|
|
||||||
downloadPath = await this.core.apis.FileApi.downloadFileForModelId(peer, modelId, '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//搜索名字模式
|
|
||||||
else {
|
|
||||||
const searchResult = (await this.core.apis.FileApi.searchForFile([payload.file]));
|
|
||||||
if (searchResult) {
|
|
||||||
downloadPath = await this.core.apis.FileApi.downloadFileById(searchResult.id, parseInt(searchResult.fileSize));
|
|
||||||
fileSize = parseInt(searchResult.fileSize);
|
|
||||||
fileName = searchResult.fileName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!downloadPath) {
|
|
||||||
throw new Error('file not found');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文件大小
|
|
||||||
const stats = await fs.promises.stat(downloadPath);
|
|
||||||
const totalSize = fileSize || stats.size;
|
|
||||||
|
|
||||||
// 发送文件信息
|
|
||||||
await req.send({
|
|
||||||
type: StreamStatus.Stream,
|
|
||||||
data_type: 'file_info',
|
|
||||||
file_name: fileName,
|
|
||||||
file_size: totalSize,
|
|
||||||
chunk_size: chunkSize
|
|
||||||
});
|
|
||||||
|
|
||||||
// 创建读取流并分块发送
|
|
||||||
const readStream = fs.createReadStream(downloadPath, { highWaterMark: chunkSize });
|
|
||||||
let chunkIndex = 0;
|
|
||||||
let bytesRead = 0;
|
|
||||||
|
|
||||||
for await (const chunk of readStream) {
|
|
||||||
const base64Chunk = chunk.toString('base64');
|
|
||||||
bytesRead += chunk.length;
|
|
||||||
|
|
||||||
await req.send({
|
|
||||||
type: StreamStatus.Stream,
|
|
||||||
data_type: 'file_chunk',
|
|
||||||
index: chunkIndex,
|
|
||||||
data: base64Chunk,
|
|
||||||
size: chunk.length,
|
|
||||||
progress: Math.round((bytesRead / totalSize) * 100),
|
|
||||||
base64_size: base64Chunk.length
|
|
||||||
});
|
|
||||||
|
|
||||||
chunkIndex++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回完成状态
|
|
||||||
return {
|
|
||||||
type: StreamStatus.Response,
|
|
||||||
data_type: 'file_complete',
|
|
||||||
total_chunks: chunkIndex,
|
|
||||||
total_bytes: bytesRead,
|
|
||||||
message: 'Download completed'
|
|
||||||
};
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(`Download failed: ${(error as Error).message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
# Stream-Api
|
|
||||||
|
|
||||||
## 流式接口
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import { OneBotAction, OneBotRequestToolkit } from "../OneBotAction";
|
|
||||||
import { NetworkAdapterConfig } from "@/onebot/config/config";
|
|
||||||
export type StreamPacketBasic = {
|
|
||||||
type: StreamStatus;
|
|
||||||
data_type?: string;
|
|
||||||
};
|
|
||||||
export type StreamPacket<T> = T & StreamPacketBasic;
|
|
||||||
export enum StreamStatus {
|
|
||||||
Stream = 'stream', // 分片流数据包
|
|
||||||
Response = 'response', // 流最终响应
|
|
||||||
Reset = 'reset', // 重置流
|
|
||||||
Error = 'error' // 流错误
|
|
||||||
}
|
|
||||||
export abstract class BasicStream<T, R> extends OneBotAction<T, StreamPacket<R>> {
|
|
||||||
abstract override _handle(_payload: T, _adaptername: string, _config: NetworkAdapterConfig, req: OneBotRequestToolkit): Promise<StreamPacket<R>>;
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import { ActionName } from '@/onebot/action/router';
|
|
||||||
import { OneBotAction, OneBotRequestToolkit } from '@/onebot/action/OneBotAction';
|
|
||||||
import { Static, Type } from '@sinclair/typebox';
|
|
||||||
import { NetworkAdapterConfig } from '@/onebot/config/config';
|
|
||||||
import { StreamPacket, StreamStatus } from './StreamBasic';
|
|
||||||
|
|
||||||
const SchemaData = Type.Object({
|
|
||||||
error: Type.Optional(Type.Boolean({ default: false }))
|
|
||||||
});
|
|
||||||
|
|
||||||
type Payload = Static<typeof SchemaData>;
|
|
||||||
|
|
||||||
export class TestDownloadStream extends OneBotAction<Payload, StreamPacket<{ data: string }>> {
|
|
||||||
override actionName = ActionName.TestDownloadStream;
|
|
||||||
override payloadSchema = SchemaData;
|
|
||||||
override useStream = true;
|
|
||||||
|
|
||||||
async _handle(_payload: Payload, _adaptername: string, _config: NetworkAdapterConfig, req: OneBotRequestToolkit) {
|
|
||||||
for (let i = 0; i < 10; i++) {
|
|
||||||
await req.send({ type: StreamStatus.Stream, data: `Index-> ${i + 1}`, data_type: 'data_chunk' });
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 100));
|
|
||||||
}
|
|
||||||
if( _payload.error ){
|
|
||||||
throw new Error('This is a test error');
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
type: StreamStatus.Response,
|
|
||||||
data_type: 'data_complete',
|
|
||||||
data: 'Stream transmission complete'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,346 +0,0 @@
|
|||||||
import { ActionName } from '@/onebot/action/router';
|
|
||||||
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
|
||||||
import { Static, Type } from '@sinclair/typebox';
|
|
||||||
import { NetworkAdapterConfig } from '@/onebot/config/config';
|
|
||||||
import { StreamPacket, StreamStatus } from './StreamBasic';
|
|
||||||
import fs from 'fs';
|
|
||||||
import { join as joinPath } from 'node:path';
|
|
||||||
import { randomUUID } from 'crypto';
|
|
||||||
import { createHash } from 'crypto';
|
|
||||||
import { unlink } from 'node:fs';
|
|
||||||
|
|
||||||
// 简化配置
|
|
||||||
const CONFIG = {
|
|
||||||
TIMEOUT: 10 * 60 * 1000, // 10分钟超时
|
|
||||||
MEMORY_THRESHOLD: 10 * 1024 * 1024, // 10MB,超过使用磁盘
|
|
||||||
MEMORY_LIMIT: 100 * 1024 * 1024 // 100MB内存总限制
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
const SchemaData = Type.Object({
|
|
||||||
stream_id: Type.String(),
|
|
||||||
chunk_data: Type.Optional(Type.String()),
|
|
||||||
chunk_index: Type.Optional(Type.Number()),
|
|
||||||
total_chunks: Type.Optional(Type.Number()),
|
|
||||||
file_size: Type.Optional(Type.Number()),
|
|
||||||
expected_sha256: Type.Optional(Type.String()),
|
|
||||||
is_complete: Type.Optional(Type.Boolean()),
|
|
||||||
filename: Type.Optional(Type.String()),
|
|
||||||
reset: Type.Optional(Type.Boolean()),
|
|
||||||
verify_only: Type.Optional(Type.Boolean()),
|
|
||||||
file_retention: Type.Number({ default: 5 * 60 * 1000 }) // 默认5分钟 回收 不设置或0为不回收
|
|
||||||
});
|
|
||||||
|
|
||||||
type Payload = Static<typeof SchemaData>;
|
|
||||||
|
|
||||||
// 简化流状态接口
|
|
||||||
interface StreamState {
|
|
||||||
id: string;
|
|
||||||
filename: string;
|
|
||||||
totalChunks: number;
|
|
||||||
receivedChunks: number;
|
|
||||||
missingChunks: Set<number>;
|
|
||||||
|
|
||||||
// 可选属性
|
|
||||||
fileSize?: number;
|
|
||||||
expectedSha256?: string;
|
|
||||||
|
|
||||||
// 存储策略
|
|
||||||
useMemory: boolean;
|
|
||||||
memoryChunks?: Map<number, Buffer>;
|
|
||||||
tempDir?: string;
|
|
||||||
finalPath?: string;
|
|
||||||
fileRetention?: number;
|
|
||||||
|
|
||||||
// 管理
|
|
||||||
createdAt: number;
|
|
||||||
timeoutId: NodeJS.Timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface StreamResult {
|
|
||||||
stream_id: string;
|
|
||||||
status: 'file_created' | 'chunk_received' | 'file_complete';
|
|
||||||
received_chunks: number;
|
|
||||||
total_chunks: number;
|
|
||||||
file_path?: string;
|
|
||||||
file_size?: number;
|
|
||||||
sha256?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class UploadFileStream extends OneBotAction<Payload, StreamPacket<StreamResult>> {
|
|
||||||
override actionName = ActionName.UploadFileStream;
|
|
||||||
override payloadSchema = SchemaData;
|
|
||||||
override useStream = true;
|
|
||||||
|
|
||||||
private static streams = new Map<string, StreamState>();
|
|
||||||
private static memoryUsage = 0;
|
|
||||||
|
|
||||||
async _handle(payload: Payload, _adaptername: string, _config: NetworkAdapterConfig): Promise<StreamPacket<StreamResult>> {
|
|
||||||
const { stream_id, reset, verify_only } = payload;
|
|
||||||
|
|
||||||
if (reset) {
|
|
||||||
this.cleanupStream(stream_id);
|
|
||||||
throw new Error('Stream reset completed');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (verify_only) {
|
|
||||||
const stream = UploadFileStream.streams.get(stream_id);
|
|
||||||
if (!stream) throw new Error('Stream not found');
|
|
||||||
return this.getStreamStatus(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
const stream = this.getOrCreateStream(payload);
|
|
||||||
|
|
||||||
if (payload.chunk_data && payload.chunk_index !== undefined) {
|
|
||||||
return await this.processChunk(stream, payload.chunk_data, payload.chunk_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (payload.is_complete || stream.receivedChunks === stream.totalChunks) {
|
|
||||||
return await this.completeStream(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.getStreamStatus(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private getOrCreateStream(payload: Payload): StreamState {
|
|
||||||
let stream = UploadFileStream.streams.get(payload.stream_id);
|
|
||||||
|
|
||||||
if (!stream) {
|
|
||||||
if (!payload.total_chunks) {
|
|
||||||
throw new Error('total_chunks required for new stream');
|
|
||||||
}
|
|
||||||
stream = this.createStream(payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
private createStream(payload: Payload): StreamState {
|
|
||||||
const { stream_id, total_chunks, file_size, filename, expected_sha256 } = payload;
|
|
||||||
|
|
||||||
const useMemory = this.shouldUseMemory(file_size);
|
|
||||||
if (useMemory && file_size && (UploadFileStream.memoryUsage + file_size) > CONFIG.MEMORY_LIMIT) {
|
|
||||||
throw new Error('Memory limit exceeded');
|
|
||||||
}
|
|
||||||
|
|
||||||
const stream: StreamState = {
|
|
||||||
id: stream_id,
|
|
||||||
filename: filename || `upload_${randomUUID()}`,
|
|
||||||
totalChunks: total_chunks!,
|
|
||||||
receivedChunks: 0,
|
|
||||||
missingChunks: new Set(Array.from({ length: total_chunks! }, (_, i) => i)),
|
|
||||||
fileSize: file_size,
|
|
||||||
expectedSha256: expected_sha256,
|
|
||||||
useMemory,
|
|
||||||
createdAt: Date.now(),
|
|
||||||
timeoutId: this.setupTimeout(stream_id),
|
|
||||||
fileRetention: payload.file_retention
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
if (useMemory) {
|
|
||||||
stream.memoryChunks = new Map();
|
|
||||||
if (file_size) UploadFileStream.memoryUsage += file_size;
|
|
||||||
} else {
|
|
||||||
this.setupDiskStorage(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
UploadFileStream.streams.set(stream_id, stream);
|
|
||||||
return stream;
|
|
||||||
} catch (error) {
|
|
||||||
// 如果设置存储失败,清理已创建的资源
|
|
||||||
clearTimeout(stream.timeoutId);
|
|
||||||
if (stream.tempDir && fs.existsSync(stream.tempDir)) {
|
|
||||||
try {
|
|
||||||
fs.rmSync(stream.tempDir, { recursive: true, force: true });
|
|
||||||
} catch (cleanupError) {
|
|
||||||
console.error(`Failed to cleanup temp dir during creation error:`, cleanupError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldUseMemory(fileSize?: number): boolean {
|
|
||||||
return fileSize !== undefined && fileSize <= CONFIG.MEMORY_THRESHOLD;
|
|
||||||
}
|
|
||||||
|
|
||||||
private setupDiskStorage(stream: StreamState): void {
|
|
||||||
const tempDir = joinPath(this.core.NapCatTempPath, `upload_${stream.id}`);
|
|
||||||
const finalPath = joinPath(this.core.NapCatTempPath, stream.filename);
|
|
||||||
|
|
||||||
fs.mkdirSync(tempDir, { recursive: true });
|
|
||||||
|
|
||||||
stream.tempDir = tempDir;
|
|
||||||
stream.finalPath = finalPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
private setupTimeout(streamId: string): NodeJS.Timeout {
|
|
||||||
return setTimeout(() => {
|
|
||||||
console.log(`Stream ${streamId} timeout`);
|
|
||||||
this.cleanupStream(streamId);
|
|
||||||
}, CONFIG.TIMEOUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async processChunk(stream: StreamState, chunkData: string, chunkIndex: number): Promise<StreamPacket<StreamResult>> {
|
|
||||||
// 验证索引
|
|
||||||
if (chunkIndex < 0 || chunkIndex >= stream.totalChunks) {
|
|
||||||
throw new Error(`Invalid chunk index: ${chunkIndex}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查重复
|
|
||||||
if (!stream.missingChunks.has(chunkIndex)) {
|
|
||||||
return this.getStreamStatus(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
const buffer = Buffer.from(chunkData, 'base64');
|
|
||||||
|
|
||||||
// 存储分片
|
|
||||||
if (stream.useMemory) {
|
|
||||||
stream.memoryChunks!.set(chunkIndex, buffer);
|
|
||||||
} else {
|
|
||||||
const chunkPath = joinPath(stream.tempDir!, `${chunkIndex}.chunk`);
|
|
||||||
await fs.promises.writeFile(chunkPath, buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新状态
|
|
||||||
stream.missingChunks.delete(chunkIndex);
|
|
||||||
stream.receivedChunks++;
|
|
||||||
this.refreshTimeout(stream);
|
|
||||||
|
|
||||||
return {
|
|
||||||
type: StreamStatus.Stream,
|
|
||||||
stream_id: stream.id,
|
|
||||||
status: 'chunk_received',
|
|
||||||
received_chunks: stream.receivedChunks,
|
|
||||||
total_chunks: stream.totalChunks
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private refreshTimeout(stream: StreamState): void {
|
|
||||||
clearTimeout(stream.timeoutId);
|
|
||||||
stream.timeoutId = this.setupTimeout(stream.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
private getStreamStatus(stream: StreamState): StreamPacket<StreamResult> {
|
|
||||||
return {
|
|
||||||
type: StreamStatus.Stream,
|
|
||||||
stream_id: stream.id,
|
|
||||||
status: 'file_created',
|
|
||||||
received_chunks: stream.receivedChunks,
|
|
||||||
total_chunks: stream.totalChunks
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private async completeStream(stream: StreamState): Promise<StreamPacket<StreamResult>> {
|
|
||||||
// 合并分片
|
|
||||||
const finalBuffer = stream.useMemory ?
|
|
||||||
await this.mergeMemoryChunks(stream) :
|
|
||||||
await this.mergeDiskChunks(stream);
|
|
||||||
|
|
||||||
// 验证SHA256
|
|
||||||
const sha256 = this.validateSha256(stream, finalBuffer);
|
|
||||||
|
|
||||||
// 保存文件
|
|
||||||
const finalPath = stream.finalPath || joinPath(this.core.NapCatTempPath, stream.filename);
|
|
||||||
await fs.promises.writeFile(finalPath, finalBuffer);
|
|
||||||
|
|
||||||
// 清理资源但保留文件
|
|
||||||
this.cleanupStream(stream.id, false);
|
|
||||||
if (stream.fileRetention && stream.fileRetention > 0) {
|
|
||||||
setTimeout(() => {
|
|
||||||
unlink(finalPath, err => {
|
|
||||||
if (err) this.core.context.logger.logError(`Failed to delete retained file ${finalPath}:`, err);
|
|
||||||
});
|
|
||||||
}, stream.fileRetention);
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
type: StreamStatus.Response,
|
|
||||||
stream_id: stream.id,
|
|
||||||
status: 'file_complete',
|
|
||||||
received_chunks: stream.receivedChunks,
|
|
||||||
total_chunks: stream.totalChunks,
|
|
||||||
file_path: finalPath,
|
|
||||||
file_size: finalBuffer.length,
|
|
||||||
sha256
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private async mergeMemoryChunks(stream: StreamState): Promise<Buffer> {
|
|
||||||
const chunks: Buffer[] = [];
|
|
||||||
for (let i = 0; i < stream.totalChunks; i++) {
|
|
||||||
const chunk = stream.memoryChunks!.get(i);
|
|
||||||
if (!chunk) throw new Error(`Missing memory chunk ${i}`);
|
|
||||||
chunks.push(chunk);
|
|
||||||
}
|
|
||||||
return Buffer.concat(chunks);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async mergeDiskChunks(stream: StreamState): Promise<Buffer> {
|
|
||||||
const chunks: Buffer[] = [];
|
|
||||||
for (let i = 0; i < stream.totalChunks; i++) {
|
|
||||||
const chunkPath = joinPath(stream.tempDir!, `${i}.chunk`);
|
|
||||||
if (!fs.existsSync(chunkPath)) throw new Error(`Missing chunk file ${i}`);
|
|
||||||
chunks.push(await fs.promises.readFile(chunkPath));
|
|
||||||
}
|
|
||||||
return Buffer.concat(chunks);
|
|
||||||
}
|
|
||||||
|
|
||||||
private validateSha256(stream: StreamState, buffer: Buffer): string | undefined {
|
|
||||||
if (!stream.expectedSha256) return undefined;
|
|
||||||
|
|
||||||
const actualSha256 = createHash('sha256').update(buffer).digest('hex');
|
|
||||||
if (actualSha256 !== stream.expectedSha256) {
|
|
||||||
throw new Error(`SHA256 mismatch. Expected: ${stream.expectedSha256}, Got: ${actualSha256}`);
|
|
||||||
}
|
|
||||||
return actualSha256;
|
|
||||||
}
|
|
||||||
|
|
||||||
private cleanupStream(streamId: string, deleteFinalFile = true): void {
|
|
||||||
const stream = UploadFileStream.streams.get(streamId);
|
|
||||||
if (!stream) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 清理超时
|
|
||||||
clearTimeout(stream.timeoutId);
|
|
||||||
|
|
||||||
// 清理内存
|
|
||||||
if (stream.useMemory) {
|
|
||||||
if (stream.fileSize) {
|
|
||||||
UploadFileStream.memoryUsage = Math.max(0, UploadFileStream.memoryUsage - stream.fileSize);
|
|
||||||
}
|
|
||||||
stream.memoryChunks?.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 清理临时文件夹及其所有内容
|
|
||||||
if (stream.tempDir) {
|
|
||||||
try {
|
|
||||||
if (fs.existsSync(stream.tempDir)) {
|
|
||||||
fs.rmSync(stream.tempDir, { recursive: true, force: true });
|
|
||||||
console.log(`Cleaned up temp directory: ${stream.tempDir}`);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Failed to cleanup temp directory ${stream.tempDir}:`, error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除最终文件(如果需要)
|
|
||||||
if (deleteFinalFile && stream.finalPath) {
|
|
||||||
try {
|
|
||||||
if (fs.existsSync(stream.finalPath)) {
|
|
||||||
fs.unlinkSync(stream.finalPath);
|
|
||||||
console.log(`Deleted final file: ${stream.finalPath}`);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Failed to delete final file ${stream.finalPath}:`, error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Cleanup error for stream ${streamId}:`, error);
|
|
||||||
} finally {
|
|
||||||
UploadFileStream.streams.delete(streamId);
|
|
||||||
console.log(`Stream ${streamId} cleaned up`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,239 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
NapCat OneBot WebSocket 文件流上传测试脚本
|
|
||||||
用于测试 UploadFileStream 接口的一次性分片上传功能
|
|
||||||
"""
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import json
|
|
||||||
import base64
|
|
||||||
import hashlib
|
|
||||||
import os
|
|
||||||
import uuid
|
|
||||||
from typing import List, Optional
|
|
||||||
import websockets
|
|
||||||
import argparse
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
class OneBotUploadTester:
|
|
||||||
def __init__(self, ws_url: str = "ws://localhost:3001", access_token: Optional[str] = None):
|
|
||||||
self.ws_url = ws_url
|
|
||||||
self.access_token = access_token
|
|
||||||
self.websocket = None
|
|
||||||
|
|
||||||
async def connect(self):
|
|
||||||
"""连接到 OneBot WebSocket"""
|
|
||||||
headers = {}
|
|
||||||
if self.access_token:
|
|
||||||
headers["Authorization"] = f"Bearer {self.access_token}"
|
|
||||||
|
|
||||||
print(f"连接到 {self.ws_url}")
|
|
||||||
self.websocket = await websockets.connect(self.ws_url, additional_headers=headers)
|
|
||||||
print("WebSocket 连接成功")
|
|
||||||
|
|
||||||
async def disconnect(self):
|
|
||||||
"""断开 WebSocket 连接"""
|
|
||||||
if self.websocket:
|
|
||||||
await self.websocket.close()
|
|
||||||
print("WebSocket 连接已断开")
|
|
||||||
|
|
||||||
def calculate_file_chunks(self, file_path: str, chunk_size: int = 64) -> tuple[List[bytes], str, int]:
|
|
||||||
"""
|
|
||||||
计算文件分片和 SHA256
|
|
||||||
|
|
||||||
Args:
|
|
||||||
file_path: 文件路径
|
|
||||||
chunk_size: 分片大小(默认64KB)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
(chunks, sha256_hash, total_size)
|
|
||||||
"""
|
|
||||||
chunks = []
|
|
||||||
hasher = hashlib.sha256()
|
|
||||||
total_size = 0
|
|
||||||
|
|
||||||
with open(file_path, 'rb') as f:
|
|
||||||
while True:
|
|
||||||
chunk = f.read(chunk_size)
|
|
||||||
if not chunk:
|
|
||||||
break
|
|
||||||
chunks.append(chunk)
|
|
||||||
hasher.update(chunk)
|
|
||||||
total_size += len(chunk)
|
|
||||||
|
|
||||||
sha256_hash = hasher.hexdigest()
|
|
||||||
print(f"文件分析完成:")
|
|
||||||
print(f" - 文件大小: {total_size} 字节")
|
|
||||||
print(f" - 分片数量: {len(chunks)}")
|
|
||||||
print(f" - SHA256: {sha256_hash}")
|
|
||||||
|
|
||||||
return chunks, sha256_hash, total_size
|
|
||||||
|
|
||||||
async def send_action(self, action: str, params: dict, echo: str = None) -> dict:
|
|
||||||
"""发送 OneBot 动作请求"""
|
|
||||||
if not echo:
|
|
||||||
echo = str(uuid.uuid4())
|
|
||||||
|
|
||||||
message = {
|
|
||||||
"action": action,
|
|
||||||
"params": params,
|
|
||||||
"echo": echo
|
|
||||||
}
|
|
||||||
|
|
||||||
print(f"发送请求: {action}")
|
|
||||||
await self.websocket.send(json.dumps(message))
|
|
||||||
|
|
||||||
# 等待响应
|
|
||||||
while True:
|
|
||||||
response = await self.websocket.recv()
|
|
||||||
data = json.loads(response)
|
|
||||||
|
|
||||||
# 检查是否是我们的响应
|
|
||||||
if data.get("echo") == echo:
|
|
||||||
return data
|
|
||||||
else:
|
|
||||||
# 可能是其他消息,继续等待
|
|
||||||
print(f"收到其他消息: {data}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
async def upload_file_stream_batch(self, file_path: str, chunk_size: int = 64 ) -> str:
|
|
||||||
"""
|
|
||||||
一次性批量上传文件流
|
|
||||||
|
|
||||||
Args:
|
|
||||||
file_path: 要上传的文件路径
|
|
||||||
chunk_size: 分片大小
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
上传完成后的文件路径
|
|
||||||
"""
|
|
||||||
file_path = Path(file_path)
|
|
||||||
if not file_path.exists():
|
|
||||||
raise FileNotFoundError(f"文件不存在: {file_path}")
|
|
||||||
|
|
||||||
# 分析文件
|
|
||||||
chunks, sha256_hash, total_size = self.calculate_file_chunks(str(file_path), chunk_size)
|
|
||||||
stream_id = str(uuid.uuid4())
|
|
||||||
|
|
||||||
print(f"\n开始上传文件: {file_path.name}")
|
|
||||||
print(f"流ID: {stream_id}")
|
|
||||||
|
|
||||||
# 一次性发送所有分片
|
|
||||||
total_chunks = len(chunks)
|
|
||||||
|
|
||||||
for chunk_index, chunk_data in enumerate(chunks):
|
|
||||||
# 将分片数据编码为 base64
|
|
||||||
chunk_base64 = base64.b64encode(chunk_data).decode('utf-8')
|
|
||||||
|
|
||||||
# 构建参数
|
|
||||||
params = {
|
|
||||||
"stream_id": stream_id,
|
|
||||||
"chunk_data": chunk_base64,
|
|
||||||
"chunk_index": chunk_index,
|
|
||||||
"total_chunks": total_chunks,
|
|
||||||
"file_size": total_size,
|
|
||||||
"expected_sha256": sha256_hash,
|
|
||||||
"filename": file_path.name,
|
|
||||||
"file_retention": 30 * 1000
|
|
||||||
}
|
|
||||||
|
|
||||||
# 发送分片
|
|
||||||
response = await self.send_action("upload_file_stream", params)
|
|
||||||
|
|
||||||
if response.get("status") != "ok":
|
|
||||||
raise Exception(f"上传分片 {chunk_index} 失败: {response}")
|
|
||||||
|
|
||||||
# 解析流响应
|
|
||||||
stream_data = response.get("data", {})
|
|
||||||
print(f"分片 {chunk_index + 1}/{total_chunks} 上传成功 "
|
|
||||||
f"(接收: {stream_data.get('received_chunks', 0)}/{stream_data.get('total_chunks', 0)})")
|
|
||||||
|
|
||||||
# 发送完成信号
|
|
||||||
print(f"\n所有分片发送完成,请求文件合并...")
|
|
||||||
complete_params = {
|
|
||||||
"stream_id": stream_id,
|
|
||||||
"is_complete": True
|
|
||||||
}
|
|
||||||
|
|
||||||
response = await self.send_action("upload_file_stream", complete_params)
|
|
||||||
|
|
||||||
if response.get("status") != "ok":
|
|
||||||
raise Exception(f"文件合并失败: {response}")
|
|
||||||
|
|
||||||
result = response.get("data", {})
|
|
||||||
|
|
||||||
if result.get("status") == "file_complete":
|
|
||||||
print(f"✅ 文件上传成功!")
|
|
||||||
print(f" - 文件路径: {result.get('file_path')}")
|
|
||||||
print(f" - 文件大小: {result.get('file_size')} 字节")
|
|
||||||
print(f" - SHA256: {result.get('sha256')}")
|
|
||||||
return result.get('file_path')
|
|
||||||
else:
|
|
||||||
raise Exception(f"文件状态异常: {result}")
|
|
||||||
|
|
||||||
async def test_upload(self, file_path: str, chunk_size: int = 64 ):
|
|
||||||
"""测试文件上传"""
|
|
||||||
try:
|
|
||||||
await self.connect()
|
|
||||||
|
|
||||||
# 执行上传
|
|
||||||
uploaded_path = await self.upload_file_stream_batch(file_path, chunk_size)
|
|
||||||
|
|
||||||
print(f"\n🎉 测试完成! 上传后的文件路径: {uploaded_path}")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"❌ 测试失败: {e}")
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
await self.disconnect()
|
|
||||||
|
|
||||||
def create_test_file(file_path: str, size_mb: float = 1):
|
|
||||||
"""创建测试文件"""
|
|
||||||
size_bytes = int(size_mb * 1024 * 1024)
|
|
||||||
|
|
||||||
with open(file_path, 'wb') as f:
|
|
||||||
# 写入一些有意义的测试数据
|
|
||||||
test_data = b"NapCat Upload Test Data - " * 100
|
|
||||||
written = 0
|
|
||||||
while written < size_bytes:
|
|
||||||
write_size = min(len(test_data), size_bytes - written)
|
|
||||||
f.write(test_data[:write_size])
|
|
||||||
written += write_size
|
|
||||||
|
|
||||||
print(f"创建测试文件: {file_path} ({size_mb}MB)")
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
parser = argparse.ArgumentParser(description="NapCat OneBot 文件流上传测试")
|
|
||||||
parser.add_argument("--url", default="ws://localhost:3001", help="WebSocket URL")
|
|
||||||
parser.add_argument("--token", help="访问令牌")
|
|
||||||
parser.add_argument("--file", help="要上传的文件路径")
|
|
||||||
parser.add_argument("--chunk-size", type=int, default=64*1024, help="分片大小(字节)")
|
|
||||||
parser.add_argument("--create-test", type=float, help="创建测试文件(MB)")
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
# 创建测试文件
|
|
||||||
if args.create_test:
|
|
||||||
test_file = "test_upload_file.bin"
|
|
||||||
create_test_file(test_file, args.create_test)
|
|
||||||
if not args.file:
|
|
||||||
args.file = test_file
|
|
||||||
|
|
||||||
if not args.file:
|
|
||||||
print("请指定要上传的文件路径,或使用 --create-test 创建测试文件")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 创建测试器并运行
|
|
||||||
tester = OneBotUploadTester(args.url, args.token)
|
|
||||||
await tester.test_upload(args.file, args.chunk_size)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# 安装依赖提示
|
|
||||||
try:
|
|
||||||
import websockets
|
|
||||||
except ImportError:
|
|
||||||
print("请先安装依赖: pip install websockets")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
@@ -578,7 +578,7 @@ export class OneBotMsgApi {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!context.peer || !atQQ || context.peer.chatType == ChatType.KCHATTYPEC2C) return undefined; // 过滤掉空atQQ
|
if (!context.peer || context.peer.chatType == ChatType.KCHATTYPEC2C) return undefined;
|
||||||
if (atQQ === 'all') return at(atQQ, atQQ, NTMsgAtType.ATTYPEALL, '全体成员');
|
if (atQQ === 'all') return at(atQQ, atQQ, NTMsgAtType.ATTYPEALL, '全体成员');
|
||||||
const atMember = await this.core.apis.GroupApi.getGroupMember(context.peer.peerUid, atQQ);
|
const atMember = await this.core.apis.GroupApi.getGroupMember(context.peer.peerUid, atQQ);
|
||||||
if (atMember) {
|
if (atMember) {
|
||||||
@@ -1124,13 +1124,10 @@ export class OneBotMsgApi {
|
|||||||
if (ignoreTypes.includes(sendMsg.type)) {
|
if (ignoreTypes.includes(sendMsg.type)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const converter = this.ob11ToRawConverters[sendMsg.type] as ((
|
const converter = this.ob11ToRawConverters[sendMsg.type] as (
|
||||||
sendMsg: Extract<OB11MessageData, { type: OB11MessageData['type'] }>,
|
sendMsg: Extract<OB11MessageData, { type: OB11MessageData['type'] }>,
|
||||||
context: SendMessageContext,
|
context: SendMessageContext,
|
||||||
) => Promise<SendMessageElement | undefined>) | undefined;
|
) => Promise<SendMessageElement | undefined>;
|
||||||
if (converter == undefined) {
|
|
||||||
throw new Error('未知的消息类型:' + sendMsg.type);
|
|
||||||
}
|
|
||||||
const callResult = converter(
|
const callResult = converter(
|
||||||
sendMsg,
|
sendMsg,
|
||||||
{ peer, deleteAfterSentFiles },
|
{ peer, deleteAfterSentFiles },
|
||||||
|
|||||||
@@ -217,15 +217,6 @@ export class NapCatOneBot11Adapter {
|
|||||||
//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);
|
await this.reloadNetwork(prev, newConfig);
|
||||||
});
|
});
|
||||||
WebUiDataRuntime.setCleanCacheCall(async () => {
|
|
||||||
try {
|
|
||||||
await this.actions.get('clean_cache')?.handle({});
|
|
||||||
return { result: true, message: '缓存清理成功' };
|
|
||||||
} catch (error) {
|
|
||||||
this.context.logger.logError('清理缓存失败:', error);
|
|
||||||
return { result: false, message: `清理缓存失败: ${(error as Error).message}` };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export abstract class IOB11NetworkAdapter<CT extends NetworkAdapterConfig> {
|
|||||||
this.logger = core.context.logger;
|
this.logger = core.context.logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract onEvent<T extends OB11EmitEventContent>(event: T): Promise<void>;
|
abstract onEvent<T extends OB11EmitEventContent>(event: T): void;
|
||||||
|
|
||||||
abstract open(): void | Promise<void>;
|
abstract open(): void | Promise<void>;
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export class OB11HttpClientAdapter extends IOB11NetworkAdapter<HttpClientConfig>
|
|||||||
super(name, config, core, obContext, actions);
|
super(name, config, core, obContext, actions);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
this.emitEventAsync(event).catch(e => this.logger.logError('[OneBot] [Http Client] 新消息事件HTTP上报返回快速操作失败', e));
|
this.emitEventAsync(event).catch(e => this.logger.logError('[OneBot] [Http Client] 新消息事件HTTP上报返回快速操作失败', e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export class OB11HttpSSEServerAdapter extends OB11HttpServerAdapter {
|
|||||||
if (req.path === '/_events') {
|
if (req.path === '/_events') {
|
||||||
this.createSseSupport(req, res);
|
this.createSseSupport(req, res);
|
||||||
} else {
|
} else {
|
||||||
super.httpApiRequest(req, res, true);
|
super.httpApiRequest(req, res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,22 +23,11 @@ export class OB11HttpSSEServerAdapter extends OB11HttpServerAdapter {
|
|||||||
req.on('close', () => {
|
req.on('close', () => {
|
||||||
this.sseClients = this.sseClients.filter((client) => client !== res);
|
this.sseClients = this.sseClients.filter((client) => client !== res);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override async onEvent<T extends OB11EmitEventContent>(event: T) {
|
override onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
let promises: Promise<void>[] = [];
|
|
||||||
this.sseClients.forEach((res) => {
|
this.sseClients.forEach((res) => {
|
||||||
promises.push(new Promise<void>((resolve, reject) => {
|
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
||||||
res.write(`data: ${JSON.stringify(event)}\n\n`, (err) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err);
|
|
||||||
} else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
await Promise.allSettled(promises);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
override async onEvent<T extends OB11EmitEventContent>(_event: T) {
|
override onEvent<T extends OB11EmitEventContent>(_event: T) {
|
||||||
// http server is passive, no need to emit event
|
// http server is passive, no need to emit event
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async httpApiRequest(req: Request, res: Response, request_sse: boolean = false) {
|
async httpApiRequest(req: Request, res: Response) {
|
||||||
let payload = req.body;
|
let payload = req.body;
|
||||||
if (req.method == 'get') {
|
if (req.method == 'get') {
|
||||||
payload = req.query;
|
payload = req.query;
|
||||||
@@ -117,35 +117,17 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
|||||||
return res.json(hello);
|
return res.json(hello);
|
||||||
}
|
}
|
||||||
const actionName = req.path.split('/')[1];
|
const actionName = req.path.split('/')[1];
|
||||||
const payload_echo = payload['echo'];
|
|
||||||
const real_echo = payload_echo ?? Math.random().toString(36).substring(2, 15);
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const action = this.actions.get(actionName as any);
|
const action = this.actions.get(actionName as any);
|
||||||
if (action) {
|
if (action) {
|
||||||
const useStream = action.useStream;
|
|
||||||
try {
|
try {
|
||||||
const result = await action.handle(payload, this.name, this.config, {
|
const result = await action.handle(payload, this.name, this.config);
|
||||||
send: request_sse ? async (data: object) => {
|
|
||||||
await this.onEvent({ ...OB11Response.ok(data, real_echo, true) } as unknown as OB11EmitEventContent);
|
|
||||||
} : async (data: object) => {
|
|
||||||
let newPromise = new Promise<void>((resolve, _reject) => {
|
|
||||||
res.write(JSON.stringify({ ...OB11Response.ok(data, real_echo, true) }) + "\r\n\r\n", () => {
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return newPromise;
|
|
||||||
}
|
|
||||||
}, real_echo);
|
|
||||||
if (useStream) {
|
|
||||||
res.write(JSON.stringify({ ...result }) + "\r\n\r\n");
|
|
||||||
return res.end();
|
|
||||||
};
|
|
||||||
return res.json(result);
|
return res.json(result);
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return res.json(OB11Response.error((error as Error)?.stack?.toString() || (error as Error)?.message || 'Error Handle', 200, real_echo));
|
return res.json(OB11Response.error((error as Error)?.stack?.toString() || (error as Error)?.message || 'Error Handle', 200));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return res.json(OB11Response.error('不支持的Api ' + actionName, 200, real_echo));
|
return res.json(OB11Response.error('不支持的Api ' + actionName, 200));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ export class OB11NetworkManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async emitEvent(event: OB11EmitEventContent) {
|
async emitEvent(event: OB11EmitEventContent) {
|
||||||
return Promise.all(Array.from(this.adapters.values()).map(async adapter => {
|
return Promise.all(Array.from(this.adapters.values()).map(adapter => {
|
||||||
if (adapter.isEnable) {
|
if (adapter.isEnable) {
|
||||||
return await adapter.onEvent(event);
|
return adapter.onEvent(event);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -32,19 +32,19 @@ export class OB11NetworkManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async emitEventByName(names: string[], event: OB11EmitEventContent) {
|
async emitEventByName(names: string[], event: OB11EmitEventContent) {
|
||||||
return Promise.all(names.map(async name => {
|
return Promise.all(names.map(name => {
|
||||||
const adapter = this.adapters.get(name);
|
const adapter = this.adapters.get(name);
|
||||||
if (adapter && adapter.isEnable) {
|
if (adapter && adapter.isEnable) {
|
||||||
return await adapter.onEvent(event);
|
return adapter.onEvent(event);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async emitEventByNames(map: Map<string, OB11EmitEventContent>) {
|
async emitEventByNames(map: Map<string, OB11EmitEventContent>) {
|
||||||
return Promise.all(Array.from(map.entries()).map(async ([name, event]) => {
|
return Promise.all(Array.from(map.entries()).map(([name, event]) => {
|
||||||
const adapter = this.adapters.get(name);
|
const adapter = this.adapters.get(name);
|
||||||
if (adapter && adapter.isEnable) {
|
if (adapter && adapter.isEnable) {
|
||||||
return await adapter.onEvent(event);
|
return adapter.onEvent(event);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -251,20 +251,14 @@ export class OB11PluginMangerAdapter extends IOB11NetworkAdapter<PluginConfig> {
|
|||||||
this.logger.log(`[Plugin Adapter] Unloaded plugin: ${pluginName}`);
|
this.logger.log(`[Plugin Adapter] Unloaded plugin: ${pluginName}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
if (!this.isEnable) {
|
if (!this.isEnable) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 遍历所有已加载的插件,调用它们的事件处理方法
|
// 遍历所有已加载的插件,调用它们的事件处理方法
|
||||||
try {
|
for (const [, plugin] of this.loadedPlugins) {
|
||||||
await Promise.allSettled(
|
this.callPluginEventHandler(plugin, event);
|
||||||
Array.from(this.loadedPlugins.values()).map((plugin) =>
|
|
||||||
this.callPluginEventHandler(plugin, event)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.logError('[Plugin Adapter] Error handling event:', error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -251,7 +251,7 @@ export class OB11PluginAdapter extends IOB11NetworkAdapter<PluginConfig> {
|
|||||||
this.logger.log(`[Plugin Adapter] Unloaded plugin: ${pluginName}`);
|
this.logger.log(`[Plugin Adapter] Unloaded plugin: ${pluginName}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
if (!this.isEnable) {
|
if (!this.isEnable) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
|||||||
super(name, config, core, obContext, actions);
|
super(name, config, core, obContext, actions);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
|
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
|
||||||
this.connection.send(JSON.stringify(event));
|
this.connection.send(JSON.stringify(event));
|
||||||
}
|
}
|
||||||
@@ -62,15 +62,10 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async checkStateAndReply<T>(data: T) {
|
private checkStateAndReply<T>(data: T) {
|
||||||
return new Promise<void>((resolve, reject) => {
|
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
|
||||||
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
|
this.connection.send(JSON.stringify(data));
|
||||||
this.connection.send(JSON.stringify(data));
|
}
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject(new Error('WebSocket is not open'));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async tryConnect() {
|
private async tryConnect() {
|
||||||
@@ -97,7 +92,7 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
|||||||
});
|
});
|
||||||
this.connection.on('open', () => {
|
this.connection.on('open', () => {
|
||||||
try {
|
try {
|
||||||
this.connectEvent(this.core).catch(e => this.logger.logError('[OneBot] [WebSocket Client] 发送连接生命周期失败', e));
|
this.connectEvent(this.core);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.logError('[OneBot] [WebSocket Client] 发送连接生命周期失败', e);
|
this.logger.logError('[OneBot] [WebSocket Client] 发送连接生命周期失败', e);
|
||||||
}
|
}
|
||||||
@@ -128,9 +123,9 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async connectEvent(core: NapCatCore) {
|
connectEvent(core: NapCatCore) {
|
||||||
try {
|
try {
|
||||||
await this.checkStateAndReply<unknown>(new OB11LifeCycleEvent(core, LifeCycleSubType.CONNECT));
|
this.checkStateAndReply<unknown>(new OB11LifeCycleEvent(core, LifeCycleSubType.CONNECT));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.logError('[OneBot] [WebSocket Client] 发送生命周期失败', e);
|
this.logger.logError('[OneBot] [WebSocket Client] 发送生命周期失败', e);
|
||||||
}
|
}
|
||||||
@@ -145,7 +140,7 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
|||||||
echo = receiveData.echo;
|
echo = receiveData.echo;
|
||||||
this.logger.logDebug('[OneBot] [WebSocket Client] 收到正向Websocket消息', receiveData);
|
this.logger.logDebug('[OneBot] [WebSocket Client] 收到正向Websocket消息', receiveData);
|
||||||
} catch {
|
} catch {
|
||||||
await this.checkStateAndReply<unknown>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo));
|
this.checkStateAndReply<unknown>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
receiveData.params = (receiveData?.params) ? receiveData.params : {};// 兼容类型验证
|
receiveData.params = (receiveData?.params) ? receiveData.params : {};// 兼容类型验证
|
||||||
@@ -153,15 +148,11 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
|||||||
const action = this.actions.get(receiveData.action as any);
|
const action = this.actions.get(receiveData.action as any);
|
||||||
if (!action) {
|
if (!action) {
|
||||||
this.logger.logError('[OneBot] [WebSocket Client] 发生错误', '不支持的Api ' + receiveData.action);
|
this.logger.logError('[OneBot] [WebSocket Client] 发生错误', '不支持的Api ' + receiveData.action);
|
||||||
await this.checkStateAndReply<unknown>(OB11Response.error('不支持的Api ' + receiveData.action, 1404, echo));
|
this.checkStateAndReply<unknown>(OB11Response.error('不支持的Api ' + receiveData.action, 1404, echo));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const retdata = await action.websocketHandle(receiveData.params, echo ?? '', this.name, this.config, {
|
const retdata = await action.websocketHandle(receiveData.params, echo ?? '', this.name, this.config);
|
||||||
send: async (data: object) => {
|
this.checkStateAndReply<unknown>({ ...retdata });
|
||||||
await this.checkStateAndReply<unknown>({ ...OB11Response.ok(data, echo ?? '', true) });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
await this.checkStateAndReply<unknown>({ ...retdata });
|
|
||||||
}
|
}
|
||||||
async reload(newConfig: WebsocketClientConfig) {
|
async reload(newConfig: WebsocketClientConfig) {
|
||||||
const wasEnabled = this.isEnable;
|
const wasEnabled = this.isEnable;
|
||||||
|
|||||||
@@ -83,25 +83,17 @@ export class OB11WebSocketServerAdapter extends IOB11NetworkAdapter<WebsocketSer
|
|||||||
}
|
}
|
||||||
connectEvent(core: NapCatCore, wsClient: WebSocket) {
|
connectEvent(core: NapCatCore, wsClient: WebSocket) {
|
||||||
try {
|
try {
|
||||||
this.checkStateAndReply<unknown>(new OB11LifeCycleEvent(core, LifeCycleSubType.CONNECT), wsClient).catch(e => this.logger.logError('[OneBot] [WebSocket Server] 发送生命周期失败', e));
|
this.checkStateAndReply<unknown>(new OB11LifeCycleEvent(core, LifeCycleSubType.CONNECT), wsClient);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.logError('[OneBot] [WebSocket Server] 发送生命周期失败', e);
|
this.logger.logError('[OneBot] [WebSocket Server] 发送生命周期失败', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
this.wsClientsMutex.runExclusive(async () => {
|
this.wsClientsMutex.runExclusive(async () => {
|
||||||
let promises = this.wsClientWithEvent.map((wsClient) => {
|
this.wsClientWithEvent.forEach((wsClient) => {
|
||||||
return new Promise<void>((resolve, reject) => {
|
wsClient.send(JSON.stringify(event));
|
||||||
if (wsClient.readyState === WebSocket.OPEN) {
|
|
||||||
wsClient.send(JSON.stringify(event));
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject(new Error('WebSocket is not open'));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
await Promise.allSettled(promises);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,15 +160,10 @@ export class OB11WebSocketServerAdapter extends IOB11NetworkAdapter<WebsocketSer
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async checkStateAndReply<T>(data: T, wsClient: WebSocket) {
|
private checkStateAndReply<T>(data: T, wsClient: WebSocket) {
|
||||||
return await new Promise<void>((resolve, reject) => {
|
if (wsClient.readyState === WebSocket.OPEN) {
|
||||||
if (wsClient.readyState === WebSocket.OPEN) {
|
wsClient.send(JSON.stringify(data));
|
||||||
wsClient.send(JSON.stringify(data));
|
}
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject(new Error('WebSocket is not open'));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleMessage(wsClient: WebSocket, message: RawData) {
|
private async handleMessage(wsClient: WebSocket, message: RawData) {
|
||||||
@@ -188,7 +175,7 @@ export class OB11WebSocketServerAdapter extends IOB11NetworkAdapter<WebsocketSer
|
|||||||
echo = receiveData.echo;
|
echo = receiveData.echo;
|
||||||
//this.logger.logDebug('收到正向Websocket消息', receiveData);
|
//this.logger.logDebug('收到正向Websocket消息', receiveData);
|
||||||
} catch {
|
} catch {
|
||||||
await this.checkStateAndReply<unknown>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo), wsClient);
|
this.checkStateAndReply<unknown>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo), wsClient);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
receiveData.params = (receiveData?.params) ? receiveData.params : {};//兼容类型验证 不然类型校验爆炸
|
receiveData.params = (receiveData?.params) ? receiveData.params : {};//兼容类型验证 不然类型校验爆炸
|
||||||
@@ -196,15 +183,11 @@ export class OB11WebSocketServerAdapter extends IOB11NetworkAdapter<WebsocketSer
|
|||||||
const action = this.actions.get(receiveData.action as any);
|
const action = this.actions.get(receiveData.action as any);
|
||||||
if (!action) {
|
if (!action) {
|
||||||
this.logger.logError('[OneBot] [WebSocket Client] 发生错误', '不支持的API ' + receiveData.action);
|
this.logger.logError('[OneBot] [WebSocket Client] 发生错误', '不支持的API ' + receiveData.action);
|
||||||
await this.checkStateAndReply<unknown>(OB11Response.error('不支持的API ' + receiveData.action, 1404, echo), wsClient);
|
this.checkStateAndReply<unknown>(OB11Response.error('不支持的API ' + receiveData.action, 1404, echo), wsClient);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const retdata = await action.websocketHandle(receiveData.params, echo ?? '', this.name, this.config, {
|
const retdata = await action.websocketHandle(receiveData.params, echo ?? '', this.name, this.config);
|
||||||
send: async (data: object) => {
|
this.checkStateAndReply<unknown>({ ...retdata }, wsClient);
|
||||||
await this.checkStateAndReply<unknown>({ ...OB11Response.ok(data, echo ?? '', true) }, wsClient);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
await this.checkStateAndReply<unknown>({ ...retdata }, wsClient);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async reload(newConfig: WebsocketServerConfig) {
|
async reload(newConfig: WebsocketServerConfig) {
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ export interface OB11Return<DataType> {
|
|||||||
message: string;
|
message: string;
|
||||||
echo?: unknown; // ws调用api才有此字段
|
echo?: unknown; // ws调用api才有此字段
|
||||||
wording?: string; // go-cqhttp字段,错误信息
|
wording?: string; // go-cqhttp字段,错误信息
|
||||||
stream?: 'stream-action' | 'normal-action' ; // 流式返回标记
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 消息数据类型枚举
|
// 消息数据类型枚举
|
||||||
|
|||||||
@@ -203,13 +203,7 @@ export class WindowsPtyAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _getWindowsBuildNumber(): number {
|
private _getWindowsBuildNumber(): number {
|
||||||
const release = os.release();
|
const osVersion = (/(\d+)\.(\d+)\.(\d+)/g).exec(os.release());
|
||||||
// Limit input length to prevent potential DoS attacks
|
|
||||||
if (release.length > 50) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// Use non-global regex with more specific pattern to prevent backtracking
|
|
||||||
const osVersion = /^(\d{1,5})\.(\d{1,5})\.(\d{1,10})/.exec(release);
|
|
||||||
let buildNumber: number = 0;
|
let buildNumber: number = 0;
|
||||||
if (osVersion && osVersion.length === 4) {
|
if (osVersion && osVersion.length === 4) {
|
||||||
buildNumber = parseInt(osVersion[3]!);
|
buildNumber = parseInt(osVersion[3]!);
|
||||||
|
|||||||
@@ -4,14 +4,14 @@
|
|||||||
|
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { createServer } from 'http';
|
import { createServer } from 'http';
|
||||||
import { randomUUID } from 'node:crypto'
|
import { randomUUID, randomBytes } from 'node:crypto'
|
||||||
import { createServer as createHttpsServer } from 'https';
|
import { createServer as createHttpsServer } from 'https';
|
||||||
import { LogWrapper } from '@/common/log';
|
import { LogWrapper } from '@/common/log';
|
||||||
import { NapCatPathWrapper } from '@/common/path';
|
import { NapCatPathWrapper } from '@/common/path';
|
||||||
import { WebUiConfigWrapper } from '@webapi/helper/config';
|
import { WebUiConfigWrapper } from '@webapi/helper/config';
|
||||||
import { ALLRouter } from '@webapi/router';
|
import { ALLRouter } from '@webapi/router';
|
||||||
import { cors } from '@webapi/middleware/cors';
|
import { cors } from '@webapi/middleware/cors';
|
||||||
import { createUrl, getRandomToken } from '@webapi/utils/url';
|
import { createUrl } from '@webapi/utils/url';
|
||||||
import { sendError } from '@webapi/utils/response';
|
import { sendError } from '@webapi/utils/response';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { terminalManager } from '@webapi/terminal/terminal_manager';
|
import { terminalManager } from '@webapi/terminal/terminal_manager';
|
||||||
@@ -90,9 +90,9 @@ export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapp
|
|||||||
let config = await WebUiConfig.GetWebUIConfig();
|
let config = await WebUiConfig.GetWebUIConfig();
|
||||||
|
|
||||||
// 检查并更新默认密码 - 最高优先级
|
// 检查并更新默认密码 - 最高优先级
|
||||||
if (config.token === 'napcat' || !config.token) {
|
if (config.defaultToken || config.token === 'napcat' || !config.token) {
|
||||||
const randomToken = getRandomToken(8);
|
const randomToken = randomBytes(6).toString('hex');
|
||||||
await WebUiConfig.UpdateWebUIConfig({ token: randomToken });
|
await WebUiConfig.UpdateWebUIConfig({ token: randomToken, defaultToken: false });
|
||||||
logger.log(`[NapCat] [WebUi] 🔐 检测到默认密码,已自动更新为安全密码`);
|
logger.log(`[NapCat] [WebUi] 🔐 检测到默认密码,已自动更新为安全密码`);
|
||||||
|
|
||||||
// 存储token到全局变量,等待QQ登录成功后发送
|
// 存储token到全局变量,等待QQ登录成功后发送
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ export const UpdateTokenHandler: RequestHandler = async (req, res) => {
|
|||||||
return sendError(res, '旧 token 不匹配');
|
return sendError(res, '旧 token 不匹配');
|
||||||
}
|
}
|
||||||
// 直接更新配置文件中的token,不需要通过WebUiConfig.UpdateToken方法
|
// 直接更新配置文件中的token,不需要通过WebUiConfig.UpdateToken方法
|
||||||
await WebUiConfig.UpdateWebUIConfig({ token: newToken });
|
await WebUiConfig.UpdateWebUIConfig({ token: newToken, defaultToken: false });
|
||||||
// 更新内存中的缓存token,使新密码立即生效
|
// 更新内存中的缓存token,使新密码立即生效
|
||||||
setInitialWebUiToken(newToken);
|
setInitialWebUiToken(newToken);
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,3 @@ export const SetThemeConfigHandler: RequestHandler = async (req, res) => {
|
|||||||
await WebUiConfig.UpdateTheme(theme);
|
await WebUiConfig.UpdateTheme(theme);
|
||||||
sendSuccess(res, { message: '更新成功' });
|
sendSuccess(res, { message: '更新成功' });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CleanCacheHandler: RequestHandler = async (_, res) => {
|
|
||||||
const result = await WebUiDataRuntime.requestCleanCache();
|
|
||||||
sendSuccess(res, result);
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -156,6 +156,10 @@ const checkSameTypeExists = async (pathToCheck: string, isDirectory: boolean): P
|
|||||||
|
|
||||||
// 获取目录内容
|
// 获取目录内容
|
||||||
export const ListFilesHandler: RequestHandler = async (req, res) => {
|
export const ListFilesHandler: RequestHandler = async (req, res) => {
|
||||||
|
const webuiToken = await WebUiConfig.GetWebUIConfig();
|
||||||
|
if (webuiToken.defaultToken) {
|
||||||
|
return sendError(res, '默认密码禁止使用');
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const requestPath = getQueryStringParam(req.query['path']) || (isWindows ? process.env['USERPROFILE'] || 'C:\\' : '/');
|
const requestPath = getQueryStringParam(req.query['path']) || (isWindows ? process.env['USERPROFILE'] || 'C:\\' : '/');
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,9 @@ export const CreateTerminalHandler: RequestHandler = async (req, res) => {
|
|||||||
if (isMacOS) {
|
if (isMacOS) {
|
||||||
return sendError(res, 'MacOS不支持终端');
|
return sendError(res, 'MacOS不支持终端');
|
||||||
}
|
}
|
||||||
|
if ((await WebUiConfig.GetWebUIConfig()).defaultToken) {
|
||||||
|
return sendError(res, '该密码禁止创建终端');
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const { cols, rows } = req.body;
|
const { cols, rows } = req.body;
|
||||||
const { id } = terminalManager.createTerminal(cols, rows);
|
const { id } = terminalManager.createTerminal(cols, rows);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { RequestHandler } from 'express';
|
|||||||
import { existsSync, readFileSync } from 'node:fs';
|
import { existsSync, readFileSync } from 'node:fs';
|
||||||
import { resolve } from 'node:path';
|
import { resolve } from 'node:path';
|
||||||
import { loadConfig, OneBotConfig } from '@/onebot/config/config';
|
import { loadConfig, OneBotConfig } from '@/onebot/config/config';
|
||||||
import { webUiPathWrapper } from '@/webui';
|
import { WebUiConfig, webUiPathWrapper } from '@/webui';
|
||||||
import { WebUiDataRuntime } from '@webapi/helper/Data';
|
import { WebUiDataRuntime } from '@webapi/helper/Data';
|
||||||
import { sendError, sendSuccess } from '@webapi/utils/response';
|
import { sendError, sendSuccess } from '@webapi/utils/response';
|
||||||
import { isEmpty } from '@webapi/utils/check';
|
import { isEmpty } from '@webapi/utils/check';
|
||||||
@@ -47,6 +47,10 @@ export const OB11SetConfigHandler: RequestHandler = async (req, res) => {
|
|||||||
if (isEmpty(req.body.config)) {
|
if (isEmpty(req.body.config)) {
|
||||||
return sendError(res, 'config is empty');
|
return sendError(res, 'config is empty');
|
||||||
}
|
}
|
||||||
|
const webuiToken = await WebUiConfig.GetWebUIConfig();
|
||||||
|
if (webuiToken.defaultToken) {
|
||||||
|
return sendError(res, '默认密码禁止写入配置');
|
||||||
|
}
|
||||||
// 写入配置
|
// 写入配置
|
||||||
try {
|
try {
|
||||||
// 解析并加载配置
|
// 解析并加载配置
|
||||||
|
|||||||
@@ -27,9 +27,6 @@ const LoginRuntime: LoginRuntimeType = {
|
|||||||
onQuickLoginRequested: async () => {
|
onQuickLoginRequested: async () => {
|
||||||
return { result: false, message: '' };
|
return { result: false, message: '' };
|
||||||
},
|
},
|
||||||
onCleanCacheRequested: async () => {
|
|
||||||
return { result: false, message: '' };
|
|
||||||
},
|
|
||||||
QQLoginList: [],
|
QQLoginList: [],
|
||||||
NewQQLoginList: [],
|
NewQQLoginList: [],
|
||||||
},
|
},
|
||||||
@@ -133,14 +130,6 @@ export const WebUiDataRuntime = {
|
|||||||
return LoginRuntime.NapCatHelper.onOB11ConfigChanged(ob11);
|
return LoginRuntime.NapCatHelper.onOB11ConfigChanged(ob11);
|
||||||
} as LoginRuntimeType['NapCatHelper']['onOB11ConfigChanged'],
|
} as LoginRuntimeType['NapCatHelper']['onOB11ConfigChanged'],
|
||||||
|
|
||||||
setCleanCacheCall(func: LoginRuntimeType['NapCatHelper']['onCleanCacheRequested']): void {
|
|
||||||
LoginRuntime.NapCatHelper.onCleanCacheRequested = func;
|
|
||||||
},
|
|
||||||
|
|
||||||
requestCleanCache: function () {
|
|
||||||
return LoginRuntime.NapCatHelper.onCleanCacheRequested();
|
|
||||||
} as LoginRuntimeType['NapCatHelper']['onCleanCacheRequested'],
|
|
||||||
|
|
||||||
getPackageJson() {
|
getPackageJson() {
|
||||||
return LoginRuntime.packageJson;
|
return LoginRuntime.packageJson;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -7,17 +7,17 @@ import { resolve } from 'node:path';
|
|||||||
|
|
||||||
import { deepMerge } from '../utils/object';
|
import { deepMerge } from '../utils/object';
|
||||||
import { themeType } from '../types/theme';
|
import { themeType } from '../types/theme';
|
||||||
import { getRandomToken } from '../utils/url'
|
|
||||||
|
|
||||||
// 限制尝试端口的次数,避免死循环
|
// 限制尝试端口的次数,避免死循环
|
||||||
// 定义配置的类型
|
// 定义配置的类型
|
||||||
const WebUiConfigSchema = Type.Object({
|
const WebUiConfigSchema = Type.Object({
|
||||||
host: Type.String({ default: '0.0.0.0' }),
|
host: Type.String({ default: '0.0.0.0' }),
|
||||||
port: Type.Number({ default: 6099 }),
|
port: Type.Number({ default: 6099 }),
|
||||||
token: Type.String({ default: getRandomToken(12) }),
|
token: Type.String({ default: 'napcat' }),
|
||||||
loginRate: Type.Number({ default: 10 }),
|
loginRate: Type.Number({ default: 10 }),
|
||||||
autoLoginAccount: Type.String({ default: '' }),
|
autoLoginAccount: Type.String({ default: '' }),
|
||||||
theme: themeType,
|
theme: themeType,
|
||||||
|
defaultToken: Type.Boolean({ default: true }),
|
||||||
// 是否关闭WebUI
|
// 是否关闭WebUI
|
||||||
disableWebUI: Type.Boolean({ default: false }),
|
disableWebUI: Type.Boolean({ default: false }),
|
||||||
// 是否关闭非局域网访问
|
// 是否关闭非局域网访问
|
||||||
@@ -64,7 +64,7 @@ export class WebUiConfigWrapper {
|
|||||||
|
|
||||||
async GetWebUIConfig(): Promise<WebUiConfigType> {
|
async GetWebUIConfig(): Promise<WebUiConfigType> {
|
||||||
if (this.WebUiConfigData) {
|
if (this.WebUiConfigData) {
|
||||||
return this.WebUiConfigData
|
return this.WebUiConfigData
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -75,17 +75,16 @@ export class WebUiConfigWrapper {
|
|||||||
this.WebUiConfigData = {
|
this.WebUiConfigData = {
|
||||||
...parsedConfig,
|
...parsedConfig,
|
||||||
// 首次读取内存中是没有token的,需要进行一层兜底
|
// 首次读取内存中是没有token的,需要进行一层兜底
|
||||||
token: getInitialWebUiToken() || parsedConfig.token,
|
token: getInitialWebUiToken() || parsedConfig.token
|
||||||
};
|
};
|
||||||
return this.WebUiConfigData;
|
return this.WebUiConfigData;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('读取配置文件失败', e);
|
console.log('读取配置文件失败', e);
|
||||||
const defaultConfig = this.validateAndApplyDefaults({});
|
const defaultConfig = this.validateAndApplyDefaults({});
|
||||||
this.WebUiConfigData = {
|
return {
|
||||||
...defaultConfig,
|
...defaultConfig,
|
||||||
token: getInitialWebUiToken() || defaultConfig.token,
|
token: defaultConfig.token
|
||||||
}
|
};
|
||||||
return this.WebUiConfigData;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +126,7 @@ export class WebUiConfigWrapper {
|
|||||||
if (tokenToCheck !== oldToken) {
|
if (tokenToCheck !== oldToken) {
|
||||||
throw new Error('旧 token 不匹配');
|
throw new Error('旧 token 不匹配');
|
||||||
}
|
}
|
||||||
await this.UpdateWebUIConfig({ token: newToken });
|
await this.UpdateWebUIConfig({ token: newToken, defaultToken: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取日志文件夹路径
|
// 获取日志文件夹路径
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import { CleanCacheHandler, GetThemeConfigHandler, PackageInfoHandler, QQVersionHandler, SetThemeConfigHandler } from '../api/BaseInfo';
|
import { GetThemeConfigHandler, PackageInfoHandler, QQVersionHandler, SetThemeConfigHandler } from '../api/BaseInfo';
|
||||||
import { StatusRealTimeHandler } from '@webapi/api/Status';
|
import { StatusRealTimeHandler } from '@webapi/api/Status';
|
||||||
import { GetProxyHandler } from '../api/Proxy';
|
import { GetProxyHandler } from '../api/Proxy';
|
||||||
|
|
||||||
@@ -11,6 +11,5 @@ router.get('/GetSysStatusRealTime', StatusRealTimeHandler);
|
|||||||
router.get('/proxy', GetProxyHandler);
|
router.get('/proxy', GetProxyHandler);
|
||||||
router.get('/Theme', GetThemeConfigHandler);
|
router.get('/Theme', GetThemeConfigHandler);
|
||||||
router.post('/SetTheme', SetThemeConfigHandler);
|
router.post('/SetTheme', SetThemeConfigHandler);
|
||||||
router.post('/CleanCache', CleanCacheHandler);
|
|
||||||
|
|
||||||
export { router as BaseRouter };
|
export { router as BaseRouter };
|
||||||
|
|||||||
@@ -1,30 +1,21 @@
|
|||||||
import path from 'path'
|
import path from 'path';
|
||||||
import { fileURLToPath } from 'url'
|
|
||||||
|
|
||||||
export function callsites () {
|
|
||||||
const _prepareStackTrace = Error.prepareStackTrace
|
|
||||||
try {
|
|
||||||
let result: NodeJS.CallSite[] = []
|
|
||||||
Error.prepareStackTrace = (_, callSites) => {
|
|
||||||
const callSitesWithoutCurrent = callSites.slice(1)
|
|
||||||
result = callSitesWithoutCurrent
|
|
||||||
return callSitesWithoutCurrent
|
|
||||||
}
|
|
||||||
|
|
||||||
new Error().stack
|
|
||||||
return result
|
|
||||||
} finally {
|
|
||||||
Error.prepareStackTrace = _prepareStackTrace
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.defineProperty(global, '__dirname', {
|
Object.defineProperty(global, '__dirname', {
|
||||||
get () {
|
get() {
|
||||||
const sites = callsites()
|
const err = new Error();
|
||||||
const file = sites?.[1]?.getFileName()
|
const stack = err.stack?.split('\n') || [];
|
||||||
if (file) {
|
let callerFile = '';
|
||||||
return path.dirname(fileURLToPath(file))
|
// 遍历错误堆栈,跳过当前文件所在行
|
||||||
|
// 注意:堆栈格式可能不同,请根据实际环境调整索引及正则表达式
|
||||||
|
for (const line of stack) {
|
||||||
|
const match = line.match(/\((.*):\d+:\d+\)/);
|
||||||
|
if (match?.[1]) {
|
||||||
|
callerFile = match[1];
|
||||||
|
if (!callerFile.includes('init-dynamic-dirname.ts')) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ''
|
return callerFile ? path.dirname(callerFile) : '';
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// import './init-dynamic-dirname';
|
import './init-dynamic-dirname';
|
||||||
import { WebUiConfig } from '@/webui';
|
import { WebUiConfig } from '@/webui';
|
||||||
import { AuthHelper } from '../helper/SignToken';
|
import { AuthHelper } from '../helper/SignToken';
|
||||||
import { LogWrapper } from '@/common/log';
|
import { LogWrapper } from '@/common/log';
|
||||||
|
|||||||
1
src/webui/src/types/data.d.ts
vendored
1
src/webui/src/types/data.d.ts
vendored
@@ -15,7 +15,6 @@ interface LoginRuntimeType {
|
|||||||
NapCatHelper: {
|
NapCatHelper: {
|
||||||
onQuickLoginRequested: (uin: string) => Promise<{ result: boolean; message: string }>;
|
onQuickLoginRequested: (uin: string) => Promise<{ result: boolean; message: string }>;
|
||||||
onOB11ConfigChanged: (ob11: OneBotConfig) => Promise<void>;
|
onOB11ConfigChanged: (ob11: OneBotConfig) => Promise<void>;
|
||||||
onCleanCacheRequested: () => Promise<{ result: boolean; message: string }>;
|
|
||||||
QQLoginList: string[];
|
QQLoginList: string[];
|
||||||
NewQQLoginList: LoginListItem[];
|
NewQQLoginList: LoginListItem[];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -66,15 +66,7 @@ export const createDiskStorage = (uploadPath: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const createDiskUpload = (uploadPath: string) => {
|
export const createDiskUpload = (uploadPath: string) => {
|
||||||
const upload = multer({
|
const upload = multer({ storage: createDiskStorage(uploadPath) }).array('files');
|
||||||
storage: createDiskStorage(uploadPath),
|
|
||||||
limits: {
|
|
||||||
fileSize: 100 * 1024 * 1024, // 100MB 文件大小限制
|
|
||||||
files: 20, // 最多同时上传20个文件
|
|
||||||
fieldSize: 1024 * 1024, // 1MB 字段大小限制
|
|
||||||
fields: 10 // 最多10个字段
|
|
||||||
}
|
|
||||||
}).array('files');
|
|
||||||
return upload;
|
return upload;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -84,18 +76,6 @@ const diskUploader = (req: Request, res: Response) => {
|
|||||||
createDiskUpload(uploadPath)(req, res, (error) => {
|
createDiskUpload(uploadPath)(req, res, (error) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
// 错误处理
|
// 错误处理
|
||||||
if (error.code === 'LIMIT_FILE_SIZE') {
|
|
||||||
return reject(new Error('文件大小超过限制(最大100MB)'));
|
|
||||||
}
|
|
||||||
if (error.code === 'LIMIT_FILE_COUNT') {
|
|
||||||
return reject(new Error('文件数量超过限制(最多20个文件)'));
|
|
||||||
}
|
|
||||||
if (error.code === 'LIMIT_FIELD_VALUE') {
|
|
||||||
return reject(new Error('字段值大小超过限制'));
|
|
||||||
}
|
|
||||||
if (error.code === 'LIMIT_FIELD_COUNT') {
|
|
||||||
return reject(new Error('字段数量超过限制'));
|
|
||||||
}
|
|
||||||
return reject(error);
|
return reject(error);
|
||||||
}
|
}
|
||||||
return resolve(true);
|
return resolve(true);
|
||||||
|
|||||||
@@ -1,57 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* @file URL工具
|
* @file URL工具
|
||||||
*/
|
*/
|
||||||
import fs from 'node:fs'
|
|
||||||
import { isIP } from 'node:net'
|
|
||||||
import { randomBytes } from 'node:crypto'
|
|
||||||
|
|
||||||
type Protocol = 'http' | 'https'
|
import { isIP } from 'node:net';
|
||||||
|
|
||||||
let isDockerCached: boolean
|
|
||||||
|
|
||||||
function hasDockerEnv () {
|
|
||||||
try {
|
|
||||||
fs.statSync('/.dockerenv')
|
|
||||||
return true
|
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function hasDockerCGroup () {
|
|
||||||
try {
|
|
||||||
return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker')
|
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const hasContainerEnv = () => {
|
|
||||||
try {
|
|
||||||
fs.statSync('/run/.containerenv')
|
|
||||||
return true
|
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const isDocker = () => {
|
|
||||||
if (isDockerCached === undefined) {
|
|
||||||
isDockerCached = hasContainerEnv() || hasDockerEnv() || hasDockerCGroup()
|
|
||||||
}
|
|
||||||
|
|
||||||
return isDockerCached
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取默认host地址
|
|
||||||
* @returns 根据环境返回合适的host地址
|
|
||||||
* @example getDefaultHost() => '127.0.0.1' // 非Docker环境
|
|
||||||
* @example getDefaultHost() => '0.0.0.0' // Docker环境
|
|
||||||
*/
|
|
||||||
export const getDefaultHost = (): string => {
|
|
||||||
return isDocker() ? '0.0.0.0' : '127.0.0.1'
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将 host(主机地址) 转换为标准格式
|
* 将 host(主机地址) 转换为标准格式
|
||||||
@@ -62,9 +13,9 @@ export const getDefaultHost = (): string => {
|
|||||||
* @example normalizeHost('2001:4860:4801:51::27') => '[2001:4860:4801:51::27]'
|
* @example normalizeHost('2001:4860:4801:51::27') => '[2001:4860:4801:51::27]'
|
||||||
*/
|
*/
|
||||||
export const normalizeHost = (host: string) => {
|
export const normalizeHost = (host: string) => {
|
||||||
if (isIP(host) === 6) return `[${host}]`
|
if (isIP(host) === 6) return `[${host}]`;
|
||||||
return host
|
return host;
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建URL
|
* 创建URL
|
||||||
@@ -83,23 +34,13 @@ export const createUrl = (
|
|||||||
search?: Record<string, any>,
|
search?: Record<string, any>,
|
||||||
protocol: Protocol = 'http'
|
protocol: Protocol = 'http'
|
||||||
) => {
|
) => {
|
||||||
const url = new URL(`${protocol}://${normalizeHost(host)}`)
|
const url = new URL(`${protocol}://${normalizeHost(host)}`);
|
||||||
url.port = port
|
url.port = port;
|
||||||
url.pathname = path
|
url.pathname = path;
|
||||||
if (search) {
|
if (search) {
|
||||||
for (const key in search) {
|
for (const key in search) {
|
||||||
url.searchParams.set(key, search[key])
|
url.searchParams.set(key, search[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return url.toString()
|
return url.toString();
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成随机Token
|
|
||||||
* @param length Token长度 默认8
|
|
||||||
* @returns 随机Token字符串
|
|
||||||
* @example getRandomToken
|
|
||||||
*/
|
|
||||||
export const getRandomToken = (length = 8) => {
|
|
||||||
return randomBytes(36).toString('hex').slice(0, length)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user