feat(forwarding): Add unified forwarding system docs and tests; update build script and .gitignore
This commit is contained in:
120
ts/examples/forwarding-example.ts
Normal file
120
ts/examples/forwarding-example.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import { createServer } from 'http';
|
||||
import { Socket } from 'net';
|
||||
import {
|
||||
DomainManager,
|
||||
DomainManagerEvents,
|
||||
createDomainConfig,
|
||||
helpers
|
||||
} from '../smartproxy/forwarding/index.js';
|
||||
|
||||
/**
|
||||
* Example showing how to use the unified forwarding system
|
||||
*/
|
||||
async function main() {
|
||||
console.log('Initializing forwarding example...');
|
||||
|
||||
// Create the domain manager
|
||||
const domainManager = new DomainManager();
|
||||
|
||||
// Set up event listeners
|
||||
domainManager.on(DomainManagerEvents.DOMAIN_ADDED, (data) => {
|
||||
console.log(`Domain added: ${data.domains.join(', ')} (${data.forwardingType})`);
|
||||
});
|
||||
|
||||
domainManager.on(DomainManagerEvents.DOMAIN_MATCHED, (data) => {
|
||||
console.log(`Domain matched: ${data.domain} (${data.handlerType})`);
|
||||
});
|
||||
|
||||
domainManager.on(DomainManagerEvents.DOMAIN_MATCH_FAILED, (data) => {
|
||||
console.log(`Domain match failed: ${data.domain}`);
|
||||
});
|
||||
|
||||
domainManager.on(DomainManagerEvents.ERROR, (data) => {
|
||||
console.error(`Error:`, data);
|
||||
});
|
||||
|
||||
// Add example domains with different forwarding types
|
||||
|
||||
// Example 1: HTTP-only forwarding
|
||||
await domainManager.addDomainConfig(
|
||||
createDomainConfig('example.com', helpers.httpOnly('localhost', 3000))
|
||||
);
|
||||
|
||||
// Example 2: HTTPS termination with HTTP backend
|
||||
await domainManager.addDomainConfig(
|
||||
createDomainConfig('secure.example.com', helpers.tlsTerminateToHttp('localhost', 3000))
|
||||
);
|
||||
|
||||
// Example 3: HTTPS termination with HTTPS backend
|
||||
await domainManager.addDomainConfig(
|
||||
createDomainConfig('api.example.com', helpers.tlsTerminateToHttps('localhost', 8443))
|
||||
);
|
||||
|
||||
// Example 4: SNI passthrough
|
||||
await domainManager.addDomainConfig(
|
||||
createDomainConfig('passthrough.example.com', helpers.sniPassthrough('10.0.0.5', 443))
|
||||
);
|
||||
|
||||
// Example 5: Custom configuration for a more complex setup
|
||||
await domainManager.addDomainConfig(
|
||||
createDomainConfig(['*.example.com', '*.example.org'], {
|
||||
type: 'https-terminate-to-http',
|
||||
target: {
|
||||
host: ['10.0.0.10', '10.0.0.11'], // Round-robin load balancing
|
||||
port: 8080
|
||||
},
|
||||
http: {
|
||||
enabled: true,
|
||||
redirectToHttps: false // Allow both HTTP and HTTPS
|
||||
},
|
||||
acme: {
|
||||
enabled: true,
|
||||
maintenance: true,
|
||||
production: false, // Use staging for testing
|
||||
forwardChallenges: {
|
||||
host: '192.168.1.100',
|
||||
port: 8080
|
||||
}
|
||||
},
|
||||
security: {
|
||||
allowedIps: ['10.0.0.*', '192.168.1.*'],
|
||||
maxConnections: 100
|
||||
},
|
||||
advanced: {
|
||||
headers: {
|
||||
'X-Forwarded-For': '{clientIp}',
|
||||
'X-Forwarded-Host': '{sni}'
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Create a simple HTTP server to demonstrate HTTP handler
|
||||
const httpServer = createServer((req, res) => {
|
||||
// Extract the domain from the Host header
|
||||
const domain = req.headers.host?.split(':')[0] || 'unknown';
|
||||
|
||||
// Forward the request to the appropriate handler
|
||||
if (!domainManager.handleHttpRequest(domain, req, res)) {
|
||||
// No handler found, send a default response
|
||||
res.statusCode = 404;
|
||||
res.end(`No handler found for domain: ${domain}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Listen on HTTP port
|
||||
httpServer.listen(80, () => {
|
||||
console.log('HTTP server listening on port 80');
|
||||
});
|
||||
|
||||
// For HTTPS and SNI, we would need to set up a TLS server
|
||||
// This is a simplified example that just shows how the domain manager works
|
||||
|
||||
console.log('Forwarding example initialized successfully');
|
||||
}
|
||||
|
||||
// Run the example
|
||||
main().catch(error => {
|
||||
console.error('Error running example:', error);
|
||||
});
|
||||
Reference in New Issue
Block a user