Giter Club home page Giter Club logo

Comments (9)

MliKiowa avatar MliKiowa commented on August 10, 2024

看起来很麻烦 你是什么需要这个需求呢

from napcatqq.

garveen avatar garveen commented on August 10, 2024

远端接口需要响应部分事件(加群等),但是不适合长连接,全post过去无效请求太多
允许自己写个中间件过滤请求也行
--filter=filter.js
然后filter.js是一个module,export filter: (body) => {return BOOL}
就足够好用了

from napcatqq.

MliKiowa avatar MliKiowa commented on August 10, 2024

考虑正反ws呗 全双工 效率高

from napcatqq.

garveen avatar garveen commented on August 10, 2024

远端服务器是非常老的php鉴权用,所以真不合适ws

from napcatqq.

MliKiowa avatar MliKiowa commented on August 10, 2024

(你要不考虑自己用py之类的中转一下

from napcatqq.

garveen avatar garveen commented on August 10, 2024
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.

MliKiowa avatar MliKiowa commented on August 10, 2024

好好好

from napcatqq.

garveen avatar garveen commented on August 10, 2024

有空麻烦代码合并进去,我自己patch好麻烦

from napcatqq.

MliKiowa avatar MliKiowa commented on August 10, 2024

看了下实现不太好 之后有空我再看看怎么实现更好

from napcatqq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.