smartproxy/test/test.port-forwarding-fix.ts
2025-05-19 17:59:12 +00:00

86 lines
1.9 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as net from 'net';
import { SmartProxy } from '../ts/proxies/smart-proxy/smart-proxy.js';
let echoServer: net.Server;
let proxy: SmartProxy;
tap.test('port forwarding should not immediately close connections', async () => {
// Create an echo server
echoServer = await new Promise<net.Server>((resolve) => {
const server = net.createServer((socket) => {
socket.on('data', (data) => {
socket.write(`ECHO: ${data}`);
});
});
server.listen(8888, () => {
console.log('Echo server listening on port 8888');
resolve(server);
});
});
// Create proxy with forwarding route
proxy = new SmartProxy({
routes: [{
id: 'test',
match: { ports: 9999 },
action: {
type: 'forward',
target: { host: 'localhost', port: 8888 }
}
}]
});
await proxy.start();
// Test connection through proxy
const client = net.createConnection(9999, 'localhost');
const result = await new Promise<string>((resolve, reject) => {
client.on('data', (data) => {
resolve(data.toString());
});
client.on('error', reject);
client.write('Hello');
});
expect(result).toEqual('ECHO: Hello');
client.end();
});
tap.test('TLS passthrough should work correctly', async () => {
// Create proxy with TLS passthrough
proxy = new SmartProxy({
routes: [{
id: 'tls-test',
match: { ports: 8443, domains: 'test.example.com' },
action: {
type: 'forward',
tls: { mode: 'passthrough' },
target: { host: 'localhost', port: 443 }
}
}]
});
await proxy.start();
// For now just verify the proxy starts correctly with TLS passthrough route
expect(proxy).toBeDefined();
await proxy.stop();
});
tap.test('cleanup', async () => {
if (echoServer) {
echoServer.close();
}
if (proxy) {
await proxy.stop();
}
});
export default tap.start();