feat(PortProxy): Add optional source IP preservation support in PortProxy

This commit is contained in:
2025-02-21 19:44:59 +00:00
parent 483cbb3634
commit ee03224561
4 changed files with 63 additions and 19 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartproxy',
version: '3.6.0',
version: '3.7.0',
description: 'a proxy for handling high workloads of proxying'
}

View File

@ -17,6 +17,7 @@ export interface ProxySettings extends plugins.tls.TlsOptions {
domains: DomainConfig[];
sniEnabled?: boolean;
defaultAllowedIPs?: string[]; // Optional default IP patterns if no matching domain found
preserveSourceIP?: boolean; // Whether to preserve the client's source IP when proxying
}
export class PortProxy {
@ -123,12 +124,18 @@ export class PortProxy {
const domainConfig = serverName ? findMatchingDomain(serverName) : undefined;
const targetHost = domainConfig?.targetIP || this.settings.toHost!;
// Create connection with IP binding to preserve original client IP
const to = plugins.net.createConnection({
// Create connection, optionally preserving the client's source IP
const connectionOptions: plugins.net.NetConnectOpts = {
host: targetHost,
port: this.settings.toPort,
localAddress: remoteIP.replace('::ffff:', ''), // Remove IPv6 mapping if present
});
};
// Only set localAddress if preserveSourceIP is enabled
if (this.settings.preserveSourceIP) {
connectionOptions.localAddress = remoteIP.replace('::ffff:', ''); // Remove IPv6 mapping if present
}
const to = plugins.net.createConnection(connectionOptions);
console.log(`Connection established: ${remoteIP} -> ${targetHost}:${this.settings.toPort}${serverName ? ` (SNI: ${serverName})` : ''}`);
from.setTimeout(120000);
from.pipe(to);