import { expect, tap } from '@git.zone/tstest/tapbundle'; import * as net from 'net'; import { SmartProxy } from '../ts/proxies/smart-proxy/smart-proxy.js'; import type { IRouteConfig } from '../ts/proxies/smart-proxy/models/route-types.js'; // Test to verify NFTables forwarding doesn't terminate connections tap.test('NFTables forwarding should not terminate connections', async () => { // Create a test server that receives connections const testServer = net.createServer((socket) => { socket.write('Connected to test server\n'); socket.on('data', (data) => { socket.write(`Echo: ${data}`); }); }); // Start test server await new Promise((resolve) => { testServer.listen(8001, '127.0.0.1', () => { console.log('Test server listening on port 8001'); resolve(); }); }); // Create SmartProxy with NFTables route const smartProxy = new SmartProxy({ enableDetailedLogging: true, routes: [ { id: 'nftables-test', name: 'NFTables Test Route', match: { port: 8080, }, action: { type: 'forward', forwardingEngine: 'nftables', target: { host: '127.0.0.1', port: 8001, }, }, }, // Also add regular forwarding route for comparison { id: 'regular-test', name: 'Regular Forward Route', match: { port: 8081, }, action: { type: 'forward', target: { host: '127.0.0.1', port: 8001, }, }, }, ], }); await smartProxy.start(); // Test NFTables route const nftablesConnection = await new Promise((resolve, reject) => { const client = net.connect(8080, '127.0.0.1', () => { console.log('Connected to NFTables route'); resolve(client); }); client.on('error', reject); }); // Add timeout to check if connection stays alive await new Promise((resolve) => { let dataReceived = false; nftablesConnection.on('data', (data) => { console.log('NFTables route data:', data.toString()); dataReceived = true; }); // Send test data nftablesConnection.write('Test NFTables'); // Check connection after 100ms setTimeout(() => { // Connection should still be alive even if app doesn't handle it expect(nftablesConnection.destroyed).toBe(false); nftablesConnection.end(); resolve(); }, 100); }); // Test regular forwarding route for comparison const regularConnection = await new Promise((resolve, reject) => { const client = net.connect(8081, '127.0.0.1', () => { console.log('Connected to regular route'); resolve(client); }); client.on('error', reject); }); // Test regular connection works await new Promise((resolve) => { regularConnection.on('data', (data) => { console.log('Regular route data:', data.toString()); expect(data.toString()).toContain('Connected to test server'); regularConnection.end(); resolve(); }); }); // Cleanup await smartProxy.stop(); testServer.close(); }); export default tap.start();