fix(rustproxy): Use cooperative cancellation for background tasks, prune stale caches and metric entries, and switch tests to dynamic port allocation to avoid port conflicts

This commit is contained in:
2026-02-24 20:56:37 +00:00
parent 755c81c042
commit 33cd5330c4
24 changed files with 535 additions and 560 deletions

View File

@@ -5,6 +5,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { SmartProxy } from '../ts/proxies/smart-proxy/smart-proxy.js';
import type { IRouteConfig } from '../ts/proxies/smart-proxy/models/route-types.js';
import { findFreePorts, assertPortsFree } from './helpers/port-allocator.js';
// Setup test infrastructure
const testCertPath = path.join(process.cwd(), 'test', 'helpers', 'test-cert.pem');
@@ -13,8 +14,14 @@ const testKeyPath = path.join(process.cwd(), 'test', 'helpers', 'test-key.pem');
let testServer: net.Server;
let tlsTestServer: tls.Server;
let smartProxy: SmartProxy;
let PROXY_TCP_PORT: number;
let PROXY_TLS_PORT: number;
let TCP_SERVER_PORT: number;
let TLS_SERVER_PORT: number;
tap.test('setup test servers', async () => {
[PROXY_TCP_PORT, PROXY_TLS_PORT, TCP_SERVER_PORT, TLS_SERVER_PORT] = await findFreePorts(4);
// Create TCP test server
testServer = net.createServer((socket) => {
socket.write('Connected to TCP test server\n');
@@ -24,8 +31,8 @@ tap.test('setup test servers', async () => {
});
await new Promise<void>((resolve) => {
testServer.listen(47712, '127.0.0.1', () => {
console.log('TCP test server listening on port 47712');
testServer.listen(TCP_SERVER_PORT, '127.0.0.1', () => {
console.log(`TCP test server listening on port ${TCP_SERVER_PORT}`);
resolve();
});
});
@@ -45,8 +52,8 @@ tap.test('setup test servers', async () => {
);
await new Promise<void>((resolve) => {
tlsTestServer.listen(47713, '127.0.0.1', () => {
console.log('TLS test server listening on port 47713');
tlsTestServer.listen(TLS_SERVER_PORT, '127.0.0.1', () => {
console.log(`TLS test server listening on port ${TLS_SERVER_PORT}`);
resolve();
});
});
@@ -60,13 +67,13 @@ tap.test('should forward TCP connections correctly', async () => {
{
name: 'tcp-forward',
match: {
ports: 47710,
ports: PROXY_TCP_PORT,
},
action: {
type: 'forward',
targets: [{
host: '127.0.0.1',
port: 47712,
port: TCP_SERVER_PORT,
}],
},
},
@@ -77,7 +84,7 @@ tap.test('should forward TCP connections correctly', async () => {
// Test TCP forwarding
const client = await new Promise<net.Socket>((resolve, reject) => {
const socket = net.connect(47710, '127.0.0.1', () => {
const socket = net.connect(PROXY_TCP_PORT, '127.0.0.1', () => {
console.log('Connected to proxy');
resolve(socket);
});
@@ -106,7 +113,7 @@ tap.test('should handle TLS passthrough correctly', async () => {
{
name: 'tls-passthrough',
match: {
ports: 47711,
ports: PROXY_TLS_PORT,
domains: 'test.example.com',
},
action: {
@@ -116,7 +123,7 @@ tap.test('should handle TLS passthrough correctly', async () => {
},
targets: [{
host: '127.0.0.1',
port: 47713,
port: TLS_SERVER_PORT,
}],
},
},
@@ -129,7 +136,7 @@ tap.test('should handle TLS passthrough correctly', async () => {
const client = await new Promise<tls.TLSSocket>((resolve, reject) => {
const socket = tls.connect(
{
port: 47711,
port: PROXY_TLS_PORT,
host: '127.0.0.1',
servername: 'test.example.com',
rejectUnauthorized: false,
@@ -164,7 +171,7 @@ tap.test('should handle SNI-based forwarding', async () => {
{
name: 'domain-a',
match: {
ports: 47711,
ports: PROXY_TLS_PORT,
domains: 'a.example.com',
},
action: {
@@ -174,14 +181,14 @@ tap.test('should handle SNI-based forwarding', async () => {
},
targets: [{
host: '127.0.0.1',
port: 47713,
port: TLS_SERVER_PORT,
}],
},
},
{
name: 'domain-b',
match: {
ports: 47711,
ports: PROXY_TLS_PORT,
domains: 'b.example.com',
},
action: {
@@ -191,7 +198,7 @@ tap.test('should handle SNI-based forwarding', async () => {
},
targets: [{
host: '127.0.0.1',
port: 47713,
port: TLS_SERVER_PORT,
}],
},
},
@@ -204,7 +211,7 @@ tap.test('should handle SNI-based forwarding', async () => {
const clientA = await new Promise<tls.TLSSocket>((resolve, reject) => {
const socket = tls.connect(
{
port: 47711,
port: PROXY_TLS_PORT,
host: '127.0.0.1',
servername: 'a.example.com',
rejectUnauthorized: false,
@@ -231,7 +238,7 @@ tap.test('should handle SNI-based forwarding', async () => {
const clientB = await new Promise<tls.TLSSocket>((resolve, reject) => {
const socket = tls.connect(
{
port: 47711,
port: PROXY_TLS_PORT,
host: '127.0.0.1',
servername: 'b.example.com',
rejectUnauthorized: false,
@@ -261,6 +268,7 @@ tap.test('should handle SNI-based forwarding', async () => {
tap.test('cleanup', async () => {
testServer.close();
tlsTestServer.close();
await assertPortsFree([PROXY_TCP_PORT, PROXY_TLS_PORT, TCP_SERVER_PORT, TLS_SERVER_PORT]);
});
export default tap.start();