165 lines
4.6 KiB
TypeScript
165 lines
4.6 KiB
TypeScript
/**
|
|
* Route Migration Utilities
|
|
*
|
|
* This file provides utility functions for migrating from legacy domain-based
|
|
* configuration to the new route-based configuration system. These functions
|
|
* are temporary and will be removed after the migration is complete.
|
|
*/
|
|
|
|
import type { TForwardingType } from '../../../forwarding/config/forwarding-types.js';
|
|
import type { IRouteConfig, IRouteMatch, IRouteAction, IRouteTarget } from '../models/route-types.js';
|
|
|
|
/**
|
|
* Legacy domain config interface (for migration only)
|
|
* @deprecated This interface will be removed in a future version
|
|
*/
|
|
export interface ILegacyDomainConfig {
|
|
domains: string[];
|
|
forwarding: {
|
|
type: TForwardingType;
|
|
target: {
|
|
host: string | string[];
|
|
port: number;
|
|
};
|
|
[key: string]: any;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Convert a legacy domain config to a route-based config
|
|
* @param domainConfig Legacy domain configuration
|
|
* @param additionalOptions Additional options to add to the route
|
|
* @returns Route configuration
|
|
* @deprecated This function will be removed in a future version
|
|
*/
|
|
export function domainConfigToRouteConfig(
|
|
domainConfig: ILegacyDomainConfig,
|
|
additionalOptions: Partial<IRouteConfig> = {}
|
|
): IRouteConfig {
|
|
// Default port based on forwarding type
|
|
let defaultPort = 80;
|
|
let tlsMode: 'passthrough' | 'terminate' | 'terminate-and-reencrypt' | undefined;
|
|
|
|
switch (domainConfig.forwarding.type) {
|
|
case 'http-only':
|
|
defaultPort = 80;
|
|
break;
|
|
case 'https-passthrough':
|
|
defaultPort = 443;
|
|
tlsMode = 'passthrough';
|
|
break;
|
|
case 'https-terminate-to-http':
|
|
defaultPort = 443;
|
|
tlsMode = 'terminate';
|
|
break;
|
|
case 'https-terminate-to-https':
|
|
defaultPort = 443;
|
|
tlsMode = 'terminate-and-reencrypt';
|
|
break;
|
|
}
|
|
|
|
// Create route match criteria
|
|
const match: IRouteMatch = {
|
|
ports: additionalOptions.match?.ports || defaultPort,
|
|
domains: domainConfig.domains
|
|
};
|
|
|
|
// Create route target
|
|
const target: IRouteTarget = {
|
|
host: domainConfig.forwarding.target.host,
|
|
port: domainConfig.forwarding.target.port
|
|
};
|
|
|
|
// Create route action
|
|
const action: IRouteAction = {
|
|
type: 'forward',
|
|
target
|
|
};
|
|
|
|
// Add TLS configuration if needed
|
|
if (tlsMode) {
|
|
action.tls = {
|
|
mode: tlsMode,
|
|
certificate: 'auto'
|
|
};
|
|
|
|
// If the legacy config has custom certificates, use them
|
|
if (domainConfig.forwarding.https?.customCert) {
|
|
action.tls.certificate = {
|
|
key: domainConfig.forwarding.https.customCert.key,
|
|
cert: domainConfig.forwarding.https.customCert.cert
|
|
};
|
|
}
|
|
}
|
|
|
|
// Add security options if present
|
|
if (domainConfig.forwarding.security) {
|
|
action.security = domainConfig.forwarding.security;
|
|
}
|
|
|
|
// Create the route config
|
|
const routeConfig: IRouteConfig = {
|
|
match,
|
|
action,
|
|
// Include a name based on domains if not provided
|
|
name: additionalOptions.name || `Legacy route for ${domainConfig.domains.join(', ')}`,
|
|
// Include a note that this was converted from a legacy config
|
|
description: additionalOptions.description || 'Converted from legacy domain configuration'
|
|
};
|
|
|
|
// Add optional properties if provided
|
|
if (additionalOptions.priority !== undefined) {
|
|
routeConfig.priority = additionalOptions.priority;
|
|
}
|
|
|
|
if (additionalOptions.tags) {
|
|
routeConfig.tags = additionalOptions.tags;
|
|
}
|
|
|
|
return routeConfig;
|
|
}
|
|
|
|
/**
|
|
* Convert an array of legacy domain configs to route configurations
|
|
* @param domainConfigs Array of legacy domain configurations
|
|
* @returns Array of route configurations
|
|
* @deprecated This function will be removed in a future version
|
|
*/
|
|
export function domainConfigsToRouteConfigs(
|
|
domainConfigs: ILegacyDomainConfig[]
|
|
): IRouteConfig[] {
|
|
return domainConfigs.map(config => domainConfigToRouteConfig(config));
|
|
}
|
|
|
|
/**
|
|
* Extract domains from a route configuration
|
|
* @param route Route configuration
|
|
* @returns Array of domains
|
|
*/
|
|
export function extractDomainsFromRoute(route: IRouteConfig): string[] {
|
|
if (!route.match.domains) {
|
|
return [];
|
|
}
|
|
|
|
return Array.isArray(route.match.domains)
|
|
? route.match.domains
|
|
: [route.match.domains];
|
|
}
|
|
|
|
/**
|
|
* Extract domains from an array of route configurations
|
|
* @param routes Array of route configurations
|
|
* @returns Array of unique domains
|
|
*/
|
|
export function extractDomainsFromRoutes(routes: IRouteConfig[]): string[] {
|
|
const domains = new Set<string>();
|
|
|
|
for (const route of routes) {
|
|
const routeDomains = extractDomainsFromRoute(route);
|
|
for (const domain of routeDomains) {
|
|
domains.add(domain);
|
|
}
|
|
}
|
|
|
|
return Array.from(domains);
|
|
} |