2025-05-09 21:21:28 +00:00
|
|
|
import * as plugins from '../../plugins.js';
|
|
|
|
import type { DomainConfig, SmartProxyOptions } from './models/interfaces.js';
|
|
|
|
import type { ForwardingType, ForwardConfig } from '../../forwarding/config/forwarding-types.js';
|
|
|
|
import type { ForwardingHandler } from '../../forwarding/handlers/base-handler.js';
|
|
|
|
import { ForwardingHandlerFactory } from '../../forwarding/factory/forwarding-factory.js';
|
2025-03-14 09:53:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages domain configurations and target selection
|
|
|
|
*/
|
|
|
|
export class DomainConfigManager {
|
|
|
|
// Track round-robin indices for domain configs
|
2025-05-09 21:21:28 +00:00
|
|
|
private domainTargetIndices: Map<DomainConfig, number> = new Map();
|
2025-05-09 14:15:45 +00:00
|
|
|
|
|
|
|
// Cache forwarding handlers for each domain config
|
2025-05-09 21:21:28 +00:00
|
|
|
private forwardingHandlers: Map<DomainConfig, ForwardingHandler> = new Map();
|
2025-05-09 14:15:45 +00:00
|
|
|
|
2025-05-09 21:21:28 +00:00
|
|
|
constructor(private settings: SmartProxyOptions) {}
|
2025-03-14 09:53:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the domain configurations
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public updateDomainConfigs(newDomainConfigs: DomainConfig[]): void {
|
2025-03-14 09:53:25 +00:00
|
|
|
this.settings.domainConfigs = newDomainConfigs;
|
2025-05-09 14:15:45 +00:00
|
|
|
|
2025-03-14 09:53:25 +00:00
|
|
|
// Reset target indices for removed configs
|
|
|
|
const currentConfigSet = new Set(newDomainConfigs);
|
|
|
|
for (const [config] of this.domainTargetIndices) {
|
|
|
|
if (!currentConfigSet.has(config)) {
|
|
|
|
this.domainTargetIndices.delete(config);
|
|
|
|
}
|
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
|
|
|
|
// Clear handlers for removed configs and create handlers for new configs
|
2025-05-09 21:21:28 +00:00
|
|
|
const handlersToRemove: DomainConfig[] = [];
|
2025-05-09 14:15:45 +00:00
|
|
|
for (const [config] of this.forwardingHandlers) {
|
|
|
|
if (!currentConfigSet.has(config)) {
|
|
|
|
handlersToRemove.push(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove handlers that are no longer needed
|
|
|
|
for (const config of handlersToRemove) {
|
|
|
|
this.forwardingHandlers.delete(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create handlers for new configs
|
|
|
|
for (const config of newDomainConfigs) {
|
|
|
|
if (!this.forwardingHandlers.has(config)) {
|
|
|
|
try {
|
|
|
|
const handler = this.createForwardingHandler(config);
|
|
|
|
this.forwardingHandlers.set(config, handler);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(`Error creating forwarding handler for domain ${config.domains.join(', ')}: ${err}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-03-14 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all domain configurations
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getDomainConfigs(): DomainConfig[] {
|
2025-03-14 09:53:25 +00:00
|
|
|
return this.settings.domainConfigs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find domain config matching a server name
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public findDomainConfig(serverName: string): DomainConfig | undefined {
|
2025-03-14 09:53:25 +00:00
|
|
|
if (!serverName) return undefined;
|
|
|
|
|
|
|
|
return this.settings.domainConfigs.find((config) =>
|
|
|
|
config.domains.some((d) => plugins.minimatch(serverName, d))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find domain config for a specific port
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public findDomainConfigForPort(port: number): DomainConfig | undefined {
|
2025-03-14 09:53:25 +00:00
|
|
|
return this.settings.domainConfigs.find(
|
2025-05-09 15:39:15 +00:00
|
|
|
(domain) => {
|
|
|
|
const portRanges = domain.forwarding?.advanced?.portRanges;
|
|
|
|
return portRanges &&
|
|
|
|
portRanges.length > 0 &&
|
|
|
|
this.isPortInRanges(port, portRanges);
|
|
|
|
}
|
2025-03-14 09:53:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a port is within any of the given ranges
|
|
|
|
*/
|
|
|
|
public isPortInRanges(port: number, ranges: Array<{ from: number; to: number }>): boolean {
|
|
|
|
return ranges.some((range) => port >= range.from && port <= range.to);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get target IP with round-robin support
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getTargetIP(domainConfig: DomainConfig): string {
|
2025-05-09 14:15:45 +00:00
|
|
|
const targetHosts = Array.isArray(domainConfig.forwarding.target.host)
|
|
|
|
? domainConfig.forwarding.target.host
|
|
|
|
: [domainConfig.forwarding.target.host];
|
|
|
|
|
|
|
|
if (targetHosts.length > 0) {
|
2025-03-14 09:53:25 +00:00
|
|
|
const currentIndex = this.domainTargetIndices.get(domainConfig) || 0;
|
2025-05-09 14:15:45 +00:00
|
|
|
const ip = targetHosts[currentIndex % targetHosts.length];
|
2025-03-14 09:53:25 +00:00
|
|
|
this.domainTargetIndices.set(domainConfig, currentIndex + 1);
|
|
|
|
return ip;
|
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
|
2025-03-14 09:53:25 +00:00
|
|
|
return this.settings.targetIP || 'localhost';
|
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
|
2025-05-09 15:39:15 +00:00
|
|
|
/**
|
|
|
|
* Get target host with round-robin support (for tests)
|
|
|
|
* This is just an alias for getTargetIP for easier test compatibility
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getTargetHost(domainConfig: DomainConfig): string {
|
2025-05-09 15:39:15 +00:00
|
|
|
return this.getTargetIP(domainConfig);
|
|
|
|
}
|
|
|
|
|
2025-05-09 14:15:45 +00:00
|
|
|
/**
|
|
|
|
* Get target port from domain config
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getTargetPort(domainConfig: DomainConfig, defaultPort: number): number {
|
2025-05-09 14:15:45 +00:00
|
|
|
return domainConfig.forwarding.target.port || defaultPort;
|
|
|
|
}
|
2025-03-14 09:53:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a domain should use NetworkProxy
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public shouldUseNetworkProxy(domainConfig: DomainConfig): boolean {
|
2025-05-09 14:15:45 +00:00
|
|
|
const forwardingType = this.getForwardingType(domainConfig);
|
2025-05-09 15:39:15 +00:00
|
|
|
return forwardingType === 'https-terminate-to-http' ||
|
|
|
|
forwardingType === 'https-terminate-to-https';
|
2025-03-14 09:53:25 +00:00
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
|
2025-03-14 09:53:25 +00:00
|
|
|
/**
|
|
|
|
* Gets the NetworkProxy port for a domain
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getNetworkProxyPort(domainConfig: DomainConfig): number | undefined {
|
2025-05-09 14:15:45 +00:00
|
|
|
// First check if we should use NetworkProxy at all
|
|
|
|
if (!this.shouldUseNetworkProxy(domainConfig)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2025-05-09 15:39:15 +00:00
|
|
|
return domainConfig.forwarding.advanced?.networkProxyPort || this.settings.networkProxyPort;
|
2025-03-14 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get effective allowed and blocked IPs for a domain
|
2025-05-09 15:39:15 +00:00
|
|
|
*
|
|
|
|
* This method combines domain-specific security rules from the forwarding configuration
|
|
|
|
* with global security defaults when necessary.
|
2025-03-14 09:53:25 +00:00
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getEffectiveIPRules(domainConfig: DomainConfig): {
|
2025-03-14 09:53:25 +00:00
|
|
|
allowedIPs: string[],
|
|
|
|
blockedIPs: string[]
|
|
|
|
} {
|
2025-05-09 14:15:45 +00:00
|
|
|
// Start with empty arrays
|
|
|
|
const allowedIPs: string[] = [];
|
|
|
|
const blockedIPs: string[] = [];
|
|
|
|
|
2025-05-09 15:39:15 +00:00
|
|
|
// Add IPs from forwarding security settings if available
|
2025-05-09 14:15:45 +00:00
|
|
|
if (domainConfig.forwarding?.security?.allowedIps) {
|
|
|
|
allowedIPs.push(...domainConfig.forwarding.security.allowedIps);
|
2025-05-09 15:39:15 +00:00
|
|
|
} else {
|
|
|
|
// If no allowed IPs are specified in forwarding config and global defaults exist, use them
|
|
|
|
if (this.settings.defaultAllowedIPs && this.settings.defaultAllowedIPs.length > 0) {
|
|
|
|
allowedIPs.push(...this.settings.defaultAllowedIPs);
|
|
|
|
} else {
|
|
|
|
// Default to allow all if no specific rules
|
|
|
|
allowedIPs.push('*');
|
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
}
|
|
|
|
|
2025-05-09 15:39:15 +00:00
|
|
|
// Add blocked IPs from forwarding security settings if available
|
2025-05-09 14:15:45 +00:00
|
|
|
if (domainConfig.forwarding?.security?.blockedIps) {
|
|
|
|
blockedIPs.push(...domainConfig.forwarding.security.blockedIps);
|
|
|
|
}
|
|
|
|
|
2025-05-09 15:39:15 +00:00
|
|
|
// Always add global blocked IPs, even if domain has its own rules
|
|
|
|
// This ensures that global blocks take precedence
|
2025-05-09 14:15:45 +00:00
|
|
|
if (this.settings.defaultBlockedIPs && this.settings.defaultBlockedIPs.length > 0) {
|
2025-05-09 15:39:15 +00:00
|
|
|
// Add only unique IPs that aren't already in the list
|
|
|
|
for (const ip of this.settings.defaultBlockedIPs) {
|
|
|
|
if (!blockedIPs.includes(ip)) {
|
|
|
|
blockedIPs.push(ip);
|
|
|
|
}
|
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
}
|
|
|
|
|
2025-03-14 09:53:25 +00:00
|
|
|
return {
|
2025-05-09 14:15:45 +00:00
|
|
|
allowedIPs,
|
|
|
|
blockedIPs
|
2025-03-14 09:53:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get connection timeout for a domain
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getConnectionTimeout(domainConfig?: DomainConfig): number {
|
2025-05-09 15:39:15 +00:00
|
|
|
if (domainConfig?.forwarding.advanced?.timeout) {
|
2025-05-09 14:15:45 +00:00
|
|
|
return domainConfig.forwarding.advanced.timeout;
|
|
|
|
}
|
|
|
|
|
2025-03-14 09:53:25 +00:00
|
|
|
return this.settings.maxConnectionLifetime || 86400000; // 24 hours default
|
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a forwarding handler for a domain configuration
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
private createForwardingHandler(domainConfig: DomainConfig): ForwardingHandler {
|
2025-05-09 14:15:45 +00:00
|
|
|
// Create a new handler using the factory
|
|
|
|
const handler = ForwardingHandlerFactory.createHandler(domainConfig.forwarding);
|
|
|
|
|
|
|
|
// Initialize the handler
|
|
|
|
handler.initialize().catch(err => {
|
|
|
|
console.log(`Error initializing forwarding handler for ${domainConfig.domains.join(', ')}: ${err}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a forwarding handler for a domain config
|
|
|
|
* If no handler exists, creates one
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getForwardingHandler(domainConfig: DomainConfig): ForwardingHandler {
|
2025-05-09 14:15:45 +00:00
|
|
|
// If we already have a handler, return it
|
|
|
|
if (this.forwardingHandlers.has(domainConfig)) {
|
|
|
|
return this.forwardingHandlers.get(domainConfig)!;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise create a new handler
|
|
|
|
const handler = this.createForwardingHandler(domainConfig);
|
|
|
|
this.forwardingHandlers.set(domainConfig, handler);
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the forwarding type for a domain config
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public getForwardingType(domainConfig?: DomainConfig): ForwardingType | undefined {
|
2025-05-09 14:15:45 +00:00
|
|
|
if (!domainConfig?.forwarding) return undefined;
|
|
|
|
return domainConfig.forwarding.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the forwarding type requires TLS termination
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public requiresTlsTermination(domainConfig?: DomainConfig): boolean {
|
2025-05-09 14:15:45 +00:00
|
|
|
if (!domainConfig) return false;
|
|
|
|
|
|
|
|
const forwardingType = this.getForwardingType(domainConfig);
|
|
|
|
return forwardingType === 'https-terminate-to-http' ||
|
|
|
|
forwardingType === 'https-terminate-to-https';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the forwarding type supports HTTP
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public supportsHttp(domainConfig?: DomainConfig): boolean {
|
2025-05-09 14:15:45 +00:00
|
|
|
if (!domainConfig) return false;
|
|
|
|
|
|
|
|
const forwardingType = this.getForwardingType(domainConfig);
|
|
|
|
|
|
|
|
// HTTP-only always supports HTTP
|
|
|
|
if (forwardingType === 'http-only') return true;
|
|
|
|
|
|
|
|
// For termination types, check the HTTP settings
|
|
|
|
if (forwardingType === 'https-terminate-to-http' ||
|
|
|
|
forwardingType === 'https-terminate-to-https') {
|
|
|
|
// HTTP is supported by default for termination types
|
|
|
|
return domainConfig.forwarding?.http?.enabled !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPS-passthrough doesn't support HTTP
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if HTTP requests should be redirected to HTTPS
|
|
|
|
*/
|
2025-05-09 21:21:28 +00:00
|
|
|
public shouldRedirectToHttps(domainConfig?: DomainConfig): boolean {
|
2025-05-09 14:15:45 +00:00
|
|
|
if (!domainConfig?.forwarding) return false;
|
|
|
|
|
|
|
|
// Only check for redirect if HTTP is enabled
|
|
|
|
if (this.supportsHttp(domainConfig)) {
|
|
|
|
return !!domainConfig.forwarding.http?.redirectToHttps;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2025-03-14 09:53:25 +00:00
|
|
|
}
|