feat(router): Add detailed routing tests and refactor ProxyRouter for improved path matching
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartproxy',
|
||||
version: '3.27.0',
|
||||
version: '3.28.0',
|
||||
description: 'A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.'
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as http from 'http';
|
||||
import * as url from 'url';
|
||||
import * as tsclass from '@tsclass/tsclass';
|
||||
|
||||
/**
|
||||
* Optional path pattern configuration that can be added to proxy configs
|
||||
@ -11,31 +13,37 @@ export interface IPathPatternConfig {
|
||||
* Interface for router result with additional metadata
|
||||
*/
|
||||
export interface IRouterResult {
|
||||
config: plugins.tsclass.network.IReverseProxyConfig;
|
||||
config: tsclass.network.IReverseProxyConfig;
|
||||
pathMatch?: string;
|
||||
pathParams?: Record<string, string>;
|
||||
pathRemainder?: string;
|
||||
}
|
||||
|
||||
export class ProxyRouter {
|
||||
// Using a Map for O(1) hostname lookups instead of array search
|
||||
private hostMap: Map<string, plugins.tsclass.network.IReverseProxyConfig[]> = new Map();
|
||||
// Store original configs for reference
|
||||
private reverseProxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
||||
private reverseProxyConfigs: tsclass.network.IReverseProxyConfig[] = [];
|
||||
// Default config to use when no match is found (optional)
|
||||
private defaultConfig?: plugins.tsclass.network.IReverseProxyConfig;
|
||||
private defaultConfig?: tsclass.network.IReverseProxyConfig;
|
||||
// Store path patterns separately since they're not in the original interface
|
||||
private pathPatterns: Map<plugins.tsclass.network.IReverseProxyConfig, string> = new Map();
|
||||
private pathPatterns: Map<tsclass.network.IReverseProxyConfig, string> = new Map();
|
||||
// Logger interface
|
||||
private logger: {
|
||||
error: (message: string, data?: any) => void;
|
||||
warn: (message: string, data?: any) => void;
|
||||
info: (message: string, data?: any) => void;
|
||||
debug: (message: string, data?: any) => void;
|
||||
};
|
||||
|
||||
constructor(
|
||||
configs?: plugins.tsclass.network.IReverseProxyConfig[],
|
||||
private readonly logger: {
|
||||
configs?: tsclass.network.IReverseProxyConfig[],
|
||||
logger?: {
|
||||
error: (message: string, data?: any) => void;
|
||||
warn: (message: string, data?: any) => void;
|
||||
info: (message: string, data?: any) => void;
|
||||
debug: (message: string, data?: any) => void;
|
||||
} = console
|
||||
}
|
||||
) {
|
||||
this.logger = logger || console;
|
||||
if (configs) {
|
||||
this.setNewProxyConfigs(configs);
|
||||
}
|
||||
@ -45,61 +53,13 @@ export class ProxyRouter {
|
||||
* Sets a new set of reverse configs to be routed to
|
||||
* @param reverseCandidatesArg Array of reverse proxy configurations
|
||||
*/
|
||||
public setNewProxyConfigs(reverseCandidatesArg: plugins.tsclass.network.IReverseProxyConfig[]): void {
|
||||
public setNewProxyConfigs(reverseCandidatesArg: tsclass.network.IReverseProxyConfig[]): void {
|
||||
this.reverseProxyConfigs = [...reverseCandidatesArg];
|
||||
|
||||
// Reset the host map and path patterns
|
||||
this.hostMap.clear();
|
||||
this.pathPatterns.clear();
|
||||
|
||||
// Find default config if any (config with "*" as hostname)
|
||||
this.defaultConfig = this.reverseProxyConfigs.find(config => config.hostName === '*');
|
||||
|
||||
// Group configs by hostname for faster lookups
|
||||
for (const config of this.reverseProxyConfigs) {
|
||||
// Skip the default config as it's stored separately
|
||||
if (config.hostName === '*') continue;
|
||||
|
||||
const hostname = config.hostName.toLowerCase(); // Case-insensitive hostname lookup
|
||||
|
||||
if (!this.hostMap.has(hostname)) {
|
||||
this.hostMap.set(hostname, []);
|
||||
}
|
||||
|
||||
// Check for path pattern in extended properties
|
||||
// (using any to access custom properties not in the interface)
|
||||
const extendedConfig = config as any;
|
||||
if (extendedConfig.pathPattern) {
|
||||
this.pathPatterns.set(config, extendedConfig.pathPattern);
|
||||
}
|
||||
|
||||
// Add to the list of configs for this hostname
|
||||
this.hostMap.get(hostname).push(config);
|
||||
}
|
||||
|
||||
// Sort configs for each hostname by specificity
|
||||
// More specific path patterns should be checked first
|
||||
for (const [hostname, configs] of this.hostMap.entries()) {
|
||||
if (configs.length > 1) {
|
||||
// Sort by pathPattern - most specific first
|
||||
// (null comes last, exact paths before patterns with wildcards)
|
||||
configs.sort((a, b) => {
|
||||
const aPattern = this.pathPatterns.get(a);
|
||||
const bPattern = this.pathPatterns.get(b);
|
||||
|
||||
// If one has a path and the other doesn't, the one with a path comes first
|
||||
if (!aPattern && bPattern) return 1;
|
||||
if (aPattern && !bPattern) return -1;
|
||||
if (!aPattern && !bPattern) return 0;
|
||||
|
||||
// Both have path patterns - more specific (longer) first
|
||||
// This is a simple heuristic; we could use a more sophisticated approach
|
||||
return bPattern.length - aPattern.length;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.info(`Router initialized with ${this.reverseProxyConfigs.length} configs (${this.hostMap.size} unique hosts)`);
|
||||
this.logger.info(`Router initialized with ${this.reverseProxyConfigs.length} configs (${this.getHostnames().length} unique hosts)`);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -107,7 +67,7 @@ export class ProxyRouter {
|
||||
* @param req The incoming HTTP request
|
||||
* @returns The matching proxy config or undefined if no match found
|
||||
*/
|
||||
public routeReq(req: plugins.http.IncomingMessage): plugins.tsclass.network.IReverseProxyConfig {
|
||||
public routeReq(req: http.IncomingMessage): tsclass.network.IReverseProxyConfig {
|
||||
const result = this.routeReqWithDetails(req);
|
||||
return result ? result.config : undefined;
|
||||
}
|
||||
@ -117,7 +77,7 @@ export class ProxyRouter {
|
||||
* @param req The incoming HTTP request
|
||||
* @returns Detailed routing result including matched config and path information
|
||||
*/
|
||||
public routeReqWithDetails(req: plugins.http.IncomingMessage): IRouterResult | undefined {
|
||||
public routeReqWithDetails(req: http.IncomingMessage): IRouterResult | undefined {
|
||||
// Extract and validate host header
|
||||
const originalHost = req.headers.host;
|
||||
if (!originalHost) {
|
||||
@ -126,52 +86,27 @@ export class ProxyRouter {
|
||||
}
|
||||
|
||||
// Parse URL for path matching
|
||||
const urlPath = new URL(
|
||||
req.url || '/',
|
||||
`http://${originalHost}`
|
||||
).pathname;
|
||||
const parsedUrl = url.parse(req.url || '/');
|
||||
const urlPath = parsedUrl.pathname || '/';
|
||||
|
||||
// Extract hostname without port
|
||||
const hostWithoutPort = originalHost.split(':')[0].toLowerCase();
|
||||
|
||||
// Find configs for this hostname
|
||||
const configs = this.hostMap.get(hostWithoutPort);
|
||||
|
||||
if (configs && configs.length > 0) {
|
||||
// Check each config for path matching
|
||||
for (const config of configs) {
|
||||
// Get the path pattern if any
|
||||
const pathPattern = this.pathPatterns.get(config);
|
||||
|
||||
// If no path pattern specified, this config matches all paths
|
||||
if (!pathPattern) {
|
||||
return { config };
|
||||
}
|
||||
|
||||
// Check if path matches the pattern
|
||||
const pathMatch = this.matchPath(urlPath, pathPattern);
|
||||
if (pathMatch) {
|
||||
return {
|
||||
config,
|
||||
pathMatch: pathMatch.matched,
|
||||
pathParams: pathMatch.params,
|
||||
pathRemainder: pathMatch.remainder
|
||||
};
|
||||
}
|
||||
}
|
||||
// First try exact hostname match
|
||||
const exactConfig = this.findConfigForHost(hostWithoutPort, urlPath);
|
||||
if (exactConfig) {
|
||||
return exactConfig;
|
||||
}
|
||||
|
||||
// Try wildcard subdomains if no direct match found
|
||||
// For example, if request is for sub.example.com, try *.example.com
|
||||
const domainParts = hostWithoutPort.split('.');
|
||||
if (domainParts.length > 2) {
|
||||
const wildcardDomain = `*.${domainParts.slice(1).join('.')}`;
|
||||
const wildcardConfigs = this.hostMap.get(wildcardDomain);
|
||||
|
||||
if (wildcardConfigs && wildcardConfigs.length > 0) {
|
||||
// Use the first matching wildcard config
|
||||
// Could add path matching logic here as well
|
||||
return { config: wildcardConfigs[0] };
|
||||
// Try wildcard subdomain
|
||||
if (hostWithoutPort.includes('.')) {
|
||||
const domainParts = hostWithoutPort.split('.');
|
||||
if (domainParts.length > 2) {
|
||||
const wildcardDomain = `*.${domainParts.slice(1).join('.')}`;
|
||||
const wildcardConfig = this.findConfigForHost(wildcardDomain, urlPath);
|
||||
if (wildcardConfig) {
|
||||
return wildcardConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -186,23 +121,62 @@ export class ProxyRouter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a path pattern for an existing config
|
||||
* @param config The existing configuration
|
||||
* @param pathPattern The path pattern to set
|
||||
* @returns Boolean indicating if the config was found and updated
|
||||
* Find a config for a specific host and path
|
||||
*/
|
||||
public setPathPattern(
|
||||
config: plugins.tsclass.network.IReverseProxyConfig,
|
||||
pathPattern: string
|
||||
): boolean {
|
||||
const exists = this.reverseProxyConfigs.includes(config);
|
||||
if (exists) {
|
||||
this.pathPatterns.set(config, pathPattern);
|
||||
return true;
|
||||
private findConfigForHost(hostname: string, path: string): IRouterResult | undefined {
|
||||
// Find all configs for this hostname
|
||||
const configs = this.reverseProxyConfigs.filter(
|
||||
config => config.hostName.toLowerCase() === hostname.toLowerCase()
|
||||
);
|
||||
|
||||
if (configs.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
return false;
|
||||
|
||||
// First try configs with path patterns
|
||||
const configsWithPaths = configs.filter(config => this.pathPatterns.has(config));
|
||||
|
||||
// Sort by path pattern specificity - more specific first
|
||||
configsWithPaths.sort((a, b) => {
|
||||
const aPattern = this.pathPatterns.get(a) || '';
|
||||
const bPattern = this.pathPatterns.get(b) || '';
|
||||
|
||||
// Exact patterns come before wildcard patterns
|
||||
const aHasWildcard = aPattern.includes('*');
|
||||
const bHasWildcard = bPattern.includes('*');
|
||||
|
||||
if (aHasWildcard && !bHasWildcard) return 1;
|
||||
if (!aHasWildcard && bHasWildcard) return -1;
|
||||
|
||||
// Longer patterns are considered more specific
|
||||
return bPattern.length - aPattern.length;
|
||||
});
|
||||
|
||||
// Check each config with path pattern
|
||||
for (const config of configsWithPaths) {
|
||||
const pathPattern = this.pathPatterns.get(config);
|
||||
if (pathPattern) {
|
||||
const pathMatch = this.matchPath(path, pathPattern);
|
||||
if (pathMatch) {
|
||||
return {
|
||||
config,
|
||||
pathMatch: pathMatch.matched,
|
||||
pathParams: pathMatch.params,
|
||||
pathRemainder: pathMatch.remainder
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no path pattern matched, use the first config without a path pattern
|
||||
const configWithoutPath = configs.find(config => !this.pathPatterns.has(config));
|
||||
if (configWithoutPath) {
|
||||
return { config: configWithoutPath };
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Matches a URL path against a pattern
|
||||
* Supports:
|
||||
@ -242,62 +216,51 @@ export class ProxyRouter {
|
||||
}
|
||||
|
||||
// Handle path parameters
|
||||
const patternParts = pattern.split('/');
|
||||
const pathParts = path.split('/');
|
||||
const patternParts = pattern.split('/').filter(p => p);
|
||||
const pathParts = path.split('/').filter(p => p);
|
||||
|
||||
// Check if paths are compatible length
|
||||
if (
|
||||
// If pattern doesn't end with wildcard, paths must have the same number of parts
|
||||
(!pattern.endsWith('/*') && patternParts.length !== pathParts.length) ||
|
||||
// If pattern ends with wildcard, path must have at least as many parts as the pattern
|
||||
(pattern.endsWith('/*') && pathParts.length < patternParts.length - 1)
|
||||
) {
|
||||
// Too few path parts to match
|
||||
if (pathParts.length < patternParts.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const params: Record<string, string> = {};
|
||||
const matchedParts: string[] = [];
|
||||
|
||||
// Compare path parts
|
||||
// Compare each part
|
||||
for (let i = 0; i < patternParts.length; i++) {
|
||||
const patternPart = patternParts[i];
|
||||
|
||||
// Handle wildcard at the end
|
||||
if (patternPart === '*' && i === patternParts.length - 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
// If pathParts[i] doesn't exist, we've reached the end of the path
|
||||
if (i >= pathParts.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pathPart = pathParts[i];
|
||||
|
||||
// Handle parameter
|
||||
if (patternPart.startsWith(':')) {
|
||||
const paramName = patternPart.slice(1);
|
||||
params[paramName] = pathPart;
|
||||
matchedParts.push(pathPart);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle wildcard at the end
|
||||
if (patternPart === '*' && i === patternParts.length - 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Handle exact match for this part
|
||||
if (patternPart !== pathPart) {
|
||||
return null;
|
||||
}
|
||||
|
||||
matchedParts.push(pathPart);
|
||||
}
|
||||
|
||||
// Calculate the remainder
|
||||
let remainder = '';
|
||||
if (pattern.endsWith('/*')) {
|
||||
remainder = '/' + pathParts.slice(patternParts.length - 1).join('/');
|
||||
}
|
||||
// Calculate the remainder - the unmatched path parts
|
||||
const remainderParts = pathParts.slice(patternParts.length);
|
||||
const remainder = remainderParts.length ? '/' + remainderParts.join('/') : '';
|
||||
|
||||
// Calculate the matched path
|
||||
const matchedParts = patternParts.map((part, i) => {
|
||||
return part.startsWith(':') ? pathParts[i] : part;
|
||||
});
|
||||
const matched = '/' + matchedParts.join('/');
|
||||
|
||||
return {
|
||||
matched: matchedParts.join('/'),
|
||||
matched,
|
||||
params,
|
||||
remainder
|
||||
};
|
||||
@ -307,7 +270,7 @@ export class ProxyRouter {
|
||||
* Gets all currently active proxy configurations
|
||||
* @returns Array of all active configurations
|
||||
*/
|
||||
public getProxyConfigs(): plugins.tsclass.network.IReverseProxyConfig[] {
|
||||
public getProxyConfigs(): tsclass.network.IReverseProxyConfig[] {
|
||||
return [...this.reverseProxyConfigs];
|
||||
}
|
||||
|
||||
@ -316,7 +279,13 @@ export class ProxyRouter {
|
||||
* @returns Array of hostnames
|
||||
*/
|
||||
public getHostnames(): string[] {
|
||||
return Array.from(this.hostMap.keys());
|
||||
const hostnames = new Set<string>();
|
||||
for (const config of this.reverseProxyConfigs) {
|
||||
if (config.hostName !== '*') {
|
||||
hostnames.add(config.hostName.toLowerCase());
|
||||
}
|
||||
}
|
||||
return Array.from(hostnames);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -325,7 +294,7 @@ export class ProxyRouter {
|
||||
* @param pathPattern Optional path pattern for route matching
|
||||
*/
|
||||
public addProxyConfig(
|
||||
config: plugins.tsclass.network.IReverseProxyConfig,
|
||||
config: tsclass.network.IReverseProxyConfig,
|
||||
pathPattern?: string
|
||||
): void {
|
||||
this.reverseProxyConfigs.push(config);
|
||||
@ -334,8 +303,24 @@ export class ProxyRouter {
|
||||
if (pathPattern) {
|
||||
this.pathPatterns.set(config, pathPattern);
|
||||
}
|
||||
|
||||
this.setNewProxyConfigs(this.reverseProxyConfigs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a path pattern for an existing config
|
||||
* @param config The existing configuration
|
||||
* @param pathPattern The path pattern to set
|
||||
* @returns Boolean indicating if the config was found and updated
|
||||
*/
|
||||
public setPathPattern(
|
||||
config: tsclass.network.IReverseProxyConfig,
|
||||
pathPattern: string
|
||||
): boolean {
|
||||
const exists = this.reverseProxyConfigs.includes(config);
|
||||
if (exists) {
|
||||
this.pathPatterns.set(config, pathPattern);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -345,15 +330,22 @@ export class ProxyRouter {
|
||||
*/
|
||||
public removeProxyConfig(hostname: string): boolean {
|
||||
const initialCount = this.reverseProxyConfigs.length;
|
||||
|
||||
// Find configs to remove
|
||||
const configsToRemove = this.reverseProxyConfigs.filter(
|
||||
config => config.hostName === hostname
|
||||
);
|
||||
|
||||
// Remove them from the patterns map
|
||||
for (const config of configsToRemove) {
|
||||
this.pathPatterns.delete(config);
|
||||
}
|
||||
|
||||
// Filter them out of the configs array
|
||||
this.reverseProxyConfigs = this.reverseProxyConfigs.filter(
|
||||
config => config.hostName !== hostname
|
||||
);
|
||||
|
||||
if (initialCount !== this.reverseProxyConfigs.length) {
|
||||
this.setNewProxyConfigs(this.reverseProxyConfigs);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return this.reverseProxyConfigs.length !== initialCount;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user