10 KiB
SmartProxy Fully Unified Configuration Plan (Updated)
Project Goal
Redesign SmartProxy's configuration for a more elegant, unified, and comprehensible approach by:
- Creating a single, unified configuration model that covers both "where to listen" and "how to forward"
- Eliminating the confusion between domain configs and port forwarding
- Providing a clear, declarative API that makes the intent obvious
- Enhancing maintainability and extensibility for future features
- Completely removing legacy code to eliminate technical debt
Current Issues
The current approach has several issues:
-
Dual Configuration Systems:
- Port configuration (
fromPort
,toPort
,globalPortRanges
) for "where to listen" - Domain configuration (
domainConfigs
) for "how to forward" - Unclear relationship between these two systems
- Port configuration (
-
Mixed Concerns:
- Port management is mixed with forwarding logic
- Domain routing is separated from port listening
- Security settings defined in multiple places
-
Complex Logic:
- PortRangeManager must coordinate with domain configuration
- Implicit rules for handling connections based on port and domain
-
Difficult to Understand and Configure:
- Two separate configuration hierarchies that must work together
- Unclear which settings take precedence
Proposed Solution: Fully Unified Routing Configuration
Replace both port and domain configuration with a single, unified configuration:
// The core unified configuration interface
interface IRouteConfig {
// What to match
match: {
// Listen on these ports (required)
ports: number | number[] | Array<{ from: number, to: number }>;
// Optional domain patterns to match (default: all domains)
domains?: string | string[];
// Advanced matching criteria
path?: string; // Match specific paths
clientIp?: string[]; // Match specific client IPs
tlsVersion?: string[]; // Match specific TLS versions
};
// What to do with matched traffic
action: {
// Basic routing
type: 'forward' | 'redirect' | 'block';
// Target for forwarding
target?: {
host: string | string[]; // Support single host or round-robin
port: number;
preservePort?: boolean; // Use incoming port as target port
};
// TLS handling
tls?: {
mode: 'passthrough' | 'terminate' | 'terminate-and-reencrypt';
certificate?: 'auto' | { // Auto = use ACME
key: string;
cert: string;
};
};
// For redirects
redirect?: {
to: string; // URL or template with {domain}, {port}, etc.
status: 301 | 302 | 307 | 308;
};
// Security options
security?: {
allowedIps?: string[];
blockedIps?: string[];
maxConnections?: number;
authentication?: {
type: 'basic' | 'digest' | 'oauth';
// Auth-specific options
};
};
// Advanced options
advanced?: {
timeout?: number;
headers?: Record<string, string>;
keepAlive?: boolean;
// etc.
};
};
// Optional metadata
name?: string; // Human-readable name for this route
description?: string; // Description of the route's purpose
priority?: number; // Controls matching order (higher = matched first)
tags?: string[]; // Arbitrary tags for categorization
}
// Main SmartProxy options
interface ISmartProxyOptions {
// The unified configuration array (required)
routes: IRouteConfig[];
// Global/default settings
defaults?: {
target?: {
host: string;
port: number;
};
security?: {
// Global security defaults
};
tls?: {
// Global TLS defaults
};
// ...other defaults
};
// Other global settings remain (acme, etc.)
acme?: IAcmeOptions;
// Advanced settings remain as well
// ...
}
Revised Implementation Plan
Phase 1: Core Design & Interface Definition
-
Define New Core Interfaces:
- Create
IRouteConfig
interface withmatch
andaction
branches - Define all sub-interfaces for matching and actions
- Create new
ISmartProxyOptions
to useroutes
array exclusively - Define template variable system for dynamic values
- Create
-
Create Helper Functions:
createRoute()
- Basic route creation with reasonable defaultscreateHttpRoute()
,createHttpsRoute()
,createRedirect()
- Common scenarioscreateLoadBalancer()
- For multi-target setupsmergeSecurity()
,mergeDefaults()
- For combining configs
-
Design Router:
- Decision tree for route matching algorithm
- Priority system for route ordering
- Optimized lookup strategy for fast routing
Phase 2: Core Implementation
-
Create RouteManager:
- Build a new RouteManager to replace both PortRangeManager and DomainConfigManager
- Implement port and domain matching in one unified system
- Create efficient route lookup algorithms
-
Implement New ConnectionHandler:
- Create a new ConnectionHandler built from scratch for routes
- Implement the routing logic with the new match/action pattern
- Support template processing for headers and other dynamic values
-
Implement New SmartProxy Core:
- Create new SmartProxy implementation using routes exclusively
- Build network servers based on port specifications
- Manage TLS contexts and certificates
Phase 3: Legacy Code Removal
-
Identify Legacy Components:
- Create an inventory of all files and components to be removed
- Document dependencies between legacy components
- Create a removal plan that minimizes disruption
-
Remove Legacy Components:
- Remove PortRangeManager and related code
- Remove DomainConfigManager and related code
- Remove old ConnectionHandler implementation
- Remove other legacy components
-
Clean Interface Adaptations:
- Remove all legacy interfaces and types
- Update type exports to only expose route-based interfaces
- Remove any adapter or backward compatibility code
Phase 4: Updated Documentation & Examples
-
Update Core Documentation:
- Rewrite README.md with a focus on route-based configuration exclusively
- Create interface reference documentation
- Document all template variables
-
Create Example Library:
- Common configuration patterns using the new API
- Complex use cases for advanced features
- Infrastructure-as-code examples
-
Add Validation Tools:
- Configuration validator to check for issues
- Schema definitions for IDE autocomplete
- Runtime validation helpers
Phase 5: Testing
-
Unit Tests:
- Test route matching logic
- Validate priority handling
- Test template processing
-
Integration Tests:
- Verify full proxy flows with the new system
- Test complex routing scenarios
- Ensure all features work as expected
-
Performance Testing:
- Benchmark routing performance
- Evaluate memory usage
- Test with large numbers of routes
Implementation Strategy
Code Organization
-
New Files:
route-config.ts
- Core route interfacesroute-manager.ts
- Route matching and managementroute-connection-handler.ts
- Connection handling with routesroute-smart-proxy.ts
- Main SmartProxy implementationtemplate-engine.ts
- For variable substitution
-
File Removal:
- Remove
port-range-manager.ts
- Remove
domain-config-manager.ts
- Remove legacy interfaces and adapter code
- Remove backward compatibility shims
- Remove
Transition Strategy
-
Breaking Change Approach:
- This will be a major version update with breaking changes
- No backward compatibility will be maintained
- Clear migration documentation will guide users to the new API
-
Package Structure:
@push.rocks/smartproxy
package will be updated to v14.0.0- Legacy code fully removed, only route-based API exposed
- Support documentation provided for migration
-
Migration Documentation:
- Provide a migration guide with examples
- Show equivalent route configurations for common legacy patterns
- Offer code transformation helpers for complex setups
Benefits of the Clean Approach
-
Reduced Complexity:
- No overlapping or conflicting configuration systems
- No dual maintenance of backward compatibility code
- Simplified internal architecture
-
Cleaner Code Base:
- Removal of technical debt
- Better separation of concerns
- More maintainable codebase
-
Better User Experience:
- Consistent, predictable API
- No confusing overlapping options
- Clear documentation of one approach, not two
-
Future-Proof Design:
- Easier to extend with new features
- Better performance without legacy overhead
- Cleaner foundation for future enhancements
Migration Support
While we're removing backward compatibility from the codebase, we'll provide extensive migration support:
-
Migration Guide:
- Detailed documentation on moving from legacy to route-based config
- Pattern-matching examples for all common use cases
- Troubleshooting guide for common migration issues
-
Conversion Tool:
- Provide a standalone one-time conversion tool
- Takes legacy configuration and outputs route-based equivalents
- Will not be included in the main package to avoid bloat
-
Version Policy:
- Maintain the legacy version (13.x) for security updates
- Make the route-based version a clear major version change (14.0.0)
- Clearly communicate the breaking changes
Timeline and Versioning
-
Development:
- Develop route-based implementation in a separate branch
- Complete full test coverage of new implementation
- Ensure documentation is complete
-
Release:
- Release as version 14.0.0
- Clearly mark as breaking change
- Provide migration guide at release time
-
Support:
- Offer extended support for migration questions
- Consider maintaining security updates for v13.x for 6 months
- Focus active development on route-based version only