Comments (9)
看起来很麻烦 你是什么需要这个需求呢
from napcatqq.
远端接口需要响应部分事件(加群等),但是不适合长连接,全post过去无效请求太多
允许自己写个中间件过滤请求也行
--filter=filter.js
然后filter.js是一个module,export filter: (body) => {return BOOL}
就足够好用了
from napcatqq.
考虑正反ws呗 全双工 效率高
from napcatqq.
远端服务器是非常老的php鉴权用,所以真不合适ws
from napcatqq.
(你要不考虑自己用py之类的中转一下
from napcatqq.
diff --git a/src/onebot11/config.ts b/src/onebot11/config.ts
index d113755..5153842 100644
--- a/src/onebot11/config.ts
+++ b/src/onebot11/config.ts
@@ -64,6 +64,22 @@ class Config extends ConfigBase<OB11Config> implements OB11Config {
musicSignUrl = "";
reportSelfMessage = false;
token = "";
+ filter: any = (...args: any) => {return true};
+
+ constructor() {
+ super();
+ const filterPath = path.join(this.getConfigDir(), 'filter.js');
+ if (fs.existsSync(filterPath)) {
+ import(filterPath).then(filterModule => {
+ this.filter = filterModule.default
+ logDebug('过滤器已加载')
+ }).catch(() => {
+ logError('过滤器加载失败')
+ })
+ } else {
+ logDebug('过滤器未加载')
+ }
+ }
getConfigPath() {
return path.join(this.getConfigDir(), `onebot11_${selfInfo.uin}.json`);
diff --git a/src/onebot11/filter.js b/src/onebot11/filter.js
new file mode 100644
index 0000000..b7d7751
--- /dev/null
+++ b/src/onebot11/filter.js
@@ -0,0 +1,15 @@
+export default (type, payload) => {
+ switch (type) {
+ // 反向http
+ case 'http':
+ return true;
+
+ // websocket连接
+ case 'ws':
+ return true;
+
+ // 控制台日志
+ case 'log':
+ return true;
+ }
+}
diff --git a/src/onebot11/log.ts b/src/onebot11/log.ts
index 1c313ed..abd7a69 100644
--- a/src/onebot11/log.ts
+++ b/src/onebot11/log.ts
@@ -3,9 +3,14 @@ import { log } from '@/common/utils/log';
import { getGroup, getGroupMember, selfInfo } from '@/core/data';
import exp from 'constants';
import { Group } from '@/core';
+import { ob11Config } from '@/onebot11/config';
+
// todo: 应该放到core去用RawMessage解析打印
export async function logMessage(ob11Message: OB11Message){
+ if (!ob11Config.filter('log', ob11Message)) {
+ return;
+ }
const isSelfSent = ob11Message.sender.user_id.toString() === selfInfo.uin;
let prefix = '';
let group: Group | undefined;
diff --git a/src/onebot11/server/postOB11Event.ts b/src/onebot11/server/postOB11Event.ts
index 1c1f167..726328c 100644
--- a/src/onebot11/server/postOB11Event.ts
+++ b/src/onebot11/server/postOB11Event.ts
@@ -63,6 +63,9 @@ export function unregisterWsEventSender(ws: WebSocketClass) {
}
export function postWsEvent(event: PostEventType) {
+ if (!ob11Config.filter('ws', event)) {
+ return;
+ }
for (const ws of eventWSList) {
new Promise(() => {
wsReply(ws, event);
@@ -79,7 +82,7 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = t
return;
}
}
- if (config.http.enablePost) {
+ if (config.http.enablePost && ob11Config.filter('http', msg)) {
const msgStr = JSON.stringify(msg);
const hmac = crypto.createHmac('sha1', ob11Config.http.secret);
hmac.update(msgStr);
diff --git a/vite.config.ts b/vite.config.ts
index e651ac7..62a9399 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -44,6 +44,7 @@ const baseConfigPlugin: PluginOption[] = [
{ src: './src/napcat.json', dest: 'dist/config/' },
{ src: './static/', dest: 'dist/static/', flatten: false },
{ src: './src/onebot11/onebot11.json', dest: 'dist/config/' },
+ { src: './src/onebot11/filter.js', dest: 'dist/config/' },
{ src: './package.json', dest: 'dist' },
{ src: './README.md', dest: 'dist' },
{ src: './logo.png', dest: 'dist/logs' },
懒得做pr了
from napcatqq.
好好好
from napcatqq.
有空麻烦代码合并进去,我自己patch好麻烦
from napcatqq.
看了下实现不太好 之后有空我再看看怎么实现更好
from napcatqq.
Related Issues (20)
- [BUG] 图片文件下载失败 HOT 1
- [BUG] 想请问一下后续http会支持群聊临时消息吗、
- [BUG] 启动失败 code: 'ERR_DLOPEN_FAILED' HOT 4
- [BUG] 下载base64文件时报错 HOT 3
- [BUG] Linux版本启动报错,以及 Supervisor 运行问题 HOT 2
- 会考虑加入频道的API吗,gocq的那种 HOT 1
- [BUG] 调用get_group_list时会返回已被踢出/退出的群 HOT 2
- yobot框架可以用napcatqq接收消息但无法发送消息 HOT 6
- [BUG] 群聊单条消息包含多个图片,napcat.linux获取的全是第一张 HOT 1
- [BUG] 没有收到群成员减少事件 HOT 7
- [Feature Request] 更好的配置文件组织方式 HOT 1
- [Bug] 多个正向ws链接时,每个链接的心跳都重复上报了 HOT 1
- [BUG] bot私聊信息发不出来,并且导致程序崩溃 HOT 19
- [BUG] 无法使用 systemctl 守护 HOT 5
- [BUG] 真寻bot的消息全部发送到私聊了 HOT 2
- [BUG] 成功发送了图片但仍然报错 HOT 5
- [BUG] 不兼容旧版本QQ消息的换行符解析 HOT 10
- 可以同时登录手机qq 和napcat吗》 HOT 2
- [BUG] 报错退出FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from napcatqq.