fix(routing): unify route based architecture

This commit is contained in:
2025-05-13 12:48:41 +00:00
parent fe632bde67
commit fcc8cf9caa
46 changed files with 7943 additions and 1332 deletions

View File

@ -33,10 +33,8 @@ export interface ISmartProxyOptions {
// The unified configuration array (required)
routes: IRouteConfig[];
// Port range configuration
globalPortRanges?: Array<{ from: number; to: number }>;
forwardAllGlobalRanges?: boolean;
preserveSourceIP?: boolean;
// Port configuration
preserveSourceIP?: boolean; // Preserve client IP when forwarding
// Global/default settings
defaults?: {
@ -140,6 +138,11 @@ export interface IConnectionRecord {
hasReceivedInitialData: boolean; // Whether initial data has been received
routeConfig?: IRouteConfig; // Associated route config for this connection
// Target information (for dynamic port/host mapping)
targetHost?: string; // Resolved target host
targetPort?: number; // Resolved target port
tlsVersion?: string; // TLS version (for routing context)
// Keep-alive tracking
hasKeepAlive: boolean; // Whether keep-alive is enabled for this connection
inactivityWarningIssued?: boolean; // Whether an inactivity warning has been issued

View File

@ -34,13 +34,43 @@ export interface IRouteMatch {
headers?: Record<string, string | RegExp>; // Match specific HTTP headers
}
/**
* Context provided to port and host mapping functions
*/
export interface IRouteContext {
// Connection information
port: number; // The matched incoming port
domain?: string; // The domain from SNI or Host header
clientIp: string; // The client's IP address
serverIp: string; // The server's IP address
path?: string; // URL path (for HTTP connections)
query?: string; // Query string (for HTTP connections)
headers?: Record<string, string>; // HTTP headers (for HTTP connections)
// TLS information
isTls: boolean; // Whether the connection is TLS
tlsVersion?: string; // TLS version if applicable
// Route information
routeName?: string; // The name of the matched route
routeId?: string; // The ID of the matched route
// Target information (resolved from dynamic mapping)
targetHost?: string; // The resolved target host
targetPort?: number; // The resolved target port
// Additional properties
timestamp: number; // The request timestamp
connectionId: string; // Unique connection identifier
}
/**
* Target configuration for forwarding
*/
export interface IRouteTarget {
host: string | string[]; // Support single host or round-robin
port: number;
preservePort?: boolean; // Use incoming port as target port
host: string | string[] | ((context: any) => string | string[]); // Support static or dynamic host selection with any compatible context
port: number | ((context: any) => number); // Support static or dynamic port mapping with any compatible context
preservePort?: boolean; // Use incoming port as target port (ignored if port is a function)
}
/**
@ -115,6 +145,16 @@ export interface IRouteTestResponse {
body: string;
}
/**
* URL rewriting configuration
*/
export interface IRouteUrlRewrite {
pattern: string; // RegExp pattern to match in URL
target: string; // Replacement pattern (supports template variables like {domain})
flags?: string; // RegExp flags like 'g' for global replacement
onlyRewritePath?: boolean; // Only apply to path, not query string
}
/**
* Advanced options for route actions
*/
@ -124,6 +164,7 @@ export interface IRouteAdvanced {
keepAlive?: boolean;
staticFiles?: IRouteStaticFiles;
testResponse?: IRouteTestResponse;
urlRewrite?: IRouteUrlRewrite; // URL rewriting configuration
// Additional advanced options would go here
}
@ -131,10 +172,15 @@ export interface IRouteAdvanced {
* WebSocket configuration
*/
export interface IRouteWebSocket {
enabled: boolean;
pingInterval?: number;
pingTimeout?: number;
maxPayloadSize?: number;
enabled: boolean; // Whether WebSockets are enabled for this route
pingInterval?: number; // Interval for sending ping frames (ms)
pingTimeout?: number; // Timeout for pong response (ms)
maxPayloadSize?: number; // Maximum message size in bytes
customHeaders?: Record<string, string>; // Custom headers for WebSocket handshake
subprotocols?: string[]; // Supported subprotocols
rewritePath?: string; // Path rewriting for WebSocket connections
allowedOrigins?: string[]; // Allowed origins for WebSocket connections
authenticateRequest?: boolean; // Whether to apply route security to WebSocket connections
}
/**
@ -181,6 +227,12 @@ export interface IRouteAction {
// Advanced options
advanced?: IRouteAdvanced;
// Additional options for backend-specific settings
options?: {
backendProtocol?: 'http1' | 'http2';
[key: string]: any;
};
}
/**
@ -219,12 +271,27 @@ export interface IRouteSecurity {
ipBlockList?: string[];
}
/**
* CORS configuration for a route
*/
export interface IRouteCors {
enabled: boolean; // Whether CORS is enabled for this route
allowOrigin?: string | string[]; // Allowed origins (*,domain.com,[domain1,domain2])
allowMethods?: string; // Allowed methods (GET,POST,etc.)
allowHeaders?: string; // Allowed headers
allowCredentials?: boolean; // Whether to allow credentials
exposeHeaders?: string; // Headers to expose to the client
maxAge?: number; // Preflight cache duration in seconds
preflight?: boolean; // Whether to respond to preflight requests
}
/**
* Headers configuration
*/
export interface IRouteHeaders {
request?: Record<string, string>;
response?: Record<string, string>;
request?: Record<string, string>; // Headers to add/modify for requests to backend
response?: Record<string, string>; // Headers to add/modify for responses to client
cors?: IRouteCors; // CORS configuration
}
/**