BREAKING CHANGE(forwarding): Rename sniPassthrough export to httpsPassthrough for consistent naming and remove outdated forwarding example
This commit is contained in:
		| @@ -3,6 +3,6 @@ | ||||
|  */ | ||||
| export const commitinfo = { | ||||
|   name: '@push.rocks/smartproxy', | ||||
|   version: '11.0.0', | ||||
|   version: '12.0.0', | ||||
|   description: 'A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, dynamic routing with authentication options, and automatic ACME certificate management.' | ||||
| } | ||||
|   | ||||
| @@ -1,128 +0,0 @@ | ||||
| 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({ | ||||
|       target: { host: 'localhost', port: 3000 } | ||||
|     })) | ||||
|   ); | ||||
|  | ||||
|   // Example 2: HTTPS termination with HTTP backend | ||||
|   await domainManager.addDomainConfig( | ||||
|     createDomainConfig('secure.example.com', helpers.tlsTerminateToHttp({ | ||||
|       target: { host: 'localhost', port: 3000 } | ||||
|     })) | ||||
|   ); | ||||
|  | ||||
|   // Example 3: HTTPS termination with HTTPS backend | ||||
|   await domainManager.addDomainConfig( | ||||
|     createDomainConfig('api.example.com', helpers.tlsTerminateToHttps({ | ||||
|       target: { host: 'localhost', port: 8443 } | ||||
|     })) | ||||
|   ); | ||||
|  | ||||
|   // Example 4: SNI passthrough | ||||
|   await domainManager.addDomainConfig( | ||||
|     createDomainConfig('passthrough.example.com', helpers.sniPassthrough({ | ||||
|       target: { host: '10.0.0.5', port: 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); | ||||
| }); | ||||
| @@ -48,5 +48,5 @@ export const helpers = { | ||||
|   httpOnly, | ||||
|   tlsTerminateToHttp, | ||||
|   tlsTerminateToHttps, | ||||
|   sniPassthrough: httpsPassthrough // Alias for backward compatibility | ||||
|   httpsPassthrough | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user