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

@ -175,35 +175,66 @@ tap.test('should stop port proxy', async () => {
});
// Cleanup
tap.test('should preserve client IP through chained proxies', async () => {
// Create two proxies in chain
const firstProxy = new PortProxy({
tap.test('should support optional source IP preservation in chained proxies', async () => {
// Test 1: Without IP preservation (default behavior)
const firstProxyDefault = new PortProxy({
fromPort: PROXY_PORT + 4,
toPort: PROXY_PORT + 5, // Point to second proxy
toPort: PROXY_PORT + 5,
toHost: 'localhost',
domains: [],
sniEnabled: false,
defaultAllowedIPs: ['127.0.0.1']
defaultAllowedIPs: ['127.0.0.1', '::ffff:127.0.0.1']
});
const secondProxy = new PortProxy({
const secondProxyDefault = new PortProxy({
fromPort: PROXY_PORT + 5,
toPort: TEST_SERVER_PORT,
toHost: 'localhost',
domains: [],
sniEnabled: false,
defaultAllowedIPs: ['127.0.0.1']
defaultAllowedIPs: ['127.0.0.1', '::ffff:127.0.0.1']
});
await secondProxy.start();
await firstProxy.start();
await secondProxyDefault.start();
await firstProxyDefault.start();
// Connect through the chain
const response = await createTestClient(PROXY_PORT + 4, TEST_DATA);
expect(response).toEqual(`Echo: ${TEST_DATA}`);
// This should work because we explicitly allow both IPv4 and IPv6 formats
const response1 = await createTestClient(PROXY_PORT + 4, TEST_DATA);
expect(response1).toEqual(`Echo: ${TEST_DATA}`);
await firstProxy.stop();
await secondProxy.stop();
await firstProxyDefault.stop();
await secondProxyDefault.stop();
// Test 2: With IP preservation
const firstProxyPreserved = new PortProxy({
fromPort: PROXY_PORT + 6,
toPort: PROXY_PORT + 7,
toHost: 'localhost',
domains: [],
sniEnabled: false,
defaultAllowedIPs: ['127.0.0.1'],
preserveSourceIP: true
});
const secondProxyPreserved = new PortProxy({
fromPort: PROXY_PORT + 7,
toPort: TEST_SERVER_PORT,
toHost: 'localhost',
domains: [],
sniEnabled: false,
defaultAllowedIPs: ['127.0.0.1'],
preserveSourceIP: true
});
await secondProxyPreserved.start();
await firstProxyPreserved.start();
// This should work with just IPv4 because source IP is preserved
const response2 = await createTestClient(PROXY_PORT + 6, TEST_DATA);
expect(response2).toEqual(`Echo: ${TEST_DATA}`);
await firstProxyPreserved.stop();
await secondProxyPreserved.stop();
});
tap.test('cleanup port proxy test environment', async () => {