feat(PortProxy): Add optional source IP preservation support in PortProxy
This commit is contained in:
@ -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'
|
||||
}
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user