feat(socket-utils): implement socket cleanup utilities and enhance socket handling in forwarding handlers

This commit is contained in:
2025-06-01 07:51:20 +00:00
parent c7c325a7d8
commit eb2e67fecc
9 changed files with 273 additions and 181 deletions

View File

@ -2,6 +2,7 @@ import * as plugins from '../../plugins.js';
import { ForwardingHandler } from './base-handler.js';
import type { IForwardConfig } from '../config/forwarding-types.js';
import { ForwardingHandlerEvents } from '../config/forwarding-types.js';
import { setupSocketHandlers } from '../../core/utils/socket-utils.js';
/**
* Handler for HTTP-only forwarding
@ -40,12 +41,15 @@ export class HttpForwardingHandler extends ForwardingHandler {
const remoteAddress = socket.remoteAddress || 'unknown';
const localPort = socket.localPort || 80;
socket.on('close', (hadError) => {
// Set up socket handlers with proper cleanup
const handleClose = (reason: string) => {
this.emit(ForwardingHandlerEvents.DISCONNECTED, {
remoteAddress,
hadError
reason
});
});
};
setupSocketHandlers(socket, handleClose, 'http');
socket.on('error', (error) => {
this.emit(ForwardingHandlerEvents.ERROR, {