feat(forwarding): Add unified forwarding system docs and tests; update build script and .gitignore
This commit is contained in:
		
							
								
								
									
										87
									
								
								ts/common/port80-adapter.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								ts/common/port80-adapter.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,87 @@ | ||||
| import * as plugins from '../plugins.js'; | ||||
|  | ||||
| import type {  | ||||
|   IForwardConfig as ILegacyForwardConfig,  | ||||
|   IDomainOptions  | ||||
| } from './types.js'; | ||||
|  | ||||
| import type {  | ||||
|   IForwardConfig as INewForwardConfig  | ||||
| } from '../smartproxy/types/forwarding.types.js'; | ||||
|  | ||||
| /** | ||||
|  * Converts a new forwarding configuration target to the legacy format | ||||
|  * for Port80Handler | ||||
|  */ | ||||
| export function convertToLegacyForwardConfig( | ||||
|   forwardConfig: INewForwardConfig | ||||
| ): ILegacyForwardConfig { | ||||
|   // Determine host from the target configuration | ||||
|   const host = Array.isArray(forwardConfig.target.host) | ||||
|     ? forwardConfig.target.host[0]  // Use the first host in the array | ||||
|     : forwardConfig.target.host; | ||||
|    | ||||
|   return { | ||||
|     ip: host, | ||||
|     port: forwardConfig.target.port | ||||
|   }; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * Creates Port80Handler domain options from a domain name and forwarding config | ||||
|  */ | ||||
| export function createPort80HandlerOptions( | ||||
|   domain: string, | ||||
|   forwardConfig: INewForwardConfig | ||||
| ): IDomainOptions { | ||||
|   // Determine if we should redirect HTTP to HTTPS | ||||
|   let sslRedirect = false; | ||||
|   if (forwardConfig.http?.redirectToHttps) { | ||||
|     sslRedirect = true; | ||||
|   } | ||||
|    | ||||
|   // Determine if ACME maintenance should be enabled | ||||
|   // Enable by default for termination types, unless explicitly disabled | ||||
|   const requiresTls =  | ||||
|     forwardConfig.type === 'https-terminate-to-http' ||  | ||||
|     forwardConfig.type === 'https-terminate-to-https'; | ||||
|    | ||||
|   const acmeMaintenance =  | ||||
|     requiresTls &&  | ||||
|     forwardConfig.acme?.enabled !== false; | ||||
|    | ||||
|   // Set up forwarding configuration | ||||
|   const options: IDomainOptions = { | ||||
|     domainName: domain, | ||||
|     sslRedirect, | ||||
|     acmeMaintenance | ||||
|   }; | ||||
|    | ||||
|   // Add ACME challenge forwarding if configured | ||||
|   if (forwardConfig.acme?.forwardChallenges) { | ||||
|     options.acmeForward = { | ||||
|       ip: Array.isArray(forwardConfig.acme.forwardChallenges.host)  | ||||
|         ? forwardConfig.acme.forwardChallenges.host[0] | ||||
|         : forwardConfig.acme.forwardChallenges.host, | ||||
|       port: forwardConfig.acme.forwardChallenges.port | ||||
|     }; | ||||
|   } | ||||
|    | ||||
|   // Add HTTP forwarding if this is an HTTP-only config or if HTTP is enabled | ||||
|   const supportsHttp =  | ||||
|     forwardConfig.type === 'http-only' ||  | ||||
|     (forwardConfig.http?.enabled !== false && | ||||
|      (forwardConfig.type === 'https-terminate-to-http' ||  | ||||
|       forwardConfig.type === 'https-terminate-to-https')); | ||||
|    | ||||
|   if (supportsHttp) { | ||||
|     options.forward = { | ||||
|       ip: Array.isArray(forwardConfig.target.host)  | ||||
|         ? forwardConfig.target.host[0] | ||||
|         : forwardConfig.target.host, | ||||
|       port: forwardConfig.target.port | ||||
|     }; | ||||
|   } | ||||
|    | ||||
|   return options; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user