100 lines
2.4 KiB
TypeScript
100 lines
2.4 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 (tools) => {
|
|
// Set a timeout for this test
|
|
tools.timeout(10000); // 10 seconds
|
|
// 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) => {
|
|
const response = data.toString();
|
|
client.end(); // Close the connection after receiving data
|
|
resolve(response);
|
|
});
|
|
|
|
client.on('error', reject);
|
|
|
|
client.write('Hello');
|
|
});
|
|
|
|
expect(result).toEqual('ECHO: Hello');
|
|
});
|
|
|
|
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) {
|
|
await new Promise<void>((resolve) => {
|
|
echoServer.close(() => {
|
|
console.log('Echo server closed');
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
if (proxy) {
|
|
await proxy.stop();
|
|
console.log('Proxy stopped');
|
|
}
|
|
});
|
|
|
|
export default tap.start().then(() => {
|
|
// Force exit after tests complete
|
|
setTimeout(() => {
|
|
console.log('Forcing process exit');
|
|
process.exit(0);
|
|
}, 1000);
|
|
}); |