Refactor routing and proxy components for improved structure and compatibility
- Removed deprecated route utility functions in favor of direct matcher usage. - Updated imports to reflect new module structure for routing utilities. - Consolidated route manager functionality into SharedRouteManager for better consistency. - Eliminated legacy routing methods and interfaces, streamlining the HttpProxy and associated components. - Enhanced WebSocket and HTTP request handling to utilize the new unified HttpRouter. - Updated route matching logic to leverage matcher classes for domain, path, and header checks. - Cleaned up legacy compatibility code across various modules, ensuring a more maintainable codebase.
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { IRouteConfig } from '../../proxies/smart-proxy/models/route-types.js';
|
||||
import type { IReverseProxyConfig } from '../../proxies/http-proxy/models/types.js';
|
||||
import { DomainMatcher, PathMatcher } from '../../core/routing/matchers/index.js';
|
||||
|
||||
/**
|
||||
@ -13,15 +12,6 @@ export interface RouterResult {
|
||||
pathRemainder?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy interface for backward compatibility
|
||||
*/
|
||||
export interface LegacyRouterResult {
|
||||
config: IReverseProxyConfig;
|
||||
pathMatch?: string;
|
||||
pathParams?: Record<string, string>;
|
||||
pathRemainder?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logger interface for HttpRouter
|
||||
@ -36,8 +26,6 @@ export interface ILogger {
|
||||
/**
|
||||
* Unified HTTP Router for reverse proxy requests
|
||||
*
|
||||
* Supports both modern IRouteConfig and legacy IReverseProxyConfig formats
|
||||
*
|
||||
* Domain matching patterns:
|
||||
* - Exact matches: "example.com"
|
||||
* - Wildcard subdomains: "*.example.com" (matches any subdomain of example.com)
|
||||
@ -275,140 +263,4 @@ export class HttpRouter {
|
||||
return false;
|
||||
}
|
||||
|
||||
// ===== LEGACY COMPATIBILITY METHODS =====
|
||||
|
||||
/**
|
||||
* Legacy method that returns IReverseProxyConfig for backward compatibility
|
||||
* @param req The incoming HTTP request
|
||||
* @returns The matching proxy config in legacy format or undefined
|
||||
*/
|
||||
public routeReqLegacy(req: plugins.http.IncomingMessage): IReverseProxyConfig | undefined {
|
||||
const result = this.routeReqWithDetails(req);
|
||||
if (!result) return undefined;
|
||||
|
||||
return this.convertRouteToLegacy(result.route);
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy method for backward compatibility with ProxyRouter
|
||||
* Converts IReverseProxyConfig to IRouteConfig and sets routes
|
||||
*
|
||||
* @param configs Array of legacy proxy configurations
|
||||
*/
|
||||
public setNewProxyConfigs(configs: IReverseProxyConfig[]): void {
|
||||
const routes = configs.map(config => this.convertLegacyConfig(config));
|
||||
this.setRoutes(routes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy method for backward compatibility
|
||||
* Gets all proxy configs by converting routes back to legacy format
|
||||
*/
|
||||
public getProxyConfigs(): IReverseProxyConfig[] {
|
||||
return this.routes.map(route => this.convertRouteToLegacy(route));
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy method: Adds a proxy config with optional path pattern
|
||||
* @param config The legacy configuration to add
|
||||
* @param pathPattern Optional path pattern for route matching
|
||||
*/
|
||||
public addProxyConfig(
|
||||
config: IReverseProxyConfig,
|
||||
pathPattern?: string
|
||||
): void {
|
||||
const route = this.convertLegacyConfig(config, pathPattern);
|
||||
this.addRoute(route);
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy method: Remove proxy config by hostname
|
||||
* @param hostname The hostname to remove
|
||||
* @returns Boolean indicating whether any configs were removed
|
||||
*/
|
||||
public removeProxyConfig(hostname: string): boolean {
|
||||
return this.removeRoutesByDomain(hostname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert legacy IReverseProxyConfig to IRouteConfig
|
||||
*/
|
||||
private convertLegacyConfig(config: IReverseProxyConfig, pathPattern?: string): IRouteConfig {
|
||||
return {
|
||||
match: {
|
||||
ports: config.destinationPorts?.[0] || 443,
|
||||
domains: config.hostName,
|
||||
path: pathPattern
|
||||
},
|
||||
action: {
|
||||
type: 'forward',
|
||||
target: {
|
||||
host: Array.isArray(config.destinationIps) ? config.destinationIps : config.destinationIps,
|
||||
port: config.destinationPorts?.[0] || 443
|
||||
},
|
||||
tls: {
|
||||
mode: 'terminate',
|
||||
certificate: {
|
||||
key: config.privateKey,
|
||||
cert: config.publicKey
|
||||
}
|
||||
}
|
||||
},
|
||||
security: config.authentication ? {
|
||||
basicAuth: {
|
||||
enabled: true,
|
||||
users: [{
|
||||
username: config.authentication.user,
|
||||
password: config.authentication.pass
|
||||
}],
|
||||
realm: 'Protected'
|
||||
}
|
||||
} : undefined,
|
||||
name: `Legacy - ${config.hostName}`,
|
||||
enabled: true
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert IRouteConfig back to legacy IReverseProxyConfig format
|
||||
*/
|
||||
private convertRouteToLegacy(route: IRouteConfig): IReverseProxyConfig {
|
||||
const action = route.action;
|
||||
const target = action.target || { host: 'localhost', port: 80 };
|
||||
|
||||
// Extract certificate if available
|
||||
let privateKey = '';
|
||||
let publicKey = '';
|
||||
|
||||
if (action.tls?.certificate && typeof action.tls.certificate === 'object') {
|
||||
privateKey = action.tls.certificate.key || '';
|
||||
publicKey = action.tls.certificate.cert || '';
|
||||
}
|
||||
|
||||
return {
|
||||
hostName: Array.isArray(route.match.domains)
|
||||
? route.match.domains[0]
|
||||
: route.match.domains || '*',
|
||||
destinationIps: Array.isArray(target.host) ? target.host : [target.host as string],
|
||||
destinationPorts: [
|
||||
typeof target.port === 'number'
|
||||
? target.port
|
||||
: typeof target.port === 'function'
|
||||
? 443 // Default port for function-based
|
||||
: 443
|
||||
],
|
||||
privateKey,
|
||||
publicKey,
|
||||
authentication: route.security?.basicAuth?.enabled && route.security.basicAuth.users.length > 0 ? {
|
||||
type: 'Basic',
|
||||
user: route.security.basicAuth.users[0].username || '',
|
||||
pass: route.security.basicAuth.users[0].password || ''
|
||||
} : undefined,
|
||||
rewriteHostHeader: route.headers?.request?.['Host'] ? true : undefined
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Export backward compatibility aliases
|
||||
export { HttpRouter as ProxyRouter };
|
||||
export { HttpRouter as RouteRouter };
|
||||
}
|
Reference in New Issue
Block a user