fix: listener proxy

This commit is contained in:
linyuchen
2024-04-27 17:04:53 +08:00
parent 768cef648d
commit c9b47b41e6
7 changed files with 32 additions and 15 deletions

View File

@@ -5,9 +5,11 @@ import { logDebug, logError } from '@/common/utils/log';
import { ConfigBase } from '@/common/utils/ConfigBase';
export interface OB11Config {
httpHost: string;
httpPort: number;
httpPostUrls: string[];
httpSecret: string;
wsHost: string;
wsPort: number;
wsReverseUrls: string[];
enableHttp: boolean;
@@ -29,9 +31,11 @@ export interface OB11Config {
class Config extends ConfigBase<OB11Config> implements OB11Config {
httpHost: string = '';
httpPort: number = 3000;
httpPostUrls: string[] = [];
httpSecret = '';
wsHost: string = '';
wsPort = 3001;
wsReverseUrls: string[] = [];
enableHttp = false;

View File

@@ -52,10 +52,10 @@ export class NapCatOnebot11 {
selfInfo.nick = user.nick;
}).catch(logError);
if (ob11Config.enableHttp) {
ob11HTTPServer.start(ob11Config.httpPort);
ob11HTTPServer.start(ob11Config.httpPort, ob11Config.httpHost);
}
if (ob11Config.enableWs) {
ob11WebsocketServer.start(ob11Config.wsPort);
ob11WebsocketServer.start(ob11Config.wsPort, ob11Config.wsHost);
}
if (ob11Config.enableWsReverse) {
ob11ReverseWebsockets.start();

View File

@@ -18,9 +18,9 @@ const heartbeatRunning = false;
class OB11WebsocketServer extends WebsocketServerBase {
public start(port: number) {
public start(port: number, host: string) {
this.token = ob11Config.token;
super.start(port);
super.start(port, host);
}
authorizeFailed(wsClient: WebSocket) {
@@ -43,6 +43,8 @@ class OB11WebsocketServer extends WebsocketServerBase {
onConnect(wsClient: WebSocket, url: string, req: IncomingMessage) {
if (url == '/api' || url == '/api/' || url == '/') {
wsClient.on('message', async (msg) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
let receiveData: { action: ActionName, params: any, echo?: any } = { action: '', params: {} };
let echo = null;
try {

View File

@@ -1,14 +1,18 @@
import { WebSocket as WebSocketClass } from 'ws';
import { OB11Response } from '../../action/OB11Response';
import { PostEventType } from '../postOB11Event';
import { log, logDebug, logError } from '../../../common/utils/log';
import { isNull } from '../../../common/utils/helper';
import { log, logDebug, logError } from '@/common/utils/log';
import { isNull } from '@/common/utils/helper';
export function wsReply(wsClient: WebSocketClass, data: OB11Response | PostEventType) {
try {
const packet = Object.assign({}, data);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (isNull(packet['echo'])) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
delete packet['echo'];
}
wsClient.send(JSON.stringify(packet));