fix(PortProxy): Refactor and optimize PortProxy for improved readability and maintainability
This commit is contained in:
parent
133d5a47e0
commit
b9210d891e
@ -1,5 +1,13 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2025-02-23 - 3.10.3 - fix(PortProxy)
|
||||||
|
Refactor and optimize PortProxy for improved readability and maintainability
|
||||||
|
|
||||||
|
- Simplified and clarified inline comments.
|
||||||
|
- Optimized the extractSNI function for better readability.
|
||||||
|
- Streamlined the cleanup process for connections in PortProxy.
|
||||||
|
- Improved handling and logging of incoming and outgoing connections.
|
||||||
|
|
||||||
## 2025-02-23 - 3.10.2 - fix(PortProxy)
|
## 2025-02-23 - 3.10.2 - fix(PortProxy)
|
||||||
Fix connection handling to include timeouts for SNI-enabled connections.
|
Fix connection handling to include timeouts for SNI-enabled connections.
|
||||||
|
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartproxy',
|
name: '@push.rocks/smartproxy',
|
||||||
version: '3.10.2',
|
version: '3.10.3',
|
||||||
description: 'a proxy for handling high workloads of proxying'
|
description: 'a proxy for handling high workloads of proxying'
|
||||||
}
|
}
|
||||||
|
@ -1,106 +1,73 @@
|
|||||||
import * as plugins from './plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
export interface IDomainConfig {
|
export interface IDomainConfig {
|
||||||
domain: string; // glob pattern for domain
|
domain: string; // Glob pattern for domain
|
||||||
allowedIPs: string[]; // glob patterns for IPs allowed to access this domain
|
allowedIPs: string[]; // Glob patterns for allowed IPs
|
||||||
targetIP?: string; // Optional target IP for this domain
|
targetIP?: string; // Optional target IP for this domain
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IProxySettings extends plugins.tls.TlsOptions {
|
export interface IProxySettings extends plugins.tls.TlsOptions {
|
||||||
// Port configuration
|
|
||||||
fromPort: number;
|
fromPort: number;
|
||||||
toPort: number;
|
toPort: number;
|
||||||
toHost?: string; // Target host to proxy to, defaults to 'localhost'
|
toHost?: string; // Target host to proxy to, defaults to 'localhost'
|
||||||
|
|
||||||
// Domain and security settings
|
|
||||||
domains: IDomainConfig[];
|
domains: IDomainConfig[];
|
||||||
sniEnabled?: boolean;
|
sniEnabled?: boolean;
|
||||||
defaultAllowedIPs?: string[]; // Optional default IP patterns if no matching domain found
|
defaultAllowedIPs?: string[];
|
||||||
preserveSourceIP?: boolean; // Whether to preserve the client's source IP when proxying
|
preserveSourceIP?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract SNI (Server Name Indication) from a TLS ClientHello packet.
|
* Extracts the SNI (Server Name Indication) from a TLS ClientHello packet.
|
||||||
* Returns the server name if found, or undefined.
|
* @param buffer - Buffer containing the TLS ClientHello.
|
||||||
|
* @returns The server name if found, otherwise undefined.
|
||||||
*/
|
*/
|
||||||
function extractSNI(buffer: Buffer): string | undefined {
|
function extractSNI(buffer: Buffer): string | undefined {
|
||||||
let offset = 0;
|
let offset = 0;
|
||||||
// We need at least 5 bytes for the record header.
|
if (buffer.length < 5) return undefined;
|
||||||
if (buffer.length < 5) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TLS record header
|
|
||||||
const recordType = buffer.readUInt8(0);
|
const recordType = buffer.readUInt8(0);
|
||||||
if (recordType !== 22) { // 22 = handshake
|
if (recordType !== 22) return undefined; // 22 = handshake
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
// Read record length
|
|
||||||
const recordLength = buffer.readUInt16BE(3);
|
const recordLength = buffer.readUInt16BE(3);
|
||||||
if (buffer.length < 5 + recordLength) {
|
if (buffer.length < 5 + recordLength) return undefined;
|
||||||
// Not all data arrived yet; in production you might need to accumulate more data.
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
offset = 5;
|
offset = 5;
|
||||||
// Handshake message type should be 1 for ClientHello.
|
|
||||||
const handshakeType = buffer.readUInt8(offset);
|
const handshakeType = buffer.readUInt8(offset);
|
||||||
if (handshakeType !== 1) {
|
if (handshakeType !== 1) return undefined; // 1 = ClientHello
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
// Skip handshake header (1 byte type + 3 bytes length)
|
|
||||||
offset += 4;
|
|
||||||
|
|
||||||
// Skip client version (2 bytes) and random (32 bytes)
|
offset += 4; // Skip handshake header (type + length)
|
||||||
offset += 2 + 32;
|
offset += 2 + 32; // Skip client version and random
|
||||||
|
|
||||||
// Session ID
|
|
||||||
const sessionIDLength = buffer.readUInt8(offset);
|
const sessionIDLength = buffer.readUInt8(offset);
|
||||||
offset += 1 + sessionIDLength;
|
offset += 1 + sessionIDLength; // Skip session ID
|
||||||
|
|
||||||
// Cipher suites
|
|
||||||
const cipherSuitesLength = buffer.readUInt16BE(offset);
|
const cipherSuitesLength = buffer.readUInt16BE(offset);
|
||||||
offset += 2 + cipherSuitesLength;
|
offset += 2 + cipherSuitesLength; // Skip cipher suites
|
||||||
|
|
||||||
// Compression methods
|
|
||||||
const compressionMethodsLength = buffer.readUInt8(offset);
|
const compressionMethodsLength = buffer.readUInt8(offset);
|
||||||
offset += 1 + compressionMethodsLength;
|
offset += 1 + compressionMethodsLength; // Skip compression methods
|
||||||
|
|
||||||
// Extensions length
|
if (offset + 2 > buffer.length) return undefined;
|
||||||
if (offset + 2 > buffer.length) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const extensionsLength = buffer.readUInt16BE(offset);
|
const extensionsLength = buffer.readUInt16BE(offset);
|
||||||
offset += 2;
|
offset += 2;
|
||||||
const extensionsEnd = offset + extensionsLength;
|
const extensionsEnd = offset + extensionsLength;
|
||||||
|
|
||||||
// Iterate over extensions
|
|
||||||
while (offset + 4 <= extensionsEnd) {
|
while (offset + 4 <= extensionsEnd) {
|
||||||
const extensionType = buffer.readUInt16BE(offset);
|
const extensionType = buffer.readUInt16BE(offset);
|
||||||
const extensionLength = buffer.readUInt16BE(offset + 2);
|
const extensionLength = buffer.readUInt16BE(offset + 2);
|
||||||
offset += 4;
|
offset += 4;
|
||||||
|
if (extensionType === 0x0000) { // SNI extension
|
||||||
// Check for SNI extension (type 0)
|
if (offset + 2 > buffer.length) return undefined;
|
||||||
if (extensionType === 0x0000) {
|
|
||||||
// SNI extension: first 2 bytes are the SNI list length.
|
|
||||||
if (offset + 2 > buffer.length) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const sniListLength = buffer.readUInt16BE(offset);
|
const sniListLength = buffer.readUInt16BE(offset);
|
||||||
offset += 2;
|
offset += 2;
|
||||||
const sniListEnd = offset + sniListLength;
|
const sniListEnd = offset + sniListLength;
|
||||||
// Loop through the list; typically there is one entry.
|
|
||||||
while (offset + 3 < sniListEnd) {
|
while (offset + 3 < sniListEnd) {
|
||||||
const nameType = buffer.readUInt8(offset);
|
const nameType = buffer.readUInt8(offset++);
|
||||||
offset++;
|
|
||||||
const nameLen = buffer.readUInt16BE(offset);
|
const nameLen = buffer.readUInt16BE(offset);
|
||||||
offset += 2;
|
offset += 2;
|
||||||
if (nameType === 0) { // host_name
|
if (nameType === 0) { // host_name
|
||||||
if (offset + nameLen > buffer.length) {
|
if (offset + nameLen > buffer.length) return undefined;
|
||||||
return undefined;
|
return buffer.toString('utf8', offset, offset + nameLen);
|
||||||
}
|
|
||||||
const serverName = buffer.toString('utf8', offset, offset + nameLen);
|
|
||||||
return serverName;
|
|
||||||
}
|
}
|
||||||
offset += nameLen;
|
offset += nameLen;
|
||||||
}
|
}
|
||||||
@ -115,15 +82,11 @@ function extractSNI(buffer: Buffer): string | undefined {
|
|||||||
export class PortProxy {
|
export class PortProxy {
|
||||||
netServer: plugins.net.Server;
|
netServer: plugins.net.Server;
|
||||||
settings: IProxySettings;
|
settings: IProxySettings;
|
||||||
// Track active incoming connections
|
|
||||||
private activeConnections: Set<plugins.net.Socket> = new Set();
|
private activeConnections: Set<plugins.net.Socket> = new Set();
|
||||||
// Record start times for incoming connections
|
|
||||||
private incomingConnectionTimes: Map<plugins.net.Socket, number> = new Map();
|
private incomingConnectionTimes: Map<plugins.net.Socket, number> = new Map();
|
||||||
// Record start times for outgoing connections
|
|
||||||
private outgoingConnectionTimes: Map<plugins.net.Socket, number> = new Map();
|
private outgoingConnectionTimes: Map<plugins.net.Socket, number> = new Map();
|
||||||
private connectionLogger: NodeJS.Timeout | null = null;
|
private connectionLogger: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
// Overall termination statistics
|
|
||||||
private terminationStats: {
|
private terminationStats: {
|
||||||
incoming: Record<string, number>;
|
incoming: Record<string, number>;
|
||||||
outgoing: Record<string, number>;
|
outgoing: Record<string, number>;
|
||||||
@ -135,90 +98,66 @@ export class PortProxy {
|
|||||||
constructor(settings: IProxySettings) {
|
constructor(settings: IProxySettings) {
|
||||||
this.settings = {
|
this.settings = {
|
||||||
...settings,
|
...settings,
|
||||||
toHost: settings.toHost || 'localhost'
|
toHost: settings.toHost || 'localhost',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to update termination stats.
|
|
||||||
private incrementTerminationStat(side: 'incoming' | 'outgoing', reason: string): void {
|
private incrementTerminationStat(side: 'incoming' | 'outgoing', reason: string): void {
|
||||||
if (!this.terminationStats[side][reason]) {
|
this.terminationStats[side][reason] = (this.terminationStats[side][reason] || 0) + 1;
|
||||||
this.terminationStats[side][reason] = 1;
|
|
||||||
} else {
|
|
||||||
this.terminationStats[side][reason]++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async start() {
|
public async start() {
|
||||||
// Adjusted cleanUpSockets: forcefully destroy both sockets if they haven't been destroyed.
|
// Helper to forcefully destroy sockets.
|
||||||
const cleanUpSockets = (from: plugins.net.Socket, to?: plugins.net.Socket) => {
|
const cleanUpSockets = (socketA: plugins.net.Socket, socketB?: plugins.net.Socket) => {
|
||||||
if (!from.destroyed) {
|
if (!socketA.destroyed) socketA.destroy();
|
||||||
from.destroy();
|
if (socketB && !socketB.destroyed) socketB.destroy();
|
||||||
}
|
|
||||||
if (to && !to.destroyed) {
|
|
||||||
to.destroy();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Normalize an IP to include both IPv4 and IPv6 representations.
|
||||||
const normalizeIP = (ip: string): string[] => {
|
const normalizeIP = (ip: string): string[] => {
|
||||||
// Handle IPv4-mapped IPv6 addresses
|
|
||||||
if (ip.startsWith('::ffff:')) {
|
if (ip.startsWith('::ffff:')) {
|
||||||
const ipv4 = ip.slice(7); // Remove '::ffff:' prefix
|
const ipv4 = ip.slice(7);
|
||||||
return [ip, ipv4];
|
return [ip, ipv4];
|
||||||
}
|
}
|
||||||
// Handle IPv4 addresses by adding IPv4-mapped IPv6 variant
|
|
||||||
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(ip)) {
|
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(ip)) {
|
||||||
return [ip, `::ffff:${ip}`];
|
return [ip, `::ffff:${ip}`];
|
||||||
}
|
}
|
||||||
return [ip];
|
return [ip];
|
||||||
};
|
};
|
||||||
|
|
||||||
const isAllowed = (value: string, patterns: string[]): boolean => {
|
// Check if a given IP matches any of the glob patterns.
|
||||||
// Expand patterns to include both IPv4 and IPv6 variants
|
const isAllowed = (ip: string, patterns: string[]): boolean => {
|
||||||
|
const normalizedIPVariants = normalizeIP(ip);
|
||||||
const expandedPatterns = patterns.flatMap(normalizeIP);
|
const expandedPatterns = patterns.flatMap(normalizeIP);
|
||||||
// Check if any variant of the IP matches any expanded pattern
|
return normalizedIPVariants.some(ipVariant =>
|
||||||
return normalizeIP(value).some(ip =>
|
expandedPatterns.some(pattern => plugins.minimatch(ipVariant, pattern))
|
||||||
expandedPatterns.some(pattern => plugins.minimatch(ip, pattern))
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const findMatchingDomain = (serverName: string): IDomainConfig | undefined => {
|
// Find a matching domain config based on the SNI.
|
||||||
return this.settings.domains.find(config => plugins.minimatch(serverName, config.domain));
|
const findMatchingDomain = (serverName: string): IDomainConfig | undefined =>
|
||||||
};
|
this.settings.domains.find(config => plugins.minimatch(serverName, config.domain));
|
||||||
|
|
||||||
// Create a plain net server for TLS passthrough.
|
|
||||||
this.netServer = plugins.net.createServer((socket: plugins.net.Socket) => {
|
this.netServer = plugins.net.createServer((socket: plugins.net.Socket) => {
|
||||||
const remoteIP = socket.remoteAddress || '';
|
const remoteIP = socket.remoteAddress || '';
|
||||||
|
|
||||||
// Record start time for the incoming connection.
|
|
||||||
this.activeConnections.add(socket);
|
this.activeConnections.add(socket);
|
||||||
this.incomingConnectionTimes.set(socket, Date.now());
|
this.incomingConnectionTimes.set(socket, Date.now());
|
||||||
console.log(`New connection from ${remoteIP}. Active connections: ${this.activeConnections.size}`);
|
console.log(`New connection from ${remoteIP}. Active connections: ${this.activeConnections.size}`);
|
||||||
|
|
||||||
// Flag to detect if we've received the first data chunk.
|
|
||||||
let initialDataReceived = false;
|
let initialDataReceived = false;
|
||||||
|
let incomingTerminationReason: string | null = null;
|
||||||
// Local termination reason trackers for each side.
|
let outgoingTerminationReason: string | null = null;
|
||||||
let incomingTermReason: string | null = null;
|
let targetSocket: plugins.net.Socket | null = null;
|
||||||
let outgoingTermReason: string | null = null;
|
let connectionClosed = false;
|
||||||
|
|
||||||
// Immediately attach an error handler to catch early errors.
|
|
||||||
socket.on('error', (err: Error) => {
|
|
||||||
if (!initialDataReceived) {
|
|
||||||
console.log(`(Premature) Incoming socket error from ${remoteIP} before data received: ${err.message}`);
|
|
||||||
} else {
|
|
||||||
console.log(`(Immediate) Incoming socket error from ${remoteIP}: ${err.message}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Ensure cleanup happens only once.
|
// Ensure cleanup happens only once.
|
||||||
let connectionClosed = false;
|
|
||||||
const cleanupOnce = () => {
|
const cleanupOnce = () => {
|
||||||
if (!connectionClosed) {
|
if (!connectionClosed) {
|
||||||
connectionClosed = true;
|
connectionClosed = true;
|
||||||
cleanUpSockets(socket, to || undefined);
|
cleanUpSockets(socket, targetSocket || undefined);
|
||||||
this.incomingConnectionTimes.delete(socket);
|
this.incomingConnectionTimes.delete(socket);
|
||||||
if (to) {
|
if (targetSocket) {
|
||||||
this.outgoingConnectionTimes.delete(to);
|
this.outgoingConnectionTimes.delete(targetSocket);
|
||||||
}
|
}
|
||||||
if (this.activeConnections.has(socket)) {
|
if (this.activeConnections.has(socket)) {
|
||||||
this.activeConnections.delete(socket);
|
this.activeConnections.delete(socket);
|
||||||
@ -227,10 +166,24 @@ export class PortProxy {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Outgoing connection placeholder.
|
// Helper to reject an incoming connection.
|
||||||
let to: plugins.net.Socket | null = null;
|
const rejectIncomingConnection = (reason: string, logMessage: string) => {
|
||||||
|
console.log(logMessage);
|
||||||
|
socket.end();
|
||||||
|
if (incomingTerminationReason === null) {
|
||||||
|
incomingTerminationReason = reason;
|
||||||
|
this.incrementTerminationStat('incoming', reason);
|
||||||
|
}
|
||||||
|
cleanupOnce();
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.on('error', (err: Error) => {
|
||||||
|
const errorMessage = initialDataReceived
|
||||||
|
? `(Immediate) Incoming socket error from ${remoteIP}: ${err.message}`
|
||||||
|
: `(Premature) Incoming socket error from ${remoteIP} before data received: ${err.message}`;
|
||||||
|
console.log(errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
// Handle errors by recording termination reason and cleaning up.
|
|
||||||
const handleError = (side: 'incoming' | 'outgoing') => (err: Error) => {
|
const handleError = (side: 'incoming' | 'outgoing') => (err: Error) => {
|
||||||
const code = (err as any).code;
|
const code = (err as any).code;
|
||||||
let reason = 'error';
|
let reason = 'error';
|
||||||
@ -240,73 +193,47 @@ export class PortProxy {
|
|||||||
} else {
|
} else {
|
||||||
console.log(`Error on ${side} side from ${remoteIP}: ${err.message}`);
|
console.log(`Error on ${side} side from ${remoteIP}: ${err.message}`);
|
||||||
}
|
}
|
||||||
if (side === 'incoming' && incomingTermReason === null) {
|
if (side === 'incoming' && incomingTerminationReason === null) {
|
||||||
incomingTermReason = reason;
|
incomingTerminationReason = reason;
|
||||||
this.incrementTerminationStat('incoming', reason);
|
this.incrementTerminationStat('incoming', reason);
|
||||||
} else if (side === 'outgoing' && outgoingTermReason === null) {
|
} else if (side === 'outgoing' && outgoingTerminationReason === null) {
|
||||||
outgoingTermReason = reason;
|
outgoingTerminationReason = reason;
|
||||||
this.incrementTerminationStat('outgoing', reason);
|
this.incrementTerminationStat('outgoing', reason);
|
||||||
}
|
}
|
||||||
cleanupOnce();
|
cleanupOnce();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle close events. If no termination reason was recorded, mark as "normal".
|
|
||||||
const handleClose = (side: 'incoming' | 'outgoing') => () => {
|
const handleClose = (side: 'incoming' | 'outgoing') => () => {
|
||||||
console.log(`Connection closed on ${side} side from ${remoteIP}`);
|
console.log(`Connection closed on ${side} side from ${remoteIP}`);
|
||||||
if (side === 'incoming' && incomingTermReason === null) {
|
if (side === 'incoming' && incomingTerminationReason === null) {
|
||||||
incomingTermReason = 'normal';
|
incomingTerminationReason = 'normal';
|
||||||
this.incrementTerminationStat('incoming', 'normal');
|
this.incrementTerminationStat('incoming', 'normal');
|
||||||
} else if (side === 'outgoing' && outgoingTermReason === null) {
|
} else if (side === 'outgoing' && outgoingTerminationReason === null) {
|
||||||
outgoingTermReason = 'normal';
|
outgoingTerminationReason = 'normal';
|
||||||
this.incrementTerminationStat('outgoing', 'normal');
|
this.incrementTerminationStat('outgoing', 'normal');
|
||||||
}
|
}
|
||||||
cleanupOnce();
|
cleanupOnce();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Setup connection, optionally accepting the initial data chunk.
|
|
||||||
const setupConnection = (serverName: string, initialChunk?: Buffer) => {
|
const setupConnection = (serverName: string, initialChunk?: Buffer) => {
|
||||||
// Check if the IP is allowed by default.
|
const defaultAllowed = this.settings.defaultAllowedIPs && isAllowed(remoteIP, this.settings.defaultAllowedIPs);
|
||||||
const isDefaultAllowed = this.settings.defaultAllowedIPs && isAllowed(remoteIP, this.settings.defaultAllowedIPs);
|
|
||||||
if (!isDefaultAllowed && serverName) {
|
if (!defaultAllowed && serverName) {
|
||||||
const domainConfig = findMatchingDomain(serverName);
|
const domainConfig = findMatchingDomain(serverName);
|
||||||
if (!domainConfig) {
|
if (!domainConfig) {
|
||||||
console.log(`Connection rejected: No matching domain config for ${serverName} from ${remoteIP}`);
|
return rejectIncomingConnection('rejected', `Connection rejected: No matching domain config for ${serverName} from ${remoteIP}`);
|
||||||
socket.end();
|
|
||||||
if (incomingTermReason === null) {
|
|
||||||
incomingTermReason = 'rejected';
|
|
||||||
this.incrementTerminationStat('incoming', 'rejected');
|
|
||||||
}
|
|
||||||
cleanupOnce();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (!isAllowed(remoteIP, domainConfig.allowedIPs)) {
|
if (!isAllowed(remoteIP, domainConfig.allowedIPs)) {
|
||||||
console.log(`Connection rejected: IP ${remoteIP} not allowed for domain ${serverName}`);
|
return rejectIncomingConnection('rejected', `Connection rejected: IP ${remoteIP} not allowed for domain ${serverName}`);
|
||||||
socket.end();
|
|
||||||
if (incomingTermReason === null) {
|
|
||||||
incomingTermReason = 'rejected';
|
|
||||||
this.incrementTerminationStat('incoming', 'rejected');
|
|
||||||
}
|
|
||||||
cleanupOnce();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else if (!isDefaultAllowed && !serverName) {
|
} else if (!defaultAllowed && !serverName) {
|
||||||
console.log(`Connection rejected: No SNI and IP ${remoteIP} not in default allowed list`);
|
return rejectIncomingConnection('rejected', `Connection rejected: No SNI and IP ${remoteIP} not in default allowed list`);
|
||||||
socket.end();
|
} else if (defaultAllowed && !serverName) {
|
||||||
if (incomingTermReason === null) {
|
|
||||||
incomingTermReason = 'rejected';
|
|
||||||
this.incrementTerminationStat('incoming', 'rejected');
|
|
||||||
}
|
|
||||||
cleanupOnce();
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
console.log(`Connection allowed: IP ${remoteIP} is in default allowed list`);
|
console.log(`Connection allowed: IP ${remoteIP} is in default allowed list`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine target host.
|
|
||||||
const domainConfig = serverName ? findMatchingDomain(serverName) : undefined;
|
const domainConfig = serverName ? findMatchingDomain(serverName) : undefined;
|
||||||
const targetHost = domainConfig?.targetIP || this.settings.toHost!;
|
const targetHost = domainConfig?.targetIP || this.settings.toHost!;
|
||||||
|
|
||||||
// Create connection options.
|
|
||||||
const connectionOptions: plugins.net.NetConnectOpts = {
|
const connectionOptions: plugins.net.NetConnectOpts = {
|
||||||
host: targetHost,
|
host: targetHost,
|
||||||
port: this.settings.toPort,
|
port: this.settings.toPort,
|
||||||
@ -315,49 +242,47 @@ export class PortProxy {
|
|||||||
connectionOptions.localAddress = remoteIP.replace('::ffff:', '');
|
connectionOptions.localAddress = remoteIP.replace('::ffff:', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establish outgoing connection.
|
targetSocket = plugins.net.connect(connectionOptions);
|
||||||
to = plugins.net.connect(connectionOptions);
|
if (targetSocket) {
|
||||||
if (to) {
|
this.outgoingConnectionTimes.set(targetSocket, Date.now());
|
||||||
this.outgoingConnectionTimes.set(to, Date.now());
|
|
||||||
}
|
}
|
||||||
console.log(`Connection established: ${remoteIP} -> ${targetHost}:${this.settings.toPort}${serverName ? ` (SNI: ${serverName})` : ''}`);
|
console.log(
|
||||||
|
`Connection established: ${remoteIP} -> ${targetHost}:${this.settings.toPort}` +
|
||||||
// Push back the initial chunk if provided.
|
`${serverName ? ` (SNI: ${serverName})` : ''}`
|
||||||
|
);
|
||||||
|
|
||||||
if (initialChunk) {
|
if (initialChunk) {
|
||||||
socket.unshift(initialChunk);
|
socket.unshift(initialChunk);
|
||||||
}
|
}
|
||||||
socket.setTimeout(120000);
|
socket.setTimeout(120000);
|
||||||
socket.pipe(to!);
|
socket.pipe(targetSocket);
|
||||||
to!.pipe(socket);
|
targetSocket.pipe(socket);
|
||||||
|
|
||||||
// Attach event handlers for both sockets.
|
|
||||||
socket.on('error', handleError('incoming'));
|
socket.on('error', handleError('incoming'));
|
||||||
to!.on('error', handleError('outgoing'));
|
targetSocket.on('error', handleError('outgoing'));
|
||||||
socket.on('close', handleClose('incoming'));
|
socket.on('close', handleClose('incoming'));
|
||||||
to!.on('close', handleClose('outgoing'));
|
targetSocket.on('close', handleClose('outgoing'));
|
||||||
socket.on('timeout', () => {
|
socket.on('timeout', () => {
|
||||||
console.log(`Timeout on incoming side from ${remoteIP}`);
|
console.log(`Timeout on incoming side from ${remoteIP}`);
|
||||||
if (incomingTermReason === null) {
|
if (incomingTerminationReason === null) {
|
||||||
incomingTermReason = 'timeout';
|
incomingTerminationReason = 'timeout';
|
||||||
this.incrementTerminationStat('incoming', 'timeout');
|
this.incrementTerminationStat('incoming', 'timeout');
|
||||||
}
|
}
|
||||||
cleanupOnce();
|
cleanupOnce();
|
||||||
});
|
});
|
||||||
to!.on('timeout', () => {
|
targetSocket.on('timeout', () => {
|
||||||
console.log(`Timeout on outgoing side from ${remoteIP}`);
|
console.log(`Timeout on outgoing side from ${remoteIP}`);
|
||||||
if (outgoingTermReason === null) {
|
if (outgoingTerminationReason === null) {
|
||||||
outgoingTermReason = 'timeout';
|
outgoingTerminationReason = 'timeout';
|
||||||
this.incrementTerminationStat('outgoing', 'timeout');
|
this.incrementTerminationStat('outgoing', 'timeout');
|
||||||
}
|
}
|
||||||
cleanupOnce();
|
cleanupOnce();
|
||||||
});
|
});
|
||||||
socket.on('end', handleClose('incoming'));
|
socket.on('end', handleClose('incoming'));
|
||||||
to!.on('end', handleClose('outgoing'));
|
targetSocket.on('end', handleClose('outgoing'));
|
||||||
};
|
};
|
||||||
|
|
||||||
// For SNI-enabled connections, set an initial data timeout before waiting for data.
|
|
||||||
if (this.settings.sniEnabled) {
|
if (this.settings.sniEnabled) {
|
||||||
// Set an initial timeout for receiving data (e.g., 5 seconds)
|
|
||||||
socket.setTimeout(5000, () => {
|
socket.setTimeout(5000, () => {
|
||||||
console.log(`Initial data timeout for ${remoteIP}`);
|
console.log(`Initial data timeout for ${remoteIP}`);
|
||||||
socket.end();
|
socket.end();
|
||||||
@ -365,7 +290,6 @@ export class PortProxy {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.once('data', (chunk: Buffer) => {
|
socket.once('data', (chunk: Buffer) => {
|
||||||
// Clear the initial timeout since data has been received
|
|
||||||
socket.setTimeout(0);
|
socket.setTimeout(0);
|
||||||
initialDataReceived = true;
|
initialDataReceived = true;
|
||||||
const serverName = extractSNI(chunk) || '';
|
const serverName = extractSNI(chunk) || '';
|
||||||
@ -373,27 +297,22 @@ export class PortProxy {
|
|||||||
setupConnection(serverName, chunk);
|
setupConnection(serverName, chunk);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// For non-SNI connections, simply check defaultAllowedIPs.
|
|
||||||
initialDataReceived = true;
|
initialDataReceived = true;
|
||||||
if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
|
if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
|
||||||
console.log(`Connection rejected: IP ${remoteIP} not allowed for non-SNI connection`);
|
return rejectIncomingConnection('rejected', `Connection rejected: IP ${remoteIP} not allowed for non-SNI connection`);
|
||||||
socket.end();
|
|
||||||
if (incomingTermReason === null) {
|
|
||||||
incomingTermReason = 'rejected';
|
|
||||||
this.incrementTerminationStat('incoming', 'rejected');
|
|
||||||
}
|
|
||||||
cleanupOnce();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
setupConnection('');
|
setupConnection('');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('error', (err: Error) => {
|
.on('error', (err: Error) => {
|
||||||
console.log(`Server Error: ${err.message}`);
|
console.log(`Server Error: ${err.message}`);
|
||||||
})
|
})
|
||||||
.listen(this.settings.fromPort, () => {
|
.listen(this.settings.fromPort, () => {
|
||||||
console.log(`PortProxy -> OK: Now listening on port ${this.settings.fromPort}${this.settings.sniEnabled ? ' (SNI passthrough enabled)' : ''}`);
|
console.log(
|
||||||
});
|
`PortProxy -> OK: Now listening on port ${this.settings.fromPort}` +
|
||||||
|
`${this.settings.sniEnabled ? ' (SNI passthrough enabled)' : ''}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// Log active connection count, longest running connection durations,
|
// Log active connection count, longest running connection durations,
|
||||||
// and termination statistics every 10 seconds.
|
// and termination statistics every 10 seconds.
|
||||||
@ -401,19 +320,18 @@ export class PortProxy {
|
|||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
let maxIncoming = 0;
|
let maxIncoming = 0;
|
||||||
for (const startTime of this.incomingConnectionTimes.values()) {
|
for (const startTime of this.incomingConnectionTimes.values()) {
|
||||||
const duration = now - startTime;
|
maxIncoming = Math.max(maxIncoming, now - startTime);
|
||||||
if (duration > maxIncoming) {
|
|
||||||
maxIncoming = duration;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let maxOutgoing = 0;
|
let maxOutgoing = 0;
|
||||||
for (const startTime of this.outgoingConnectionTimes.values()) {
|
for (const startTime of this.outgoingConnectionTimes.values()) {
|
||||||
const duration = now - startTime;
|
maxOutgoing = Math.max(maxOutgoing, now - startTime);
|
||||||
if (duration > maxOutgoing) {
|
|
||||||
maxOutgoing = duration;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
console.log(`(Interval Log) Active connections: ${this.activeConnections.size}. Longest running incoming: ${plugins.prettyMs(maxIncoming)}, outgoing: ${plugins.prettyMs(maxOutgoing)}. Termination stats (incoming): ${JSON.stringify(this.terminationStats.incoming)}, (outgoing): ${JSON.stringify(this.terminationStats.outgoing)}`);
|
console.log(
|
||||||
|
`(Interval Log) Active connections: ${this.activeConnections.size}. ` +
|
||||||
|
`Longest running incoming: ${plugins.prettyMs(maxIncoming)}, outgoing: ${plugins.prettyMs(maxOutgoing)}. ` +
|
||||||
|
`Termination stats (incoming): ${JSON.stringify(this.terminationStats.incoming)}, ` +
|
||||||
|
`(outgoing): ${JSON.stringify(this.terminationStats.outgoing)}`
|
||||||
|
);
|
||||||
}, 10000);
|
}, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user