fix: listener proxy

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

View File

@ -53,12 +53,12 @@ export abstract class HttpServerBase {
next(); next();
} }
start(port: number) { start(port: number, host: string) {
try { try {
this.expressAPP.get('/', (req: Request, res: Response) => { this.expressAPP.get('/', (req: Request, res: Response) => {
res.send(`${this.name}已启动`); res.send(`${this.name}已启动`);
}); });
this.listen(port); this.listen(port, host);
} catch (e: any) { } catch (e: any) {
logError('HTTP服务启动失败', e.toString()); logError('HTTP服务启动失败', e.toString());
// llonebotError.httpServerError = "HTTP服务启动失败, " + e.toString() // llonebotError.httpServerError = "HTTP服务启动失败, " + e.toString()
@ -73,9 +73,9 @@ export abstract class HttpServerBase {
} }
} }
restart(port: number) { restart(port: number, host: string) {
this.stop(); this.stop();
this.start(port); this.start(port, host);
} }
abstract handleFailed(res: Response, payload: any, err: any): void abstract handleFailed(res: Response, payload: any, err: any): void
@ -108,9 +108,10 @@ export abstract class HttpServerBase {
}); });
} }
protected listen(port: number) { protected listen(port: number, host: string = '0.0.0.0') {
this.server = this.expressAPP.listen(port, '0.0.0.0', () => { host = host || '0.0.0.0';
const info = `${this.name} started 0.0.0.0:${port}`; this.server = this.expressAPP.listen(port, host, () => {
const info = `${this.name} started ${host}:${port}`;
log(info); log(info);
}); });
} }

View File

@ -27,12 +27,14 @@ export class WebsocketServerBase {
constructor() { constructor() {
} }
start(port: number) { start(port: number, host: string = '') {
try { try {
this.ws = new WebSocketServer({ this.ws = new WebSocketServer({
port , port ,
host: '',
maxPayload: 1024 * 1024 * 1024 maxPayload: 1024 * 1024 * 1024
}); });
log(`ws服务启动成功, ${host}:${port}`);
} catch (e: any) { } catch (e: any) {
throw Error('ws服务启动失败, ' + e.toString()); throw Error('ws服务启动失败, ' + e.toString());
} }

View File

@ -36,6 +36,9 @@ export class ConfigBase<T>{
const data = JSON.parse(fs.readFileSync(configPath, 'utf-8')); const data = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
logDebug(`配置文件${configPath}已加载`, data); logDebug(`配置文件${configPath}已加载`, data);
Object.assign(this, data); Object.assign(this, data);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this.save(this); // 保存一次,让新版本的字段写入
return this; return this;
} catch (e: any) { } catch (e: any) {
if (e instanceof SyntaxError) { if (e instanceof SyntaxError) {
@ -48,9 +51,10 @@ export class ConfigBase<T>{
} }
save(config: T) { save(config: T) {
Object.assign(this, config);
const configPath = this.getConfigPath(); const configPath = this.getConfigPath();
try { try {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); fs.writeFileSync(configPath, JSON.stringify(this, null, 2));
} catch (e: any) { } catch (e: any) {
logError(`保存配置文件 ${configPath} 时发生错误:`, e.message); logError(`保存配置文件 ${configPath} 时发生错误:`, e.message);
} }

View File

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

View File

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

View File

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

View File

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