4.3 KiB
4.3 KiB
SmartProxy Enhanced Routing Plan
Goal
Implement enhanced routing structure with multiple targets per route, sub-matching capabilities, and target-specific overrides to enable more elegant and DRY configurations.
Key Changes
1. Update Route Target Interface
- Add
match
property toIRouteTarget
for sub-matching within routes - Add target-specific override properties (tls, websocket, loadBalancing, etc.)
- Add priority field for controlling match order
2. Update Route Action Interface
- Remove singular
target
property - Use only
targets
array (single target = array with one element) - Maintain backwards compatibility during migration
3. Implementation Steps
Phase 1: Type Updates
- Update
IRouteTarget
interface inroute-types.ts
- Add
match?: ITargetMatch
property - Add override properties (tls, websocket, etc.)
- Add
priority?: number
field
- Add
- Create
ITargetMatch
interface for sub-matching criteria - Update
IRouteAction
to use onlytargets: IRouteTarget[]
Phase 2: Route Resolution Logic
- Update route matching logic to handle multiple targets
- Implement target sub-matching algorithm:
- Sort targets by priority (highest first)
- For each target with a match property, check if request matches
- Use first matching target, or fallback to target without match
- Ensure target-specific settings override route-level settings
Phase 3: Code Migration
- Find all occurrences of
action.target
and update toaction.targets[0]
- Update route helpers and utilities
- Update certificate manager to handle multiple targets
- Update connection handlers
Phase 4: Testing
- Update existing tests to use new format
- Add tests for multi-target scenarios
- Add tests for sub-matching logic
- Add tests for setting overrides
Phase 5: Documentation
- Update type documentation
- Add examples of new routing patterns
- Document migration path for existing configs
Example Configurations
Before (Current)
// Need separate routes for different ports/paths
[
{
match: { domains: ['api.example.com'], ports: [80] },
action: {
type: 'forward',
target: { host: 'backend', port: 8080 },
tls: { mode: 'terminate' }
}
},
{
match: { domains: ['api.example.com'], ports: [443] },
action: {
type: 'forward',
target: { host: 'backend', port: 8081 },
tls: { mode: 'passthrough' }
}
}
]
After (Enhanced)
// Single route with multiple targets
{
match: { domains: ['api.example.com'], ports: [80, 443] },
action: {
type: 'forward',
targets: [
{
match: { ports: [80] },
host: 'backend',
port: 8080,
tls: { mode: 'terminate' }
},
{
match: { ports: [443] },
host: 'backend',
port: 8081,
tls: { mode: 'passthrough' }
}
]
}
}
Advanced Example
{
match: { domains: ['app.example.com'], ports: [443] },
action: {
type: 'forward',
tls: { mode: 'terminate', certificate: 'auto' }, // Route-level default
websocket: { enabled: true }, // Route-level default
targets: [
{
match: { path: '/api/v2/*' },
host: 'api-v2',
port: 8082,
priority: 10
},
{
match: { path: '/api/*', headers: { 'X-Version': 'v1' } },
host: 'api-v1',
port: 8081,
priority: 5
},
{
match: { path: '/ws/*' },
host: 'websocket-server',
port: 8090,
websocket: {
enabled: true,
rewritePath: '/' // Strip /ws prefix
}
},
{
// Default target (no match property)
host: 'web-backend',
port: 8080
}
]
}
}
Benefits
- DRY Configuration: No need to duplicate common settings across routes
- Flexibility: Different backends for different ports/paths within same domain
- Clarity: All routing for a domain in one place
- Performance: Single route lookup instead of multiple
- Backwards Compatible: Can migrate gradually
Migration Strategy
- Keep support for
target
temporarily with deprecation warning - Auto-convert
target
totargets: [target]
internally - Update documentation with migration examples
- Remove
target
support in next major version