smartproxy/examples/dynamic-port-management.ts
2025-05-18 18:31:40 +00:00

131 lines
4.1 KiB
TypeScript

/**
* Dynamic Port Management Example
*
* This example demonstrates how to dynamically add and remove ports
* while SmartProxy is running, without requiring a restart.
*/
import { SmartProxy } from '../dist_ts/index.js';
import type { IRouteConfig } from '../dist_ts/index.js';
async function main() {
// Create a SmartProxy instance with initial routes
const proxy = new SmartProxy({
routes: [
// Initial route on port 8080
{
match: {
ports: 8080,
domains: ['example.com', '*.example.com']
},
action: {
type: 'forward',
target: { host: 'localhost', port: 3000 }
},
name: 'Initial HTTP Route'
}
]
});
// Start the proxy
await proxy.start();
console.log('SmartProxy started with initial configuration');
console.log('Listening on ports:', proxy.getListeningPorts());
// Wait 3 seconds
console.log('Waiting 3 seconds before adding a new port...');
await new Promise(resolve => setTimeout(resolve, 3000));
// Add a new port listener without changing routes yet
await proxy.addListeningPort(8081);
console.log('Added port 8081 without any routes yet');
console.log('Now listening on ports:', proxy.getListeningPorts());
// Wait 3 more seconds
console.log('Waiting 3 seconds before adding a route for the new port...');
await new Promise(resolve => setTimeout(resolve, 3000));
// Get current routes and add a new one for port 8081
const currentRoutes = proxy.settings.routes;
// Create a new route for port 8081
const newRoute: IRouteConfig = {
match: {
ports: 8081,
domains: ['api.example.com']
},
action: {
type: 'forward' as const,
target: { host: 'localhost', port: 4000 }
},
name: 'API Route'
};
// Update routes to include the new one
await proxy.updateRoutes([...currentRoutes, newRoute]);
console.log('Added new route for port 8081');
// Wait 3 more seconds
console.log('Waiting 3 seconds before adding another port through updateRoutes...');
await new Promise(resolve => setTimeout(resolve, 3000));
// Add a completely new port via updateRoutes, which will automatically start listening
const thirdRoute: IRouteConfig = {
match: {
ports: 8082,
domains: ['admin.example.com']
},
action: {
type: 'forward' as const,
target: { host: 'localhost', port: 5000 }
},
name: 'Admin Route'
};
// Update routes again to include the third route
await proxy.updateRoutes([...currentRoutes, newRoute, thirdRoute]);
console.log('Added new route for port 8082 through updateRoutes');
console.log('Now listening on ports:', proxy.getListeningPorts());
// Wait 3 more seconds
console.log('Waiting 3 seconds before removing port 8081...');
await new Promise(resolve => setTimeout(resolve, 3000));
// Remove a port without changing routes
await proxy.removeListeningPort(8081);
console.log('Removed port 8081 (but route still exists)');
console.log('Now listening on ports:', proxy.getListeningPorts());
// Wait 3 more seconds
console.log('Waiting 3 seconds before stopping all routes on port 8082...');
await new Promise(resolve => setTimeout(resolve, 3000));
// Remove all routes for port 8082
const routesWithout8082 = currentRoutes.filter(route => {
// Check if this route includes port 8082
const ports = proxy.routeManager.expandPortRange(route.match.ports);
return !ports.includes(8082);
});
// Update routes without any for port 8082
await proxy.updateRoutes([...routesWithout8082, newRoute]);
console.log('Removed routes for port 8082 through updateRoutes');
console.log('Now listening on ports:', proxy.getListeningPorts());
// Show statistics
console.log('Statistics:', proxy.getStatistics());
// Wait 3 more seconds, then shut down
console.log('Waiting 3 seconds before shutdown...');
await new Promise(resolve => setTimeout(resolve, 3000));
// Stop the proxy
await proxy.stop();
console.log('SmartProxy stopped');
}
// Run the example
main().catch(err => {
console.error('Error in example:', err);
process.exit(1);
});