fix(PortProxy): Normalize IP addresses for port proxy to handle IPv4-mapped IPv6 addresses.

This commit is contained in:
Philipp Kunz 2025-02-21 18:43:08 +00:00
parent 4bee483954
commit 5150ddc18e
4 changed files with 29 additions and 4 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## 2025-02-21 - 3.4.1 - fix(PortProxy)
Normalize IP addresses for port proxy to handle IPv4-mapped IPv6 addresses.
- Improved IP normalization logic in PortProxy to support IPv4-mapped IPv6 addresses.
- Updated isAllowed function to expand patterns for better matching accuracy.
## 2025-02-21 - 3.4.0 - feat(PortProxy)
Enhanced PortProxy with custom target host and improved testing

View File

@ -64,7 +64,7 @@ tap.test('setup port proxy test environment', async () => {
toHost: 'localhost',
domains: [],
sniEnabled: false,
defaultAllowedIPs: ['127.0.0.1', '::ffff:127.0.0.1']
defaultAllowedIPs: ['127.0.0.1']
});
});
@ -86,7 +86,7 @@ tap.test('should forward TCP connections to custom host', async () => {
toHost: '127.0.0.1',
domains: [],
sniEnabled: false,
defaultAllowedIPs: ['127.0.0.1', '::ffff:127.0.0.1']
defaultAllowedIPs: ['127.0.0.1']
});
await customHostProxy.start();

View File

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

View File

@ -40,8 +40,26 @@ export class PortProxy {
from.destroy();
to.destroy();
};
const normalizeIP = (ip: string): string[] => {
// Handle IPv4-mapped IPv6 addresses
if (ip.startsWith('::ffff:')) {
const ipv4 = ip.slice(7); // Remove '::ffff:' prefix
return [ip, ipv4];
}
// Handle IPv4 addresses by adding IPv4-mapped IPv6 variant
if (ip.match(/^\d{1,3}(\.\d{1,3}){3}$/)) {
return [ip, `::ffff:${ip}`];
}
return [ip];
};
const isAllowed = (value: string, patterns: string[]): boolean => {
return patterns.some(pattern => plugins.minimatch(value, pattern));
// Expand patterns to include both IPv4 and IPv6 variants
const expandedPatterns = patterns.flatMap(normalizeIP);
// Check if any variant of the IP matches any expanded pattern
return normalizeIP(value).some(ip =>
expandedPatterns.some(pattern => plugins.minimatch(ip, pattern))
);
};
const findMatchingDomain = (serverName: string): DomainConfig | undefined => {
@ -83,6 +101,7 @@ export class PortProxy {
host: this.settings.toHost!,
port: this.settings.toPort,
});
console.log(`Connection established: ${remoteIP} -> ${this.settings.toHost}:${this.settings.toPort}${this.settings.sniEnabled ? ` (SNI: ${(from as any).servername || 'none'})` : ''}`);
from.setTimeout(120000);
from.pipe(to);
to.pipe(from);