mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
Compare commits
115 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a849b5edc0 | ||
![]() |
be2f3be4bd | ||
![]() |
f7cc25adc1 | ||
![]() |
441a34e0bf | ||
![]() |
7a4c82bded | ||
![]() |
5fa2e9d8f5 | ||
![]() |
b40873ada7 | ||
![]() |
4db65cf860 | ||
![]() |
58d2bd3c81 | ||
![]() |
6534d05b76 | ||
![]() |
2d7de174c5 | ||
![]() |
79aa1dc67f | ||
![]() |
7792ad9ea0 | ||
![]() |
be6671923b | ||
![]() |
0fa1b3f044 | ||
![]() |
4ab751696b | ||
![]() |
dce4eedf7d | ||
![]() |
129b67b751 | ||
![]() |
9ab776d53a | ||
![]() |
2759a34d96 | ||
![]() |
2f9f42750e | ||
![]() |
30abd1f904 | ||
![]() |
008075466e | ||
![]() |
5b4035c320 | ||
![]() |
e3feb6a73c | ||
![]() |
40fe73317d | ||
![]() |
073745030c | ||
![]() |
c523437506 | ||
![]() |
9eef570d37 | ||
![]() |
be37b8cbbd | ||
![]() |
c635496677 | ||
![]() |
8753ecfd92 | ||
![]() |
5eda1f2870 | ||
![]() |
d5a60074f7 | ||
![]() |
91df57d932 | ||
![]() |
e27d4c4302 | ||
![]() |
55847f6e10 | ||
![]() |
b39d8bae27 | ||
![]() |
b0cf23f775 | ||
![]() |
c641246056 | ||
![]() |
1e5bc9bbea | ||
![]() |
99b504b5f6 | ||
![]() |
1146454fec | ||
![]() |
805e014a75 | ||
![]() |
d3acd1efc1 | ||
![]() |
9fcd218a5a | ||
![]() |
d6a0830cfe | ||
![]() |
40a63b9c66 | ||
![]() |
eeb19a04cc | ||
![]() |
91e457eb03 | ||
![]() |
78d1919d7f | ||
![]() |
8393acf173 | ||
![]() |
bca152a047 | ||
![]() |
6a15908a93 | ||
![]() |
c626bbab74 | ||
![]() |
c5c7dcc6f2 | ||
![]() |
03dafe727e | ||
![]() |
744921c45e | ||
![]() |
abc4a4dcba | ||
![]() |
7e0da2f929 | ||
![]() |
a3b70d0f1f | ||
![]() |
d291724f06 | ||
![]() |
122a9ca2cc | ||
![]() |
48aaddd32b | ||
![]() |
47401af856 | ||
![]() |
709adfd812 | ||
![]() |
038d0c5412 | ||
![]() |
6bb4362ed4 | ||
![]() |
e617f9452d | ||
![]() |
6d8bb49a37 | ||
![]() |
4f6073ee86 | ||
![]() |
2e7176304b | ||
![]() |
e36cf11004 | ||
![]() |
0e49e17f68 | ||
![]() |
524de45f6b | ||
![]() |
85741a4b60 | ||
![]() |
610e07ac32 | ||
![]() |
f9ccb8c978 | ||
![]() |
ea3d069e49 | ||
![]() |
3e6024f183 | ||
![]() |
337871693a | ||
![]() |
2d921c4577 | ||
![]() |
9accff7323 | ||
![]() |
88b1ee8c31 | ||
![]() |
3ac618bb4e | ||
![]() |
0051df3741 | ||
![]() |
7eb4e010b0 | ||
![]() |
33cc23ada3 | ||
![]() |
e5aee372e3 | ||
![]() |
6b6ce4a761 | ||
![]() |
8c4ea7f8f2 | ||
![]() |
c8b268b806 | ||
![]() |
cf5e0e0f14 | ||
![]() |
7b79f9cc17 | ||
![]() |
708d599966 | ||
![]() |
1ecd5b78e6 | ||
![]() |
fca2e3c51a | ||
![]() |
95ea761b2d | ||
![]() |
6b3bfa1ee9 | ||
![]() |
df3e302a9d | ||
![]() |
c88a68c9a8 | ||
![]() |
92d01b9cdd | ||
![]() |
fe04fa5986 | ||
![]() |
c382f541b4 | ||
![]() |
f420527207 | ||
![]() |
e0c83ebf79 | ||
![]() |
c7fb18fc08 | ||
![]() |
2db8ab937d | ||
![]() |
819f5dd8e5 | ||
![]() |
d4a8ed735e | ||
![]() |
f07e3bb4d5 | ||
![]() |
fa5ef0c221 | ||
![]() |
da7499ec0b | ||
![]() |
d2f4327e44 | ||
![]() |
29ae55f340 |
115
.vscode/launch.json
vendored
Normal file
115
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "dev:shell",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"dev:shell"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "build:shell",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"build:shell"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "build:universal",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"build:universal"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "build:framework",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"build:framework"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "build:webui",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"build:webui"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "dev:universal",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"dev:universal"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "dev:framework",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"dev:framework"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "dev:webui",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"dev:webui"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "lint",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"lint"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "depend",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"depend"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "dev:depend",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"dev:depend"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -6,5 +6,7 @@
|
||||
"tsconfig.json": "tsconfig.*.json, env.d.ts, vite.config.ts",
|
||||
"package.json": "package-lock.json, eslint*, .prettier*, .editorconfig, manifest.json, logo.png, .gitignore, LICENSE"
|
||||
},
|
||||
"css.customData": [".vscode/tailwindcss.json"],
|
||||
"css.customData": [
|
||||
".vscode/tailwindcss.json"
|
||||
],
|
||||
}
|
128
CODE_OF_CONDUCT.md
Normal file
128
CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,128 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
We as members, contributors, and leaders pledge to make participation in our
|
||||
community a harassment-free experience for everyone, regardless of age, body
|
||||
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
||||
identity and expression, level of experience, education, socio-economic status,
|
||||
nationality, personal appearance, race, religion, or sexual identity
|
||||
and orientation.
|
||||
|
||||
We pledge to act and interact in ways that contribute to an open, welcoming,
|
||||
diverse, inclusive, and healthy community.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to a positive environment for our
|
||||
community include:
|
||||
|
||||
* Demonstrating empathy and kindness toward other people
|
||||
* Being respectful of differing opinions, viewpoints, and experiences
|
||||
* Giving and gracefully accepting constructive feedback
|
||||
* Accepting responsibility and apologizing to those affected by our mistakes,
|
||||
and learning from the experience
|
||||
* Focusing on what is best not just for us as individuals, but for the
|
||||
overall community
|
||||
|
||||
Examples of unacceptable behavior include:
|
||||
|
||||
* The use of sexualized language or imagery, and sexual attention or
|
||||
advances of any kind
|
||||
* Trolling, insulting or derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or email
|
||||
address, without their explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Enforcement Responsibilities
|
||||
|
||||
Community leaders are responsible for clarifying and enforcing our standards of
|
||||
acceptable behavior and will take appropriate and fair corrective action in
|
||||
response to any behavior that they deem inappropriate, threatening, offensive,
|
||||
or harmful.
|
||||
|
||||
Community leaders have the right and responsibility to remove, edit, or reject
|
||||
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
||||
decisions when appropriate.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies within all community spaces, and also applies when
|
||||
an individual is officially representing the community in public spaces.
|
||||
Examples of representing our community include using an official e-mail address,
|
||||
posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported to the community leaders responsible for enforcement at
|
||||
nanaeonn@outlook.com.
|
||||
All complaints will be reviewed and investigated promptly and fairly.
|
||||
|
||||
All community leaders are obligated to respect the privacy and security of the
|
||||
reporter of any incident.
|
||||
|
||||
## Enforcement Guidelines
|
||||
|
||||
Community leaders will follow these Community Impact Guidelines in determining
|
||||
the consequences for any action they deem in violation of this Code of Conduct:
|
||||
|
||||
### 1. Correction
|
||||
|
||||
**Community Impact**: Use of inappropriate language or other behavior deemed
|
||||
unprofessional or unwelcome in the community.
|
||||
|
||||
**Consequence**: A private, written warning from community leaders, providing
|
||||
clarity around the nature of the violation and an explanation of why the
|
||||
behavior was inappropriate. A public apology may be requested.
|
||||
|
||||
### 2. Warning
|
||||
|
||||
**Community Impact**: A violation through a single incident or series
|
||||
of actions.
|
||||
|
||||
**Consequence**: A warning with consequences for continued behavior. No
|
||||
interaction with the people involved, including unsolicited interaction with
|
||||
those enforcing the Code of Conduct, for a specified period of time. This
|
||||
includes avoiding interactions in community spaces as well as external channels
|
||||
like social media. Violating these terms may lead to a temporary or
|
||||
permanent ban.
|
||||
|
||||
### 3. Temporary Ban
|
||||
|
||||
**Community Impact**: A serious violation of community standards, including
|
||||
sustained inappropriate behavior.
|
||||
|
||||
**Consequence**: A temporary ban from any sort of interaction or public
|
||||
communication with the community for a specified period of time. No public or
|
||||
private interaction with the people involved, including unsolicited interaction
|
||||
with those enforcing the Code of Conduct, is allowed during this period.
|
||||
Violating these terms may lead to a permanent ban.
|
||||
|
||||
### 4. Permanent Ban
|
||||
|
||||
**Community Impact**: Demonstrating a pattern of violation of community
|
||||
standards, including sustained inappropriate behavior, harassment of an
|
||||
individual, or aggression toward or disparagement of classes of individuals.
|
||||
|
||||
**Consequence**: A permanent ban from any sort of public interaction within
|
||||
the community.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||
version 2.0, available at
|
||||
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
||||
|
||||
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
||||
enforcement ladder](https://github.com/mozilla/diversity).
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
For answers to common questions about this code of conduct, see the FAQ at
|
||||
https://www.contributor-covenant.org/faq. Translations are available at
|
||||
https://www.contributor-covenant.org/translations.
|
83
README.md
83
README.md
@@ -1,67 +1,62 @@
|
||||
<div align="center">
|
||||
|
||||
|
||||
# NapCat
|
||||
|
||||

|
||||
|
||||
|
||||
_Modern protocol-side framework implemented based on NTQQ._
|
||||
|
||||
> 云起兮风生,心向远方兮路未曾至.
|
||||
|
||||
</div>
|
||||
|
||||
---
|
||||
## 欢迎回家
|
||||
NapCatQQ 是现代化的基于 NTQQ 的 Bot 协议端实现
|
||||
|
||||
## 特性介绍
|
||||
- [x] **安装简单**:就算是笨蛋也能使用
|
||||
- [x] **性能友好**:就算是低内存也能使用
|
||||
- [x] **接口丰富**:就算是没有也能使用
|
||||
- [x] **稳定好用**:就算是被捉也能使用
|
||||
## Welcome
|
||||
+ NapCatQQ is a modern implementation of the Bot protocol based on NTQQ.
|
||||
- NapCatQQ 是现代化的基于 NTQQ 的 Bot 协议端实现
|
||||
|
||||
## 使用框架
|
||||
## Feature
|
||||
|
||||
+ **Easy to Use**
|
||||
- 作为初学者能够轻松使用.
|
||||
+ **Quick and Efficient**
|
||||
- 在低内存操作系统长时运行.
|
||||
+ **Rich API Interface**
|
||||
- 完整实现了大部分标准接口.
|
||||
+ **Stable and Reliable**
|
||||
- 持续稳定的开发与维护.
|
||||
## Quick Start
|
||||
|
||||
可前往 [Release](https://github.com/NapNeko/NapCatQQ/releases/) 页面下载最新版本
|
||||
|
||||
**首次使用**请务必查看如下文档看使用教程
|
||||
|
||||
### 文档地址
|
||||
## Link
|
||||
|
||||
[Cloudflare.Worker](https://doc.napneko.icu/)
|
||||
| Docs | [](https://napneko.github.io/) | [](https://doc.napneko.icu/) | [](https://napcat.napneko.icu/) |
|
||||
|:-:|:-:|:-:|:-:|
|
||||
|
||||
[Cloudflare.HKServer](https://napcat.napneko.icu/)
|
||||
| Docs | [](https://napneko.pages.dev/) | [](https://docs.napcat.cyou/) | [](https://www.napcat.wiki) |
|
||||
|:-:|:-:|:-:|:-:|
|
||||
|
||||
[Github.IO](https://napneko.github.io/)
|
||||
| Contact | [](https://qm.qq.com/q/I6LU87a0Yq) | [](https://qm.qq.com/q/HaRcfrHpUk) | [](https://t.me/MelodicMoonlight) |
|
||||
|:-:|:-:|:-:|:-:|
|
||||
|
||||
[Cloudflare.Pages](https://napneko.pages.dev/)
|
||||
## Thanks
|
||||
|
||||
[Server.Other](https://docs.napcat.cyou/)
|
||||
+ [Lagrange](https://github.com/LagrangeDev/Lagrange.Core) 对本项目的大力支持 参考部分代码 已获授权
|
||||
|
||||
[NapCat.Wiki](https://www.napcat.wiki)
|
||||
+ [LLOneBot](https://github.com/LLOneBot/LLOneBot) 相关的开发曾参与本项目部分开发
|
||||
|
||||
## 回家旅途
|
||||
[QQ Group#1](https://qm.qq.com/q/I6LU87a0Yq)
|
||||
|
||||
[QQ Group#2](https://qm.qq.com/q/HaRcfrHpUk)
|
||||
|
||||
[Telegram](https://t.me/MelodicMoonlight)
|
||||
|
||||
> QQ Group#2 准许Bot / Telegram与QQ Group#2 为新建Group
|
||||
|
||||
## 性能设计/协议标准
|
||||
NapCat 已实现90%+的 OneBot / GoCQ 标准接口,并提供兼容性保留接口,其设计理念遵守 无数据库/异步优先/后台刷新 的性能思想。
|
||||
|
||||
由此设计带来一系列好处,在开发中,获取群员列表通常小于50Ms,单条文本消息发送在320Ms以内,在1k+的群聊流畅运行,同时带来一些副作用,消息Id无法持久,无法上报撤回消息原始内容。
|
||||
|
||||
NapCat 在设计理念下遵守 OneBot 规范大多数要求并且积极改进,任何合理的标准化 Issue 与 Pr 将被接收。
|
||||
|
||||
## 感谢他们
|
||||
感谢 [Lagrange](https://github.com/LagrangeDev/Lagrange.Core) 对本项目的大力支持 参考部分代码 已获授权
|
||||
|
||||
感谢 React 强力驱动 NapCat.WebUi
|
||||
|
||||
不过最最重要的 还是需要感谢屏幕前的你哦~
|
||||
+ 不过最最重要的 还是需要感谢屏幕前的你哦~
|
||||
|
||||
---
|
||||
|
||||
## 特殊感谢
|
||||
[LLOneBot](https://github.com/LLOneBot/LLOneBot) 相关的开发曾参与本项目
|
||||
## License
|
||||
本项目采用 混合协议 开源,因此使用本项目时,你需要注意以下几点:
|
||||
1. 第三方库代码或修改部分遵循其原始开源许可.
|
||||
2. 本项目获取部分项目授权而不受部分约束
|
||||
2. 项目其余逻辑代码采用[本仓库开源许可](./LICENSE).
|
||||
|
||||
## 开源附加
|
||||
|
||||
任何使用本仓库代码的地方,都应当严格遵守[本仓库开源许可](./LICENSE)。**本仓库仅用于提高易用性,实现消息推送类功能,此外,禁止任何项目未经仓库主作者授权基于 NapCat 代码开发。使用请遵守当地法律法规,由此造成的问题由使用者和提供违规使用教程者负责。**
|
||||
**本仓库仅用于提高易用性,实现消息推送类功能,此外,禁止任何项目未经仓库主作者授权基于 NapCat 代码开发。使用请遵守当地法律法规,由此造成的问题由使用者和提供违规使用教程者负责。**
|
||||
|
11
SECURITY.md
Normal file
11
SECURITY.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
| Version | Supported |
|
||||
| ------- | ------------------ |
|
||||
| > 4.0 | :white_check_mark: |
|
||||
| < 4.0 | :x: |
|
||||
|
||||
## Reporting a Vulnerability
|
||||
you should open an issue
|
BIN
external/LiteLoaderWrapper.zip
vendored
BIN
external/LiteLoaderWrapper.zip
vendored
Binary file not shown.
@@ -19,7 +19,7 @@ for %%a in ("%RetString%") do (
|
||||
SET QQPath=%pathWithoutUninstall%QQ.exe
|
||||
|
||||
if not exist "%QQpath%" (
|
||||
echo provided QQ path is invalid: %QQpath%
|
||||
echo provided QQ path is invalid
|
||||
pause
|
||||
exit /b
|
||||
)
|
||||
|
@@ -19,7 +19,7 @@ for %%a in ("%RetString%") do (
|
||||
SET QQPath=%pathWithoutUninstall%QQ.exe
|
||||
|
||||
if not exist "%QQpath%" (
|
||||
echo provided QQ path is invalid: %QQpath%
|
||||
echo provided QQ path is invalid
|
||||
pause
|
||||
exit /b
|
||||
)
|
||||
|
@@ -27,8 +27,8 @@ for %%a in ("%RetString%") do (
|
||||
|
||||
SET QQPath=%pathWithoutUninstall%QQ.exe
|
||||
|
||||
if not exist "%QQpath%" (
|
||||
echo provided QQ path is invalid: %QQpath%
|
||||
if not exist "%QQPath%" (
|
||||
echo provided QQ path is invalid
|
||||
pause
|
||||
exit /b
|
||||
)
|
||||
|
@@ -27,8 +27,8 @@ for %%a in ("%RetString%") do (
|
||||
|
||||
SET QQPath=%pathWithoutUninstall%QQ.exe
|
||||
|
||||
if not exist "%QQpath%" (
|
||||
echo provided QQ path is invalid: %QQpath%
|
||||
if not exist "%QQPath%" (
|
||||
echo provided QQ path is invalid
|
||||
pause
|
||||
exit /b
|
||||
)
|
||||
|
@@ -1,10 +1,9 @@
|
||||
{
|
||||
"name": "qq-chat",
|
||||
"version": "9.9.17-30899",
|
||||
"verHash": "ececf273",
|
||||
"linuxVersion": "3.2.15-30899",
|
||||
"linuxVerHash": "63c751e8",
|
||||
"type": "module",
|
||||
"version": "9.9.18-32793",
|
||||
"verHash": "d43f097e",
|
||||
"linuxVersion": "3.2.16-32793",
|
||||
"linuxVerHash": "ee4bd910",
|
||||
"private": true,
|
||||
"description": "QQ",
|
||||
"productName": "QQ",
|
||||
@@ -17,10 +16,27 @@
|
||||
"bin": {
|
||||
"qd": "externals/devtools/cli/index.js"
|
||||
},
|
||||
"appid": {
|
||||
"win32": "537258389",
|
||||
"darwin": "537258412",
|
||||
"linux": "537258424"
|
||||
},
|
||||
"main": "./loadNapCat.js",
|
||||
"buildVersion": "30899",
|
||||
"peerDependenciesMeta": {
|
||||
"*": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"pnpm": {
|
||||
"patchedDependencies": {
|
||||
"@vue/runtime-dom@3.5.12": "patches/@vue__runtime-dom@3.5.12.patch",
|
||||
"@swc/helpers@0.5.3": "patches/@swc__helpers@0.5.3.patch",
|
||||
"vuex@4.1.0": "patches/vuex@4.1.0.patch"
|
||||
}
|
||||
},
|
||||
"buildVersion": "32793",
|
||||
"isPureShell": true,
|
||||
"isByteCodeShell": true,
|
||||
"platform": "win32",
|
||||
"eleArch": "x64"
|
||||
}
|
||||
}
|
@@ -4,7 +4,7 @@
|
||||
"name": "NapCatQQ",
|
||||
"slug": "NapCat.Framework",
|
||||
"description": "高性能的 OneBot 11 协议实现",
|
||||
"version": "4.5.20",
|
||||
"version": "4.7.6",
|
||||
"icon": "./logo.png",
|
||||
"authors": [
|
||||
{
|
||||
|
@@ -136,7 +136,7 @@ const OneBotApiDebug: React.FC<OneBotApiDebugProps> = (props) => {
|
||||
</div>
|
||||
<Card
|
||||
shadow="sm"
|
||||
className="my-4 bg-opacity-50 backdrop-blur-md overflow-visible z-20"
|
||||
className="my-4 bg-opacity-50 backdrop-blur-md overflow-visible"
|
||||
>
|
||||
<CardHeader className="font-bold text-lg gap-1 pb-0">
|
||||
<span className="mr-2">请求体</span>
|
||||
|
@@ -1,19 +1,21 @@
|
||||
import { PlayMode } from '@/const/enum'
|
||||
|
||||
import WebUIManager from '@/controllers/webui_manager'
|
||||
import type {
|
||||
FinalMusic,
|
||||
Music163ListResponse,
|
||||
Music163URLResponse
|
||||
} from '@/types/music'
|
||||
|
||||
import WebUIManager from '@/controllers/webui_manager'
|
||||
/**
|
||||
* 获取网易云音乐歌单
|
||||
* @param id 歌单id
|
||||
* @returns 歌单信息
|
||||
*/
|
||||
export const get163MusicList = async (id: string) => {
|
||||
let res = await WebUIManager.proxy<Music163ListResponse>('https://wavesgame.top/playlist/track/all?id=' + id);
|
||||
let res = await WebUIManager.proxy<Music163ListResponse>(
|
||||
'https://wavesgame.top/playlist/track/all?id=' + id
|
||||
)
|
||||
// const res = await request.get<Music163ListResponse>(
|
||||
// `https://wavesgame.top/playlist/track/all?id=${id}`
|
||||
// )
|
||||
@@ -71,7 +73,7 @@ export const get163MusicListSongs = async (id: string) => {
|
||||
if (songURL) {
|
||||
finalMusic.push({
|
||||
id: song.id,
|
||||
url: songURL,
|
||||
url: songURL.replace(/http:\/\//, '//').replace(/https:\/\//, '//'),
|
||||
title: song.name,
|
||||
artist: song.ar.map((p) => p.name).join('/'),
|
||||
cover: song.al.picUrl
|
||||
|
26
package.json
26
package.json
@@ -2,7 +2,7 @@
|
||||
"name": "napcat",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"version": "4.5.20",
|
||||
"version": "4.7.6",
|
||||
"scripts": {
|
||||
"build:universal": "npm run build:webui && vite build --mode universal || exit 1",
|
||||
"build:framework": "npm run build:webui && vite build --mode framework || exit 1",
|
||||
@@ -23,6 +23,7 @@
|
||||
"@eslint/js": "^9.14.0",
|
||||
"@ffmpeg.wasm/main": "^0.13.1",
|
||||
"@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
|
||||
"@hono/node-server": "^1.13.8",
|
||||
"@log4js-node/log4js-api": "^1.0.2",
|
||||
"@napneko/nap-proto-core": "^0.0.4",
|
||||
"@rollup/plugin-node-resolve": "^16.0.0",
|
||||
@@ -42,31 +43,30 @@
|
||||
"ajv": "^8.13.0",
|
||||
"async-mutex": "^0.5.0",
|
||||
"commander": "^13.0.0",
|
||||
"compressing": "^1.10.1",
|
||||
"cors": "^2.8.5",
|
||||
"esbuild": "0.24.0",
|
||||
"esbuild": "0.25.0",
|
||||
"eslint": "^9.14.0",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
"eslint-import-resolver-typescript": "^4.0.0",
|
||||
"eslint-plugin-import": "^2.29.1",
|
||||
"express-rate-limit": "^7.5.0",
|
||||
"fast-xml-parser": "^4.3.6",
|
||||
"file-type": "^20.0.0",
|
||||
"globals": "^15.12.0",
|
||||
"globals": "^16.0.0",
|
||||
"hono": "^4.7.2",
|
||||
"image-size": "^1.1.1",
|
||||
"json5": "^2.2.3",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"napcat.protobuf": "^1.1.3",
|
||||
"typescript": "^5.3.3",
|
||||
"typescript-eslint": "^8.13.0",
|
||||
"vite": "^6.0.1",
|
||||
"vite-plugin-cp": "^4.0.8",
|
||||
"vite-plugin-wasm": "^3.4.1",
|
||||
"vite-tsconfig-paths": "^5.1.0",
|
||||
"silk-wasm": "^3.6.1",
|
||||
"@hono/node-ws": "^1.1.0",
|
||||
"winston": "^3.17.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ffmpeg.wasm/core-mt": "^0.13.2",
|
||||
"compressing": "^1.10.1",
|
||||
"express": "^5.0.0",
|
||||
"piscina": "^4.7.0",
|
||||
"silk-wasm": "^3.6.1",
|
||||
"ws": "^8.18.0"
|
||||
}
|
||||
}
|
||||
"dependencies": {}
|
||||
}
|
@@ -1,9 +1,20 @@
|
||||
import { encode } from 'silk-wasm';
|
||||
import { parentPort } from 'worker_threads';
|
||||
|
||||
export interface EncodeArgs {
|
||||
input: ArrayBufferView | ArrayBuffer
|
||||
sampleRate: number
|
||||
}
|
||||
export default async ({ input, sampleRate }: EncodeArgs) => {
|
||||
export function recvTask<T>(cb: (taskData: T) => Promise<unknown>) {
|
||||
parentPort?.on('message', async (taskData: T) => {
|
||||
try {
|
||||
let ret = await cb(taskData);
|
||||
parentPort?.postMessage(ret);
|
||||
} catch (error: unknown) {
|
||||
parentPort?.postMessage({ error: (error as Error).message });
|
||||
}
|
||||
});
|
||||
}
|
||||
recvTask<EncodeArgs>(async ({ input, sampleRate }) => {
|
||||
return await encode(input, sampleRate);
|
||||
};
|
||||
});
|
@@ -1,4 +1,3 @@
|
||||
import Piscina from 'piscina';
|
||||
import fsPromise from 'fs/promises';
|
||||
import path from 'node:path';
|
||||
import { randomUUID } from 'crypto';
|
||||
@@ -6,16 +5,16 @@ import { EncodeResult, getDuration, getWavFileInfo, isSilk, isWav } from 'silk-w
|
||||
import { LogWrapper } from '@/common/log';
|
||||
import { EncodeArgs } from '@/common/audio-worker';
|
||||
import { FFmpegService } from '@/common/ffmpeg';
|
||||
import { runTask } from './worker';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const ALLOW_SAMPLE_RATE = [8000, 12000, 16000, 24000, 32000, 44100, 48000];
|
||||
|
||||
async function getWorkerPath() {
|
||||
return new URL(/* @vite-ignore */ './audio-worker.mjs', import.meta.url).href;
|
||||
function getWorkerPath() {
|
||||
//return new URL(/* @vite-ignore */ './audio-worker.mjs', import.meta.url).href;
|
||||
return path.join(path.dirname(fileURLToPath(import.meta.url)), 'audio-worker.mjs');
|
||||
}
|
||||
|
||||
const piscina = new Piscina<EncodeArgs, EncodeResult>({
|
||||
filename: await getWorkerPath(),
|
||||
});
|
||||
|
||||
async function guessDuration(pttPath: string, logger: LogWrapper) {
|
||||
const pttFileInfo = await fsPromise.stat(pttPath);
|
||||
@@ -46,7 +45,7 @@ export async function encodeSilk(filePath: string, TEMP_DIR: string, logger: Log
|
||||
const { input, sampleRate } = isWav(file)
|
||||
? await handleWavFile(file, filePath, pcmPath)
|
||||
: { input: await FFmpegService.convert(filePath, pcmPath), sampleRate: 24000 };
|
||||
const silk = await piscina.run({ input: input, sampleRate: sampleRate });
|
||||
const silk = await runTask<EncodeArgs, EncodeResult>(getWorkerPath(), { input: input, sampleRate: sampleRate });
|
||||
fsPromise.unlink(pcmPath).catch((e) => logger.logError('删除临时文件失败', pcmPath, e));
|
||||
await fsPromise.writeFile(pttPath, Buffer.from(silk.data));
|
||||
logger.log(`语音文件${filePath}转换成功!`, pttPath, '时长:', silk.duration);
|
||||
|
@@ -5,6 +5,17 @@ import { readFileSync, statSync, writeFileSync } from 'fs';
|
||||
import type { VideoInfo } from './video';
|
||||
import { fileTypeFromFile } from 'file-type';
|
||||
import imageSize from 'image-size';
|
||||
import { parentPort } from 'worker_threads';
|
||||
export function recvTask<T>(cb: (taskData: T) => Promise<unknown>) {
|
||||
parentPort?.on('message', async (taskData: T) => {
|
||||
try {
|
||||
let ret = await cb(taskData);
|
||||
parentPort?.postMessage(ret);
|
||||
} catch (error: unknown) {
|
||||
parentPort?.postMessage({ error: (error as Error).message });
|
||||
}
|
||||
});
|
||||
}
|
||||
class FFmpegService {
|
||||
public static async extractThumbnail(videoPath: string, thumbnailPath: string): Promise<void> {
|
||||
const ffmpegInstance = await FFmpeg.create({ core: '@ffmpeg.wasm/core-mt' });
|
||||
@@ -137,15 +148,18 @@ interface FFmpegTask {
|
||||
}
|
||||
export default async function handleFFmpegTask({ method, args }: FFmpegTask): Promise<any> {
|
||||
switch (method) {
|
||||
case 'extractThumbnail':
|
||||
return await FFmpegService.extractThumbnail(...args as [string, string]);
|
||||
case 'convertFile':
|
||||
return await FFmpegService.convertFile(...args as [string, string, string]);
|
||||
case 'convert':
|
||||
return await FFmpegService.convert(...args as [string, string]);
|
||||
case 'getVideoInfo':
|
||||
return await FFmpegService.getVideoInfo(...args as [string, string]);
|
||||
default:
|
||||
throw new Error(`Unknown method: ${method}`);
|
||||
case 'extractThumbnail':
|
||||
return await FFmpegService.extractThumbnail(...args as [string, string]);
|
||||
case 'convertFile':
|
||||
return await FFmpegService.convertFile(...args as [string, string, string]);
|
||||
case 'convert':
|
||||
return await FFmpegService.convert(...args as [string, string]);
|
||||
case 'getVideoInfo':
|
||||
return await FFmpegService.getVideoInfo(...args as [string, string]);
|
||||
default:
|
||||
throw new Error(`Unknown method: ${method}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
recvTask<FFmpegTask>(async ({ method, args }: FFmpegTask) => {
|
||||
return await handleFFmpegTask({ method, args });
|
||||
});
|
@@ -1,6 +1,8 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import Piscina from 'piscina';
|
||||
import { VideoInfo } from './video';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { runTask } from './worker';
|
||||
|
||||
type EncodeArgs = {
|
||||
method: 'extractThumbnail' | 'convertFile' | 'convert' | 'getVideoInfo';
|
||||
@@ -9,42 +11,26 @@ type EncodeArgs = {
|
||||
|
||||
type EncodeResult = any;
|
||||
|
||||
async function getWorkerPath() {
|
||||
return new URL(/* @vite-ignore */ './ffmpeg-worker.mjs', import.meta.url).href;
|
||||
function getWorkerPath() {
|
||||
return path.join(path.dirname(fileURLToPath(import.meta.url)), './ffmpeg-worker.mjs');
|
||||
}
|
||||
|
||||
export class FFmpegService {
|
||||
public static async extractThumbnail(videoPath: string, thumbnailPath: string): Promise<void> {
|
||||
const piscina = new Piscina<EncodeArgs, EncodeResult>({
|
||||
filename: await getWorkerPath(),
|
||||
});
|
||||
await piscina.run({ method: 'extractThumbnail', args: [videoPath, thumbnailPath] });
|
||||
await piscina.destroy();
|
||||
await runTask<EncodeArgs, EncodeResult>(getWorkerPath(), { method: 'extractThumbnail', args: [videoPath, thumbnailPath] });
|
||||
}
|
||||
|
||||
public static async convertFile(inputFile: string, outputFile: string, format: string): Promise<void> {
|
||||
const piscina = new Piscina<EncodeArgs, EncodeResult>({
|
||||
filename: await getWorkerPath(),
|
||||
});
|
||||
await piscina.run({ method: 'convertFile', args: [inputFile, outputFile, format] });
|
||||
await piscina.destroy();
|
||||
await runTask<EncodeArgs, EncodeResult>(getWorkerPath(), { method: 'convertFile', args: [inputFile, outputFile, format] });
|
||||
}
|
||||
|
||||
public static async convert(filePath: string, pcmPath: string): Promise<Buffer> {
|
||||
const piscina = new Piscina<EncodeArgs, EncodeResult>({
|
||||
filename: await getWorkerPath(),
|
||||
});
|
||||
const result = await piscina.run({ method: 'convert', args: [filePath, pcmPath] });
|
||||
await piscina.destroy();
|
||||
const result = await runTask<EncodeArgs, EncodeResult>(getWorkerPath(), { method: 'convert', args: [filePath, pcmPath] });
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async getVideoInfo(videoPath: string, thumbnailPath: string): Promise<VideoInfo> {
|
||||
const piscina = new Piscina<EncodeArgs, EncodeResult>({
|
||||
filename: await getWorkerPath(),
|
||||
});
|
||||
const result = await piscina.run({ method: 'getVideoInfo', args: [videoPath, thumbnailPath] });
|
||||
await piscina.destroy();
|
||||
const result = await await runTask<EncodeArgs, EncodeResult>(getWorkerPath(), { method: 'getVideoInfo', args: [videoPath, thumbnailPath] });
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -232,7 +232,7 @@ export function rawMessageToText(msg: RawMessage, recursiveLevel = 0): string {
|
||||
tokens.push(`群聊 [${msg.peerName}(${msg.peerUin})]`);
|
||||
}
|
||||
if (msg.senderUin !== '0') {
|
||||
tokens.push(`[${msg.sendMemberName ?? msg.sendRemarkName ?? msg.sendNickName}(${msg.senderUin})]`);
|
||||
tokens.push(`[${msg.sendMemberName || msg.sendRemarkName || msg.sendNickName}(${msg.senderUin})]`);
|
||||
}
|
||||
} else if (msg.chatType == ChatType.KCHATTYPEDATALINE) {
|
||||
tokens.push('移动设备');
|
||||
|
@@ -163,7 +163,7 @@ class Store {
|
||||
const current = this.get<StoreValueType>(key);
|
||||
|
||||
if (current === null) {
|
||||
this.set(key, 1);
|
||||
this.set(key, 1, 60);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -180,7 +180,7 @@ class Store {
|
||||
}
|
||||
|
||||
const newValue = numericValue + 1;
|
||||
this.set(key, newValue);
|
||||
this.set(key, newValue, 60);
|
||||
return newValue;
|
||||
}
|
||||
}
|
||||
|
@@ -1 +1 @@
|
||||
export const napCatVersion = '4.5.20';
|
||||
export const napCatVersion = '4.7.6';
|
||||
|
29
src/common/worker.ts
Normal file
29
src/common/worker.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Worker } from 'worker_threads';
|
||||
|
||||
export async function runTask<T, R>(workerScript: string, taskData: T): Promise<R> {
|
||||
let worker = new Worker(workerScript);
|
||||
try {
|
||||
return await new Promise<R>((resolve, reject) => {
|
||||
worker.on('message', (result: R) => {
|
||||
resolve(result);
|
||||
});
|
||||
|
||||
worker.on('error', (error) => {
|
||||
reject(new Error(`Worker error: ${error.message}`));
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
if (code !== 0) {
|
||||
reject(new Error(`Worker stopped with exit code ${code}`));
|
||||
}
|
||||
});
|
||||
worker.postMessage(taskData);
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
throw new Error(`Failed to run task: ${(error as Error).message}`);
|
||||
} finally {
|
||||
// Ensure the worker is terminated after the promise is settled
|
||||
worker.terminate();
|
||||
}
|
||||
}
|
||||
|
@@ -41,7 +41,8 @@ export class NTQQFileApi {
|
||||
this.context = context;
|
||||
this.core = core;
|
||||
this.rkeyManager = new RkeyManager([
|
||||
'https://rkey.napneko.icu/rkeys'
|
||||
'https://ss.xingzhige.com/music_card/rkey', // 国内
|
||||
'https://secret-service.bietiaop.com/rkeys',//国内
|
||||
],
|
||||
this.context.logger
|
||||
);
|
||||
|
@@ -27,6 +27,9 @@ export class NTQQGroupApi {
|
||||
this.core = core;
|
||||
}
|
||||
|
||||
async setGroupRemark(groupCode: string, remark: string) {
|
||||
return this.context.session.getGroupService().modifyGroupRemark(groupCode, remark);
|
||||
}
|
||||
async fetchGroupDetail(groupCode: string) {
|
||||
const [, detailInfo] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelGroupService/getGroupDetailInfo',
|
||||
@@ -165,7 +168,13 @@ export class NTQQGroupApi {
|
||||
|
||||
return this.groupMemberCache.get(groupCode);
|
||||
}
|
||||
|
||||
async refreshGroupMemberCachePartial(groupCode: string, uid: string) {
|
||||
const member = await this.getGroupMemberEx(groupCode, uid, true);
|
||||
if (member) {
|
||||
this.groupMemberCache.get(groupCode)?.set(uid, member);
|
||||
}
|
||||
return member;
|
||||
}
|
||||
async getGroupMember(groupCode: string | number, memberUinOrUid: string | number) {
|
||||
const groupCodeStr = groupCode.toString();
|
||||
const memberUinOrUidStr = memberUinOrUid.toString();
|
||||
@@ -339,9 +348,9 @@ export class NTQQGroupApi {
|
||||
return this.context.session.getGroupService().uploadGroupBulletinPic(groupCode, _Pskey, imageurl);
|
||||
}
|
||||
|
||||
async handleGroupRequest(notify: GroupNotify, operateType: NTGroupRequestOperateTypes, reason?: string) {
|
||||
async handleGroupRequest(doubt: boolean, notify: GroupNotify, operateType: NTGroupRequestOperateTypes, reason?: string) {
|
||||
return this.context.session.getGroupService().operateSysNotify(
|
||||
false,
|
||||
doubt,
|
||||
{
|
||||
operateType: operateType,
|
||||
targetMsg: {
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { ChatType, GetFileListParam, Peer, RawMessage, SendMessageElement, SendStatusType } from '@/core/types';
|
||||
import { GroupFileInfoUpdateItem, InstanceContext, NapCatCore } from '@/core';
|
||||
import { GroupFileInfoUpdateItem, InstanceContext, NapCatCore, NodeIKernelMsgService } from '@/core';
|
||||
import { GeneralCallResult } from '@/core/services/common';
|
||||
|
||||
export class NTQQMsgApi {
|
||||
@@ -12,6 +12,11 @@ export class NTQQMsgApi {
|
||||
this.context = context;
|
||||
this.core = core;
|
||||
}
|
||||
|
||||
async clickInlineKeyboardButton(...params: Parameters<NodeIKernelMsgService['clickInlineKeyboardButton']>) {
|
||||
return this.context.session.getMsgService().clickInlineKeyboardButton(...params);
|
||||
}
|
||||
|
||||
getMsgByClientSeqAndTime(peer: Peer, replyMsgClientSeq: string, replyMsgTime: string) {
|
||||
// https://bot.q.qq.com/wiki/develop/api-v2/openapi/emoji/model.html#EmojiType 可以用过特殊方式拉取
|
||||
return this.context.session.getMsgService().getMsgByClientSeqAndTime(peer, replyMsgClientSeq, replyMsgTime);
|
||||
@@ -131,6 +136,20 @@ export class NTQQMsgApi {
|
||||
});
|
||||
}
|
||||
|
||||
async queryFirstMsgBySender(peer: Peer, SendersUid: string[]) {
|
||||
console.log(peer, SendersUid);
|
||||
return await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', '0', {
|
||||
chatInfo: peer,
|
||||
filterMsgType: [],
|
||||
filterSendersUid: SendersUid,
|
||||
filterMsgToTime: '0',
|
||||
filterMsgFromTime: '0',
|
||||
isReverseOrder: true,
|
||||
isIncludeCurrent: true,
|
||||
pageLimit: 20000,
|
||||
});
|
||||
}
|
||||
|
||||
async setMsgRead(peer: Peer) {
|
||||
return this.context.session.getMsgService().setMsgRead(peer);
|
||||
}
|
||||
|
42
src/core/external/appid.json
vendored
42
src/core/external/appid.json
vendored
@@ -186,5 +186,45 @@
|
||||
"9.9.17-31363": {
|
||||
"appid": 537266500,
|
||||
"qua": "V1_WIN_NQ_9.9.17_31363_GW_B"
|
||||
},
|
||||
"3.2.16-32690": {
|
||||
"appid": 537271229,
|
||||
"qua": "V1_LNX_NQ_3.2.16_32690_GW_B"
|
||||
},
|
||||
"9.9.18-32690": {
|
||||
"appid": 537271194,
|
||||
"qua": "V1_WIN_NQ_9.9.18_32690_GW_B"
|
||||
},
|
||||
"6.9.66-32690": {
|
||||
"appid": 537271218,
|
||||
"qua": "V1_MAC_NQ_6.9.66_32690_GW_B"
|
||||
},
|
||||
"3.2.16-32721": {
|
||||
"appid": 537271229,
|
||||
"qua": "V1_LNX_NQ_3.2.16_32721_GW_B"
|
||||
},
|
||||
"9.9.18-32793": {
|
||||
"appid": 537271244,
|
||||
"qua": "V1_WIN_NQ_9.9.18_32793_GW_B"
|
||||
},
|
||||
"3.2.16-32793": {
|
||||
"appid": 537271279,
|
||||
"qua": "V1_LNX_NQ_3.2.16_32793_GW_B"
|
||||
},
|
||||
"3.2.16-32869": {
|
||||
"appid": 537271329,
|
||||
"qua": "V1_LNX_NQ_3.2.16_32869_GW_B"
|
||||
},
|
||||
"9.9.18-32869": {
|
||||
"appid": 537271294,
|
||||
"qua": "V1_WIN_NQ_9.9.18_32869_GW_B"
|
||||
},
|
||||
"3.2.16-33139": {
|
||||
"appid": 537273909,
|
||||
"qua": "V1_LNX_NQ_3.2.16_33139_GW_B"
|
||||
},
|
||||
"9.9.18-33139": {
|
||||
"appid": 537273874,
|
||||
"qua": "V1_WIN_NQ_9.9.18_33139_GW_B"
|
||||
}
|
||||
}
|
||||
}
|
5
src/core/external/napcat.json
vendored
5
src/core/external/napcat.json
vendored
@@ -4,5 +4,6 @@
|
||||
"fileLogLevel": "debug",
|
||||
"consoleLogLevel": "info",
|
||||
"packetBackend": "auto",
|
||||
"packetServer": ""
|
||||
}
|
||||
"packetServer": "",
|
||||
"o3HookMode": 1
|
||||
}
|
60
src/core/external/offset.json
vendored
60
src/core/external/offset.json
vendored
@@ -175,7 +175,7 @@
|
||||
"send": "713A318",
|
||||
"recv": "713DB50"
|
||||
},
|
||||
"6.9.63.30851-x64": {
|
||||
"6.9.63-30851-x64": {
|
||||
"send": "46C8040",
|
||||
"recv": "46CA8AC"
|
||||
},
|
||||
@@ -246,5 +246,61 @@
|
||||
"6.9.65-31363-arm64": {
|
||||
"send": "422CEF8",
|
||||
"recv": "422F710"
|
||||
},
|
||||
"9.9.18-32690-x64": {
|
||||
"send": "39F9630",
|
||||
"recv": "39FDE30"
|
||||
},
|
||||
"3.2.16-32690-x64": {
|
||||
"send": "A5E24C0",
|
||||
"recv": "A5E5EE0"
|
||||
},
|
||||
"3.2.16-32690-arm64": {
|
||||
"send": "7226630",
|
||||
"recv": "7229F60"
|
||||
},
|
||||
"3.2.16-32721-x64": {
|
||||
"send": "A5E24C0",
|
||||
"recv": "A5E5EE0"
|
||||
},
|
||||
"3.2.16-32721-arm64": {
|
||||
"send": "7226630",
|
||||
"recv": "7229F60"
|
||||
},
|
||||
"9.9.18-32793-x64": {
|
||||
"send": "39F9A30",
|
||||
"recv": "39FE230"
|
||||
},
|
||||
"3.2.16-32793-x64": {
|
||||
"send": "A5E24C0",
|
||||
"recv": "A5E5EE0"
|
||||
},
|
||||
"3.2.16-32793-arm64": {
|
||||
"send": "7226630",
|
||||
"recv": "7229F60"
|
||||
},
|
||||
"9.9.18-32869-x64": {
|
||||
"send": "39F9A30",
|
||||
"recv": "39FE230"
|
||||
},
|
||||
"3.2.16-32869-x64": {
|
||||
"send": "A5E24C0",
|
||||
"recv": "A5E5EE0"
|
||||
},
|
||||
"3.2.16-32869-arm64": {
|
||||
"send": "7226630",
|
||||
"recv": "7229F60"
|
||||
},
|
||||
"9.9.18-33139-x64": {
|
||||
"send": "39F5870",
|
||||
"recv": "39FA070"
|
||||
},
|
||||
"3.2.16-33139-x64": {
|
||||
"send": "A634F60",
|
||||
"recv": "A638980"
|
||||
},
|
||||
"3.2.16-33139-arm64": {
|
||||
"send": "7262BB0",
|
||||
"recv": "72664E0"
|
||||
}
|
||||
}
|
||||
}
|
@@ -10,6 +10,7 @@ export const NapcatConfigSchema = Type.Object({
|
||||
consoleLogLevel: Type.String({ default: 'info' }),
|
||||
packetBackend: Type.String({ default: 'auto' }),
|
||||
packetServer: Type.String({ default: '' }),
|
||||
o3HookMode: Type.Number({ default: 0 }),
|
||||
});
|
||||
|
||||
export type NapcatConfig = Static<typeof NapcatConfigSchema>;
|
||||
|
@@ -1,5 +1,5 @@
|
||||
export class NodeIKernelLoginListener {
|
||||
onLoginConnected(...args: any[]): any {
|
||||
onLoginConnected(): Promise<void> | void {
|
||||
}
|
||||
|
||||
onLoginDisConnected(...args: any[]): any {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { ChatType } from '@/core';
|
||||
import { ChatType, RawMessage } from '@/core';
|
||||
export interface SearchGroupInfo {
|
||||
groupCode: string;
|
||||
ownerUid: string;
|
||||
@@ -56,7 +56,7 @@ export interface GroupSearchResult {
|
||||
nextPos: number;
|
||||
}
|
||||
export interface NodeIKernelSearchListener {
|
||||
|
||||
|
||||
onSearchGroupResult(params: GroupSearchResult): any;
|
||||
|
||||
onSearchFileKeywordsResult(params: {
|
||||
@@ -94,4 +94,27 @@ export interface NodeIKernelSearchListener {
|
||||
}[]
|
||||
}[]
|
||||
}): any;
|
||||
|
||||
onSearchMsgKeywordsResult(params: {
|
||||
searchId: string,
|
||||
hasMore: boolean,
|
||||
resultItems: Array<{
|
||||
msgId: string,
|
||||
msgSeq: string,
|
||||
msgTime: string,
|
||||
senderUid: string,
|
||||
senderUin: string,
|
||||
senderNick: string,
|
||||
senderNickHits: unknown[],
|
||||
senderRemark: string,
|
||||
senderRemarkHits: unknown[],
|
||||
senderCard: string,
|
||||
senderCardHits: unknown[],
|
||||
fieldType: number,
|
||||
fieldText: string,
|
||||
msgRecord: RawMessage;
|
||||
hitsInfo: Array<unknown>,
|
||||
msgAbstract: unknown,
|
||||
}>
|
||||
}): void | Promise<void>;
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ import { PacketLogger } from '@/core/packet/context/loggerContext';
|
||||
|
||||
// 0 send 1 recv
|
||||
export interface NativePacketExportType {
|
||||
InitHook?: (send: string, recv: string, callback: (type: number, uin: string, cmd: string, seq: number, hex_data: string) => void) => boolean;
|
||||
InitHook?: (send: string, recv: string, callback: (type: number, uin: string, cmd: string, seq: number, hex_data: string) => void, o3_hook: boolean) => boolean;
|
||||
SendPacket?: (cmd: string, data: string, trace_id: string) => void;
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ export class NativePacketClient extends IPacketClient {
|
||||
const platform = process.platform + '.' + process.arch;
|
||||
const moehoo_path = path.join(dirname(fileURLToPath(import.meta.url)), './moehoo/MoeHoo.' + platform + '.node');
|
||||
process.dlopen(this.MoeHooExport, moehoo_path, constants.dlopen.RTLD_LAZY);
|
||||
|
||||
this.MoeHooExport.exports.InitHook?.(send, recv, (type: number, uin: string, cmd: string, seq: number, hex_data: string) => {
|
||||
const trace_id = createHash('md5').update(Buffer.from(hex_data, 'hex')).digest('hex');
|
||||
if (type === 0 && this.cb.get(trace_id + 'recv')) {
|
||||
@@ -55,7 +56,7 @@ export class NativePacketClient extends IPacketClient {
|
||||
// console.log('callback:', callback, trace_id);
|
||||
callback?.({ seq, cmd, hex_data });
|
||||
}
|
||||
});
|
||||
}, this.napcore.config.o3HookMode == 1);
|
||||
this.available = true;
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,3 @@
|
||||
import { Data, WebSocket, ErrorEvent } from 'ws';
|
||||
import { IPacketClient, RecvPacket } from '@/core/packet/client/baseClient';
|
||||
import { LogStack } from '@/core/packet/context/clientContext';
|
||||
import { NapCoreContext } from '@/core/packet/context/napCoreContext';
|
||||
@@ -83,14 +82,14 @@ export class WsPacketClient extends IPacketClient {
|
||||
this.logger.warn('WebSocket 连接关闭,尝试重连...');
|
||||
reject(new Error('WebSocket 连接关闭'));
|
||||
};
|
||||
this.websocket.onmessage = (event) => this.handleMessage(event.data).catch(err => {
|
||||
this.websocket.onmessage = (ev: MessageEvent<any>) => this.handleMessage(ev).catch(err => {
|
||||
this.logger.error(`处理消息时出错: ${err}`);
|
||||
});
|
||||
this.websocket.onerror = (event: ErrorEvent) => {
|
||||
this.websocket.onerror = (event) => {
|
||||
this.available = false;
|
||||
this.logger.error(`WebSocket 出错: ${event.message}`);
|
||||
this.logger.error(`WebSocket 出错: ${event}`);
|
||||
this.websocket?.close();
|
||||
reject(new Error(`WebSocket 出错: ${event.message}`));
|
||||
reject(new Error(`WebSocket 出错: ${event}`));
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -99,9 +98,9 @@ export class WsPacketClient extends IPacketClient {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
private async handleMessage(message: Data): Promise<void> {
|
||||
private async handleMessage(message: MessageEvent): Promise<void> {
|
||||
try {
|
||||
const json: RecvPacket = JSON.parse(message.toString());
|
||||
const json: RecvPacket = JSON.parse(message.data.toString());
|
||||
const trace_id_md5 = json.trace_id_md5;
|
||||
const action = json?.type ?? 'init';
|
||||
const event = this.cb.get(`${trace_id_md5}${action}`);
|
||||
|
@@ -1,22 +1,22 @@
|
||||
import * as crypto from 'crypto';
|
||||
import {PacketContext} from '@/core/packet/context/packetContext';
|
||||
import { PacketContext } from '@/core/packet/context/packetContext';
|
||||
import * as trans from '@/core/packet/transformer';
|
||||
import {PacketMsg} from '@/core/packet/message/message';
|
||||
import { PacketMsg } from '@/core/packet/message/message';
|
||||
import {
|
||||
PacketMsgFileElement,
|
||||
PacketMsgPicElement,
|
||||
PacketMsgPttElement,
|
||||
PacketMsgVideoElement
|
||||
} from '@/core/packet/message/element';
|
||||
import {ChatType, MsgSourceType, NTMsgType, RawMessage} from '@/core';
|
||||
import {MiniAppRawData, MiniAppReqParams} from '@/core/packet/entities/miniApp';
|
||||
import {AIVoiceChatType} from '@/core/packet/entities/aiChat';
|
||||
import {NapProtoDecodeStructType, NapProtoEncodeStructType, NapProtoMsg} from '@napneko/nap-proto-core';
|
||||
import {IndexNode, LongMsgResult, MsgInfo} from '@/core/packet/transformer/proto';
|
||||
import {OidbPacket} from '@/core/packet/transformer/base';
|
||||
import {ImageOcrResult} from '@/core/packet/entities/ocrResult';
|
||||
import {gunzipSync} from 'zlib';
|
||||
import {PacketMsgConverter} from '@/core/packet/message/converter';
|
||||
import { ChatType, MsgSourceType, NTMsgType, RawMessage } from '@/core';
|
||||
import { MiniAppRawData, MiniAppReqParams } from '@/core/packet/entities/miniApp';
|
||||
import { AIVoiceChatType } from '@/core/packet/entities/aiChat';
|
||||
import { NapProtoDecodeStructType, NapProtoEncodeStructType, NapProtoMsg } from '@napneko/nap-proto-core';
|
||||
import { IndexNode, LongMsgResult, MsgInfo } from '@/core/packet/transformer/proto';
|
||||
import { OidbPacket } from '@/core/packet/transformer/base';
|
||||
import { ImageOcrResult } from '@/core/packet/entities/ocrResult';
|
||||
import { gunzipSync } from 'zlib';
|
||||
import { PacketMsgConverter } from '@/core/packet/message/converter';
|
||||
|
||||
export class PacketOperationContext {
|
||||
private readonly context: PacketContext;
|
||||
@@ -59,10 +59,10 @@ export class PacketOperationContext {
|
||||
const res = trans.GetStrangerInfo.parse(resp);
|
||||
const extBigInt = BigInt(res.data.status.value);
|
||||
if (extBigInt <= 10n) {
|
||||
return {status: Number(extBigInt) * 10, ext_status: 0};
|
||||
return { status: Number(extBigInt) * 10, ext_status: 0 };
|
||||
}
|
||||
status = Number((extBigInt & 0xff00n) + ((extBigInt >> 16n) & 0xffn));
|
||||
return {status: 10, ext_status: status};
|
||||
return { status: 10, ext_status: status };
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
@@ -79,13 +79,13 @@ export class PacketOperationContext {
|
||||
const reqList = msg.flatMap(m =>
|
||||
m.msg.map(e => {
|
||||
if (e instanceof PacketMsgPicElement) {
|
||||
return this.context.highway.uploadImage({chatType, peerUid}, e);
|
||||
return this.context.highway.uploadImage({ chatType, peerUid }, e);
|
||||
} else if (e instanceof PacketMsgVideoElement) {
|
||||
return this.context.highway.uploadVideo({chatType, peerUid}, e);
|
||||
return this.context.highway.uploadVideo({ chatType, peerUid }, e);
|
||||
} else if (e instanceof PacketMsgPttElement) {
|
||||
return this.context.highway.uploadPtt({chatType, peerUid}, e);
|
||||
return this.context.highway.uploadPtt({ chatType, peerUid }, e);
|
||||
} else if (e instanceof PacketMsgFileElement) {
|
||||
return this.context.highway.uploadFile({chatType, peerUid}, e);
|
||||
return this.context.highway.uploadFile({ chatType, peerUid }, e);
|
||||
}
|
||||
return null;
|
||||
}).filter(Boolean)
|
||||
@@ -160,6 +160,12 @@ export class PacketOperationContext {
|
||||
const res = trans.DownloadGroupFile.parse(resp);
|
||||
return `https://${res.download.downloadDns}/ftn_handler/${Buffer.from(res.download.downloadUrl).toString('hex')}/?fname=`;
|
||||
}
|
||||
async GetPrivateFileUrl(self_id: string, fileUUID: string, md5: string) {
|
||||
const req = trans.DownloadPrivateFile.build(self_id, fileUUID, md5);
|
||||
const resp = await this.context.client.sendOidbPacket(req, true);
|
||||
const res = trans.DownloadPrivateFile.parse(resp);
|
||||
return `http://${res.body?.result?.server}:${res.body?.result?.port}${res.body?.result?.url?.slice(8)}&isthumb=0`;
|
||||
}
|
||||
|
||||
async GetGroupPttUrl(groupUin: number, node: NapProtoEncodeStructType<typeof IndexNode>) {
|
||||
const req = trans.DownloadGroupPtt.build(groupUin, node);
|
||||
|
@@ -144,7 +144,7 @@ export class PacketHighwayContext {
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != '') {
|
||||
this.logger.debug(`[Highway] uploadGroupImageReq get upload ukey: ${ukey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0].index;
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0]!.index;
|
||||
const sha1 = Buffer.from(index.info.fileSha1, 'hex');
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
const extend = new NapProtoMsg(proto.NTV2RichMediaHighwayExt).encode({
|
||||
@@ -181,7 +181,7 @@ export class PacketHighwayContext {
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != '') {
|
||||
this.logger.debug(`[Highway] uploadC2CImageReq get upload ukey: ${ukey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0].index;
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0]!.index;
|
||||
const sha1 = Buffer.from(index.info.fileSha1, 'hex');
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
const extend = new NapProtoMsg(proto.NTV2RichMediaHighwayExt).encode({
|
||||
@@ -219,7 +219,7 @@ export class PacketHighwayContext {
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != '') {
|
||||
this.logger.debug(`[Highway] uploadGroupVideoReq get upload video ukey: ${ukey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0].index;
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0]!.index;
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
const extend = new NapProtoMsg(proto.NTV2RichMediaHighwayExt).encode({
|
||||
fileUuid: index.fileUuid,
|
||||
@@ -244,16 +244,16 @@ export class PacketHighwayContext {
|
||||
this.logger.debug(`[Highway] uploadGroupVideoReq get upload invalid ukey ${ukey}, don't need upload!`);
|
||||
}
|
||||
const subFile = preRespData.upload.subFileInfos[0];
|
||||
if (subFile.uKey && subFile.uKey != '') {
|
||||
this.logger.debug(`[Highway] uploadGroupVideoReq get upload video thumb ukey: ${subFile.uKey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[1].index;
|
||||
if (subFile!.uKey && subFile!.uKey != '') {
|
||||
this.logger.debug(`[Highway] uploadGroupVideoReq get upload video thumb ukey: ${subFile!.uKey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[1]!.index;
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
const sha1 = Buffer.from(index.info.fileSha1, 'hex');
|
||||
const extend = new NapProtoMsg(proto.NTV2RichMediaHighwayExt).encode({
|
||||
fileUuid: index.fileUuid,
|
||||
uKey: subFile.uKey,
|
||||
uKey: subFile!.uKey,
|
||||
network: {
|
||||
ipv4S: oidbIpv4s2HighwayIpv4s(subFile.ipv4S)
|
||||
ipv4S: oidbIpv4s2HighwayIpv4s(subFile!.ipv4S)
|
||||
},
|
||||
msgInfoBody: preRespData.upload.msgInfo.msgInfoBody,
|
||||
blockSize: BlockSize,
|
||||
@@ -269,7 +269,7 @@ export class PacketHighwayContext {
|
||||
extend
|
||||
);
|
||||
} else {
|
||||
this.logger.debug(`[Highway] uploadGroupVideoReq get upload invalid thumb ukey ${subFile.uKey}, don't need upload!`);
|
||||
this.logger.debug(`[Highway] uploadGroupVideoReq get upload invalid thumb ukey ${subFile!.uKey}, don't need upload!`);
|
||||
}
|
||||
video.msgInfo = preRespData.upload.msgInfo;
|
||||
}
|
||||
@@ -284,7 +284,7 @@ export class PacketHighwayContext {
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != '') {
|
||||
this.logger.debug(`[Highway] uploadC2CVideoReq get upload video ukey: ${ukey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0].index;
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0]!.index;
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
const extend = new NapProtoMsg(proto.NTV2RichMediaHighwayExt).encode({
|
||||
fileUuid: index.fileUuid,
|
||||
@@ -309,16 +309,16 @@ export class PacketHighwayContext {
|
||||
this.logger.debug(`[Highway] uploadC2CVideoReq get upload invalid ukey ${ukey}, don't need upload!`);
|
||||
}
|
||||
const subFile = preRespData.upload.subFileInfos[0];
|
||||
if (subFile.uKey && subFile.uKey != '') {
|
||||
this.logger.debug(`[Highway] uploadC2CVideoReq get upload video thumb ukey: ${subFile.uKey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[1].index;
|
||||
if (subFile!.uKey && subFile!.uKey != '') {
|
||||
this.logger.debug(`[Highway] uploadC2CVideoReq get upload video thumb ukey: ${subFile!.uKey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[1]!.index;
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
const sha1 = Buffer.from(index.info.fileSha1, 'hex');
|
||||
const extend = new NapProtoMsg(proto.NTV2RichMediaHighwayExt).encode({
|
||||
fileUuid: index.fileUuid,
|
||||
uKey: subFile.uKey,
|
||||
uKey: subFile!.uKey,
|
||||
network: {
|
||||
ipv4S: oidbIpv4s2HighwayIpv4s(subFile.ipv4S)
|
||||
ipv4S: oidbIpv4s2HighwayIpv4s(subFile!.ipv4S)
|
||||
},
|
||||
msgInfoBody: preRespData.upload.msgInfo.msgInfoBody,
|
||||
blockSize: BlockSize,
|
||||
@@ -334,7 +334,7 @@ export class PacketHighwayContext {
|
||||
extend
|
||||
);
|
||||
} else {
|
||||
this.logger.debug(`[Highway] uploadC2CVideoReq get upload invalid thumb ukey ${subFile.uKey}, don't need upload!`);
|
||||
this.logger.debug(`[Highway] uploadC2CVideoReq get upload invalid thumb ukey ${subFile!.uKey}, don't need upload!`);
|
||||
}
|
||||
video.msgInfo = preRespData.upload.msgInfo;
|
||||
}
|
||||
@@ -347,7 +347,7 @@ export class PacketHighwayContext {
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != '') {
|
||||
this.logger.debug(`[Highway] uploadGroupPttReq get upload ptt ukey: ${ukey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0].index;
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0]!.index;
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
const sha1 = Buffer.from(index.info.fileSha1, 'hex');
|
||||
const extend = new NapProtoMsg(proto.NTV2RichMediaHighwayExt).encode({
|
||||
@@ -383,7 +383,7 @@ export class PacketHighwayContext {
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != '') {
|
||||
this.logger.debug(`[Highway] uploadC2CPttReq get upload ptt ukey: ${ukey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0].index;
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0]!.index;
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
const sha1 = Buffer.from(index.info.fileSha1, 'hex');
|
||||
const extend = new NapProtoMsg(proto.NTV2RichMediaHighwayExt).encode({
|
||||
|
@@ -9,15 +9,14 @@ class SetSpecialTitle extends PacketTransformer<typeof proto.OidbSvcTrpcTcpBase>
|
||||
}
|
||||
|
||||
build(groupCode: number, uid: string, tittle: string): OidbPacket {
|
||||
const oidb_0x8FC_2_body = new NapProtoMsg(proto.OidbSvcTrpcTcp0X8FC_2_Body).encode({
|
||||
targetUid: uid,
|
||||
specialTitle: tittle,
|
||||
expiredTime: -1,
|
||||
uinName: tittle
|
||||
});
|
||||
const oidb_0x8FC_2 = new NapProtoMsg(proto.OidbSvcTrpcTcp0X8FC_2).encode({
|
||||
groupUin: +groupCode,
|
||||
body: oidb_0x8FC_2_body
|
||||
body: {
|
||||
targetUid: uid,
|
||||
specialTitle: tittle,
|
||||
expiredTime: -1,
|
||||
uinName: tittle
|
||||
}
|
||||
});
|
||||
return OidbBase.build(0x8FC, 2, oidb_0x8FC_2, false, false);
|
||||
}
|
||||
|
@@ -4,12 +4,12 @@ import { ProtoField, ScalarType } from '@napneko/nap-proto-core';
|
||||
//设置群头衔 OidbSvcTrpcTcp.0x8fc_2
|
||||
export const OidbSvcTrpcTcp0X8FC_2_Body = {
|
||||
targetUid: ProtoField(1, ScalarType.STRING),
|
||||
specialTitle: ProtoField(5, ScalarType.STRING),
|
||||
expiredTime: ProtoField(6, ScalarType.SINT32),
|
||||
uinName: ProtoField(7, ScalarType.STRING),
|
||||
specialTitle: ProtoField(5, ScalarType.STRING, true),
|
||||
expiredTime: ProtoField(6, ScalarType.INT32),
|
||||
uinName: ProtoField(7, ScalarType.STRING, true),
|
||||
targetName: ProtoField(8, ScalarType.STRING),
|
||||
};
|
||||
export const OidbSvcTrpcTcp0X8FC_2 = {
|
||||
groupUin: ProtoField(1, ScalarType.UINT32),
|
||||
body: ProtoField(3, ScalarType.BYTES),
|
||||
body: ProtoField(3, () => OidbSvcTrpcTcp0X8FC_2_Body),
|
||||
};
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { DownloadBaseEmojiByIdReq, DownloadBaseEmojiByUrlReq, GetBaseEmojiPathReq, PullSysEmojisReq } from '../types';
|
||||
import { GeneralCallResult } from './common';
|
||||
|
||||
export interface NodeIKernelBaseEmojiService {
|
||||
removeKernelBaseEmojiListener(listenerId: number): void;
|
||||
@@ -7,7 +8,26 @@ export interface NodeIKernelBaseEmojiService {
|
||||
|
||||
isBaseEmojiPathExist(args: Array<string>): unknown;
|
||||
|
||||
fetchFullSysEmojis(pullSysEmojisReq: PullSysEmojisReq): unknown;
|
||||
fetchFullSysEmojis(pullSysEmojisReq: PullSysEmojisReq): Promise<GeneralCallResult & {
|
||||
rsp: {
|
||||
otherPanelResult: {
|
||||
SysEmojiGroupList: Array<unknown>,
|
||||
downloadInfo: Array<unknown>
|
||||
},
|
||||
normalPanelResult: {
|
||||
SysEmojiGroupList: Array<unknown>,
|
||||
downloadInfo: Array<unknown>
|
||||
},
|
||||
superPanelResult: {
|
||||
SysEmojiGroupList: Array<unknown>,
|
||||
downloadInfo: Array<unknown>
|
||||
},
|
||||
redHeartPanelResult: {
|
||||
SysEmojiGroupList: Array<unknown>,
|
||||
downloadInfo: Array<unknown>
|
||||
}
|
||||
}
|
||||
}>;
|
||||
|
||||
getBaseEmojiPathByIds(getBaseEmojiPathReqs: Array<GetBaseEmojiPathReq>): unknown;
|
||||
|
||||
|
@@ -165,7 +165,7 @@ export interface NodeIKernelGroupService {
|
||||
|
||||
modifyGroupName(groupCode: string, groupName: string, isNormalMember: boolean): Promise<GeneralCallResult>;
|
||||
|
||||
modifyGroupRemark(groupCode: string, remark: string): void;
|
||||
modifyGroupRemark(groupCode: string, remark: string): Promise<GeneralCallResult>;
|
||||
|
||||
modifyGroupDetailInfo(groupCode: string, arg: unknown): void;
|
||||
|
||||
@@ -253,7 +253,7 @@ export interface NodeIKernelGroupService {
|
||||
|
||||
getGroupShutUpMemberList(groupCode: string): Promise<GeneralCallResult>;
|
||||
|
||||
setMemberShutUp(groupCode: string, memberTimes: { uid: string, timeStamp: number }[]): Promise<void>;
|
||||
setMemberShutUp(groupCode: string, memberTimes: { uid: string, timeStamp: number }[]): Promise<GeneralCallResult>;
|
||||
|
||||
getGroupRecommendContactArkJson(groupCode: string): Promise<GeneralCallResult & { arkJson: string }>;
|
||||
|
||||
|
@@ -60,7 +60,10 @@ export interface QuickLoginResult {
|
||||
}
|
||||
|
||||
export interface NodeIKernelLoginService {
|
||||
getMsfStatus: () => number;
|
||||
|
||||
setLoginMiscData(arg0: string, value: string): unknown;
|
||||
|
||||
getMachineGuid(): string;
|
||||
|
||||
get(): NodeIKernelLoginService;
|
||||
|
@@ -464,11 +464,20 @@ export interface NodeIKernelMsgService {
|
||||
|
||||
setMsgEmojiLikesForRole(...args: unknown[]): unknown;
|
||||
|
||||
clickInlineKeyboardButton(...args: unknown[]): unknown;
|
||||
clickInlineKeyboardButton(params: {
|
||||
guildId?: string,
|
||||
peerId: string,
|
||||
botAppid: string,
|
||||
msgSeq: string,
|
||||
buttonId: string,
|
||||
callback_data: string,
|
||||
dmFlag: number,
|
||||
chatType: number // 1私聊 2群
|
||||
}): Promise<GeneralCallResult & { status: number, promptText: string, promptType: number, promptIcon: number }>;
|
||||
|
||||
setCurOnScreenMsg(...args: unknown[]): unknown;
|
||||
|
||||
setCurOnScreenMsgForMsgEvent(...args: unknown[]): unknown;
|
||||
setCurOnScreenMsgForMsgEvent(peer: Peer, msgRegList: Map<string, Uint8Array>): void;
|
||||
|
||||
getMiscData(key: string): unknown;
|
||||
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { NodeIKernelRobotListener } from '@/core/listeners';
|
||||
import { GeneralCallResult, Peer } from '..';
|
||||
|
||||
export interface NodeIKernelRobotService {
|
||||
fetchGroupRobotStoreDiscovery(arg: unknown): unknown;
|
||||
@@ -31,5 +32,17 @@ export interface NodeIKernelRobotService {
|
||||
|
||||
getRobotUinRange(data: unknown): Promise<{ response: { robotUinRanges: Array<unknown> } }>;
|
||||
|
||||
getRobotFunctions(peer: Peer, params: {
|
||||
uins: Array<string>,
|
||||
num: 0,
|
||||
client_info: { platform: 4, version: '', build_num: 9999 },
|
||||
tinyids: [],
|
||||
page: 0,
|
||||
full_fetch: false,
|
||||
scene: 4,
|
||||
filter: 1,
|
||||
bkn: ''
|
||||
}): Promise<GeneralCallResult & { response: { bot_features: Array<unknown>, next_page: number } }>;
|
||||
|
||||
isNull(): boolean;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { ChatType } from '@/core/types';
|
||||
import { ChatType, Peer } from '@/core/types';
|
||||
import { GeneralCallResult } from './common';
|
||||
|
||||
export interface NodeIKernelSearchService {
|
||||
@@ -54,7 +54,7 @@ export interface NodeIKernelSearchService {
|
||||
|
||||
cancelSearchChatMsgs(...args: unknown[]): unknown;// needs 3 arguments
|
||||
|
||||
searchMsgWithKeywords(...args: unknown[]): unknown;// needs 2 arguments
|
||||
searchMsgWithKeywords(keyWords: string[], param: Peer & { searchFields: number, pageLimit: number }): Promise<GeneralCallResult>;
|
||||
|
||||
searchMoreMsgWithKeywords(...args: unknown[]): unknown;// needs 1 arguments
|
||||
|
||||
|
@@ -7,13 +7,11 @@ import { SelfInfo } from '@/core/types';
|
||||
import { NodeIKernelLoginListener } from '@/core/listeners';
|
||||
import { NodeIKernelLoginService } from '@/core/services';
|
||||
import { NodeIQQNTWrapperSession, WrapperNodeApi } from '@/core/wrapper';
|
||||
import { InitWebUi, WebUiConfig } from '@/webui';
|
||||
import { NapCatOneBot11Adapter } from '@/onebot';
|
||||
|
||||
//Framework ES入口文件
|
||||
export async function getWebUiUrl() {
|
||||
const WebUiConfigData = (await WebUiConfig.GetWebUIConfig());
|
||||
return 'http://127.0.0.1:' + WebUiConfigData.port + '/webui/?token=' + WebUiConfigData.token;
|
||||
return 'http://127.0.0.1:' + 6099 + '/webui/?token=napcat';
|
||||
}
|
||||
|
||||
export async function NCoreInitFramework(
|
||||
@@ -58,8 +56,6 @@ export async function NCoreInitFramework(
|
||||
const loaderObject = new NapCatFramework(wrapper, session, logger, loginService, selfInfo, basicInfoWrapper, pathWrapper);
|
||||
await loaderObject.core.initCore();
|
||||
|
||||
//启动WebUi
|
||||
InitWebUi(logger, pathWrapper).then().catch(e => logger.logError(e));
|
||||
//初始化LLNC的Onebot实现
|
||||
await new NapCatOneBot11Adapter(loaderObject.core, loaderObject.context, pathWrapper).InitOneBot();
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
10
src/onebot/action/extends/BotExit.ts
Normal file
10
src/onebot/action/extends/BotExit.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { OneBotAction } from '../OneBotAction';
|
||||
|
||||
export class BotExit extends OneBotAction<void, void> {
|
||||
override actionName = ActionName.Exit;
|
||||
|
||||
async _handle() {
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
30
src/onebot/action/extends/ClickInlineKeyboardButton.ts
Normal file
30
src/onebot/action/extends/ClickInlineKeyboardButton.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { OneBotAction } from '../OneBotAction';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
|
||||
const SchemaData = Type.Object({
|
||||
group_id: Type.Union([Type.Number(), Type.String()]),
|
||||
bot_appid: Type.String(),
|
||||
button_id: Type.String({ default: '' }),
|
||||
callback_data: Type.String({ default: '' }),
|
||||
msg_seq: Type.String({ default: '10086' }),
|
||||
});
|
||||
|
||||
type Payload = Static<typeof SchemaData>;
|
||||
|
||||
export class ClickInlineKeyboardButton extends OneBotAction<Payload, unknown> {
|
||||
override actionName = ActionName.ClickInlineKeyboardButton;
|
||||
override payloadSchema = SchemaData;
|
||||
|
||||
async _handle(payload: Payload) {
|
||||
return await this.core.apis.MsgApi.clickInlineKeyboardButton({
|
||||
buttonId: payload.button_id,
|
||||
peerId: payload.group_id.toString(),
|
||||
botAppid: payload.bot_appid,
|
||||
msgSeq: payload.msg_seq,
|
||||
callback_data: payload.callback_data,
|
||||
dmFlag: 0,
|
||||
chatType: 2
|
||||
})
|
||||
}
|
||||
}
|
56
src/onebot/action/extends/GetUnidirectionalFriendList.ts
Normal file
56
src/onebot/action/extends/GetUnidirectionalFriendList.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { PacketHexStr } from '@/core/packet/transformer/base';
|
||||
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { ProtoBuf, ProtoBufBase, PBUint32, PBString } from 'napcat.protobuf';
|
||||
|
||||
interface Friend {
|
||||
uin: number;
|
||||
uid: string;
|
||||
nick_name: string;
|
||||
age: number;
|
||||
source: string;
|
||||
}
|
||||
|
||||
interface Block {
|
||||
str_uid: string;
|
||||
bytes_source: string;
|
||||
uint32_sex: number;
|
||||
uint32_age: number;
|
||||
bytes_nick: string;
|
||||
uint64_uin: number;
|
||||
}
|
||||
|
||||
export class GetUnidirectionalFriendList extends OneBotAction<void, Friend[]> {
|
||||
override actionName = ActionName.GetUnidirectionalFriendList;
|
||||
|
||||
async pack_data(data: string): Promise<Uint8Array> {
|
||||
return ProtoBuf(class extends ProtoBufBase {
|
||||
type = PBUint32(2, false, 0);
|
||||
data = PBString(3, false, data);
|
||||
}).encode();
|
||||
}
|
||||
|
||||
async _handle(): Promise<Friend[]> {
|
||||
const self_id = this.core.selfInfo.uin;
|
||||
const req_json = {
|
||||
uint64_uin: self_id,
|
||||
uint64_top: 0,
|
||||
uint32_req_num: 99,
|
||||
bytes_cookies: ""
|
||||
};
|
||||
const packed_data = await this.pack_data(JSON.stringify(req_json));
|
||||
const data = Buffer.from(packed_data).toString('hex');
|
||||
const rsq = { cmd: 'MQUpdateSvc_com_qq_ti.web.OidbSvc.0xe17_0', data: data as PacketHexStr };
|
||||
const rsp_data = await this.core.apis.PacketApi.pkt.operation.sendPacket(rsq, true);
|
||||
const block_json = ProtoBuf(class extends ProtoBufBase { data = PBString(4); }).decode(rsp_data);
|
||||
const block_list: Block[] = JSON.parse(block_json.data).rpt_block_list;
|
||||
|
||||
return block_list.map((block) => ({
|
||||
uin: block.uint64_uin,
|
||||
uid: block.str_uid,
|
||||
nick_name: Buffer.from(block.bytes_nick, 'base64').toString(),
|
||||
age: block.uint32_age,
|
||||
source: Buffer.from(block.bytes_source, 'base64').toString()
|
||||
}));
|
||||
}
|
||||
}
|
22
src/onebot/action/extends/SetGroupRemark.ts
Normal file
22
src/onebot/action/extends/SetGroupRemark.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
|
||||
const SchemaData = Type.Object({
|
||||
group_id: Type.String(),
|
||||
remark: Type.String(),
|
||||
});
|
||||
|
||||
type Payload = Static<typeof SchemaData>;
|
||||
|
||||
export default class SetGroupRemark extends OneBotAction<Payload, null> {
|
||||
override actionName = ActionName.SetGroupRemark;
|
||||
override payloadSchema = SchemaData;
|
||||
async _handle(payload: Payload): Promise<null> {
|
||||
let ret = await this.core.apis.GroupApi.setGroupRemark(payload.group_id, payload.remark);
|
||||
if (ret.result != 0) {
|
||||
throw new Error(`设置群备注失败, ${ret.result}:${ret.errMsg}`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -5,7 +5,7 @@ import { Static, Type } from '@sinclair/typebox';
|
||||
const SchemaData = Type.Object({
|
||||
group_id: Type.Union([Type.Number(), Type.String()]),
|
||||
user_id: Type.Union([Type.Number(), Type.String()]),
|
||||
special_title: Type.String(),
|
||||
special_title: Type.String({ default: '' }),
|
||||
});
|
||||
|
||||
type Payload = Static<typeof SchemaData>;
|
||||
@@ -16,7 +16,7 @@ export class SetSpecialTittle extends GetPacketStatusDepends<Payload, void> {
|
||||
|
||||
async _handle(payload: Payload) {
|
||||
const uid = await this.core.apis.UserApi.getUidByUinV2(payload.user_id.toString());
|
||||
if(!uid) throw new Error('User not found');
|
||||
if (!uid) throw new Error('User not found');
|
||||
await this.core.apis.PacketApi.pkt.operation.SetGroupSpecialTitle(+payload.group_id, uid, payload.special_title);
|
||||
}
|
||||
}
|
||||
|
36
src/onebot/action/file/GetPrivateFileUrl.ts
Normal file
36
src/onebot/action/file/GetPrivateFileUrl.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { FileNapCatOneBotUUID } from '@/common/file-uuid';
|
||||
import { GetPacketStatusDepends } from '@/onebot/action/packet/GetPacketStatus';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
|
||||
const SchemaData = Type.Object({
|
||||
file_id: Type.String(),
|
||||
});
|
||||
|
||||
type Payload = Static<typeof SchemaData>;
|
||||
|
||||
interface GetPrivateFileUrlResponse {
|
||||
url?: string;
|
||||
}
|
||||
|
||||
export class GetPrivateFileUrl extends GetPacketStatusDepends<Payload, GetPrivateFileUrlResponse> {
|
||||
override actionName = ActionName.NapCat_GetPrivateFileUrl;
|
||||
override payloadSchema = SchemaData;
|
||||
|
||||
async _handle(payload: Payload) {
|
||||
const contextMsgFile = FileNapCatOneBotUUID.decode(payload.file_id);
|
||||
|
||||
if (contextMsgFile?.fileUUID && contextMsgFile.msgId) {
|
||||
let msg = await this.core.apis.MsgApi.getMsgsByMsgId(contextMsgFile.peer, [contextMsgFile.msgId]);
|
||||
let self_id = this.core.selfInfo.uid;
|
||||
let file_hash = msg.msgList[0]?.elements.map(ele => ele.fileElement?.file10MMd5)[0];
|
||||
if (file_hash) {
|
||||
return {
|
||||
url: await this.core.apis.PacketApi.pkt.operation.GetPrivateFileUrl(self_id, contextMsgFile.fileUUID, file_hash)
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
throw new Error('real fileUUID not found!');
|
||||
}
|
||||
}
|
@@ -20,6 +20,7 @@ class GetGroupInfo extends OneBotAction<Payload, OB11Group> {
|
||||
const data = await this.core.apis.GroupApi.fetchGroupDetail(payload.group_id.toString());
|
||||
return {
|
||||
...data,
|
||||
group_remark: '',
|
||||
group_id: +payload.group_id,
|
||||
group_name: data.groupName,
|
||||
member_count: data.memberNum,
|
||||
|
@@ -20,11 +20,12 @@ export default class SetGroupAddRequest extends OneBotAction<Payload, null> {
|
||||
const approve = payload.approve?.toString() !== 'false';
|
||||
const reason = payload.reason ?? ' ';
|
||||
const invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(flag);
|
||||
const notify = invite_notify ?? await this.findNotify(flag);
|
||||
const { doubt, notify } = invite_notify ? { doubt: false, notify: invite_notify } : await this.findNotify(flag);
|
||||
if (!notify) {
|
||||
throw new Error('No such request');
|
||||
}
|
||||
await this.core.apis.GroupApi.handleGroupRequest(
|
||||
doubt,
|
||||
notify,
|
||||
approve ? NTGroupRequestOperateTypes.KAGREE : NTGroupRequestOperateTypes.KREFUSE,
|
||||
reason,
|
||||
@@ -36,7 +37,8 @@ export default class SetGroupAddRequest extends OneBotAction<Payload, null> {
|
||||
let notify = (await this.core.apis.GroupApi.getSingleScreenNotifies(false, 100)).find(e => e.seq == flag);
|
||||
if (!notify) {
|
||||
notify = (await this.core.apis.GroupApi.getSingleScreenNotifies(true, 100)).find(e => e.seq == flag);
|
||||
return { doubt: true, notify };
|
||||
}
|
||||
return notify;
|
||||
return { doubt: false, notify };
|
||||
}
|
||||
}
|
@@ -13,12 +13,15 @@ type Payload = Static<typeof SchemaData>;
|
||||
export default class SetGroupBan extends OneBotAction<Payload, null> {
|
||||
override actionName = ActionName.SetGroupBan;
|
||||
override payloadSchema = SchemaData;
|
||||
|
||||
async _handle(payload: Payload): Promise<null> {
|
||||
const uid = await this.core.apis.UserApi.getUidByUinV2(payload.user_id.toString());
|
||||
if (!uid) throw new Error('uid error');
|
||||
await this.core.apis.GroupApi.banMember(payload.group_id.toString(),
|
||||
let member_role = (await this.core.apis.GroupApi.getGroupMemberEx(payload.group_id.toString(), uid, true))?.role;
|
||||
if (member_role === 4) throw new Error('cannot ban owner');
|
||||
// 例如无管理员权限时 result为 120101005 errMsg为 'ERR_NOT_GROUP_ADMIN'
|
||||
let ret = await this.core.apis.GroupApi.banMember(payload.group_id.toString(),
|
||||
[{ uid: uid, timeStamp: +payload.duration }]);
|
||||
if (ret.result !== 0) throw new Error(ret.errMsg);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -104,10 +104,16 @@ import { GetClientkey } from './extends/GetClientkey';
|
||||
import { SendPacket } from './extends/SendPacket';
|
||||
import { SendPoke } from '@/onebot/action/packet/SendPoke';
|
||||
import { SetDiyOnlineStatus } from './extends/SetDiyOnlineStatus';
|
||||
import { BotExit } from './extends/BotExit';
|
||||
import { ClickInlineKeyboardButton } from './extends/ClickInlineKeyboardButton';
|
||||
import { GetPrivateFileUrl } from './file/GetPrivateFileUrl';
|
||||
import { GetUnidirectionalFriendList } from './extends/GetUnidirectionalFriendList';
|
||||
import SetGroupRemark from './extends/SetGroupRemark';
|
||||
|
||||
export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
||||
|
||||
const actionHandlers = [
|
||||
new SetGroupRemark(obContext, core),
|
||||
new GetGroupInfoEx(obContext, core),
|
||||
new FetchEmojiLike(obContext, core),
|
||||
new GetFile(obContext, core),
|
||||
@@ -221,6 +227,10 @@ export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCo
|
||||
new SendPacket(obContext, core),
|
||||
new SendPoke(obContext, core),
|
||||
new GetGroupSystemMsg(obContext, core),
|
||||
new BotExit(obContext, core),
|
||||
new ClickInlineKeyboardButton(obContext, core),
|
||||
new GetPrivateFileUrl(obContext, core),
|
||||
new GetUnidirectionalFriendList(obContext, core),
|
||||
];
|
||||
|
||||
type HandlerUnion = typeof actionHandlers[number];
|
||||
|
@@ -10,6 +10,10 @@ export interface InvalidCheckResult {
|
||||
}
|
||||
|
||||
export const ActionName = {
|
||||
SetGroupRemark: 'set_group_remark',
|
||||
NapCat_GetPrivateFileUrl: 'get_private_file_url',
|
||||
ClickInlineKeyboardButton: 'click_inline_keyboard_button',
|
||||
GetUnidirectionalFriendList: 'get_unidirectional_friend_list',
|
||||
// onebot 11
|
||||
SendPrivateMsg: 'send_private_msg',
|
||||
SendGroupMsg: 'send_group_msg',
|
||||
@@ -49,7 +53,7 @@ export const ActionName = {
|
||||
GetVersionInfo: 'get_version_info',
|
||||
// Reboot : 'set_restart',
|
||||
// CleanCache : 'clean_cache',
|
||||
|
||||
Exit: 'bot_exit',
|
||||
// go-cqhttp
|
||||
SetQQProfile: 'set_qq_profile',
|
||||
// QidianGetAccountInfo : 'qidian_get_account_info',
|
||||
@@ -141,6 +145,6 @@ export const ActionName = {
|
||||
SendGroupAiRecord: 'send_group_ai_record',
|
||||
|
||||
GetClientkey: 'get_clientkey',
|
||||
|
||||
|
||||
SendPoke: 'send_poke',
|
||||
} as const;
|
||||
|
@@ -49,6 +49,7 @@ export class OneBotGroupApi {
|
||||
duration = -1;
|
||||
}
|
||||
}
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCachePartial(GroupCode, memberUid);
|
||||
const adminUin = (await this.core.apis.GroupApi.getGroupMember(GroupCode, adminUid))?.uin;
|
||||
if (memberUin && adminUin) {
|
||||
return new OB11GroupBanEvent(
|
||||
@@ -113,12 +114,16 @@ export class OneBotGroupApi {
|
||||
async parseCardChangedEvent(msg: RawMessage) {
|
||||
if (msg.senderUin && msg.senderUin !== '0') {
|
||||
const member = await this.core.apis.GroupApi.getGroupMember(msg.peerUid, msg.senderUin);
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCachePartial(msg.peerUid, msg.senderUid);
|
||||
if (member && member.cardName !== msg.sendMemberName) {
|
||||
const newCardName = msg.sendMemberName ?? '';
|
||||
const event = new OB11GroupCardEvent(this.core, parseInt(msg.peerUid), parseInt(msg.senderUin), newCardName, member.cardName);
|
||||
member.cardName = newCardName;
|
||||
return event;
|
||||
}
|
||||
if (member && member.nick !== msg.sendNickName) {
|
||||
await this.core.apis.GroupApi.refreshGroupMemberCachePartial(msg.peerUid, msg.senderUid);
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
@@ -132,7 +132,6 @@ export class OneBotMsgApi {
|
||||
file: element.fileName,
|
||||
sub_type: element.picSubType,
|
||||
url: await this.core.apis.FileApi.getImageUrl(element),
|
||||
path: element.filePath,
|
||||
file_size: element.fileSize,
|
||||
},
|
||||
};
|
||||
@@ -148,13 +147,13 @@ export class OneBotMsgApi {
|
||||
peerUid: msg.peerUid,
|
||||
guildId: '',
|
||||
};
|
||||
const file = FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, element.fileName);
|
||||
FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, element.fileUuid);
|
||||
FileNapCatOneBotUUID.encode(peer, msg.msgId, elementWrapper.elementId, element.fileUuid, element.fileName);
|
||||
return {
|
||||
type: OB11MessageDataType.file,
|
||||
data: {
|
||||
file: file,
|
||||
path: element.filePath,
|
||||
file_id: file,
|
||||
file: element.fileName,
|
||||
file_id: element.fileUuid,
|
||||
file_size: element.fileSize,
|
||||
},
|
||||
};
|
||||
@@ -216,7 +215,6 @@ export class OneBotMsgApi {
|
||||
data: {
|
||||
summary: _.faceName, // 商城表情名称
|
||||
file: filename,
|
||||
path: url,
|
||||
url: url,
|
||||
key: _.key,
|
||||
emoji_id: _.emojiId,
|
||||
@@ -339,7 +337,6 @@ export class OneBotMsgApi {
|
||||
type: OB11MessageDataType.video,
|
||||
data: {
|
||||
file: fileCode,
|
||||
path: videoDownUrl,
|
||||
url: videoDownUrl,
|
||||
file_size: element.fileSize,
|
||||
},
|
||||
@@ -357,8 +354,8 @@ export class OneBotMsgApi {
|
||||
type: OB11MessageDataType.voice,
|
||||
data: {
|
||||
file: fileCode,
|
||||
path: element.filePath,
|
||||
file_size: element.fileSize,
|
||||
path: element.filePath,
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -658,6 +655,19 @@ export class OneBotMsgApi {
|
||||
[OB11MessageDataType.node]: async () => undefined,
|
||||
|
||||
[OB11MessageDataType.forward]: async ({ data }, context) => {
|
||||
// let id = data.id.toString();
|
||||
// let peer: Peer | undefined = context.peer;
|
||||
// if (isNumeric(id)) {
|
||||
// let msgid = '';
|
||||
// if (BigInt(data.id) > 2147483647n) {
|
||||
// peer = MessageUnique.getPeerByMsgId(id)?.Peer;
|
||||
// msgid = id;
|
||||
// } else {
|
||||
// let data = MessageUnique.getMsgIdAndPeerByShortId(parseInt(id));
|
||||
// msgid = data?.MsgId ?? '';
|
||||
// peer = data?.Peer;
|
||||
// }
|
||||
// }
|
||||
const jsonData = ForwardMsgBuilder.fromResId(data.id);
|
||||
return this.ob11ToRawConverters.json({
|
||||
data: { data: JSON.stringify(jsonData) },
|
||||
@@ -799,6 +809,7 @@ export class OneBotMsgApi {
|
||||
message_id: msg.id!,
|
||||
message_seq: msg.id!,
|
||||
real_id: msg.id!,
|
||||
real_seq: msg.msgSeq,
|
||||
message_type: msg.chatType == ChatType.KCHATTYPEGROUP ? 'group' : 'private',
|
||||
sender: {
|
||||
user_id: +(msg.senderUin ?? 0),
|
||||
@@ -996,24 +1007,17 @@ export class OneBotMsgApi {
|
||||
this.core.context.logger.logError('文件消息缺少参数', inputdata);
|
||||
throw new Error('文件消息缺少参数');
|
||||
}
|
||||
|
||||
const downloadFile = async (uri: string) => {
|
||||
const { path, fileName, errMsg, success } = await uriToLocalFile(this.core.NapCatTempPath, uri);
|
||||
if (!success) {
|
||||
this.core.context.logger.logError('文件下载失败', errMsg);
|
||||
throw new Error('文件下载失败: ' + errMsg);
|
||||
}
|
||||
return { path, fileName };
|
||||
};
|
||||
realUri = await this.handleObfuckName(realUri) ?? realUri;
|
||||
try {
|
||||
const { path, fileName } = await downloadFile(realUri);
|
||||
deleteAfterSentFiles.push(path);
|
||||
return { path, fileName: inputdata.name ?? fileName };
|
||||
} catch {
|
||||
realUri = await this.handleObfuckName(realUri);
|
||||
const { path, fileName } = await downloadFile(realUri);
|
||||
const { path, fileName, errMsg, success } = await uriToLocalFile(this.core.NapCatTempPath, realUri);
|
||||
if (!success) {
|
||||
this.core.context.logger.logError('文件处理失败', errMsg);
|
||||
throw new Error('文件处理失败: ' + errMsg);
|
||||
}
|
||||
deleteAfterSentFiles.push(path);
|
||||
return { path, fileName: inputdata.name ?? fileName };
|
||||
} catch (e: unknown) {
|
||||
throw new Error((e as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1038,7 +1042,7 @@ export class OneBotMsgApi {
|
||||
}
|
||||
return url !== '' ? url : await this.core.apis.FileApi.downloadMedia(msgId, peer.chatType, peer.peerUid, elementId, '', '');
|
||||
}
|
||||
throw new Error('文件名解析失败');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
groupChangDecreseType2String(type: number): GroupDecreaseSubType {
|
||||
|
@@ -84,17 +84,19 @@ export class OneBotQuickActionApi {
|
||||
let notify = (await this.core.apis.GroupApi.getSingleScreenNotifies(false, 100)).find(e => e.seq == flag);
|
||||
if (!notify) {
|
||||
notify = (await this.core.apis.GroupApi.getSingleScreenNotifies(true, 100)).find(e => e.seq == flag);
|
||||
return { doubt: true, notify };
|
||||
}
|
||||
return notify;
|
||||
return { doubt: false, notify };
|
||||
}
|
||||
|
||||
async handleGroupRequest(request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
|
||||
|
||||
const invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(request.flag);
|
||||
const notify = invite_notify ?? await this.findNotify(request.flag);
|
||||
const { doubt, notify } = invite_notify ? { doubt: false, notify: invite_notify } : await this.findNotify(request.flag);
|
||||
|
||||
if (!isNull(quickAction.approve) && notify) {
|
||||
this.core.apis.GroupApi.handleGroupRequest(
|
||||
doubt,
|
||||
notify,
|
||||
quickAction.approve ? NTGroupRequestOperateTypes.KAGREE : NTGroupRequestOperateTypes.KREFUSE,
|
||||
quickAction.reason,
|
||||
|
@@ -3,7 +3,7 @@ import { NapCatCore } from '@/core';
|
||||
|
||||
export class OB11GroupAdminNoticeEvent extends OB11GroupNoticeEvent {
|
||||
notice_type = 'group_admin';
|
||||
sub_type: 'set' | 'unset';
|
||||
sub_type: 'set' | 'unset';
|
||||
|
||||
constructor(core: NapCatCore, group_id: number, user_id: number, sub_type: 'set' | 'unset') {
|
||||
super(core, group_id, user_id);
|
||||
|
@@ -73,6 +73,7 @@ export class OB11Construct {
|
||||
|
||||
static group(group: Group): OB11Group {
|
||||
return {
|
||||
group_remark: group.remarkName,
|
||||
group_id: +group.groupCode,
|
||||
group_name: group.groupName,
|
||||
member_count: group.memberCount,
|
||||
|
@@ -20,8 +20,7 @@ import {
|
||||
OB11WebSocketClientAdapter,
|
||||
OB11NetworkManager,
|
||||
OB11NetworkReloadType,
|
||||
OB11HttpServerAdapter,
|
||||
OB11WebSocketServerAdapter,
|
||||
OB11HttpServerAdapter
|
||||
} from '@/onebot/network';
|
||||
import { NapCatPathWrapper } from '@/common/path';
|
||||
import {
|
||||
@@ -32,7 +31,6 @@ import {
|
||||
OneBotUserApi,
|
||||
} from '@/onebot/api';
|
||||
import { ActionMap, createActionMap } from '@/onebot/action';
|
||||
import { WebUiDataRuntime } from '@/webui/src/helper/Data';
|
||||
import { OB11InputStatusEvent } from '@/onebot/event/notice/OB11InputStatusEvent';
|
||||
import { MessageUnique } from '@/common/message-unique';
|
||||
import { proxiedListenerOf } from '@/common/proxy-handler';
|
||||
@@ -139,15 +137,6 @@ export class NapCatOneBot11Adapter {
|
||||
}
|
||||
for (const key of ob11Config.network.websocketServers) {
|
||||
if (key.enable) {
|
||||
this.networkManager.registerAdapter(
|
||||
new OB11WebSocketServerAdapter(
|
||||
key.name,
|
||||
key,
|
||||
this.core,
|
||||
this,
|
||||
this.actions
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const key of ob11Config.network.websocketClients) {
|
||||
@@ -168,64 +157,9 @@ export class NapCatOneBot11Adapter {
|
||||
this.initMsgListener();
|
||||
this.initBuddyListener();
|
||||
this.initGroupListener();
|
||||
|
||||
WebUiDataRuntime.setQQVersion(this.core.context.basicInfoWrapper.getFullQQVesion());
|
||||
WebUiDataRuntime.setQQLoginInfo(selfInfo);
|
||||
WebUiDataRuntime.setQQLoginStatus(true);
|
||||
WebUiDataRuntime.setOnOB11ConfigChanged(async (newConfig) => {
|
||||
const prev = this.configLoader.configData;
|
||||
this.configLoader.save(newConfig);
|
||||
//this.context.logger.log(`OneBot11 配置更改:${JSON.stringify(prev)} -> ${JSON.stringify(newConfig)}`);
|
||||
await this.reloadNetwork(prev, newConfig);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private async reloadNetwork(prev: OneBotConfig, now: OneBotConfig): Promise<void> {
|
||||
const prevLog = await this.creatOneBotLog(prev);
|
||||
const newLog = await this.creatOneBotLog(now);
|
||||
this.context.logger.log(`[Notice] [OneBot11] 配置变更前:\n${prevLog}`);
|
||||
this.context.logger.log(`[Notice] [OneBot11] 配置变更后:\n${newLog}`);
|
||||
|
||||
await this.handleConfigChange(prev.network.httpServers, now.network.httpServers, OB11HttpServerAdapter);
|
||||
await this.handleConfigChange(prev.network.httpClients, now.network.httpClients, OB11HttpClientAdapter);
|
||||
await this.handleConfigChange(prev.network.httpSseServers, now.network.httpSseServers, OB11HttpSSEServerAdapter);
|
||||
await this.handleConfigChange(prev.network.websocketServers, now.network.websocketServers, OB11WebSocketServerAdapter);
|
||||
await this.handleConfigChange(prev.network.websocketClients, now.network.websocketClients, OB11WebSocketClientAdapter);
|
||||
}
|
||||
|
||||
private async handleConfigChange<CT extends NetworkAdapterConfig>(
|
||||
prevConfig: NetworkAdapterConfig[],
|
||||
nowConfig: NetworkAdapterConfig[],
|
||||
adapterClass: new (
|
||||
...args: ConstructorParameters<typeof IOB11NetworkAdapter<CT>>
|
||||
) => IOB11NetworkAdapter<CT>
|
||||
): Promise<void> {
|
||||
// 比较旧的在新的找不到的回收
|
||||
for (const adapterConfig of prevConfig) {
|
||||
const existingAdapter = nowConfig.find((e) => e.name === adapterConfig.name);
|
||||
if (!existingAdapter) {
|
||||
const existingAdapter = this.networkManager.findSomeAdapter(adapterConfig.name);
|
||||
if (existingAdapter) {
|
||||
await this.networkManager.closeSomeAdaterWhenOpen([existingAdapter]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 通知新配置重载 删除关闭的 加入新开的
|
||||
for (const adapterConfig of nowConfig) {
|
||||
const existingAdapter = this.networkManager.findSomeAdapter(adapterConfig.name);
|
||||
if (existingAdapter) {
|
||||
const networkChange = await existingAdapter.reload(adapterConfig);
|
||||
if (networkChange === OB11NetworkReloadType.NetWorkClose) {
|
||||
await this.networkManager.closeSomeAdaterWhenOpen([existingAdapter]);
|
||||
}
|
||||
} else if (adapterConfig.enable) {
|
||||
const newAdapter = new adapterClass(adapterConfig.name, adapterConfig as CT, this.core, this, this.actions);
|
||||
await this.networkManager.registerAdapterAndOpen(newAdapter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private initMsgListener() {
|
||||
const msgListener = new NodeIKernelMsgListener();
|
||||
msgListener.onRecvSysMsg = (msg) => {
|
||||
|
@@ -1,33 +1,59 @@
|
||||
import { OB11EmitEventContent } from './index';
|
||||
import { Request, Response } from 'express';
|
||||
import { OB11HttpServerAdapter } from './http-server';
|
||||
import { Context } from 'hono';
|
||||
import { SSEStreamingApi, streamSSE } from 'hono/streaming';
|
||||
import { Mutex } from 'async-mutex';
|
||||
import { LifeCycleSubType, OB11LifeCycleEvent } from '../event/meta/OB11LifeCycleEvent';
|
||||
|
||||
export class OB11HttpSSEServerAdapter extends OB11HttpServerAdapter {
|
||||
private sseClients: Response[] = [];
|
||||
private sseClients: { context: Context; stream: SSEStreamingApi, mutex: Mutex }[] = [];
|
||||
|
||||
override async handleRequest(req: Request, res: Response) {
|
||||
if (req.path === '/_events') {
|
||||
this.createSseSupport(req, res);
|
||||
override async actionHandler(c: Context): Promise<any> {
|
||||
if (c.req.path === '/_events') {
|
||||
return await this.createSseSupport(c);
|
||||
} else {
|
||||
super.httpApiRequest(req, res);
|
||||
return super.actionHandler(c);
|
||||
}
|
||||
}
|
||||
|
||||
private async createSseSupport(req: Request, res: Response) {
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
res.flushHeaders();
|
||||
private async createSseSupport(c: Context) {
|
||||
return streamSSE(c, async (stream) => {
|
||||
const client = { context: c, stream, mutex: new Mutex() };
|
||||
this.sseClients.push(client);
|
||||
client.mutex.acquire();
|
||||
|
||||
this.sseClients.push(res);
|
||||
req.on('close', () => {
|
||||
this.sseClients = this.sseClients.filter((client) => client !== res);
|
||||
stream.onAbort(() => {
|
||||
this.removeClient(stream);
|
||||
client.mutex.release();
|
||||
});
|
||||
|
||||
await stream.writeSSE({ data: JSON.stringify(new OB11LifeCycleEvent(this.core, LifeCycleSubType.CONNECT)) });
|
||||
await client.mutex.waitForUnlock();
|
||||
});
|
||||
}
|
||||
|
||||
private removeClient(stream: SSEStreamingApi) {
|
||||
const index = this.sseClients.findIndex(client => client.stream === stream);
|
||||
if (index !== -1) {
|
||||
this.sseClients.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
override onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||
this.sseClients.forEach((res) => {
|
||||
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
||||
const eventData = JSON.stringify(event);
|
||||
|
||||
Promise.all(
|
||||
this.sseClients.map(async ({ stream, mutex }) => {
|
||||
try {
|
||||
await stream.writeSSE({ data: eventData });
|
||||
} catch (error) {
|
||||
mutex.release();
|
||||
this.removeClient(stream);
|
||||
}
|
||||
})
|
||||
).then().catch((error) => {
|
||||
this.core.context.logger.logError('Error sending SSE event:', error);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,19 +1,17 @@
|
||||
import { OB11EmitEventContent, OB11NetworkReloadType } from './index';
|
||||
import express, { Express, NextFunction, Request, Response } from 'express';
|
||||
import http from 'http';
|
||||
import { Context, Hono, Next } from 'hono';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { OB11Response } from '@/onebot/action/OneBotAction';
|
||||
import { ActionMap } from '@/onebot/action';
|
||||
import cors from 'cors';
|
||||
import { cors } from 'hono/cors';
|
||||
import { HttpServerConfig } from '@/onebot/config/config';
|
||||
import { NapCatOneBot11Adapter } from '@/onebot';
|
||||
import { IOB11NetworkAdapter } from '@/onebot/network/adapter';
|
||||
import json5 from 'json5';
|
||||
import { isFinished } from 'on-finished';
|
||||
import typeis from 'type-is';
|
||||
import { serve } from '@hono/node-server';
|
||||
|
||||
export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig> {
|
||||
private app: Express | undefined;
|
||||
private server: http.Server | undefined;
|
||||
private app: Hono | undefined;
|
||||
private server: ReturnType<typeof serve> | undefined;
|
||||
|
||||
constructor(name: string, config: HttpServerConfig, core: NapCatCore, obContext: NapCatOneBot11Adapter, actions: ActionMap) {
|
||||
super(name, config, core, obContext, actions);
|
||||
@@ -27,17 +25,14 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
||||
open() {
|
||||
try {
|
||||
if (this.isEnable) {
|
||||
this.core.context.logger.logError('Cannot open a closed HTTP server');
|
||||
this.core.context.logger.logError('[OneBot] [HTTP Server Adapter] 无法打开已经启动的HTTP服务器');
|
||||
return;
|
||||
}
|
||||
if (!this.isEnable) {
|
||||
this.initializeServer();
|
||||
this.isEnable = true;
|
||||
}
|
||||
this.initializeServer();
|
||||
this.isEnable = true;
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError(`[OneBot] [HTTP Server Adapter] Boot Error: ${e}`);
|
||||
this.core.context.logger.logError(`[OneBot] [HTTP Server Adapter] 启动错误: ${e}`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async close() {
|
||||
@@ -46,101 +41,159 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
||||
this.app = undefined;
|
||||
}
|
||||
|
||||
|
||||
private initializeServer() {
|
||||
this.app = express();
|
||||
this.server = http.createServer(this.app);
|
||||
this.app = new Hono();
|
||||
|
||||
// 注册全局中间件
|
||||
this.app.use(cors());
|
||||
this.app.use(express.urlencoded({ extended: true, limit: '5000mb' }));
|
||||
this.app.use(this.authMiddleware.bind(this));
|
||||
this.app.use(this.statusCheckMiddleware.bind(this));
|
||||
this.app.use(this.payloadParserMiddleware.bind(this));
|
||||
|
||||
this.app.use((req, res, next) => {
|
||||
if (isFinished(req)) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
if (!typeis.hasBody(req)) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
// 兼容处理没有带content-type的请求
|
||||
req.headers['content-type'] = 'application/json';
|
||||
let rawData = '';
|
||||
req.on('data', (chunk) => {
|
||||
rawData += chunk;
|
||||
});
|
||||
req.on('end', () => {
|
||||
try {
|
||||
req.body = { ...json5.parse(rawData || '{}'), ...req.body };
|
||||
next();
|
||||
} catch {
|
||||
return res.status(400).send('Invalid JSON');
|
||||
}
|
||||
return;
|
||||
});
|
||||
req.on('error', () => {
|
||||
return res.status(400).send('Invalid JSON');
|
||||
});
|
||||
});
|
||||
//@ts-expect-error authorize
|
||||
this.app.use((req, res, next) => this.authorize(this.config.token, req, res, next));
|
||||
this.app.use(async (req, res) => {
|
||||
await this.handleRequest(req, res);
|
||||
});
|
||||
this.server.listen(this.config.port, () => {
|
||||
this.core.context.logger.log(`[OneBot] [HTTP Server Adapter] Start On Port ${this.config.port}`);
|
||||
// 注册路由
|
||||
this.app.get('/', this.rootHandler.bind(this));
|
||||
this.app.all('/*', this.actionHandler.bind(this));
|
||||
|
||||
// 启动服务器
|
||||
this.server = serve({
|
||||
fetch: this.app.fetch.bind(this.app),
|
||||
port: this.config.port,
|
||||
});
|
||||
|
||||
this.core.context.logger.log(`[OneBot] [HTTP Server Adapter] 服务器已启动于端口 ${this.config.port}`);
|
||||
}
|
||||
|
||||
private authorize(token: string | undefined, req: Request, res: Response, next: NextFunction) {
|
||||
if (!token || token.length == 0) return next();//客户端未设置密钥
|
||||
const HeaderClientToken = req.headers.authorization?.split('Bearer ').pop() || '';
|
||||
const QueryClientToken = req.query['access_token'];
|
||||
const ClientToken = typeof (QueryClientToken) === 'string' && QueryClientToken !== '' ? QueryClientToken : HeaderClientToken;
|
||||
if (ClientToken === token) {
|
||||
/**
|
||||
* 身份验证中间件
|
||||
*/
|
||||
private async authMiddleware(c: Context, next: Next) {
|
||||
const token = this.config.token;
|
||||
if (!token || token.length === 0) {
|
||||
return next(); // 未配置token,跳过验证
|
||||
}
|
||||
|
||||
// 从请求头或查询参数获取token
|
||||
const headerToken = c.req.header('authorization')?.split('Bearer ').pop() || '';
|
||||
const queryToken = c.req.query('access_token');
|
||||
const clientToken = typeof queryToken === 'string' && queryToken !== ''
|
||||
? queryToken
|
||||
: headerToken;
|
||||
|
||||
if (clientToken === token) {
|
||||
return next();
|
||||
} else {
|
||||
return res.status(403).send(JSON.stringify({ message: 'token verify failed!' }));
|
||||
}
|
||||
|
||||
// 验证失败
|
||||
c.status(403);
|
||||
return c.json({ message: 'token验证失败' });
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务器状态检查中间件
|
||||
*/
|
||||
private async statusCheckMiddleware(c: Context, next: Next) {
|
||||
if (!this.isEnable) {
|
||||
this.core.context.logger.log('[OneBot] [HTTP Server Adapter] 服务器已关闭');
|
||||
return c.json(OB11Response.error('服务器已关闭', 200));
|
||||
}
|
||||
return next();
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求参数解析中间件
|
||||
* 按优先级解析请求参数:JSON > 表单 > 查询参数
|
||||
*/
|
||||
private async payloadParserMiddleware(c: Context, next: Next) {
|
||||
try {
|
||||
// 初始化payload对象
|
||||
let payload: Record<string, any> = {};
|
||||
|
||||
// 1. 提取查询参数
|
||||
const queryParams = c.req.query();
|
||||
if (Object.keys(queryParams).length > 0) {
|
||||
payload = { ...queryParams };
|
||||
}
|
||||
|
||||
// 2. 解析请求体
|
||||
const contentType = c.req.header('content-type') || '';
|
||||
let bodyData = {};
|
||||
|
||||
try {
|
||||
// 优先尝试以JSON格式解析
|
||||
if (contentType.includes('application/json') || contentType === '' || contentType.includes('text/plain')) {
|
||||
try {
|
||||
bodyData = await c.req.json();
|
||||
} catch {
|
||||
// JSON解析失败时,尝试其他方式
|
||||
}
|
||||
}
|
||||
|
||||
// 如果JSON解析失败或不是JSON格式,尝试其他格式
|
||||
if (Object.keys(bodyData).length === 0) {
|
||||
if (contentType.includes('application/x-www-form-urlencoded') ||
|
||||
contentType.includes('multipart/form-data')) {
|
||||
bodyData = await c.req.parseBody();
|
||||
} else if (contentType) {
|
||||
// 尝试通用解析
|
||||
bodyData = await c.req.parseBody();
|
||||
}
|
||||
}
|
||||
} catch (parseError) {
|
||||
// 所有解析方式都失败,记录错误但继续处理
|
||||
this.core.context.logger.log(`[OneBot] [HTTP Server Adapter] 请求体解析失败: ${parseError}`);
|
||||
}
|
||||
|
||||
// 3. 合并参数
|
||||
payload = { ...payload, ...bodyData };
|
||||
|
||||
// 4. 将解析结果保存到上下文
|
||||
c.set('payload', payload);
|
||||
return next();
|
||||
} catch (error) {
|
||||
this.core.context.logger.logError(`[OneBot] [HTTP Server Adapter] 请求处理错误: ${error}`);
|
||||
return c.json(OB11Response.error(`参数解析失败: ${(error as Error)?.message || '未知错误'}`, 200));
|
||||
}
|
||||
}
|
||||
|
||||
async httpApiRequest(req: Request, res: Response) {
|
||||
let payload = req.body;
|
||||
if (req.method == 'get') {
|
||||
payload = req.query;
|
||||
} else if (req.query) {
|
||||
payload = { ...req.body, ...req.query };
|
||||
}
|
||||
if (req.path === '' || req.path === '/') {
|
||||
const hello = OB11Response.ok({});
|
||||
hello.message = 'NapCat4 Is Running';
|
||||
return res.json(hello);
|
||||
}
|
||||
const actionName = req.path.split('/')[1];
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const action = this.actions.get(actionName as any);
|
||||
if (action) {
|
||||
/**
|
||||
* 根路径处理器
|
||||
*/
|
||||
private rootHandler(c: Context) {
|
||||
const response = OB11Response.ok({});
|
||||
response.message = 'NapCat4 Is Running';
|
||||
return c.json(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* API动作处理器
|
||||
*/
|
||||
async actionHandler(c: Context) {
|
||||
try {
|
||||
const payload = c.get('payload') as Record<string, any>;
|
||||
const actionName = c.req.path.split('/')[1];
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const action = this.actions.get(actionName as any);
|
||||
|
||||
if (!action) {
|
||||
return c.json(OB11Response.error(`不支持的API: ${actionName}`, 200));
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await action.handle(payload, this.name, this.config);
|
||||
return res.json(result);
|
||||
return c.json(result);
|
||||
} catch (error: unknown) {
|
||||
return res.json(OB11Response.error((error as Error)?.stack?.toString() || (error as Error)?.message || 'Error Handle', 200));
|
||||
const errorMessage = (error as Error)?.stack || (error as Error)?.message || 'Error Handle';
|
||||
this.core.context.logger.logError(`[OneBot] [HTTP Server Adapter] API处理错误: ${errorMessage}`);
|
||||
return c.json(OB11Response.error(errorMessage, 200));
|
||||
}
|
||||
} else {
|
||||
return res.json(OB11Response.error('不支持的Api ' + actionName, 200));
|
||||
} catch (error: unknown) {
|
||||
const errorMessage = (error as Error)?.message || '未知错误';
|
||||
this.core.context.logger.logError(`[OneBot] [HTTP Server Adapter] 请求处理失败: ${errorMessage}`);
|
||||
return c.json(OB11Response.error(`请求处理失败: ${errorMessage}`, 200));
|
||||
}
|
||||
}
|
||||
|
||||
async handleRequest(req: Request, res: Response) {
|
||||
if (!this.isEnable) {
|
||||
this.core.context.logger.log('[OneBot] [HTTP Server Adapter] Server is closed');
|
||||
res.json(OB11Response.error('Server is closed', 200));
|
||||
return;
|
||||
}
|
||||
this.httpApiRequest(req, res);
|
||||
return;
|
||||
}
|
||||
|
||||
async reload(newConfig: HttpServerConfig) {
|
||||
const wasEnabled = this.isEnable;
|
||||
const oldPort = this.config.port;
|
||||
@@ -164,4 +217,4 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
||||
|
||||
return OB11NetworkReloadType.Normal;
|
||||
}
|
||||
}
|
||||
}
|
@@ -103,5 +103,4 @@ export class OB11NetworkManager {
|
||||
|
||||
export * from './http-client';
|
||||
export * from './websocket-client';
|
||||
export * from './http-server';
|
||||
export * from './websocket-server';
|
||||
export * from './http-server';
|
@@ -1,5 +1,4 @@
|
||||
import { OB11EmitEventContent, OB11NetworkReloadType } from '@/onebot/network/index';
|
||||
import { RawData, WebSocket } from 'ws';
|
||||
import { OB11HeartbeatEvent } from '@/onebot/event/meta/OB11HeartbeatEvent';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
@@ -10,10 +9,12 @@ import { WebsocketClientConfig } from '@/onebot/config/config';
|
||||
import { NapCatOneBot11Adapter } from '@/onebot';
|
||||
import { IOB11NetworkAdapter } from '@/onebot/network/adapter';
|
||||
import json5 from 'json5';
|
||||
import { hc } from 'hono/client';
|
||||
|
||||
export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketClientConfig> {
|
||||
private connection: WebSocket | null = null;
|
||||
private heartbeatRef: NodeJS.Timeout | null = null;
|
||||
private client = hc(this.config.url);
|
||||
|
||||
constructor(name: string, config: WebsocketClientConfig, core: NapCatCore, obContext: NapCatOneBot11Adapter, actions: ActionMap) {
|
||||
super(name, config, core, obContext, actions);
|
||||
@@ -65,37 +66,23 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
||||
private async tryConnect() {
|
||||
if (!this.connection && this.isEnable) {
|
||||
let isClosedByError = false;
|
||||
let wsClientX = this.client['ws']?.$ws(0);
|
||||
if (!wsClientX) throw new Error('WebSocket Client Error');
|
||||
this.connection = wsClientX;
|
||||
|
||||
this.connection = new WebSocket(this.config.url, {
|
||||
maxPayload: 1024 * 1024 * 1024,
|
||||
handshakeTimeout: 2000,
|
||||
perMessageDeflate: false,
|
||||
headers: {
|
||||
'X-Self-ID': this.core.selfInfo.uin,
|
||||
'Authorization': `Bearer ${this.config.token}`,
|
||||
'x-client-role': 'Universal', // 为koishi adpter适配
|
||||
'User-Agent': 'OneBot/11',
|
||||
},
|
||||
|
||||
});
|
||||
this.connection.on('ping', () => {
|
||||
this.connection?.pong();
|
||||
});
|
||||
this.connection.on('pong', () => {
|
||||
//this.logger.logDebug('[OneBot] [WebSocket Client] 收到pong');
|
||||
});
|
||||
this.connection.on('open', () => {
|
||||
this.connection.addEventListener('open', () => {
|
||||
try {
|
||||
this.connectEvent(this.core);
|
||||
} catch (e) {
|
||||
this.logger.logError('[OneBot] [WebSocket Client] 发送连接生命周期失败', e);
|
||||
}
|
||||
});
|
||||
|
||||
this.connection.addEventListener('message', (event) => {
|
||||
this.handleMessage(event.data);
|
||||
});
|
||||
this.connection.on('message', (data) => {
|
||||
this.handleMessage(data);
|
||||
});
|
||||
this.connection.once('close', () => {
|
||||
|
||||
this.connection.addEventListener('close', () => {
|
||||
if (!isClosedByError) {
|
||||
this.logger.logError(`[OneBot] [WebSocket Client] 反向WebSocket (${this.config.url}) 连接意外关闭`);
|
||||
this.logger.logError(`[OneBot] [WebSocket Client] 在 ${Math.floor(this.config.reconnectInterval / 1000)} 秒后尝试重新连接`);
|
||||
@@ -105,7 +92,8 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
||||
}
|
||||
}
|
||||
});
|
||||
this.connection.on('error', (err) => {
|
||||
|
||||
this.connection.addEventListener('error', (err) => {
|
||||
isClosedByError = true;
|
||||
this.logger.logError(`[OneBot] [WebSocket Client] 反向WebSocket (${this.config.url}) 连接错误`, err);
|
||||
this.logger.logError(`[OneBot] [WebSocket Client] 在 ${Math.floor(this.config.reconnectInterval / 1000)} 秒后尝试重新连接`);
|
||||
@@ -124,7 +112,8 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
||||
this.logger.logError('[OneBot] [WebSocket Client] 发送生命周期失败', e);
|
||||
}
|
||||
}
|
||||
private async handleMessage(message: RawData) {
|
||||
|
||||
private async handleMessage(message: MessageEvent) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let receiveData: { action: typeof ActionName[keyof typeof ActionName], params?: any, echo?: any } = { action: ActionName.Unknown, params: {} };
|
||||
let echo = undefined;
|
||||
@@ -148,6 +137,7 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
||||
const retdata = await action.websocketHandle(receiveData.params, echo ?? '', this.name, this.config);
|
||||
this.checkStateAndReply<unknown>({ ...retdata });
|
||||
}
|
||||
|
||||
async reload(newConfig: WebsocketClientConfig) {
|
||||
const wasEnabled = this.isEnable;
|
||||
const oldUrl = this.config.url;
|
||||
@@ -187,4 +177,4 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
||||
|
||||
return OB11NetworkReloadType.Normal;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,196 +1,210 @@
|
||||
import { OB11EmitEventContent, OB11NetworkReloadType } from './index';
|
||||
import urlParse from 'url';
|
||||
import { RawData, WebSocket, WebSocketServer } from 'ws';
|
||||
import { Mutex } from 'async-mutex';
|
||||
import { OB11Response } from '@/onebot/action/OneBotAction';
|
||||
import { ActionName } from '@/onebot/action/router';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { OB11HeartbeatEvent } from '@/onebot/event/meta/OB11HeartbeatEvent';
|
||||
import { IncomingMessage } from 'http';
|
||||
import { ActionMap } from '@/onebot/action';
|
||||
import { LifeCycleSubType, OB11LifeCycleEvent } from '@/onebot/event/meta/OB11LifeCycleEvent';
|
||||
import { WebsocketServerConfig } from '@/onebot/config/config';
|
||||
import { NapCatOneBot11Adapter } from '@/onebot';
|
||||
import { IOB11NetworkAdapter } from '@/onebot/network/adapter';
|
||||
import json5 from 'json5';
|
||||
import { serve } from '@hono/node-server';
|
||||
import { Context, Hono } from 'hono';
|
||||
import { createNodeWebSocket } from '@hono/node-ws';
|
||||
import { WSContext, WSMessageReceive } from 'hono/ws';
|
||||
import { OB11Response } from '../action/OneBotAction';
|
||||
import { ActionName } from '../action/router';
|
||||
import { OB11HeartbeatEvent } from '@/onebot/event/meta/OB11HeartbeatEvent';
|
||||
import { LifeCycleSubType, OB11LifeCycleEvent } from '@/onebot/event/meta/OB11LifeCycleEvent';
|
||||
|
||||
export class OB11WebSocketServerAdapter extends IOB11NetworkAdapter<WebsocketServerConfig> {
|
||||
wsServer?: WebSocketServer;
|
||||
wsClients: WebSocket[] = [];
|
||||
wsClientsMutex = new Mutex();
|
||||
export class OB11WebsocketServerAdapter extends IOB11NetworkAdapter<WebsocketServerConfig> {
|
||||
private app: Hono | undefined;
|
||||
private server: ReturnType<typeof serve> | undefined;
|
||||
private clients: Set<WSContext<any>> = new Set();
|
||||
private eventClients: Set<WSContext<any>> = new Set(); // 仅用于接收事件的客户端
|
||||
private heartbeatIntervalId: NodeJS.Timeout | null = null;
|
||||
wsClientWithEvent: WebSocket[] = [];
|
||||
|
||||
constructor(
|
||||
name: string, config: WebsocketServerConfig, core: NapCatCore, obContext: NapCatOneBot11Adapter, actions: ActionMap
|
||||
) {
|
||||
constructor(name: string, config: WebsocketServerConfig, core: NapCatCore, obContext: NapCatOneBot11Adapter, actions: ActionMap) {
|
||||
super(name, config, core, obContext, actions);
|
||||
this.wsServer = new WebSocketServer({
|
||||
port: this.config.port,
|
||||
host: this.config.host === '0.0.0.0' ? '' : this.config.host,
|
||||
maxPayload: 1024 * 1024 * 1024,
|
||||
});
|
||||
this.createServer(this.wsServer);
|
||||
|
||||
}
|
||||
createServer(newServer: WebSocketServer) {
|
||||
newServer.on('connection', async (wsClient, wsReq) => {
|
||||
if (!this.isEnable) {
|
||||
wsClient.close();
|
||||
return;
|
||||
}
|
||||
//鉴权
|
||||
this.authorize(this.config.token, wsClient, wsReq);
|
||||
const paramUrl = wsReq.url?.indexOf('?') !== -1 ? wsReq.url?.substring(0, wsReq.url?.indexOf('?')) : wsReq.url;
|
||||
const isApiConnect = paramUrl === '/api' || paramUrl === '/api/';
|
||||
if (!isApiConnect) {
|
||||
this.connectEvent(this.core, wsClient);
|
||||
}
|
||||
|
||||
wsClient.on('error', (err) => this.logger.log('[OneBot] [WebSocket Server] Client Error:', err.message));
|
||||
wsClient.on('message', (message) => {
|
||||
this.handleMessage(wsClient, message).then().catch(e => this.logger.logError(e));
|
||||
});
|
||||
wsClient.on('ping', () => {
|
||||
wsClient.pong();
|
||||
});
|
||||
wsClient.on('pong', () => {
|
||||
//this.logger.logDebug('[OneBot] [WebSocket Server] Pong received');
|
||||
});
|
||||
wsClient.once('close', () => {
|
||||
this.wsClientsMutex.runExclusive(async () => {
|
||||
const NormolIndex = this.wsClients.indexOf(wsClient);
|
||||
if (NormolIndex !== -1) {
|
||||
this.wsClients.splice(NormolIndex, 1);
|
||||
}
|
||||
const EventIndex = this.wsClientWithEvent.indexOf(wsClient);
|
||||
if (EventIndex !== -1) {
|
||||
this.wsClientWithEvent.splice(EventIndex, 1);
|
||||
}
|
||||
override onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||
if (!this.isEnable || this.eventClients.size === 0) return;
|
||||
|
||||
});
|
||||
});
|
||||
await this.wsClientsMutex.runExclusive(async () => {
|
||||
if (!isApiConnect) {
|
||||
this.wsClientWithEvent.push(wsClient);
|
||||
}
|
||||
this.wsClients.push(wsClient);
|
||||
});
|
||||
}).on('error', (err) => this.logger.log('[OneBot] [WebSocket Server] Server Error:', err.message));
|
||||
}
|
||||
connectEvent(core: NapCatCore, wsClient: WebSocket) {
|
||||
try {
|
||||
this.checkStateAndReply<unknown>(new OB11LifeCycleEvent(core, LifeCycleSubType.CONNECT), wsClient);
|
||||
} catch (e) {
|
||||
this.logger.logError('[OneBot] [WebSocket Server] 发送生命周期失败', e);
|
||||
}
|
||||
}
|
||||
|
||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||
this.wsClientsMutex.runExclusive(async () => {
|
||||
this.wsClientWithEvent.forEach((wsClient) => {
|
||||
wsClient.send(JSON.stringify(event));
|
||||
const eventData = JSON.stringify(event);
|
||||
this.eventClients.forEach(client => {
|
||||
try {
|
||||
client.send(eventData);
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError(`[OneBot] [Websocket Server Adapter] 向客户端发送事件失败: ${e}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (this.config.debug) {
|
||||
this.core.context.logger.logDebug(`[OneBot] [Websocket Server Adapter] 已广播事件到 ${this.eventClients.size} 个客户端`);
|
||||
}
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError(`[OneBot] [Websocket Server Adapter] 事件序列化失败: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
open() {
|
||||
if (this.isEnable) {
|
||||
this.logger.logError('[OneBot] [WebSocket Server] Cannot open a opened WebSocket server');
|
||||
return;
|
||||
}
|
||||
const addressInfo = this.wsServer?.address();
|
||||
this.logger.log('[OneBot] [WebSocket Server] Server Started', typeof (addressInfo) === 'string' ? addressInfo : addressInfo?.address + ':' + addressInfo?.port);
|
||||
try {
|
||||
if (this.isEnable) {
|
||||
this.core.context.logger.logError('[OneBot] [Websocket Server Adapter] 无法打开已经启动的Websocket服务器');
|
||||
return;
|
||||
}
|
||||
this.initializeServer();
|
||||
this.isEnable = true;
|
||||
|
||||
this.isEnable = true;
|
||||
if (this.config.heartInterval > 0) {
|
||||
this.registerHeartBeat();
|
||||
// 启动心跳
|
||||
if (this.config.heartInterval > 0) {
|
||||
this.registerHeartBeat();
|
||||
}
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError(`[OneBot] [Websocket Server Adapter] 启动错误: ${e}`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async close() {
|
||||
this.isEnable = false;
|
||||
this.wsServer?.close((err) => {
|
||||
if (err) {
|
||||
this.logger.logError('[OneBot] [WebSocket Server] Error closing server:', err.message);
|
||||
} else {
|
||||
this.logger.log('[OneBot] [WebSocket Server] Server Closed');
|
||||
}
|
||||
this.clients.clear();
|
||||
this.eventClients.clear();
|
||||
|
||||
});
|
||||
// 清除心跳定时器
|
||||
if (this.heartbeatIntervalId) {
|
||||
clearInterval(this.heartbeatIntervalId);
|
||||
this.heartbeatIntervalId = null;
|
||||
}
|
||||
await this.wsClientsMutex.runExclusive(async () => {
|
||||
this.wsClients.forEach((wsClient) => {
|
||||
wsClient.close();
|
||||
});
|
||||
this.wsClients = [];
|
||||
this.wsClientWithEvent = [];
|
||||
});
|
||||
|
||||
this.server?.close();
|
||||
this.app = undefined;
|
||||
}
|
||||
|
||||
private registerHeartBeat() {
|
||||
this.heartbeatIntervalId = setInterval(() => {
|
||||
this.wsClientsMutex.runExclusive(async () => {
|
||||
this.wsClientWithEvent.forEach((wsClient) => {
|
||||
if (wsClient.readyState === WebSocket.OPEN) {
|
||||
wsClient.send(JSON.stringify(new OB11HeartbeatEvent(this.core, this.config.heartInterval, this.core.selfInfo.online ?? true, true)));
|
||||
if (!this.isEnable || this.eventClients.size === 0) return;
|
||||
|
||||
try {
|
||||
const heartbeatEvent = new OB11HeartbeatEvent(
|
||||
this.core,
|
||||
this.config.heartInterval,
|
||||
this.core.selfInfo.online ?? true,
|
||||
true
|
||||
);
|
||||
|
||||
const eventData = JSON.stringify(heartbeatEvent);
|
||||
this.eventClients.forEach(client => {
|
||||
try {
|
||||
client.send(eventData);
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError(`[OneBot] [Websocket Server Adapter] 发送心跳失败: ${e}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError(`[OneBot] [Websocket Server Adapter] 心跳事件生成失败: ${e}`);
|
||||
}
|
||||
}, this.config.heartInterval);
|
||||
}
|
||||
|
||||
private authorize(token: string | undefined, wsClient: WebSocket, wsReq: IncomingMessage) {
|
||||
if (!token || token.length == 0) return;//客户端未设置密钥
|
||||
const QueryClientToken = urlParse.parse(wsReq?.url || '', true).query['access_token'];
|
||||
const HeaderClientToken = wsReq.headers.authorization?.split('Bearer ').pop() || '';
|
||||
const ClientToken = typeof (QueryClientToken) === 'string' && QueryClientToken !== '' ? QueryClientToken : HeaderClientToken;
|
||||
if (ClientToken === token) {
|
||||
private initializeServer() {
|
||||
this.app = new Hono();
|
||||
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app: this.app });
|
||||
|
||||
// 处理所有WebSocket请求
|
||||
this.app.all('/*', upgradeWebSocket((c) => {
|
||||
// 鉴权处理
|
||||
if (this.config.token && this.config.token.length > 0) {
|
||||
const url = new URL(c.req.url, `http://${c.req.header('host') || 'localhost'}`);
|
||||
const queryToken = url.searchParams.get('access_token');
|
||||
const authHeader = c.req.header('authorization');
|
||||
const headerToken = authHeader?.startsWith('Bearer ') ? authHeader.substring(7) : '';
|
||||
const clientToken = queryToken || headerToken;
|
||||
|
||||
if (clientToken !== this.config.token) {
|
||||
return {
|
||||
onOpen: (_evt, ws) => {
|
||||
ws.send(JSON.stringify(OB11Response.res(null, 'failed', 1403, 'token验证失败')));
|
||||
ws.close();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 判断连接类型
|
||||
const url = new URL(c.req.url, `http://${c.req.header('host') || 'localhost'}`);
|
||||
const path = url.pathname;
|
||||
const isApiConnect = path === '/api' || path === '/api/';
|
||||
|
||||
return {
|
||||
onOpen: (_evt, ws) => {
|
||||
this.clients.add(ws);
|
||||
|
||||
// 仅对非API连接添加到事件客户端列表
|
||||
if (!isApiConnect) {
|
||||
this.eventClients.add(ws);
|
||||
// 发送连接生命周期事件
|
||||
try {
|
||||
ws.send(JSON.stringify(new OB11LifeCycleEvent(this.core, LifeCycleSubType.CONNECT)));
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError(`[OneBot] [Websocket Server Adapter] 发送生命周期事件失败: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
this.core.context.logger.log(`[OneBot] [Websocket Server Adapter] 客户端已连接,类型: ${isApiConnect ? 'API' : '事件'},当前连接数: ${this.clients.size}`);
|
||||
},
|
||||
onMessage: (evt, ws) => {
|
||||
this.actionHandler(c, evt, ws);
|
||||
},
|
||||
onClose: (_evt, ws) => {
|
||||
this.clients.delete(ws);
|
||||
this.eventClients.delete(ws);
|
||||
this.core.context.logger.log(`[OneBot] [Websocket Server Adapter] 客户端已断开,当前连接数: ${this.clients.size}`);
|
||||
},
|
||||
onError: (error) => {
|
||||
this.core.context.logger.logError(`[OneBot] [Websocket Server Adapter] WebSocket错误: ${error}`);
|
||||
}
|
||||
};
|
||||
}));
|
||||
|
||||
// 启动服务器
|
||||
this.server = serve({
|
||||
fetch: this.app.fetch.bind(this.app),
|
||||
port: this.config.port,
|
||||
hostname: this.config.host === '0.0.0.0' ? undefined : this.config.host,
|
||||
});
|
||||
|
||||
injectWebSocket(this.server);
|
||||
this.core.context.logger.log(`[OneBot] [Websocket Server Adapter] 服务器已启动于 ${this.config.host}:${this.config.port}`);
|
||||
}
|
||||
|
||||
async actionHandler<T>(_c: Context, evt: MessageEvent<WSMessageReceive>, ws: WSContext<T>) {
|
||||
const { data } = evt;
|
||||
if (typeof data !== 'string') {
|
||||
this.core.context.logger.logError('[OneBot] [Websocket Server Adapter] 收到非字符串消息');
|
||||
return;
|
||||
}
|
||||
wsClient.send(JSON.stringify(OB11Response.res(null, 'failed', 1403, 'token验证失败')));
|
||||
wsClient.close();
|
||||
}
|
||||
|
||||
private checkStateAndReply<T>(data: T, wsClient: WebSocket) {
|
||||
if (wsClient.readyState === WebSocket.OPEN) {
|
||||
wsClient.send(JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
|
||||
private async handleMessage(wsClient: WebSocket, message: RawData) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let receiveData: { action: typeof ActionName[keyof typeof ActionName], params?: any, echo?: any } = { action: ActionName.Unknown, params: {} };
|
||||
let echo = undefined;
|
||||
try {
|
||||
receiveData = json5.parse(message.toString());
|
||||
receiveData = JSON.parse(data);
|
||||
echo = receiveData.echo;
|
||||
//this.logger.logDebug('收到正向Websocket消息', receiveData);
|
||||
} catch {
|
||||
this.checkStateAndReply<unknown>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo), wsClient);
|
||||
return;
|
||||
return ws.send(JSON.stringify(OB11Response.error('json解析失败,请检查数据格式', 1400, echo)));
|
||||
}
|
||||
receiveData.params = (receiveData?.params) ? receiveData.params : {};//兼容类型验证 不然类型校验爆炸
|
||||
receiveData.params = (receiveData?.params) ? receiveData.params : {}; // 兼容类型验证
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const action = this.actions.get(receiveData.action as any);
|
||||
if (!action) {
|
||||
this.logger.logError('[OneBot] [WebSocket Client] 发生错误', '不支持的API ' + receiveData.action);
|
||||
this.checkStateAndReply<unknown>(OB11Response.error('不支持的API ' + receiveData.action, 1404, echo), wsClient);
|
||||
return;
|
||||
return ws.send(JSON.stringify(OB11Response.error('不支持的API ' + receiveData.action, 1404, echo)));
|
||||
}
|
||||
const retdata = await action.websocketHandle(receiveData.params, echo ?? '', this.name, this.config);
|
||||
this.checkStateAndReply<unknown>({ ...retdata }, wsClient);
|
||||
ws.send(JSON.stringify({ ...retdata }));
|
||||
}
|
||||
|
||||
async reload(newConfig: WebsocketServerConfig) {
|
||||
const wasEnabled = this.isEnable;
|
||||
const oldPort = this.config.port;
|
||||
const oldHost = this.config.host;
|
||||
const oldHeartbeatInterval = this.config.heartInterval;
|
||||
const oldHeartInterval = this.config.heartInterval;
|
||||
this.config = newConfig;
|
||||
|
||||
if (newConfig.enable && !wasEnabled) {
|
||||
@@ -201,21 +215,17 @@ export class OB11WebSocketServerAdapter extends IOB11NetworkAdapter<WebsocketSer
|
||||
return OB11NetworkReloadType.NetWorkClose;
|
||||
}
|
||||
|
||||
// 端口或主机变更需要重启服务器
|
||||
if (oldPort !== newConfig.port || oldHost !== newConfig.host) {
|
||||
this.close();
|
||||
this.wsServer = new WebSocketServer({
|
||||
port: newConfig.port,
|
||||
host: newConfig.host === '0.0.0.0' ? '' : newConfig.host,
|
||||
maxPayload: 1024 * 1024 * 1024,
|
||||
});
|
||||
this.createServer(this.wsServer);
|
||||
if (newConfig.enable) {
|
||||
this.open();
|
||||
}
|
||||
return OB11NetworkReloadType.NetWorkReload;
|
||||
}
|
||||
|
||||
if (oldHeartbeatInterval !== newConfig.heartInterval) {
|
||||
// 心跳间隔变更需要重新设置心跳
|
||||
if (oldHeartInterval !== newConfig.heartInterval) {
|
||||
if (this.heartbeatIntervalId) {
|
||||
clearInterval(this.heartbeatIntervalId);
|
||||
this.heartbeatIntervalId = null;
|
||||
@@ -228,5 +238,4 @@ export class OB11WebSocketServerAdapter extends IOB11NetworkAdapter<WebsocketSer
|
||||
|
||||
return OB11NetworkReloadType.Normal;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -57,6 +57,7 @@ export interface OB11GroupMember {
|
||||
}
|
||||
|
||||
export interface OB11Group {
|
||||
group_remark: string; // 群备注
|
||||
group_id: number; // 群ID
|
||||
group_name: string; // 群名称
|
||||
member_count?: number; // 成员数量
|
||||
|
@@ -10,6 +10,7 @@ export enum OB11MessageType {
|
||||
|
||||
// 消息接口定义
|
||||
export interface OB11Message {
|
||||
real_seq?: string;// 自行扩展
|
||||
temp_source?: number;
|
||||
message_sent_type?: string;
|
||||
target_id?: number; // 自己发送消息/私聊消息
|
||||
|
@@ -26,10 +26,10 @@ import { LoginListItem, NodeIKernelLoginService } from '@/core/services';
|
||||
import { program } from 'commander';
|
||||
import qrcode from '@/qrcode/lib/main';
|
||||
import { NapCatOneBot11Adapter } from '@/onebot';
|
||||
import { InitWebUi } from '@/webui';
|
||||
import { WebUiDataRuntime } from '@/webui/src/helper/Data';
|
||||
import { napCatVersion } from '@/common/version';
|
||||
import { NodeIO3MiscListener } from '@/core/listeners/NodeIO3MiscListener';
|
||||
import { sleep } from '@/common/helper';
|
||||
|
||||
// NapCat Shell App ES 入口文件
|
||||
async function handleUncaughtExceptions(logger: LogWrapper) {
|
||||
process.on('uncaughtException', (err) => {
|
||||
@@ -113,121 +113,96 @@ async function handleLogin(
|
||||
quickLoginUin: string | undefined,
|
||||
historyLoginList: LoginListItem[]
|
||||
): Promise<SelfInfo> {
|
||||
return new Promise<SelfInfo>((resolve) => {
|
||||
const loginListener = new NodeIKernelLoginListener();
|
||||
let isLogined = false;
|
||||
let context = { isLogined: false };
|
||||
let inner_resolve: (value: SelfInfo) => void;
|
||||
let selfInfo: Promise<SelfInfo> = new Promise((resolve) => {
|
||||
inner_resolve = resolve;
|
||||
});
|
||||
// 连接服务
|
||||
|
||||
loginListener.onUserLoggedIn = (userid: string) => {
|
||||
logger.logError(`当前账号(${userid})已登录,无法重复登录`);
|
||||
};
|
||||
|
||||
loginListener.onQRCodeLoginSucceed = async (loginResult) => {
|
||||
isLogined = true;
|
||||
resolve({
|
||||
uid: loginResult.uid,
|
||||
uin: loginResult.uin,
|
||||
nick: '',
|
||||
online: true,
|
||||
});
|
||||
};
|
||||
|
||||
loginListener.onQRCodeGetPicture = ({ pngBase64QrcodeData, qrcodeUrl }) => {
|
||||
WebUiDataRuntime.setQQLoginQrcodeURL(qrcodeUrl);
|
||||
|
||||
const realBase64 = pngBase64QrcodeData.replace(/^data:image\/\w+;base64,/, '');
|
||||
const buffer = Buffer.from(realBase64, 'base64');
|
||||
logger.logWarn('请扫描下面的二维码,然后在手Q上授权登录:');
|
||||
const qrcodePath = path.join(pathWrapper.cachePath, 'qrcode.png');
|
||||
qrcode.generate(qrcodeUrl, { small: true }, (res) => {
|
||||
logger.logWarn([
|
||||
'\n',
|
||||
res,
|
||||
'二维码解码URL: ' + qrcodeUrl,
|
||||
'如果控制台二维码无法扫码,可以复制解码url到二维码生成网站生成二维码再扫码,也可以打开下方的二维码路径图片进行扫码。',
|
||||
].join('\n'));
|
||||
fs.writeFile(qrcodePath, buffer, {}, () => {
|
||||
logger.logWarn('二维码已保存到', qrcodePath);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
loginListener.onQRCodeSessionFailed = (errType: number, errCode: number) => {
|
||||
if (!isLogined) {
|
||||
logger.logError('[Core] [Login] Login Error,ErrType: ', errType, ' ErrCode:', errCode);
|
||||
if (errType == 1 && errCode == 3) {
|
||||
// 二维码过期刷新
|
||||
}
|
||||
loginService.getQRCodePicture();
|
||||
}
|
||||
};
|
||||
|
||||
loginListener.onLoginFailed = (...args) => {
|
||||
logger.logError('[Core] [Login] Login Error , ErrInfo: ', JSON.stringify(args));
|
||||
};
|
||||
|
||||
loginService.addKernelLoginListener(proxiedListenerOf(loginListener, logger));
|
||||
const isConnect = loginService.connect();
|
||||
if (!isConnect) {
|
||||
logger.logError('核心登录服务连接失败!');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.log('核心登录服务连接成功!');
|
||||
|
||||
loginService.getLoginList().then((res) => {
|
||||
// 遍历 res.LocalLoginInfoList[x].isQuickLogin是否可以 res.LocalLoginInfoList[x].uin 转为string 加入string[] 最后遍历完成调用WebUiDataRuntime.setQQQuickLoginList
|
||||
const list = res.LocalLoginInfoList.filter((item) => item.isQuickLogin);
|
||||
WebUiDataRuntime.setQQQuickLoginList(list.map((item) => item.uin.toString()));
|
||||
WebUiDataRuntime.setQQNewLoginList(list);
|
||||
const loginListener = new NodeIKernelLoginListener();
|
||||
loginListener.onUserLoggedIn = (userid: string) => {
|
||||
logger.logError(`当前账号(${userid})已登录,无法重复登录`);
|
||||
};
|
||||
loginListener.onQRCodeLoginSucceed = async (loginResult) => {
|
||||
context.isLogined = true;
|
||||
inner_resolve({
|
||||
uid: loginResult.uid,
|
||||
uin: loginResult.uin,
|
||||
nick: '',
|
||||
online: true,
|
||||
});
|
||||
|
||||
WebUiDataRuntime.setQuickLoginCall(async (uin: string) => {
|
||||
return await new Promise((resolve) => {
|
||||
if (uin) {
|
||||
logger.log('正在快速登录 ', uin);
|
||||
loginService.quickLoginWithUin(uin).then(res => {
|
||||
if (res.loginErrorInfo.errMsg) {
|
||||
resolve({ result: false, message: res.loginErrorInfo.errMsg });
|
||||
}
|
||||
resolve({ result: true, message: '' });
|
||||
}).catch((e) => {
|
||||
logger.logError(e);
|
||||
resolve({ result: false, message: '快速登录发生错误' });
|
||||
});
|
||||
} else {
|
||||
resolve({ result: false, message: '快速登录失败' });
|
||||
}
|
||||
};
|
||||
loginListener.onLoginConnected = () => {
|
||||
waitForNetworkConnection(loginService, logger).then(() => {
|
||||
handleLoginInner(context, logger, loginService, quickLoginUin, historyLoginList).then().catch(e => logger.logError(e));
|
||||
});
|
||||
}
|
||||
loginListener.onQRCodeGetPicture = ({ pngBase64QrcodeData, qrcodeUrl }) => {
|
||||
|
||||
const realBase64 = pngBase64QrcodeData.replace(/^data:image\/\w+;base64,/, '');
|
||||
const buffer = Buffer.from(realBase64, 'base64');
|
||||
logger.logWarn('请扫描下面的二维码,然后在手Q上授权登录:');
|
||||
const qrcodePath = path.join(pathWrapper.cachePath, 'qrcode.png');
|
||||
qrcode.generate(qrcodeUrl, { small: true }, (res) => {
|
||||
logger.logWarn([
|
||||
'\n',
|
||||
res,
|
||||
'二维码解码URL: ' + qrcodeUrl,
|
||||
'如果控制台二维码无法扫码,可以复制解码url到二维码生成网站生成二维码再扫码,也可以打开下方的二维码路径图片进行扫码。',
|
||||
].join('\n'));
|
||||
fs.writeFile(qrcodePath, buffer, {}, () => {
|
||||
logger.logWarn('二维码已保存到', qrcodePath);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
if (quickLoginUin) {
|
||||
if (historyLoginList.some(u => u.uin === quickLoginUin)) {
|
||||
logger.log('正在快速登录 ', quickLoginUin);
|
||||
setTimeout(() => {
|
||||
loginService.quickLoginWithUin(quickLoginUin)
|
||||
.then(result => {
|
||||
if (result.loginErrorInfo.errMsg) {
|
||||
logger.logError('快速登录错误:', result.loginErrorInfo.errMsg);
|
||||
if (!isLogined) loginService.getQRCodePicture();
|
||||
}
|
||||
})
|
||||
.catch();
|
||||
}, 1000);
|
||||
} else {
|
||||
logger.logError('快速登录失败,未找到该 QQ 历史登录记录,将使用二维码登录方式');
|
||||
if (!isLogined) loginService.getQRCodePicture();
|
||||
}
|
||||
} else {
|
||||
logger.log('没有 -q 指令指定快速登录,将使用二维码登录方式');
|
||||
if (historyLoginList.length > 0) {
|
||||
logger.log(`可用于快速登录的 QQ:\n${historyLoginList
|
||||
.map((u, index) => `${index + 1}. ${u.uin} ${u.nickName}`)
|
||||
.join('\n')
|
||||
}`);
|
||||
loginListener.onQRCodeSessionFailed = (errType: number, errCode: number) => {
|
||||
if (!context.isLogined) {
|
||||
logger.logError('[Core] [Login] Login Error,ErrType: ', errType, ' ErrCode:', errCode);
|
||||
if (errType == 1 && errCode == 3) {
|
||||
// 二维码过期刷新
|
||||
}
|
||||
loginService.getQRCodePicture();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
loginListener.onLoginFailed = (...args) => {
|
||||
logger.logError('[Core] [Login] Login Error , ErrInfo: ', JSON.stringify(args));
|
||||
};
|
||||
|
||||
loginService.addKernelLoginListener(proxiedListenerOf(loginListener, logger));
|
||||
loginService.connect();
|
||||
return await selfInfo;
|
||||
}
|
||||
async function handleLoginInner(context: { isLogined: boolean }, logger: LogWrapper, loginService: NodeIKernelLoginService, quickLoginUin: string | undefined, historyLoginList: LoginListItem[]) {
|
||||
if (quickLoginUin) {
|
||||
if (historyLoginList.some(u => u.uin === quickLoginUin)) {
|
||||
logger.log('正在快速登录 ', quickLoginUin);
|
||||
loginService.quickLoginWithUin(quickLoginUin)
|
||||
.then(result => {
|
||||
if (result.loginErrorInfo.errMsg) {
|
||||
logger.logError('快速登录错误:', result.loginErrorInfo.errMsg);
|
||||
if (!context.isLogined) loginService.getQRCodePicture();
|
||||
}
|
||||
})
|
||||
.catch();
|
||||
} else {
|
||||
logger.logError('快速登录失败,未找到该 QQ 历史登录记录,将使用二维码登录方式');
|
||||
if (!context.isLogined) loginService.getQRCodePicture();
|
||||
}
|
||||
} else {
|
||||
logger.log('没有 -q 指令指定快速登录,将使用二维码登录方式');
|
||||
if (historyLoginList.length > 0) {
|
||||
logger.log(`可用于快速登录的 QQ:\n${historyLoginList
|
||||
.map((u, index) => `${index + 1}. ${u.uin} ${u.nickName}`)
|
||||
.join('\n')
|
||||
}`);
|
||||
}
|
||||
loginService.getQRCodePicture();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async function initializeSession(
|
||||
@@ -284,6 +259,20 @@ async function handleProxy(session: NodeIQQNTWrapperSession, logger: LogWrapper)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForNetworkConnection(loginService: NodeIKernelLoginService, logger: LogWrapper) {
|
||||
let network_ok = false;
|
||||
let tryCount = 0;
|
||||
while (!network_ok) {
|
||||
network_ok = loginService.getMsfStatus() !== 3;// win 11 0连接 1未连接
|
||||
logger.log('等待网络连接...');
|
||||
await sleep(500);
|
||||
tryCount++;
|
||||
}
|
||||
logger.log('网络已连接');
|
||||
return network_ok;
|
||||
}
|
||||
|
||||
export async function NCoreInitShell() {
|
||||
console.log('NapCat Shell App Loading...');
|
||||
const pathWrapper = new NapCatPathWrapper();
|
||||
@@ -296,8 +285,6 @@ export async function NCoreInitShell() {
|
||||
o3Service.addO3MiscListener(new NodeIO3MiscListener());
|
||||
|
||||
logger.log('[NapCat] [Core] NapCat.Core Version: ' + napCatVersion);
|
||||
InitWebUi(logger, pathWrapper).then().catch(e => logger.logError(e));
|
||||
|
||||
const engine = wrapper.NodeIQQNTWrapperEngine.get();
|
||||
const loginService = wrapper.NodeIKernelLoginService.get();
|
||||
const session = wrapper.NodeIQQNTWrapperSession.create();
|
||||
|
@@ -1,3 +0,0 @@
|
||||
# The Path of NapCatQQ
|
||||
|
||||
Tiny WebUi Backend for NapCatQQ
|
@@ -1,209 +0,0 @@
|
||||
/**
|
||||
* @file WebUI服务入口文件
|
||||
*/
|
||||
|
||||
import express from 'express';
|
||||
import { createServer } from 'http';
|
||||
import { LogWrapper } from '@/common/log';
|
||||
import { NapCatPathWrapper } from '@/common/path';
|
||||
import { WebUiConfigWrapper } from '@webapi/helper/config';
|
||||
import { ALLRouter } from '@webapi/router';
|
||||
import { cors } from '@webapi/middleware/cors';
|
||||
import { createUrl } from '@webapi/utils/url';
|
||||
import { sendError } from '@webapi/utils/response';
|
||||
import { join } from 'node:path';
|
||||
import { terminalManager } from '@webapi/terminal/terminal_manager';
|
||||
import multer from 'multer'; // 新增:引入multer用于错误捕获
|
||||
|
||||
// 实例化Express
|
||||
const app = express();
|
||||
const server = createServer(app);
|
||||
/**
|
||||
* 初始化并启动WebUI服务。
|
||||
* 该函数配置了Express服务器以支持JSON解析和静态文件服务,并监听6099端口。
|
||||
* 无需参数。
|
||||
* @returns {Promise<void>} 无返回值。
|
||||
*/
|
||||
export let WebUiConfig: WebUiConfigWrapper;
|
||||
export let webUiPathWrapper: NapCatPathWrapper;
|
||||
const MAX_PORT_TRY = 100;
|
||||
import * as net from 'node:net';
|
||||
import { WebUiDataRuntime } from './src/helper/Data';
|
||||
|
||||
export async function InitPort(parsedConfig: WebUiConfigType): Promise<[string, number, string]> {
|
||||
try {
|
||||
await tryUseHost(parsedConfig.host);
|
||||
const port = await tryUsePort(parsedConfig.port, parsedConfig.host);
|
||||
return [parsedConfig.host, port, parsedConfig.token];
|
||||
} catch (error) {
|
||||
console.log('host或port不可用', error);
|
||||
return ['', 0, ''];
|
||||
}
|
||||
}
|
||||
|
||||
export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapper) {
|
||||
webUiPathWrapper = pathWrapper;
|
||||
WebUiConfig = new WebUiConfigWrapper();
|
||||
const [host, port, token] = await InitPort(await WebUiConfig.GetWebUIConfig());
|
||||
if (port == 0) {
|
||||
logger.log('[NapCat] [WebUi] Current WebUi is not run.');
|
||||
return;
|
||||
}
|
||||
setTimeout(async () => {
|
||||
let autoLoginAccount = process.env['NAPCAT_QUICK_ACCOUNT'] || WebUiConfig.getAutoLoginAccount();
|
||||
if (autoLoginAccount) {
|
||||
try {
|
||||
const { result, message } = await WebUiDataRuntime.requestQuickLogin(autoLoginAccount);
|
||||
if (!result) {
|
||||
throw new Error(message);
|
||||
}
|
||||
console.log(`[NapCat] [WebUi] Auto login account: ${autoLoginAccount}`);
|
||||
} catch (error) {
|
||||
console.log(`[NapCat] [WebUi] Auto login account failed.` + error);
|
||||
}
|
||||
}
|
||||
}, 30000);
|
||||
// ------------注册中间件------------
|
||||
// 使用express的json中间件
|
||||
app.use(express.json());
|
||||
|
||||
// CORS中间件
|
||||
// TODO:
|
||||
app.use(cors);
|
||||
|
||||
// 如果是webui字体文件,挂载字体文件
|
||||
app.use('/webui/fonts/AaCute.woff', async (_req, res, next) => {
|
||||
const isFontExist = await WebUiConfig.CheckWebUIFontExist();
|
||||
if (isFontExist) {
|
||||
res.sendFile(WebUiConfig.GetWebUIFontPath());
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
// 如果是自定义色彩,构建一个css文件
|
||||
app.use('/files/theme.css', async (_req, res) => {
|
||||
const colors = await WebUiConfig.GetTheme();
|
||||
|
||||
let css = ':root, .light, [data-theme="light"] {';
|
||||
for (const key in colors.light) {
|
||||
css += `${key}: ${colors.light[key]};`;
|
||||
}
|
||||
css += '}';
|
||||
css += '.dark, [data-theme="dark"] {';
|
||||
for (const key in colors.dark) {
|
||||
css += `${key}: ${colors.dark[key]};`;
|
||||
}
|
||||
css += '}';
|
||||
|
||||
res.send(css);
|
||||
});
|
||||
|
||||
// ------------中间件结束------------
|
||||
|
||||
// ------------挂载路由------------
|
||||
// 挂载静态路由(前端),路径为 /webui
|
||||
app.use('/webui', express.static(pathWrapper.staticPath));
|
||||
// 初始化WebSocket服务器
|
||||
server.on('upgrade', (request, socket, head) => {
|
||||
terminalManager.initialize(request, socket, head, logger);
|
||||
});
|
||||
// 挂载API接口
|
||||
app.use('/api', ALLRouter);
|
||||
// 所有剩下的请求都转到静态页面
|
||||
const indexFile = join(pathWrapper.staticPath, 'index.html');
|
||||
|
||||
app.all(/\/webui\/(.*)/, (_req, res) => {
|
||||
res.sendFile(indexFile);
|
||||
});
|
||||
|
||||
// 初始服务(先放个首页)
|
||||
app.all('/', (_req, res) => {
|
||||
res.status(301).header('Location', '/webui').send();
|
||||
});
|
||||
|
||||
// 错误处理中间件,捕获multer的错误
|
||||
app.use((err: Error, _: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
if (err instanceof multer.MulterError) {
|
||||
return sendError(res, err.message, true);
|
||||
}
|
||||
next(err);
|
||||
});
|
||||
|
||||
// 全局错误处理中间件(非multer错误)
|
||||
app.use((_: Error, __: express.Request, res: express.Response, ___: express.NextFunction) => {
|
||||
sendError(res, 'An unknown error occurred.', true);
|
||||
});
|
||||
|
||||
// ------------启动服务------------
|
||||
server.listen(port, host, async () => {
|
||||
// 启动后打印出相关地址
|
||||
let searchParams = { token: token };
|
||||
if (host !== '' && host !== '0.0.0.0') {
|
||||
logger.log(
|
||||
`[NapCat] [WebUi] WebUi User Panel Url: ${createUrl(host, port.toString(), '/webui', searchParams)}`
|
||||
);
|
||||
}
|
||||
logger.log(
|
||||
`[NapCat] [WebUi] WebUi Local Panel Url: ${createUrl('127.0.0.1', port.toString(), '/webui', searchParams)}`
|
||||
);
|
||||
});
|
||||
// ------------Over!------------
|
||||
}
|
||||
|
||||
async function tryUseHost(host: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const server = net.createServer();
|
||||
server.on('listening', () => {
|
||||
server.close();
|
||||
resolve(host);
|
||||
});
|
||||
|
||||
server.on('error', (err: any) => {
|
||||
if (err.code === 'EADDRNOTAVAIL') {
|
||||
reject(new Error('主机地址验证失败,可能为非本机地址'));
|
||||
} else {
|
||||
reject(new Error(`遇到错误: ${err.code}`));
|
||||
}
|
||||
});
|
||||
|
||||
// 尝试监听 让系统随机分配一个端口
|
||||
server.listen(0, host);
|
||||
} catch (error) {
|
||||
// 这里捕获到的错误应该是启动服务器时的同步错误
|
||||
reject(new Error(`服务器启动时发生错误: ${error}`));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function tryUsePort(port: number, host: string, tryCount: number = 0): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const server = net.createServer();
|
||||
server.on('listening', () => {
|
||||
server.close();
|
||||
resolve(port);
|
||||
});
|
||||
|
||||
server.on('error', (err: any) => {
|
||||
if (err.code === 'EADDRINUSE') {
|
||||
if (tryCount < MAX_PORT_TRY) {
|
||||
// 使用循环代替递归
|
||||
resolve(tryUsePort(port + 1, host, tryCount + 1));
|
||||
} else {
|
||||
reject(new Error(`端口尝试失败,达到最大尝试次数: ${MAX_PORT_TRY}`));
|
||||
}
|
||||
} else {
|
||||
reject(new Error(`遇到错误: ${err.code}`));
|
||||
}
|
||||
});
|
||||
|
||||
// 尝试监听端口
|
||||
server.listen(port, host);
|
||||
} catch (error) {
|
||||
// 这里捕获到的错误应该是启动服务器时的同步错误
|
||||
reject(new Error(`服务器启动时发生错误: ${error}`));
|
||||
}
|
||||
});
|
||||
}
|
@@ -1,115 +0,0 @@
|
||||
import { RequestHandler } from 'express';
|
||||
|
||||
import { WebUiConfig } from '@/webui';
|
||||
|
||||
import { AuthHelper } from '@webapi/helper/SignToken';
|
||||
import { WebUiDataRuntime } from '@webapi/helper/Data';
|
||||
import { sendSuccess, sendError } from '@webapi/utils/response';
|
||||
import { isEmpty } from '@webapi/utils/check';
|
||||
|
||||
// 检查是否使用默认Token
|
||||
export const CheckDefaultTokenHandler: RequestHandler = async (_, res) => {
|
||||
const webuiToken = await WebUiConfig.GetWebUIConfig();
|
||||
if (webuiToken.token === 'napcat') {
|
||||
return sendSuccess(res, true);
|
||||
}
|
||||
return sendSuccess(res, false);
|
||||
};
|
||||
|
||||
// 登录
|
||||
export const LoginHandler: RequestHandler = async (req, res) => {
|
||||
// 获取WebUI配置
|
||||
const WebUiConfigData = await WebUiConfig.GetWebUIConfig();
|
||||
// 获取请求体中的token
|
||||
const { token } = req.body;
|
||||
// 获取客户端IP
|
||||
const clientIP = req.ip || req.socket.remoteAddress || '';
|
||||
|
||||
// 如果token为空,返回错误信息
|
||||
if (isEmpty(token)) {
|
||||
return sendError(res, 'token is empty');
|
||||
}
|
||||
// 检查登录频率
|
||||
if (!WebUiDataRuntime.checkLoginRate(clientIP, WebUiConfigData.loginRate)) {
|
||||
return sendError(res, 'login rate limit');
|
||||
}
|
||||
//验证config.token是否等于token
|
||||
if (WebUiConfigData.token !== token) {
|
||||
return sendError(res, 'token is invalid');
|
||||
}
|
||||
// 签发凭证
|
||||
const signCredential = Buffer.from(JSON.stringify(AuthHelper.signCredential(WebUiConfigData.token))).toString(
|
||||
'base64'
|
||||
);
|
||||
// 返回成功信息
|
||||
return sendSuccess(res, {
|
||||
Credential: signCredential,
|
||||
});
|
||||
};
|
||||
|
||||
// 退出登录
|
||||
export const LogoutHandler: RequestHandler = async (req, res) => {
|
||||
const authorization = req.headers.authorization;
|
||||
try {
|
||||
const CredentialBase64: string = authorization?.split(' ')[1] as string;
|
||||
const Credential = JSON.parse(Buffer.from(CredentialBase64, 'base64').toString());
|
||||
AuthHelper.revokeCredential(Credential);
|
||||
return sendSuccess(res, 'Logged out successfully');
|
||||
} catch (e) {
|
||||
return sendError(res, 'Logout failed');
|
||||
}
|
||||
};
|
||||
|
||||
// 检查登录状态
|
||||
export const checkHandler: RequestHandler = async (req, res) => {
|
||||
// 获取WebUI配置
|
||||
const WebUiConfigData = await WebUiConfig.GetWebUIConfig();
|
||||
// 获取请求头中的Authorization
|
||||
const authorization = req.headers.authorization;
|
||||
// 检查凭证
|
||||
try {
|
||||
// 从Authorization中获取凭证
|
||||
const CredentialBase64: string = authorization?.split(' ')[1] as string;
|
||||
// 解析凭证
|
||||
const Credential = JSON.parse(Buffer.from(CredentialBase64, 'base64').toString());
|
||||
|
||||
// 检查凭证是否已被注销
|
||||
if (AuthHelper.isCredentialRevoked(Credential)) {
|
||||
return sendError(res, 'Token has been revoked');
|
||||
}
|
||||
|
||||
// 验证凭证是否在一小时内有效
|
||||
const valid = AuthHelper.validateCredentialWithinOneHour(WebUiConfigData.token, Credential);
|
||||
// 返回成功信息
|
||||
if (valid) return sendSuccess(res, null);
|
||||
// 返回错误信息
|
||||
return sendError(res, 'Authorization Failed');
|
||||
} catch (e) {
|
||||
// 返回错误信息
|
||||
return sendError(res, 'Authorization Failed');
|
||||
}
|
||||
};
|
||||
|
||||
// 修改密码(token)
|
||||
export const UpdateTokenHandler: RequestHandler = async (req, res) => {
|
||||
const { oldToken, newToken } = req.body;
|
||||
const authorization = req.headers.authorization;
|
||||
|
||||
if (isEmpty(oldToken) || isEmpty(newToken)) {
|
||||
return sendError(res, 'oldToken or newToken is empty');
|
||||
}
|
||||
|
||||
try {
|
||||
// 注销当前的Token
|
||||
if (authorization) {
|
||||
const CredentialBase64: string = authorization.split(' ')[1] as string;
|
||||
const Credential = JSON.parse(Buffer.from(CredentialBase64, 'base64').toString());
|
||||
AuthHelper.revokeCredential(Credential);
|
||||
}
|
||||
|
||||
await WebUiConfig.UpdateToken(oldToken, newToken);
|
||||
return sendSuccess(res, 'Token updated successfully');
|
||||
} catch (e: any) {
|
||||
return sendError(res, `Failed to update token: ${e.message}`);
|
||||
}
|
||||
};
|
@@ -1,26 +0,0 @@
|
||||
import { RequestHandler } from 'express';
|
||||
import { WebUiDataRuntime } from '@webapi/helper/Data';
|
||||
|
||||
import { sendSuccess } from '@webapi/utils/response';
|
||||
import { WebUiConfig } from '@/webui';
|
||||
|
||||
export const PackageInfoHandler: RequestHandler = (_, res) => {
|
||||
const data = WebUiDataRuntime.getPackageJson();
|
||||
sendSuccess(res, data);
|
||||
};
|
||||
|
||||
export const QQVersionHandler: RequestHandler = (_, res) => {
|
||||
const data = WebUiDataRuntime.getQQVersion();
|
||||
sendSuccess(res, data);
|
||||
};
|
||||
|
||||
export const GetThemeConfigHandler: RequestHandler = async (_, res) => {
|
||||
const data = await WebUiConfig.GetTheme();
|
||||
sendSuccess(res, data);
|
||||
};
|
||||
|
||||
export const SetThemeConfigHandler: RequestHandler = async (req, res) => {
|
||||
const { theme } = req.body;
|
||||
await WebUiConfig.UpdateTheme(theme);
|
||||
sendSuccess(res, { message: '更新成功' });
|
||||
};
|
@@ -1,399 +0,0 @@
|
||||
import type { RequestHandler } from 'express';
|
||||
import { sendError, sendSuccess } from '../utils/response';
|
||||
import fsProm from 'fs/promises';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
import compressing from 'compressing';
|
||||
import { PassThrough } from 'stream';
|
||||
import multer from 'multer';
|
||||
import webUIFontUploader from '../uploader/webui_font';
|
||||
import diskUploader from '../uploader/disk';
|
||||
import { WebUiConfig } from '@/webui';
|
||||
|
||||
const isWindows = os.platform() === 'win32';
|
||||
|
||||
// 获取系统根目录列表(Windows返回盘符列表,其他系统返回['/'])
|
||||
const getRootDirs = async (): Promise<string[]> => {
|
||||
if (!isWindows) return ['/'];
|
||||
|
||||
// Windows 驱动器字母 (A-Z)
|
||||
const drives: string[] = [];
|
||||
for (let i = 65; i <= 90; i++) {
|
||||
const driveLetter = String.fromCharCode(i);
|
||||
try {
|
||||
await fsProm.access(`${driveLetter}:\\`);
|
||||
drives.push(`${driveLetter}:`);
|
||||
} catch {
|
||||
// 如果驱动器不存在或无法访问,跳过
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return drives.length > 0 ? drives : ['C:'];
|
||||
};
|
||||
|
||||
// 规范化路径
|
||||
const normalizePath = (inputPath: string): string => {
|
||||
if (!inputPath) return isWindows ? 'C:\\' : '/';
|
||||
// 如果是Windows且输入为纯盘符(可能带或不带斜杠),统一返回 "X:\"
|
||||
if (isWindows && /^[A-Z]:[\\/]*$/i.test(inputPath)) {
|
||||
return inputPath.slice(0, 2) + '\\';
|
||||
}
|
||||
return path.normalize(inputPath);
|
||||
};
|
||||
|
||||
interface FileInfo {
|
||||
name: string;
|
||||
isDirectory: boolean;
|
||||
size: number;
|
||||
mtime: Date;
|
||||
}
|
||||
|
||||
// 添加系统文件黑名单
|
||||
const SYSTEM_FILES = new Set(['pagefile.sys', 'swapfile.sys', 'hiberfil.sys', 'System Volume Information']);
|
||||
|
||||
// 检查同类型的文件或目录是否存在
|
||||
const checkSameTypeExists = async (pathToCheck: string, isDirectory: boolean): Promise<boolean> => {
|
||||
try {
|
||||
const stat = await fsProm.stat(pathToCheck);
|
||||
// 只有当类型相同时才认为是冲突
|
||||
return stat.isDirectory() === isDirectory;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// 获取目录内容
|
||||
export const ListFilesHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const requestPath = (req.query['path'] as string) || (isWindows ? 'C:\\' : '/');
|
||||
const normalizedPath = normalizePath(requestPath);
|
||||
const onlyDirectory = req.query['onlyDirectory'] === 'true';
|
||||
|
||||
// 如果是根路径且在Windows系统上,返回盘符列表
|
||||
if (isWindows && (!requestPath || requestPath === '/' || requestPath === '\\')) {
|
||||
const drives = await getRootDirs();
|
||||
const driveInfos: FileInfo[] = await Promise.all(
|
||||
drives.map(async (drive) => {
|
||||
try {
|
||||
const stat = await fsProm.stat(`${drive}\\`);
|
||||
return {
|
||||
name: drive,
|
||||
isDirectory: true,
|
||||
size: 0,
|
||||
mtime: stat.mtime,
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
name: drive,
|
||||
isDirectory: true,
|
||||
size: 0,
|
||||
mtime: new Date(),
|
||||
};
|
||||
}
|
||||
})
|
||||
);
|
||||
return sendSuccess(res, driveInfos);
|
||||
}
|
||||
|
||||
const files = await fsProm.readdir(normalizedPath);
|
||||
let fileInfos: FileInfo[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
// 跳过系统文件
|
||||
if (SYSTEM_FILES.has(file)) continue;
|
||||
|
||||
try {
|
||||
const fullPath = path.join(normalizedPath, file);
|
||||
const stat = await fsProm.stat(fullPath);
|
||||
fileInfos.push({
|
||||
name: file,
|
||||
isDirectory: stat.isDirectory(),
|
||||
size: stat.size,
|
||||
mtime: stat.mtime,
|
||||
});
|
||||
} catch (error) {
|
||||
// 忽略无法访问的文件
|
||||
// console.warn(`无法访问文件 ${file}:`, error);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果请求参数 onlyDirectory 为 true,则只返回目录信息
|
||||
if (onlyDirectory) {
|
||||
fileInfos = fileInfos.filter((info) => info.isDirectory);
|
||||
}
|
||||
|
||||
return sendSuccess(res, fileInfos);
|
||||
} catch (error) {
|
||||
console.error('读取目录失败:', error);
|
||||
return sendError(res, '读取目录失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 创建目录
|
||||
export const CreateDirHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { path: dirPath } = req.body;
|
||||
const normalizedPath = normalizePath(dirPath);
|
||||
|
||||
// 检查是否已存在同类型(目录)
|
||||
if (await checkSameTypeExists(normalizedPath, true)) {
|
||||
return sendError(res, '同名目录已存在');
|
||||
}
|
||||
|
||||
await fsProm.mkdir(normalizedPath, { recursive: true });
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '创建目录失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 删除文件/目录
|
||||
export const DeleteHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { path: targetPath } = req.body;
|
||||
const normalizedPath = normalizePath(targetPath);
|
||||
const stat = await fsProm.stat(normalizedPath);
|
||||
if (stat.isDirectory()) {
|
||||
await fsProm.rm(normalizedPath, { recursive: true });
|
||||
} else {
|
||||
await fsProm.unlink(normalizedPath);
|
||||
}
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '删除失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 批量删除文件/目录
|
||||
export const BatchDeleteHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { paths } = req.body;
|
||||
for (const targetPath of paths) {
|
||||
const normalizedPath = normalizePath(targetPath);
|
||||
const stat = await fsProm.stat(normalizedPath);
|
||||
if (stat.isDirectory()) {
|
||||
await fsProm.rm(normalizedPath, { recursive: true });
|
||||
} else {
|
||||
await fsProm.unlink(normalizedPath);
|
||||
}
|
||||
}
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '批量删除失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 读取文件内容
|
||||
export const ReadFileHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const filePath = normalizePath(req.query['path'] as string);
|
||||
const content = await fsProm.readFile(filePath, 'utf-8');
|
||||
return sendSuccess(res, content);
|
||||
} catch (error) {
|
||||
return sendError(res, '读取文件失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 写入文件内容
|
||||
export const WriteFileHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { path: filePath, content } = req.body;
|
||||
const normalizedPath = normalizePath(filePath);
|
||||
await fsProm.writeFile(normalizedPath, content, 'utf-8');
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '写入文件失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 创建新文件
|
||||
export const CreateFileHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { path: filePath } = req.body;
|
||||
const normalizedPath = normalizePath(filePath);
|
||||
|
||||
// 检查是否已存在同类型(文件)
|
||||
if (await checkSameTypeExists(normalizedPath, false)) {
|
||||
return sendError(res, '同名文件已存在');
|
||||
}
|
||||
|
||||
await fsProm.writeFile(normalizedPath, '', 'utf-8');
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '创建文件失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 重命名文件/目录
|
||||
export const RenameHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { oldPath, newPath } = req.body;
|
||||
const normalizedOldPath = normalizePath(oldPath);
|
||||
const normalizedNewPath = normalizePath(newPath);
|
||||
await fsProm.rename(normalizedOldPath, normalizedNewPath);
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '重命名失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 移动文件/目录
|
||||
export const MoveHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { sourcePath, targetPath } = req.body;
|
||||
const normalizedSourcePath = normalizePath(sourcePath);
|
||||
const normalizedTargetPath = normalizePath(targetPath);
|
||||
await fsProm.rename(normalizedSourcePath, normalizedTargetPath);
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '移动失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 批量移动
|
||||
export const BatchMoveHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { items } = req.body;
|
||||
for (const { sourcePath, targetPath } of items) {
|
||||
const normalizedSourcePath = normalizePath(sourcePath);
|
||||
const normalizedTargetPath = normalizePath(targetPath);
|
||||
await fsProm.rename(normalizedSourcePath, normalizedTargetPath);
|
||||
}
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '批量移动失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 新增:文件下载处理方法(注意流式传输,不将整个文件读入内存)
|
||||
export const DownloadHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const filePath = normalizePath(req.query['path'] as string);
|
||||
if (!filePath) {
|
||||
return sendError(res, '参数错误');
|
||||
}
|
||||
|
||||
const stat = await fsProm.stat(filePath);
|
||||
|
||||
res.setHeader('Content-Type', 'application/octet-stream');
|
||||
let filename = path.basename(filePath);
|
||||
if (stat.isDirectory()) {
|
||||
filename = path.basename(filePath) + '.zip';
|
||||
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`);
|
||||
const zipStream = new PassThrough();
|
||||
compressing.zip.compressDir(filePath, zipStream as unknown as fs.WriteStream).catch((err) => {
|
||||
console.error('压缩目录失败:', err);
|
||||
res.end();
|
||||
});
|
||||
zipStream.pipe(res);
|
||||
return;
|
||||
}
|
||||
res.setHeader('Content-Length', stat.size);
|
||||
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`);
|
||||
const stream = fs.createReadStream(filePath);
|
||||
stream.pipe(res);
|
||||
} catch (error) {
|
||||
return sendError(res, '下载失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 批量下载:将多个文件/目录打包为 zip 文件下载
|
||||
export const BatchDownloadHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { paths } = req.body as { paths: string[] };
|
||||
if (!paths || !Array.isArray(paths) || paths.length === 0) {
|
||||
return sendError(res, '参数错误');
|
||||
}
|
||||
res.setHeader('Content-Type', 'application/octet-stream');
|
||||
res.setHeader('Content-Disposition', 'attachment; filename=files.zip');
|
||||
|
||||
const zipStream = new compressing.zip.Stream();
|
||||
// 修改:根据文件类型设置 relativePath
|
||||
for (const filePath of paths) {
|
||||
const normalizedPath = normalizePath(filePath);
|
||||
const stat = await fsProm.stat(normalizedPath);
|
||||
if (stat.isDirectory()) {
|
||||
zipStream.addEntry(normalizedPath, { relativePath: '' });
|
||||
} else {
|
||||
zipStream.addEntry(normalizedPath, { relativePath: path.basename(normalizedPath) });
|
||||
}
|
||||
}
|
||||
zipStream.pipe(res);
|
||||
res.on('finish', () => {
|
||||
zipStream.destroy();
|
||||
});
|
||||
} catch (error) {
|
||||
return sendError(res, '下载失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 修改上传处理方法
|
||||
export const UploadHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
await diskUploader(req, res);
|
||||
return sendSuccess(res, true, '文件上传成功', true);
|
||||
} catch (error) {
|
||||
let errorMessage = '文件上传失败';
|
||||
|
||||
if (error instanceof multer.MulterError) {
|
||||
switch (error.code) {
|
||||
case 'LIMIT_FILE_SIZE':
|
||||
errorMessage = '文件大小超过限制(40MB)';
|
||||
break;
|
||||
case 'LIMIT_UNEXPECTED_FILE':
|
||||
errorMessage = '无效的文件上传字段';
|
||||
break;
|
||||
default:
|
||||
errorMessage = `上传错误: ${error.message}`;
|
||||
}
|
||||
} else if (error instanceof Error) {
|
||||
errorMessage = error.message;
|
||||
}
|
||||
return sendError(res, errorMessage, true);
|
||||
}
|
||||
};
|
||||
|
||||
// 上传WebUI字体文件处理方法
|
||||
export const UploadWebUIFontHandler: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
await webUIFontUploader(req, res);
|
||||
return sendSuccess(res, true, '字体文件上传成功', true);
|
||||
} catch (error) {
|
||||
let errorMessage = '字体文件上传失败';
|
||||
|
||||
if (error instanceof multer.MulterError) {
|
||||
switch (error.code) {
|
||||
case 'LIMIT_FILE_SIZE':
|
||||
errorMessage = '字体文件大小超过限制(40MB)';
|
||||
break;
|
||||
case 'LIMIT_UNEXPECTED_FILE':
|
||||
errorMessage = '无效的文件上传字段';
|
||||
break;
|
||||
default:
|
||||
errorMessage = `上传错误: ${error.message}`;
|
||||
}
|
||||
} else if (error instanceof Error) {
|
||||
errorMessage = error.message;
|
||||
}
|
||||
return sendError(res, errorMessage, true);
|
||||
}
|
||||
};
|
||||
|
||||
// 删除WebUI字体文件处理方法
|
||||
export const DeleteWebUIFontHandler: RequestHandler = async (_req, res) => {
|
||||
try {
|
||||
const fontPath = WebUiConfig.GetWebUIFontPath();
|
||||
const exists = await WebUiConfig.CheckWebUIFontExist();
|
||||
|
||||
if (!exists) {
|
||||
return sendSuccess(res, true);
|
||||
}
|
||||
|
||||
await fsProm.unlink(fontPath);
|
||||
return sendSuccess(res, true);
|
||||
} catch (error) {
|
||||
return sendError(res, '删除字体文件失败');
|
||||
}
|
||||
};
|
@@ -1,72 +0,0 @@
|
||||
import type { RequestHandler } from 'express';
|
||||
import { sendError, sendSuccess } from '../utils/response';
|
||||
import { logSubscription } from '@/common/log';
|
||||
import { terminalManager } from '../terminal/terminal_manager';
|
||||
import { WebUiConfig } from '@/webui';
|
||||
// 判断是否是 macos
|
||||
const isMacOS = process.platform === 'darwin';
|
||||
// 日志记录
|
||||
export const LogHandler: RequestHandler = async (req, res) => {
|
||||
const filename = req.query['id'];
|
||||
if (!filename || typeof filename !== 'string') {
|
||||
return sendError(res, 'ID不能为空');
|
||||
}
|
||||
|
||||
if (filename.includes('..')) {
|
||||
return sendError(res, 'ID不合法');
|
||||
}
|
||||
const logContent = await WebUiConfig.GetLogContent(filename);
|
||||
return sendSuccess(res, logContent);
|
||||
};
|
||||
|
||||
// 日志列表
|
||||
export const LogListHandler: RequestHandler = async (_, res) => {
|
||||
const logList = await WebUiConfig.GetLogsList();
|
||||
return sendSuccess(res, logList);
|
||||
};
|
||||
|
||||
// 实时日志(SSE)
|
||||
export const LogRealTimeHandler: RequestHandler = async (req, res) => {
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
const listener = (log: string) => {
|
||||
try {
|
||||
res.write(`data: ${log}\n\n`);
|
||||
} catch (error) {
|
||||
console.error('向客户端写入日志数据时出错:', error);
|
||||
}
|
||||
};
|
||||
logSubscription.subscribe(listener);
|
||||
req.on('close', () => {
|
||||
logSubscription.unsubscribe(listener);
|
||||
});
|
||||
};
|
||||
|
||||
// 终端相关处理器
|
||||
export const CreateTerminalHandler: RequestHandler = async (req, res) => {
|
||||
if (isMacOS) {
|
||||
return sendError(res, 'MacOS不支持终端');
|
||||
}
|
||||
try {
|
||||
const { cols, rows } = req.body;
|
||||
const { id } = terminalManager.createTerminal(cols, rows);
|
||||
return sendSuccess(res, { id });
|
||||
} catch (error) {
|
||||
console.error('Failed to create terminal:', error);
|
||||
return sendError(res, '创建终端失败');
|
||||
}
|
||||
};
|
||||
|
||||
export const GetTerminalListHandler: RequestHandler = (_, res) => {
|
||||
const list = terminalManager.getTerminalList();
|
||||
return sendSuccess(res, list);
|
||||
};
|
||||
|
||||
export const CloseTerminalHandler: RequestHandler = (req, res) => {
|
||||
const id = req.params['id'];
|
||||
if (!id) {
|
||||
return sendError(res, 'ID不能为空');
|
||||
}
|
||||
terminalManager.closeTerminal(id);
|
||||
return sendSuccess(res, {});
|
||||
};
|
@@ -1,60 +0,0 @@
|
||||
import { RequestHandler } from 'express';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import { loadConfig, OneBotConfig } from '@/onebot/config/config';
|
||||
import { webUiPathWrapper } from '@/webui';
|
||||
import { WebUiDataRuntime } from '@webapi/helper/Data';
|
||||
import { sendError, sendSuccess } from '@webapi/utils/response';
|
||||
import { isEmpty } from '@webapi/utils/check';
|
||||
import json5 from 'json5';
|
||||
|
||||
// 获取OneBot11配置
|
||||
export const OB11GetConfigHandler: RequestHandler = (_, res) => {
|
||||
// 获取QQ登录状态
|
||||
const isLogin = WebUiDataRuntime.getQQLoginStatus();
|
||||
// 如果未登录,返回错误
|
||||
if (!isLogin) {
|
||||
return sendError(res, 'Not Login');
|
||||
}
|
||||
// 获取登录的QQ号
|
||||
const uin = WebUiDataRuntime.getQQLoginUin();
|
||||
// 读取配置文件路径
|
||||
const configFilePath = resolve(webUiPathWrapper.configPath, `./onebot11_${uin}.json`);
|
||||
// 尝试解析配置文件
|
||||
try {
|
||||
// 读取配置文件内容
|
||||
const configFileContent = existsSync(configFilePath)
|
||||
? readFileSync(configFilePath).toString()
|
||||
: readFileSync(resolve(webUiPathWrapper.configPath, './onebot11.json')).toString();
|
||||
// 解析配置文件并加载配置
|
||||
const data = loadConfig(json5.parse(configFileContent)) as OneBotConfig;
|
||||
// 返回配置文件
|
||||
return sendSuccess(res, data);
|
||||
} catch (e) {
|
||||
return sendError(res, 'Config Get Error');
|
||||
}
|
||||
};
|
||||
|
||||
// 写入OneBot11配置
|
||||
export const OB11SetConfigHandler: RequestHandler = async (req, res) => {
|
||||
// 获取QQ登录状态
|
||||
const isLogin = WebUiDataRuntime.getQQLoginStatus();
|
||||
// 如果未登录,返回错误
|
||||
if (!isLogin) {
|
||||
return sendError(res, 'Not Login');
|
||||
}
|
||||
// 如果配置为空,返回错误
|
||||
if (isEmpty(req.body.config)) {
|
||||
return sendError(res, 'config is empty');
|
||||
}
|
||||
// 写入配置
|
||||
try {
|
||||
// 解析并加载配置
|
||||
const config = loadConfig(json5.parse(req.body.config)) as OneBotConfig;
|
||||
// 写入配置
|
||||
await WebUiDataRuntime.setOB11Config(config);
|
||||
return sendSuccess(res, null);
|
||||
} catch (e) {
|
||||
return sendError(res, 'Error: ' + e);
|
||||
}
|
||||
};
|
@@ -1,14 +0,0 @@
|
||||
import { RequestHandler } from 'express';
|
||||
import { RequestUtil } from '@/common/request';
|
||||
import { sendError, sendSuccess } from '../utils/response';
|
||||
|
||||
export const GetProxyHandler: RequestHandler = async (req, res) => {
|
||||
let { url } = req.query;
|
||||
if (url && typeof url === 'string') {
|
||||
url = decodeURIComponent(url);
|
||||
const responseText = await RequestUtil.HttpGetText(url);
|
||||
return sendSuccess(res, responseText);
|
||||
} else {
|
||||
return sendError(res, 'url参数不合法');
|
||||
}
|
||||
};
|
@@ -1,90 +0,0 @@
|
||||
import { RequestHandler } from 'express';
|
||||
|
||||
import { WebUiDataRuntime } from '@webapi/helper/Data';
|
||||
import { isEmpty } from '@webapi/utils/check';
|
||||
import { sendError, sendSuccess } from '@webapi/utils/response';
|
||||
import { WebUiConfig } from '@/webui';
|
||||
|
||||
// 获取QQ登录二维码
|
||||
export const QQGetQRcodeHandler: RequestHandler = async (_, res) => {
|
||||
// 判断是否已经登录
|
||||
if (WebUiDataRuntime.getQQLoginStatus()) {
|
||||
// 已经登录
|
||||
return sendError(res, 'QQ Is Logined');
|
||||
}
|
||||
// 获取二维码
|
||||
const qrcodeUrl = WebUiDataRuntime.getQQLoginQrcodeURL();
|
||||
// 判断二维码是否为空
|
||||
if (isEmpty(qrcodeUrl)) {
|
||||
return sendError(res, 'QRCode Get Error');
|
||||
}
|
||||
// 返回二维码URL
|
||||
const data = {
|
||||
qrcode: qrcodeUrl,
|
||||
};
|
||||
return sendSuccess(res, data);
|
||||
};
|
||||
|
||||
// 获取QQ登录状态
|
||||
export const QQCheckLoginStatusHandler: RequestHandler = async (_, res) => {
|
||||
const data = {
|
||||
isLogin: WebUiDataRuntime.getQQLoginStatus(),
|
||||
qrcodeurl: WebUiDataRuntime.getQQLoginQrcodeURL(),
|
||||
};
|
||||
return sendSuccess(res, data);
|
||||
};
|
||||
|
||||
// 快速登录
|
||||
export const QQSetQuickLoginHandler: RequestHandler = async (req, res) => {
|
||||
// 获取QQ号
|
||||
const { uin } = req.body;
|
||||
// 判断是否已经登录
|
||||
const isLogin = WebUiDataRuntime.getQQLoginStatus();
|
||||
if (isLogin) {
|
||||
return sendError(res, 'QQ Is Logined');
|
||||
}
|
||||
// 判断QQ号是否为空
|
||||
if (isEmpty(uin)) {
|
||||
return sendError(res, 'uin is empty');
|
||||
}
|
||||
|
||||
// 获取快速登录状态
|
||||
const { result, message } = await WebUiDataRuntime.requestQuickLogin(uin);
|
||||
if (!result) {
|
||||
return sendError(res, message);
|
||||
}
|
||||
//本来应该验证 但是http不宜这么搞 建议前端验证
|
||||
//isLogin = WebUiDataRuntime.getQQLoginStatus();
|
||||
return sendSuccess(res, null);
|
||||
};
|
||||
|
||||
// 获取快速登录列表
|
||||
export const QQGetQuickLoginListHandler: RequestHandler = async (_, res) => {
|
||||
const quickLoginList = WebUiDataRuntime.getQQQuickLoginList();
|
||||
return sendSuccess(res, quickLoginList);
|
||||
};
|
||||
|
||||
// 获取快速登录列表(新)
|
||||
export const QQGetLoginListNewHandler: RequestHandler = async (_, res) => {
|
||||
const newLoginList = WebUiDataRuntime.getQQNewLoginList();
|
||||
return sendSuccess(res, newLoginList);
|
||||
};
|
||||
|
||||
// 获取登录的QQ的信息
|
||||
export const getQQLoginInfoHandler: RequestHandler = async (_, res) => {
|
||||
const data = WebUiDataRuntime.getQQLoginInfo();
|
||||
return sendSuccess(res, data);
|
||||
};
|
||||
|
||||
// 获取自动登录QQ账号
|
||||
export const getAutoLoginAccountHandler: RequestHandler = async (_, res) => {
|
||||
const data = WebUiConfig.getAutoLoginAccount();
|
||||
return sendSuccess(res, data);
|
||||
};
|
||||
|
||||
// 设置自动登录QQ账号
|
||||
export const setAutoLoginAccountHandler: RequestHandler = async (req, res) => {
|
||||
const { uin } = req.body;
|
||||
await WebUiConfig.UpdateAutoLoginAccount(uin);
|
||||
return sendSuccess(res, null);
|
||||
};
|
@@ -1,19 +0,0 @@
|
||||
import { RequestHandler } from 'express';
|
||||
import { SystemStatus, statusHelperSubscription } from '@/core/helper/status';
|
||||
|
||||
export const StatusRealTimeHandler: RequestHandler = async (req, res) => {
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
const sendStatus = (status: SystemStatus) => {
|
||||
try{
|
||||
res.write(`data: ${JSON.stringify(status)}\n\n`);
|
||||
} catch (e) {
|
||||
console.error(`An error occurred when writing sendStatus data to client: ${e}`);
|
||||
}
|
||||
};
|
||||
statusHelperSubscription.on('statusUpdate', sendStatus);
|
||||
req.on('close', () => {
|
||||
statusHelperSubscription.off('statusUpdate', sendStatus);
|
||||
res.end();
|
||||
});
|
||||
};
|
@@ -1,13 +0,0 @@
|
||||
export enum HttpStatusCode {
|
||||
OK = 200,
|
||||
BadRequest = 400,
|
||||
Unauthorized = 401,
|
||||
Forbidden = 403,
|
||||
NotFound = 404,
|
||||
InternalServerError = 500,
|
||||
}
|
||||
|
||||
export enum ResponseCode {
|
||||
Success = 0,
|
||||
Error = -1,
|
||||
}
|
@@ -1,121 +0,0 @@
|
||||
import type { LoginRuntimeType } from '../types/data';
|
||||
import packageJson from '../../../../package.json';
|
||||
import store from '@/common/store';
|
||||
|
||||
const LoginRuntime: LoginRuntimeType = {
|
||||
LoginCurrentTime: Date.now(),
|
||||
LoginCurrentRate: 0,
|
||||
QQLoginStatus: false, //已实现 但太傻了 得去那边注册个回调刷新
|
||||
QQQRCodeURL: '',
|
||||
QQLoginUin: '',
|
||||
QQLoginInfo: {
|
||||
uid: '',
|
||||
uin: '',
|
||||
nick: '',
|
||||
},
|
||||
QQVersion: 'unknown',
|
||||
NapCatHelper: {
|
||||
onOB11ConfigChanged: async () => {
|
||||
return;
|
||||
},
|
||||
onQuickLoginRequested: async () => {
|
||||
return { result: false, message: '' };
|
||||
},
|
||||
QQLoginList: [],
|
||||
NewQQLoginList: [],
|
||||
},
|
||||
packageJson: packageJson,
|
||||
};
|
||||
|
||||
export const WebUiDataRuntime = {
|
||||
checkLoginRate(ip: string, RateLimit: number): boolean {
|
||||
const key = `login_rate:${ip}`;
|
||||
const count = store.get<number>(key) || 0;
|
||||
|
||||
if (count === 0) {
|
||||
// 第一次访问,设置计数器为1,并设置60秒过期
|
||||
store.set(key, 1, 60);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (count >= RateLimit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
store.incr(key);
|
||||
return true;
|
||||
},
|
||||
|
||||
getQQLoginStatus(): LoginRuntimeType['QQLoginStatus'] {
|
||||
return LoginRuntime.QQLoginStatus;
|
||||
},
|
||||
|
||||
setQQLoginStatus(status: LoginRuntimeType['QQLoginStatus']): void {
|
||||
LoginRuntime.QQLoginStatus = status;
|
||||
},
|
||||
|
||||
setQQLoginQrcodeURL(url: LoginRuntimeType['QQQRCodeURL']): void {
|
||||
LoginRuntime.QQQRCodeURL = url;
|
||||
},
|
||||
|
||||
getQQLoginQrcodeURL(): LoginRuntimeType['QQQRCodeURL'] {
|
||||
return LoginRuntime.QQQRCodeURL;
|
||||
},
|
||||
|
||||
setQQLoginInfo(info: LoginRuntimeType['QQLoginInfo']): void {
|
||||
LoginRuntime.QQLoginInfo = info;
|
||||
LoginRuntime.QQLoginUin = info.uin.toString();
|
||||
},
|
||||
|
||||
getQQLoginInfo(): LoginRuntimeType['QQLoginInfo'] {
|
||||
return LoginRuntime.QQLoginInfo;
|
||||
},
|
||||
|
||||
getQQLoginUin(): LoginRuntimeType['QQLoginUin'] {
|
||||
return LoginRuntime.QQLoginUin;
|
||||
},
|
||||
|
||||
getQQQuickLoginList(): LoginRuntimeType['NapCatHelper']['QQLoginList'] {
|
||||
return LoginRuntime.NapCatHelper.QQLoginList;
|
||||
},
|
||||
|
||||
setQQQuickLoginList(list: LoginRuntimeType['NapCatHelper']['QQLoginList']): void {
|
||||
LoginRuntime.NapCatHelper.QQLoginList = list;
|
||||
},
|
||||
|
||||
getQQNewLoginList(): LoginRuntimeType['NapCatHelper']['NewQQLoginList'] {
|
||||
return LoginRuntime.NapCatHelper.NewQQLoginList;
|
||||
},
|
||||
|
||||
setQQNewLoginList(list: LoginRuntimeType['NapCatHelper']['NewQQLoginList']): void {
|
||||
LoginRuntime.NapCatHelper.NewQQLoginList = list;
|
||||
},
|
||||
|
||||
setQuickLoginCall(func: LoginRuntimeType['NapCatHelper']['onQuickLoginRequested']): void {
|
||||
LoginRuntime.NapCatHelper.onQuickLoginRequested = func;
|
||||
},
|
||||
|
||||
requestQuickLogin: function (uin) {
|
||||
return LoginRuntime.NapCatHelper.onQuickLoginRequested(uin);
|
||||
} as LoginRuntimeType['NapCatHelper']['onQuickLoginRequested'],
|
||||
|
||||
setOnOB11ConfigChanged(func: LoginRuntimeType['NapCatHelper']['onOB11ConfigChanged']): void {
|
||||
LoginRuntime.NapCatHelper.onOB11ConfigChanged = func;
|
||||
},
|
||||
|
||||
setOB11Config: function (ob11) {
|
||||
return LoginRuntime.NapCatHelper.onOB11ConfigChanged(ob11);
|
||||
} as LoginRuntimeType['NapCatHelper']['onOB11ConfigChanged'],
|
||||
|
||||
getPackageJson() {
|
||||
return LoginRuntime.packageJson;
|
||||
},
|
||||
|
||||
setQQVersion(version: string) {
|
||||
LoginRuntime.QQVersion = version;
|
||||
},
|
||||
|
||||
getQQVersion() {
|
||||
return LoginRuntime.QQVersion;
|
||||
},
|
||||
};
|
@@ -1,88 +0,0 @@
|
||||
import crypto from 'crypto';
|
||||
import store from '@/common/store';
|
||||
export class AuthHelper {
|
||||
private static readonly secretKey = Math.random().toString(36).slice(2);
|
||||
|
||||
/**
|
||||
* 签名凭证方法。
|
||||
* @param token 待签名的凭证字符串。
|
||||
* @returns 签名后的凭证对象。
|
||||
*/
|
||||
public static signCredential(token: string): WebUiCredentialJson {
|
||||
const innerJson: WebUiCredentialInnerJson = {
|
||||
CreatedTime: Date.now(),
|
||||
TokenEncoded: token,
|
||||
};
|
||||
const jsonString = JSON.stringify(innerJson);
|
||||
const hmac = crypto.createHmac('sha256', AuthHelper.secretKey).update(jsonString, 'utf8').digest('hex');
|
||||
return { Data: innerJson, Hmac: hmac };
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查凭证是否被篡改的方法。
|
||||
* @param credentialJson 凭证的JSON对象。
|
||||
* @returns 布尔值,表示凭证是否有效。
|
||||
*/
|
||||
public static checkCredential(credentialJson: WebUiCredentialJson): boolean {
|
||||
try {
|
||||
const jsonString = JSON.stringify(credentialJson.Data);
|
||||
const calculatedHmac = crypto
|
||||
.createHmac('sha256', AuthHelper.secretKey)
|
||||
.update(jsonString, 'utf8')
|
||||
.digest('hex');
|
||||
return calculatedHmac === credentialJson.Hmac;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证凭证在1小时内有效且token与原始token相同。
|
||||
* @param token 待验证的原始token。
|
||||
* @param credentialJson 已签名的凭证JSON对象。
|
||||
* @returns 布尔值,表示凭证是否有效且token匹配。
|
||||
*/
|
||||
public static validateCredentialWithinOneHour(token: string, credentialJson: WebUiCredentialJson): boolean {
|
||||
// 首先检查凭证是否被篡改
|
||||
const isValid = AuthHelper.checkCredential(credentialJson);
|
||||
if (!isValid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查凭证是否在黑名单中
|
||||
if (AuthHelper.isCredentialRevoked(credentialJson)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentTime = Date.now() / 1000;
|
||||
const createdTime = credentialJson.Data.CreatedTime;
|
||||
const timeDifference = currentTime - createdTime;
|
||||
|
||||
return timeDifference <= 3600 && credentialJson.Data.TokenEncoded === token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销指定的Token凭证
|
||||
* @param credentialJson 凭证JSON对象
|
||||
* @returns void
|
||||
*/
|
||||
public static revokeCredential(credentialJson: WebUiCredentialJson): void {
|
||||
const jsonString = JSON.stringify(credentialJson.Data);
|
||||
const hmac = crypto.createHmac('sha256', AuthHelper.secretKey).update(jsonString, 'utf8').digest('hex');
|
||||
|
||||
// 将已注销的凭证添加到黑名单中,有效期1小时
|
||||
store.set(`revoked:${hmac}`, true, 3600);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查凭证是否已被注销
|
||||
* @param credentialJson 凭证JSON对象
|
||||
* @returns 布尔值,表示凭证是否已被注销
|
||||
*/
|
||||
public static isCredentialRevoked(credentialJson: WebUiCredentialJson): boolean {
|
||||
const jsonString = JSON.stringify(credentialJson.Data);
|
||||
const hmac = crypto.createHmac('sha256', AuthHelper.secretKey).update(jsonString, 'utf8').digest('hex');
|
||||
|
||||
return store.exists(`revoked:${hmac}`) > 0;
|
||||
}
|
||||
}
|
@@ -1,179 +0,0 @@
|
||||
import { webUiPathWrapper } from '@/webui';
|
||||
import { Type, Static } from '@sinclair/typebox';
|
||||
import Ajv from 'ajv';
|
||||
import fs, { constants } from 'node:fs/promises';
|
||||
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
import { deepMerge } from '../utils/object';
|
||||
import { themeType } from '../types/theme';
|
||||
|
||||
// 限制尝试端口的次数,避免死循环
|
||||
|
||||
// 定义配置的类型
|
||||
const WebUiConfigSchema = Type.Object({
|
||||
host: Type.String({ default: '0.0.0.0' }),
|
||||
port: Type.Number({ default: 6099 }),
|
||||
token: Type.String({ default: 'napcat' }),
|
||||
loginRate: Type.Number({ default: 10 }),
|
||||
autoLoginAccount: Type.String({ default: '' }),
|
||||
theme: themeType,
|
||||
});
|
||||
|
||||
export type WebUiConfigType = Static<typeof WebUiConfigSchema>;
|
||||
|
||||
// 读取当前目录下名为 webui.json 的配置文件,如果不存在则创建初始化配置文件
|
||||
export class WebUiConfigWrapper {
|
||||
WebUiConfigData: WebUiConfigType | undefined = undefined;
|
||||
|
||||
private validateAndApplyDefaults(config: Partial<WebUiConfigType>): WebUiConfigType {
|
||||
new Ajv({ coerceTypes: true, useDefaults: true }).compile(WebUiConfigSchema)(config);
|
||||
return config as WebUiConfigType;
|
||||
}
|
||||
|
||||
private async ensureConfigFileExists(configPath: string): Promise<void> {
|
||||
const configExists = await fs
|
||||
.access(configPath, constants.F_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
if (!configExists) {
|
||||
await fs.writeFile(configPath, JSON.stringify(this.validateAndApplyDefaults({}), null, 4));
|
||||
}
|
||||
}
|
||||
|
||||
private async readAndValidateConfig(configPath: string): Promise<WebUiConfigType> {
|
||||
const fileContent = await fs.readFile(configPath, 'utf-8');
|
||||
return this.validateAndApplyDefaults(JSON.parse(fileContent));
|
||||
}
|
||||
|
||||
private async writeConfig(configPath: string, config: WebUiConfigType): Promise<void> {
|
||||
const hasWritePermission = await fs
|
||||
.access(configPath, constants.W_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
if (hasWritePermission) {
|
||||
await fs.writeFile(configPath, JSON.stringify(config, null, 4));
|
||||
} else {
|
||||
console.warn(`文件: ${configPath} 没有写入权限, 配置的更改部分可能会在重启后还原.`);
|
||||
}
|
||||
}
|
||||
|
||||
async GetWebUIConfig(): Promise<WebUiConfigType> {
|
||||
if (this.WebUiConfigData) {
|
||||
return this.WebUiConfigData;
|
||||
}
|
||||
try {
|
||||
const configPath = resolve(webUiPathWrapper.configPath, './webui.json');
|
||||
await this.ensureConfigFileExists(configPath);
|
||||
const parsedConfig = await this.readAndValidateConfig(configPath);
|
||||
this.WebUiConfigData = parsedConfig;
|
||||
return this.WebUiConfigData;
|
||||
} catch (e) {
|
||||
console.log('读取配置文件失败', e);
|
||||
return this.validateAndApplyDefaults({});
|
||||
}
|
||||
}
|
||||
|
||||
async UpdateWebUIConfig(newConfig: Partial<WebUiConfigType>): Promise<void> {
|
||||
const configPath = resolve(webUiPathWrapper.configPath, './webui.json');
|
||||
const currentConfig = await this.GetWebUIConfig();
|
||||
const mergedConfig = deepMerge({ ...currentConfig }, newConfig);
|
||||
const updatedConfig = this.validateAndApplyDefaults(mergedConfig);
|
||||
await this.writeConfig(configPath, updatedConfig);
|
||||
this.WebUiConfigData = updatedConfig;
|
||||
}
|
||||
|
||||
async UpdateToken(oldToken: string, newToken: string): Promise<void> {
|
||||
const currentConfig = await this.GetWebUIConfig();
|
||||
if (currentConfig.token !== oldToken) {
|
||||
throw new Error('旧 token 不匹配');
|
||||
}
|
||||
await this.UpdateWebUIConfig({ token: newToken });
|
||||
}
|
||||
|
||||
// 获取日志文件夹路径
|
||||
async GetLogsPath(): Promise<string> {
|
||||
return resolve(webUiPathWrapper.logsPath);
|
||||
}
|
||||
|
||||
// 获取日志列表
|
||||
async GetLogsList(): Promise<string[]> {
|
||||
const logsPath = resolve(webUiPathWrapper.logsPath);
|
||||
const logsExist = await fs
|
||||
.access(logsPath, constants.F_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
if (logsExist) {
|
||||
return (await fs.readdir(logsPath))
|
||||
.filter((file) => file.endsWith('.log'))
|
||||
.map((file) => file.replace('.log', ''));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
// 获取指定日志文件内容
|
||||
async GetLogContent(filename: string): Promise<string> {
|
||||
const logPath = resolve(webUiPathWrapper.logsPath, `${filename}.log`);
|
||||
const logExists = await fs
|
||||
.access(logPath, constants.R_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
if (logExists) {
|
||||
return await fs.readFile(logPath, 'utf-8');
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
// 获取字体文件夹内的字体列表
|
||||
async GetFontList(): Promise<string[]> {
|
||||
const fontsPath = resolve(webUiPathWrapper.configPath, './fonts');
|
||||
const fontsExist = await fs
|
||||
.access(fontsPath, constants.F_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
if (fontsExist) {
|
||||
return (await fs.readdir(fontsPath)).filter((file) => file.endsWith('.ttf'));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
// 判断字体是否存在(webui.woff)
|
||||
async CheckWebUIFontExist(): Promise<boolean> {
|
||||
const fontsPath = resolve(webUiPathWrapper.configPath, './fonts');
|
||||
return await fs
|
||||
.access(resolve(fontsPath, './webui.woff'), constants.F_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
}
|
||||
|
||||
// 获取webui字体文件路径
|
||||
GetWebUIFontPath(): string {
|
||||
return resolve(webUiPathWrapper.configPath, './fonts/webui.woff');
|
||||
}
|
||||
|
||||
getAutoLoginAccount(): string | undefined {
|
||||
return this.WebUiConfigData?.autoLoginAccount;
|
||||
}
|
||||
|
||||
// 获取自动登录账号
|
||||
async GetAutoLoginAccount(): Promise<string> {
|
||||
return (await this.GetWebUIConfig()).autoLoginAccount;
|
||||
}
|
||||
|
||||
// 更新自动登录账号
|
||||
async UpdateAutoLoginAccount(uin: string): Promise<void> {
|
||||
await this.UpdateWebUIConfig({ autoLoginAccount: uin });
|
||||
}
|
||||
|
||||
// 获取主题内容
|
||||
async GetTheme(): Promise<WebUiConfigType['theme']> {
|
||||
const config = await this.GetWebUIConfig();
|
||||
|
||||
return config.theme;
|
||||
}
|
||||
|
||||
// 更新主题内容
|
||||
async UpdateTheme(theme: WebUiConfigType['theme']): Promise<void> {
|
||||
await this.UpdateWebUIConfig({ theme: theme });
|
||||
}
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
|
||||
import { WebUiConfig } from '@/webui';
|
||||
|
||||
import { AuthHelper } from '@webapi/helper/SignToken';
|
||||
import { sendError } from '@webapi/utils/response';
|
||||
|
||||
// 鉴权中间件
|
||||
export async function auth(req: Request, res: Response, next: NextFunction) {
|
||||
// 判断当前url是否为/login 如果是跳过鉴权
|
||||
if (req.url == '/auth/login') {
|
||||
return next();
|
||||
}
|
||||
|
||||
// 判断是否有Authorization头
|
||||
if (req.headers?.authorization) {
|
||||
// 切割参数以获取token
|
||||
const authorization = req.headers.authorization.split(' ');
|
||||
// 当Bearer后面没有参数时
|
||||
if (authorization.length < 2) {
|
||||
return sendError(res, 'Unauthorized');
|
||||
}
|
||||
// 获取token
|
||||
const token = authorization[1];
|
||||
// 解析token
|
||||
let Credential: WebUiCredentialJson;
|
||||
try {
|
||||
Credential = JSON.parse(Buffer.from(token, 'base64').toString('utf-8'));
|
||||
} catch (e) {
|
||||
return sendError(res, 'Unauthorized');
|
||||
}
|
||||
// 获取配置
|
||||
const config = await WebUiConfig.GetWebUIConfig();
|
||||
// 验证凭证在1小时内有效且token与原始token相同
|
||||
const credentialJson = AuthHelper.validateCredentialWithinOneHour(config.token, Credential);
|
||||
if (credentialJson) {
|
||||
// 通过验证
|
||||
return next();
|
||||
}
|
||||
// 验证失败
|
||||
return sendError(res, 'Unauthorized');
|
||||
}
|
||||
|
||||
// 没有Authorization头
|
||||
return sendError(res, 'Unauthorized');
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
import type { RequestHandler } from 'express';
|
||||
|
||||
// CORS 中间件,跨域用
|
||||
export const cors: RequestHandler = (req, res, next) => {
|
||||
const origin = req.headers.origin || '*';
|
||||
res.header('Access-Control-Allow-Origin', origin);
|
||||
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
|
||||
res.header('Access-Control-Allow-Credentials', 'true');
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.sendStatus(204);
|
||||
return;
|
||||
}
|
||||
next();
|
||||
};
|
@@ -1,15 +0,0 @@
|
||||
import { Router } from 'express';
|
||||
import { GetThemeConfigHandler, PackageInfoHandler, QQVersionHandler, SetThemeConfigHandler } from '../api/BaseInfo';
|
||||
import { StatusRealTimeHandler } from '@webapi/api/Status';
|
||||
import { GetProxyHandler } from '../api/Proxy';
|
||||
|
||||
const router = Router();
|
||||
// router: 获取nc的package.json信息
|
||||
router.get('/QQVersion', QQVersionHandler);
|
||||
router.get('/PackageInfo', PackageInfoHandler);
|
||||
router.get('/GetSysStatusRealTime', StatusRealTimeHandler);
|
||||
router.get('/proxy', GetProxyHandler);
|
||||
router.get('/Theme', GetThemeConfigHandler);
|
||||
router.post('/SetTheme', SetThemeConfigHandler);
|
||||
|
||||
export { router as BaseRouter };
|
@@ -1,49 +0,0 @@
|
||||
import { Router } from 'express';
|
||||
import rateLimit from 'express-rate-limit';
|
||||
import {
|
||||
ListFilesHandler,
|
||||
CreateDirHandler,
|
||||
DeleteHandler,
|
||||
ReadFileHandler,
|
||||
WriteFileHandler,
|
||||
CreateFileHandler,
|
||||
BatchDeleteHandler, // 添加这一行
|
||||
RenameHandler,
|
||||
MoveHandler,
|
||||
BatchMoveHandler,
|
||||
DownloadHandler,
|
||||
BatchDownloadHandler, // 新增下载处理方法
|
||||
UploadHandler,
|
||||
UploadWebUIFontHandler,
|
||||
DeleteWebUIFontHandler, // 添加上传处理器
|
||||
} from '../api/File';
|
||||
|
||||
const router = Router();
|
||||
|
||||
const apiLimiter = rateLimit({
|
||||
windowMs: 1 * 60 * 1000, // 1分钟内
|
||||
max: 60, // 最大60个请求
|
||||
validate: {
|
||||
xForwardedForHeader: false,
|
||||
},
|
||||
});
|
||||
|
||||
router.use(apiLimiter);
|
||||
|
||||
router.get('/list', ListFilesHandler);
|
||||
router.post('/mkdir', CreateDirHandler);
|
||||
router.post('/delete', DeleteHandler);
|
||||
router.get('/read', ReadFileHandler);
|
||||
router.post('/write', WriteFileHandler);
|
||||
router.post('/create', CreateFileHandler);
|
||||
router.post('/batchDelete', BatchDeleteHandler);
|
||||
router.post('/rename', RenameHandler);
|
||||
router.post('/move', MoveHandler);
|
||||
router.post('/batchMove', BatchMoveHandler);
|
||||
router.post('/download', DownloadHandler);
|
||||
router.post('/batchDownload', BatchDownloadHandler);
|
||||
router.post('/upload', UploadHandler);
|
||||
|
||||
router.post('/font/upload/webui', UploadWebUIFontHandler);
|
||||
router.post('/font/delete/webui', DeleteWebUIFontHandler);
|
||||
export { router as FileRouter };
|
@@ -1,23 +0,0 @@
|
||||
import { Router } from 'express';
|
||||
import {
|
||||
LogHandler,
|
||||
LogListHandler,
|
||||
LogRealTimeHandler,
|
||||
CreateTerminalHandler,
|
||||
GetTerminalListHandler,
|
||||
CloseTerminalHandler,
|
||||
} from '../api/Log';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// 日志相关路由
|
||||
router.get('/GetLog', LogHandler);
|
||||
router.get('/GetLogList', LogListHandler);
|
||||
router.get('/GetLogRealTime', LogRealTimeHandler);
|
||||
|
||||
// 终端相关路由
|
||||
router.get('/terminal/list', GetTerminalListHandler);
|
||||
router.post('/terminal/create', CreateTerminalHandler);
|
||||
router.post('/terminal/:id/close', CloseTerminalHandler);
|
||||
|
||||
export { router as LogRouter };
|
@@ -1,11 +0,0 @@
|
||||
import { Router } from 'express';
|
||||
|
||||
import { OB11GetConfigHandler, OB11SetConfigHandler } from '@webapi/api/OB11Config';
|
||||
|
||||
const router = Router();
|
||||
// router:读取配置
|
||||
router.post('/GetConfig', OB11GetConfigHandler);
|
||||
// router:写入配置
|
||||
router.post('/SetConfig', OB11SetConfigHandler);
|
||||
|
||||
export { router as OB11ConfigRouter };
|
@@ -1,32 +0,0 @@
|
||||
import { Router } from 'express';
|
||||
|
||||
import {
|
||||
QQCheckLoginStatusHandler,
|
||||
QQGetQRcodeHandler,
|
||||
QQGetQuickLoginListHandler,
|
||||
QQSetQuickLoginHandler,
|
||||
QQGetLoginListNewHandler,
|
||||
getQQLoginInfoHandler,
|
||||
getAutoLoginAccountHandler,
|
||||
setAutoLoginAccountHandler,
|
||||
} from '@webapi/api/QQLogin';
|
||||
|
||||
const router = Router();
|
||||
// router:获取快速登录列表
|
||||
router.all('/GetQuickLoginList', QQGetQuickLoginListHandler);
|
||||
// router:获取快速登录列表(新)
|
||||
router.all('/GetQuickLoginListNew', QQGetLoginListNewHandler);
|
||||
// router:检查QQ登录状态
|
||||
router.post('/CheckLoginStatus', QQCheckLoginStatusHandler);
|
||||
// router:获取QQ登录二维码
|
||||
router.post('/GetQQLoginQrcode', QQGetQRcodeHandler);
|
||||
// router:设置QQ快速登录
|
||||
router.post('/SetQuickLogin', QQSetQuickLoginHandler);
|
||||
// router:获取QQ登录信息
|
||||
router.post('/GetQQLoginInfo', getQQLoginInfoHandler);
|
||||
// router:获取快速登录QQ账号
|
||||
router.post('/GetQuickLoginQQ', getAutoLoginAccountHandler);
|
||||
// router:设置自动登录QQ账号
|
||||
router.post('/SetQuickLoginQQ', setAutoLoginAccountHandler);
|
||||
|
||||
export { router as QQLoginRouter };
|
@@ -1,23 +0,0 @@
|
||||
import { Router } from 'express';
|
||||
|
||||
import {
|
||||
CheckDefaultTokenHandler,
|
||||
checkHandler,
|
||||
LoginHandler,
|
||||
LogoutHandler,
|
||||
UpdateTokenHandler,
|
||||
} from '@webapi/api/Auth';
|
||||
|
||||
const router = Router();
|
||||
// router:登录
|
||||
router.post('/login', LoginHandler);
|
||||
// router:检查登录状态
|
||||
router.post('/check', checkHandler);
|
||||
// router:注销
|
||||
router.post('/logout', LogoutHandler);
|
||||
// router:更新token
|
||||
router.post('/update_token', UpdateTokenHandler);
|
||||
// router:检查默认token
|
||||
router.get('/check_using_default_token', CheckDefaultTokenHandler);
|
||||
|
||||
export { router as AuthRouter };
|
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
* @file 所有路由的入口文件
|
||||
*/
|
||||
|
||||
import { Router } from 'express';
|
||||
|
||||
import { OB11ConfigRouter } from '@webapi/router/OB11Config';
|
||||
import { auth } from '@webapi/middleware/auth';
|
||||
import { sendSuccess } from '@webapi/utils/response';
|
||||
|
||||
import { QQLoginRouter } from '@webapi/router/QQLogin';
|
||||
import { AuthRouter } from '@webapi/router/auth';
|
||||
import { LogRouter } from '@webapi/router/Log';
|
||||
import { BaseRouter } from '@webapi/router/Base';
|
||||
import { FileRouter } from './File';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// 鉴权中间件
|
||||
router.use(auth);
|
||||
|
||||
// router:测试用
|
||||
router.all('/test', (_, res) => {
|
||||
return sendSuccess(res);
|
||||
});
|
||||
// router:基础信息相关路由
|
||||
router.use('/base', BaseRouter);
|
||||
// router:WebUI登录相关路由
|
||||
router.use('/auth', AuthRouter);
|
||||
// router:QQ登录相关路由
|
||||
router.use('/QQLogin', QQLoginRouter);
|
||||
// router:OB11配置相关路由
|
||||
router.use('/OB11Config', OB11ConfigRouter);
|
||||
// router:日志相关路由
|
||||
router.use('/Log', LogRouter);
|
||||
// file:文件相关路由
|
||||
router.use('/File', FileRouter);
|
||||
|
||||
export { router as ALLRouter };
|
@@ -1,21 +0,0 @@
|
||||
import path from 'path';
|
||||
|
||||
Object.defineProperty(global, '__dirname', {
|
||||
get() {
|
||||
const err = new Error();
|
||||
const stack = err.stack?.split('\n') || [];
|
||||
let callerFile = '';
|
||||
// 遍历错误堆栈,跳过当前文件所在行
|
||||
// 注意:堆栈格式可能不同,请根据实际环境调整索引及正则表达式
|
||||
for (const line of stack) {
|
||||
const match = line.match(/\((.*):\d+:\d+\)/);
|
||||
if (match) {
|
||||
callerFile = match[1];
|
||||
if (!callerFile.includes('init-dynamic-dirname.ts')) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return callerFile ? path.dirname(callerFile) : '';
|
||||
},
|
||||
});
|
@@ -1,183 +0,0 @@
|
||||
import './init-dynamic-dirname';
|
||||
import { WebUiConfig } from '@/webui';
|
||||
import { AuthHelper } from '../helper/SignToken';
|
||||
import { LogWrapper } from '@/common/log';
|
||||
import { WebSocket, WebSocketServer } from 'ws';
|
||||
import os from 'os';
|
||||
import { IPty, spawn as ptySpawn } from '@/pty';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
interface TerminalInstance {
|
||||
pty: IPty; // 改用 PTY 实例
|
||||
lastAccess: number;
|
||||
sockets: Set<WebSocket>;
|
||||
// 新增标识,用于防止重复关闭
|
||||
isClosing: boolean;
|
||||
// 新增:存储终端历史输出
|
||||
buffer: string;
|
||||
}
|
||||
|
||||
class TerminalManager {
|
||||
private terminals: Map<string, TerminalInstance> = new Map();
|
||||
private wss: WebSocketServer | null = null;
|
||||
|
||||
initialize(req: any, socket: any, head: any, logger?: LogWrapper) {
|
||||
logger?.log('[NapCat] [WebUi] terminal websocket initialized');
|
||||
this.wss = new WebSocketServer({
|
||||
noServer: true,
|
||||
verifyClient: async (info, cb) => {
|
||||
// 验证 token
|
||||
const url = new URL(info.req.url || '', 'ws://localhost');
|
||||
const token = url.searchParams.get('token');
|
||||
const terminalId = url.searchParams.get('id');
|
||||
|
||||
if (!token || !terminalId) {
|
||||
cb(false, 401, 'Unauthorized');
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析token
|
||||
let Credential: WebUiCredentialJson;
|
||||
try {
|
||||
Credential = JSON.parse(Buffer.from(token, 'base64').toString('utf-8'));
|
||||
} catch (e) {
|
||||
cb(false, 401, 'Unauthorized');
|
||||
return;
|
||||
}
|
||||
const config = await WebUiConfig.GetWebUIConfig();
|
||||
const validate = AuthHelper.validateCredentialWithinOneHour(config.token, Credential);
|
||||
if (!validate) {
|
||||
cb(false, 401, 'Unauthorized');
|
||||
return;
|
||||
}
|
||||
cb(true);
|
||||
},
|
||||
});
|
||||
this.wss.handleUpgrade(req, socket, head, (ws) => {
|
||||
this.wss?.emit('connection', ws, req);
|
||||
});
|
||||
this.wss.on('connection', async (ws, req) => {
|
||||
logger?.log('建立终端连接');
|
||||
try {
|
||||
const url = new URL(req.url || '', 'ws://localhost');
|
||||
const terminalId = url.searchParams.get('id')!;
|
||||
|
||||
const instance = this.terminals.get(terminalId);
|
||||
|
||||
if (!instance) {
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
|
||||
instance.sockets.add(ws);
|
||||
instance.lastAccess = Date.now();
|
||||
|
||||
// 新增:发送当前终端内容给新连接
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: 'output', data: instance.buffer }));
|
||||
}
|
||||
|
||||
ws.on('message', (data) => {
|
||||
if (instance) {
|
||||
const result = JSON.parse(data.toString());
|
||||
if (result.type === 'input') {
|
||||
instance.pty.write(result.data);
|
||||
}
|
||||
// 新增:处理 resize 消息
|
||||
if (result.type === 'resize') {
|
||||
instance.pty.resize(result.cols, result.rows);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
instance.sockets.delete(ws);
|
||||
if (instance.sockets.size === 0 && !instance.isClosing) {
|
||||
instance.isClosing = true;
|
||||
if (os.platform() === 'win32') {
|
||||
process.kill(instance.pty.pid);
|
||||
} else {
|
||||
instance.pty.kill();
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('WebSocket authentication failed:', err);
|
||||
ws.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 修改:新增 cols 和 rows 参数,同步 xterm 尺寸,防止错位
|
||||
createTerminal(cols: number, rows: number) {
|
||||
const id = randomUUID();
|
||||
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
|
||||
const pty = ptySpawn(shell, [], {
|
||||
name: 'xterm-256color',
|
||||
cols, // 使用客户端传入的 cols
|
||||
rows, // 使用客户端传入的 rows
|
||||
cwd: process.cwd(),
|
||||
env: {
|
||||
...process.env,
|
||||
LANG: os.platform() === 'win32' ? 'chcp 65001' : 'zh_CN.UTF-8',
|
||||
TERM: 'xterm-256color',
|
||||
},
|
||||
});
|
||||
|
||||
const instance: TerminalInstance = {
|
||||
pty,
|
||||
lastAccess: Date.now(),
|
||||
sockets: new Set(),
|
||||
isClosing: false,
|
||||
buffer: '', // 初始化终端内容缓存
|
||||
};
|
||||
|
||||
pty.onData((data: any) => {
|
||||
// 追加数据到 buffer
|
||||
instance.buffer += data;
|
||||
// 发送数据给已连接的 websocket
|
||||
instance.sockets.forEach((ws) => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: 'output', data }));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
pty.onExit(() => {
|
||||
this.closeTerminal(id);
|
||||
});
|
||||
|
||||
this.terminals.set(id, instance);
|
||||
// 返回生成的 id 及对应实例,方便后续通知客户端使用该 id
|
||||
return { id, instance };
|
||||
}
|
||||
|
||||
closeTerminal(id: string) {
|
||||
const instance = this.terminals.get(id);
|
||||
if (instance) {
|
||||
if (!instance.isClosing) {
|
||||
instance.isClosing = true;
|
||||
if (os.platform() === 'win32') {
|
||||
process.kill(instance.pty.pid);
|
||||
} else {
|
||||
instance.pty.kill();
|
||||
}
|
||||
}
|
||||
instance.sockets.forEach((ws) => ws.close());
|
||||
this.terminals.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
getTerminal(id: string) {
|
||||
return this.terminals.get(id);
|
||||
}
|
||||
|
||||
getTerminalList() {
|
||||
return Array.from(this.terminals.keys()).map((id) => ({
|
||||
id,
|
||||
lastAccess: this.terminals.get(id)!.lastAccess,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export const terminalManager = new TerminalManager();
|
6
src/webui/src/types/config.d.ts
vendored
6
src/webui/src/types/config.d.ts
vendored
@@ -1,6 +0,0 @@
|
||||
interface WebUiConfigType {
|
||||
host: string;
|
||||
port: number;
|
||||
token: string;
|
||||
loginRate: number;
|
||||
}
|
19
src/webui/src/types/data.d.ts
vendored
19
src/webui/src/types/data.d.ts
vendored
@@ -1,19 +0,0 @@
|
||||
import type { LoginListItem, SelfInfo } from '@/core';
|
||||
import type { OneBotConfig } from '@/onebot/config/config';
|
||||
|
||||
interface LoginRuntimeType {
|
||||
LoginCurrentTime: number;
|
||||
LoginCurrentRate: number;
|
||||
QQLoginStatus: boolean;
|
||||
QQQRCodeURL: string;
|
||||
QQLoginUin: string;
|
||||
QQLoginInfo: SelfInfo;
|
||||
QQVersion: string;
|
||||
NapCatHelper: {
|
||||
onQuickLoginRequested: (uin: string) => Promise<{ result: boolean; message: string }>;
|
||||
onOB11ConfigChanged: (ob11: OneBotConfig) => Promise<void>;
|
||||
QQLoginList: string[];
|
||||
NewQQLoginList: LoginListItem[];
|
||||
};
|
||||
packageJson: object;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user