5.4 KiB
5.4 KiB
SmartProxy Architecture Refactoring Plan
Overview
Refactor the proxy architecture to provide clearer separation of concerns between HTTP/HTTPS traffic handling and low-level connection routing.
Current Architecture Problems
- NetworkProxy name doesn't clearly indicate it handles HTTP/HTTPS
- HTTP parsing logic is duplicated in RouteConnectionHandler
- Redirect and static route handling is embedded in SmartProxy
- Unclear separation between TCP routing and HTTP processing
Proposed Architecture
HttpProxy (renamed from NetworkProxy)
Purpose: Handle all HTTP/HTTPS traffic with TLS termination
Responsibilities:
- TLS termination for HTTPS
- HTTP/1.1 and HTTP/2 protocol handling
- HTTP request/response parsing
- HTTP to HTTPS redirects
- ACME challenge handling
- Static route handlers
- WebSocket protocol upgrades
- Connection pooling for backend servers
- Certificate management (ACME and static)
SmartProxy
Purpose: Low-level connection router and port manager
Responsibilities:
- Port management (listen on multiple ports)
- Route-based connection routing
- TLS passthrough (SNI-based routing)
- NFTables integration
- Delegate HTTP/HTTPS connections to HttpProxy
- Raw TCP proxying
- Connection lifecycle management
Implementation Plan
Phase 1: Rename and Reorganize NetworkProxy ✅
-
Rename NetworkProxy to HttpProxy
- Renamed directory from
network-proxy
tohttp-proxy
- Updated all imports and references
- Renamed directory from
-
Update class and file names
- Renamed
network-proxy.ts
tohttp-proxy.ts
- Updated
NetworkProxy
class toHttpProxy
class - Updated all type definitions and interfaces
- Renamed
-
Update exports
- Updated exports in
ts/index.ts
- Fixed imports across the codebase
- Updated exports in
Phase 2: Extract HTTP Logic from SmartProxy ✅
-
Create HTTP handler modules in HttpProxy
- Created handlers directory with:
redirect-handler.ts
- HTTP redirect logicstatic-handler.ts
- Static/ACME route handlingindex.ts
- Module exports
- Created handlers directory with:
-
Move HTTP parsing from RouteConnectionHandler
- Updated
handleRedirectAction
to delegate toRedirectHandler
- Updated
handleStaticAction
to delegate toStaticHandler
- Removed duplicated HTTP parsing logic
- Updated
-
Clean up references and naming
- Updated all NetworkProxy references to HttpProxy
- Renamed config properties:
useNetworkProxy
→useHttpProxy
- Renamed config properties:
networkProxyPort
→httpProxyPort
- Fixed HttpProxyBridge methods and references
Phase 3: Simplify SmartProxy
-
Update RouteConnectionHandler
- Remove embedded HTTP parsing
- Delegate HTTP routes to HttpProxy
- Focus on connection routing only
-
Simplified route handling
// Simplified handleRedirectAction private handleRedirectAction(socket, record, route) { // Delegate to HttpProxy this.httpProxy.handleRedirect(socket, route); } // Simplified handleStaticAction private handleStaticAction(socket, record, route) { // Delegate to HttpProxy this.httpProxy.handleStatic(socket, route); }
-
Update NetworkProxyBridge
- Rename to HttpProxyBridge
- Update integration points
Phase 4: Consolidate HTTP Utilities ✅
-
Move HTTP types to http-proxy
- Created consolidated
http-types.ts
ints/proxies/http-proxy/models/
- Includes HTTP status codes, error classes, and interfaces
- Added helper functions like
getStatusText()
- Created consolidated
-
Clean up ts/http directory
- Kept only router functionality
- Replaced local HTTP types with re-exports from HttpProxy
- Updated imports throughout the codebase to use consolidated types
Phase 5: Update Tests and Documentation ✅
-
Update test files
- Renamed NetworkProxy references to HttpProxy
- Renamed test files to match new naming
- Updated imports and references throughout tests
- Fixed certificate manager method names
-
Update documentation
- Updated README to reflect HttpProxy naming
- Updated architecture descriptions
- Updated usage examples
- Fixed all API documentation references
Migration Steps
- Create feature branch:
refactor/http-proxy-consolidation
- Phase 1: Rename NetworkProxy (1 day)
- Phase 2: Extract HTTP logic (2 days)
- Phase 3: Simplify SmartProxy (1 day)
- Phase 4: Consolidate utilities (1 day)
- Phase 5: Update tests/docs (1 day)
- Integration testing (1 day)
- Code review and merge
Benefits
- Clear Separation: HTTP/HTTPS handling is clearly separated from TCP routing
- Better Naming: HttpProxy clearly indicates its purpose
- No Duplication: HTTP parsing logic exists in one place
- Maintainability: Easier to modify HTTP handling without affecting routing
- Testability: Each component has a single responsibility
- Performance: Optimized paths for different traffic types
Future Enhancements
After this refactoring, we can more easily add:
- HTTP/3 (QUIC) support in HttpProxy
- Advanced HTTP features (compression, caching)
- HTTP middleware system
- Protocol-specific optimizations
- Better HTTP/2 multiplexing
Breaking Changes
NetworkProxy
class renamed toHttpProxy
- Import paths change from
network-proxy
tohttp-proxy
- Some type names may change for consistency
Rollback Plan
If issues arise:
- Git revert to previous commit
- Re-deploy previous version
- Document lessons learned
- Plan incremental changes