2025-03-25 22:30:57 +00:00
|
|
|
import * as plugins from '../plugins.js';
|
2025-05-04 13:49:22 +00:00
|
|
|
import type { IDomainConfig, ISmartProxyOptions } from './classes.pp.interfaces.js';
|
2025-05-09 14:15:45 +00:00
|
|
|
import type { ForwardingType, IForwardConfig } from './types/forwarding.types.js';
|
|
|
|
import { ForwardingHandlerFactory } from './forwarding/forwarding.factory.js';
|
|
|
|
import type { IForwardingHandler } from './forwarding/forwarding.handler.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
|
|
|
|
private domainTargetIndices: Map<IDomainConfig, number> = new Map();
|
2025-05-09 14:15:45 +00:00
|
|
|
|
|
|
|
// Cache forwarding handlers for each domain config
|
|
|
|
private forwardingHandlers: Map<IDomainConfig, IForwardingHandler> = new Map();
|
|
|
|
|
2025-05-04 13:49:22 +00:00
|
|
|
constructor(private settings: ISmartProxyOptions) {}
|
2025-03-14 09:53:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the domain configurations
|
|
|
|
*/
|
|
|
|
public updateDomainConfigs(newDomainConfigs: IDomainConfig[]): void {
|
|
|
|
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
|
|
|
|
const handlersToRemove: IDomainConfig[] = [];
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public getDomainConfigs(): IDomainConfig[] {
|
|
|
|
return this.settings.domainConfigs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find domain config matching a server name
|
|
|
|
*/
|
|
|
|
public findDomainConfig(serverName: string): IDomainConfig | undefined {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public findDomainConfigForPort(port: number): IDomainConfig | undefined {
|
|
|
|
return this.settings.domainConfigs.find(
|
|
|
|
(domain) =>
|
|
|
|
domain.portRanges &&
|
|
|
|
domain.portRanges.length > 0 &&
|
|
|
|
this.isPortInRanges(port, domain.portRanges)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public getTargetIP(domainConfig: IDomainConfig): 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
|
|
|
|
|
|
|
/**
|
|
|
|
* Get target port from domain config
|
|
|
|
*/
|
|
|
|
public getTargetPort(domainConfig: IDomainConfig, defaultPort: number): number {
|
|
|
|
return domainConfig.forwarding.target.port || defaultPort;
|
|
|
|
}
|
2025-03-14 09:53:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a domain should use NetworkProxy
|
|
|
|
*/
|
|
|
|
public shouldUseNetworkProxy(domainConfig: IDomainConfig): boolean {
|
2025-05-09 14:15:45 +00:00
|
|
|
// Check forwarding type first
|
|
|
|
const forwardingType = this.getForwardingType(domainConfig);
|
|
|
|
|
|
|
|
if (forwardingType === 'https-terminate-to-http' ||
|
|
|
|
forwardingType === 'https-terminate-to-https') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to legacy setting
|
2025-03-14 09:53:25 +00:00
|
|
|
return !!domainConfig.useNetworkProxy;
|
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
|
2025-03-14 09:53:25 +00:00
|
|
|
/**
|
|
|
|
* Gets the NetworkProxy port for a domain
|
|
|
|
*/
|
|
|
|
public getNetworkProxyPort(domainConfig: IDomainConfig): 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check forwarding config first
|
|
|
|
if (domainConfig.forwarding?.advanced?.networkProxyPort) {
|
|
|
|
return domainConfig.forwarding.advanced.networkProxyPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to legacy setting
|
|
|
|
return domainConfig.networkProxyPort || this.settings.networkProxyPort;
|
2025-03-14 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get effective allowed and blocked IPs for a domain
|
|
|
|
*/
|
|
|
|
public getEffectiveIPRules(domainConfig: IDomainConfig): {
|
|
|
|
allowedIPs: string[],
|
|
|
|
blockedIPs: string[]
|
|
|
|
} {
|
2025-05-09 14:15:45 +00:00
|
|
|
// Start with empty arrays
|
|
|
|
const allowedIPs: string[] = [];
|
|
|
|
const blockedIPs: string[] = [];
|
|
|
|
|
|
|
|
// Add IPs from forwarding security settings
|
|
|
|
if (domainConfig.forwarding?.security?.allowedIps) {
|
|
|
|
allowedIPs.push(...domainConfig.forwarding.security.allowedIps);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domainConfig.forwarding?.security?.blockedIps) {
|
|
|
|
blockedIPs.push(...domainConfig.forwarding.security.blockedIps);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add legacy settings
|
|
|
|
if (domainConfig.allowedIPs.length > 0) {
|
|
|
|
allowedIPs.push(...domainConfig.allowedIPs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domainConfig.blockedIPs && domainConfig.blockedIPs.length > 0) {
|
|
|
|
blockedIPs.push(...domainConfig.blockedIPs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add global defaults
|
|
|
|
if (this.settings.defaultAllowedIPs && this.settings.defaultAllowedIPs.length > 0) {
|
|
|
|
allowedIPs.push(...this.settings.defaultAllowedIPs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.settings.defaultBlockedIPs && this.settings.defaultBlockedIPs.length > 0) {
|
|
|
|
blockedIPs.push(...this.settings.defaultBlockedIPs);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public getConnectionTimeout(domainConfig?: IDomainConfig): number {
|
2025-05-09 14:15:45 +00:00
|
|
|
// First check forwarding configuration for timeout
|
|
|
|
if (domainConfig?.forwarding?.advanced?.timeout) {
|
|
|
|
return domainConfig.forwarding.advanced.timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to legacy connectionTimeout
|
2025-03-14 09:53:25 +00:00
|
|
|
if (domainConfig?.connectionTimeout) {
|
|
|
|
return domainConfig.connectionTimeout;
|
|
|
|
}
|
2025-05-09 14:15:45 +00:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
private createForwardingHandler(domainConfig: IDomainConfig): IForwardingHandler {
|
|
|
|
if (!domainConfig.forwarding) {
|
|
|
|
throw new Error(`Domain config for ${domainConfig.domains.join(', ')} has no forwarding configuration`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
public getForwardingHandler(domainConfig: IDomainConfig): IForwardingHandler {
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
public getForwardingType(domainConfig?: IDomainConfig): ForwardingType | undefined {
|
|
|
|
if (!domainConfig?.forwarding) return undefined;
|
|
|
|
return domainConfig.forwarding.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the forwarding type requires TLS termination
|
|
|
|
*/
|
|
|
|
public requiresTlsTermination(domainConfig?: IDomainConfig): boolean {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public supportsHttp(domainConfig?: IDomainConfig): boolean {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public shouldRedirectToHttps(domainConfig?: IDomainConfig): boolean {
|
|
|
|
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
|
|
|
}
|