@push.rocks/smartproxy 19.5.19 → 19.5.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/dist_ts/core/models/index.d.ts +2 -0
  2. package/dist_ts/core/models/index.js +3 -1
  3. package/dist_ts/core/models/socket-types.d.ts +14 -0
  4. package/dist_ts/core/models/socket-types.js +15 -0
  5. package/dist_ts/core/models/wrapped-socket.d.ts +34 -0
  6. package/dist_ts/core/models/wrapped-socket.js +82 -0
  7. package/dist_ts/core/routing/index.d.ts +11 -0
  8. package/dist_ts/core/routing/index.js +17 -0
  9. package/dist_ts/core/routing/matchers/domain.d.ts +34 -0
  10. package/dist_ts/core/routing/matchers/domain.js +91 -0
  11. package/dist_ts/core/routing/matchers/header.d.ts +32 -0
  12. package/dist_ts/core/routing/matchers/header.js +94 -0
  13. package/dist_ts/core/routing/matchers/index.d.ts +18 -0
  14. package/dist_ts/core/routing/matchers/index.js +20 -0
  15. package/dist_ts/core/routing/matchers/ip.d.ts +53 -0
  16. package/dist_ts/core/routing/matchers/ip.js +169 -0
  17. package/dist_ts/core/routing/matchers/path.d.ts +44 -0
  18. package/dist_ts/core/routing/matchers/path.js +148 -0
  19. package/dist_ts/core/routing/route-manager.d.ts +88 -0
  20. package/dist_ts/core/routing/route-manager.js +342 -0
  21. package/dist_ts/core/routing/route-utils.d.ts +28 -0
  22. package/dist_ts/core/routing/route-utils.js +67 -0
  23. package/dist_ts/core/routing/specificity.d.ts +30 -0
  24. package/dist_ts/core/routing/specificity.js +115 -0
  25. package/dist_ts/core/routing/types.d.ts +41 -0
  26. package/dist_ts/core/routing/types.js +5 -0
  27. package/dist_ts/core/utils/index.d.ts +1 -2
  28. package/dist_ts/core/utils/index.js +2 -3
  29. package/dist_ts/core/utils/proxy-protocol.d.ts +45 -0
  30. package/dist_ts/core/utils/proxy-protocol.js +201 -0
  31. package/dist_ts/core/utils/route-manager.d.ts +0 -30
  32. package/dist_ts/core/utils/route-manager.js +6 -47
  33. package/dist_ts/core/utils/route-utils.d.ts +2 -68
  34. package/dist_ts/core/utils/route-utils.js +21 -218
  35. package/dist_ts/core/utils/security-utils.js +4 -4
  36. package/dist_ts/index.d.ts +2 -5
  37. package/dist_ts/index.js +5 -11
  38. package/dist_ts/proxies/http-proxy/http-proxy.d.ts +0 -1
  39. package/dist_ts/proxies/http-proxy/http-proxy.js +15 -60
  40. package/dist_ts/proxies/http-proxy/models/types.d.ts +0 -90
  41. package/dist_ts/proxies/http-proxy/models/types.js +1 -242
  42. package/dist_ts/proxies/http-proxy/request-handler.d.ts +3 -5
  43. package/dist_ts/proxies/http-proxy/request-handler.js +20 -171
  44. package/dist_ts/proxies/http-proxy/websocket-handler.d.ts +2 -5
  45. package/dist_ts/proxies/http-proxy/websocket-handler.js +15 -23
  46. package/dist_ts/proxies/index.d.ts +2 -2
  47. package/dist_ts/proxies/index.js +4 -3
  48. package/dist_ts/proxies/smart-proxy/connection-manager.d.ts +3 -1
  49. package/dist_ts/proxies/smart-proxy/connection-manager.js +17 -7
  50. package/dist_ts/proxies/smart-proxy/http-proxy-bridge.d.ts +2 -1
  51. package/dist_ts/proxies/smart-proxy/http-proxy-bridge.js +5 -2
  52. package/dist_ts/proxies/smart-proxy/index.d.ts +1 -1
  53. package/dist_ts/proxies/smart-proxy/index.js +2 -2
  54. package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +7 -2
  55. package/dist_ts/proxies/smart-proxy/models/route-types.d.ts +1 -0
  56. package/dist_ts/proxies/smart-proxy/models/route-types.js +1 -1
  57. package/dist_ts/proxies/smart-proxy/route-connection-handler.d.ts +1 -1
  58. package/dist_ts/proxies/smart-proxy/route-connection-handler.js +155 -35
  59. package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +1 -1
  60. package/dist_ts/proxies/smart-proxy/smart-proxy.js +15 -4
  61. package/dist_ts/proxies/smart-proxy/utils/route-utils.js +10 -43
  62. package/dist_ts/routing/router/http-router.d.ts +89 -0
  63. package/dist_ts/routing/router/http-router.js +205 -0
  64. package/dist_ts/routing/router/index.d.ts +2 -5
  65. package/dist_ts/routing/router/index.js +3 -4
  66. package/package.json +1 -1
  67. package/readme.delete.md +187 -0
  68. package/readme.hints.md +196 -1
  69. package/readme.plan.md +625 -0
  70. package/readme.proxy-chain-summary.md +112 -0
  71. package/readme.proxy-protocol-example.md +462 -0
  72. package/readme.proxy-protocol.md +415 -0
  73. package/readme.routing.md +341 -0
  74. package/ts/core/models/index.ts +2 -0
  75. package/ts/core/models/socket-types.ts +21 -0
  76. package/ts/core/models/wrapped-socket.ts +99 -0
  77. package/ts/core/routing/index.ts +21 -0
  78. package/ts/core/routing/matchers/domain.ts +119 -0
  79. package/ts/core/routing/matchers/header.ts +120 -0
  80. package/ts/core/routing/matchers/index.ts +22 -0
  81. package/ts/core/routing/matchers/ip.ts +207 -0
  82. package/ts/core/routing/matchers/path.ts +184 -0
  83. package/ts/core/{utils → routing}/route-manager.ts +7 -57
  84. package/ts/core/routing/route-utils.ts +88 -0
  85. package/ts/core/routing/specificity.ts +141 -0
  86. package/ts/core/routing/types.ts +49 -0
  87. package/ts/core/utils/index.ts +1 -2
  88. package/ts/core/utils/proxy-protocol.ts +246 -0
  89. package/ts/core/utils/security-utils.ts +3 -7
  90. package/ts/index.ts +4 -14
  91. package/ts/proxies/http-proxy/http-proxy.ts +13 -68
  92. package/ts/proxies/http-proxy/models/types.ts +0 -324
  93. package/ts/proxies/http-proxy/request-handler.ts +15 -186
  94. package/ts/proxies/http-proxy/websocket-handler.ts +15 -26
  95. package/ts/proxies/index.ts +3 -2
  96. package/ts/proxies/smart-proxy/connection-manager.ts +17 -7
  97. package/ts/proxies/smart-proxy/http-proxy-bridge.ts +6 -2
  98. package/ts/proxies/smart-proxy/index.ts +1 -1
  99. package/ts/proxies/smart-proxy/models/interfaces.ts +9 -2
  100. package/ts/proxies/smart-proxy/models/route-types.ts +3 -0
  101. package/ts/proxies/smart-proxy/route-connection-handler.ts +173 -42
  102. package/ts/proxies/smart-proxy/smart-proxy.ts +15 -3
  103. package/ts/proxies/smart-proxy/utils/route-utils.ts +11 -49
  104. package/ts/routing/router/http-router.ts +266 -0
  105. package/ts/routing/router/index.ts +3 -8
  106. package/readme.problems.md +0 -170
  107. package/ts/core/utils/route-utils.ts +0 -312
  108. package/ts/proxies/smart-proxy/route-manager.ts +0 -554
  109. package/ts/routing/router/proxy-router.ts +0 -437
  110. package/ts/routing/router/route-router.ts +0 -482
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * HTTP routing
3
3
  */
4
- // Export selectively to avoid ambiguity between duplicate type names
5
- export { ProxyRouter } from './proxy-router.js';
6
- export { RouteRouter } from './route-router.js';
7
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi90cy9yb3V0aW5nL3JvdXRlci9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUVILHFFQUFxRTtBQUNyRSxPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sbUJBQW1CLENBQUM7QUFLaEQsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLG1CQUFtQixDQUFDIn0=
4
+ // Export the unified HttpRouter
5
+ export { HttpRouter } from './http-router.js';
6
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi90cy9yb3V0aW5nL3JvdXRlci9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUVILGdDQUFnQztBQUNoQyxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sa0JBQWtCLENBQUMifQ==
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@push.rocks/smartproxy",
3
- "version": "19.5.19",
3
+ "version": "19.5.21",
4
4
  "private": false,
5
5
  "description": "A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.",
6
6
  "main": "dist_ts/index.js",
@@ -0,0 +1,187 @@
1
+ # SmartProxy Code Deletion Plan
2
+
3
+ This document tracks all code paths that can be deleted as part of the routing unification effort.
4
+
5
+ ## Phase 1: Matching Logic Duplicates (READY TO DELETE)
6
+
7
+ ### 1. Inline Matching Functions in RouteManager
8
+ **File**: `ts/proxies/smart-proxy/route-manager.ts`
9
+ **Lines**: Approximately lines 200-400
10
+ **Duplicates**:
11
+ - `matchDomain()` method - duplicate of DomainMatcher
12
+ - `matchPath()` method - duplicate of PathMatcher
13
+ - `matchIpPattern()` method - duplicate of IpMatcher
14
+ - `matchHeaders()` method - duplicate of HeaderMatcher
15
+ **Action**: Update to use unified matchers from `ts/core/routing/matchers/`
16
+
17
+ ### 2. Duplicate Matching in Core route-utils
18
+ **File**: `ts/core/utils/route-utils.ts`
19
+ **Functions to update**:
20
+ - `matchDomain()` → Use DomainMatcher.match()
21
+ - `matchPath()` → Use PathMatcher.match()
22
+ - `matchIpPattern()` → Use IpMatcher.match()
23
+ - `matchHeader()` → Use HeaderMatcher.match()
24
+ **Action**: Update to use unified matchers, keep only unique utilities
25
+
26
+ ## Phase 2: Route Manager Duplicates (READY AFTER MIGRATION)
27
+
28
+ ### 1. SmartProxy RouteManager
29
+ **File**: `ts/proxies/smart-proxy/route-manager.ts`
30
+ **Entire file**: ~500 lines
31
+ **Reason**: 95% duplicate of SharedRouteManager
32
+ **Migration Required**:
33
+ - Update SmartProxy to use SharedRouteManager
34
+ - Update all imports
35
+ - Test thoroughly
36
+ **Action**: DELETE entire file after migration
37
+
38
+ ### 2. Deprecated Methods in SharedRouteManager
39
+ **File**: `ts/core/utils/route-manager.ts`
40
+ **Methods**:
41
+ - Any deprecated security check methods
42
+ - Legacy compatibility methods
43
+ **Action**: Remove after confirming no usage
44
+
45
+ ## Phase 3: Router Consolidation (REQUIRES REFACTORING)
46
+
47
+ ### 1. ProxyRouter vs RouteRouter Duplication
48
+ **Files**:
49
+ - `ts/routing/router/proxy-router.ts` (~250 lines)
50
+ - `ts/routing/router/route-router.ts` (~250 lines)
51
+ **Reason**: Nearly identical implementations
52
+ **Plan**: Merge into single HttpRouter with legacy adapter
53
+ **Action**: DELETE one file after consolidation
54
+
55
+ ### 2. Inline Route Matching in HttpProxy
56
+ **Location**: Various files in `ts/proxies/http-proxy/`
57
+ **Pattern**: Direct route matching without using RouteManager
58
+ **Action**: Update to use SharedRouteManager
59
+
60
+ ## Phase 4: Scattered Utilities (CLEANUP)
61
+
62
+ ### 1. Duplicate Route Utilities
63
+ **Files with duplicate logic**:
64
+ - `ts/proxies/smart-proxy/utils/route-utils.ts` - Keep (different purpose)
65
+ - `ts/proxies/smart-proxy/utils/route-validators.ts` - Review for duplicates
66
+ - `ts/proxies/smart-proxy/utils/route-patterns.ts` - Review for consolidation
67
+
68
+ ### 2. Legacy Type Definitions
69
+ **Review for removal**:
70
+ - Old route type definitions
71
+ - Deprecated configuration interfaces
72
+ - Unused type exports
73
+
74
+ ## Deletion Progress Tracker
75
+
76
+ ### Completed Deletions
77
+ - [x] Phase 1: Matching logic consolidation (Partial)
78
+ - Updated core/utils/route-utils.ts to use unified matchers
79
+ - Removed duplicate matching implementations (~200 lines)
80
+ - Marked functions as deprecated with migration path
81
+ - [x] Phase 2: RouteManager unification (COMPLETED)
82
+ - ✓ Migrated SmartProxy to use SharedRouteManager
83
+ - ✓ Updated imports in smart-proxy.ts, route-connection-handler.ts, and index.ts
84
+ - ✓ Created logger adapter to match ILogger interface expectations
85
+ - ✓ Fixed method calls (getAllRoutes → getRoutes)
86
+ - ✓ Fixed type errors in header matcher
87
+ - ✓ Removed unused ipToNumber imports and methods
88
+ - ✓ DELETED: `/ts/proxies/smart-proxy/route-manager.ts` (553 lines removed)
89
+ - [x] Phase 3: Router consolidation (COMPLETED)
90
+ - ✓ Created unified HttpRouter with legacy compatibility
91
+ - ✓ Migrated ProxyRouter and RouteRouter to use HttpRouter aliases
92
+ - ✓ Updated imports in http-proxy.ts, request-handler.ts, websocket-handler.ts
93
+ - ✓ Added routeReqLegacy() method for backward compatibility
94
+ - ✓ DELETED: `/ts/routing/router/proxy-router.ts` (437 lines)
95
+ - ✓ DELETED: `/ts/routing/router/route-router.ts` (482 lines)
96
+ - [x] Phase 4: Architecture cleanup (COMPLETED)
97
+ - ✓ Updated route-utils.ts to use unified matchers directly
98
+ - ✓ Removed deprecated methods from SharedRouteManager
99
+ - ✓ Fixed HeaderMatcher.matchMultiple → matchAll method name
100
+ - ✓ Fixed findMatchingRoute return type handling (IRouteMatchResult)
101
+ - ✓ Fixed header type conversion for RegExp patterns
102
+ - ✓ DELETED: Duplicate RouteManager class from http-proxy/models/types.ts (~200 lines)
103
+ - ✓ Updated all imports to use SharedRouteManager from core/utils
104
+ - ✓ Fixed PathMatcher exact match behavior (added $ anchor for non-wildcard patterns)
105
+ - ✓ Updated test expectations to match unified matcher behavior
106
+ - ✓ All TypeScript errors resolved and build successful
107
+ - [x] Phase 5: Remove all backward compatibility code (COMPLETED)
108
+ - ✓ Removed routeReqLegacy() method from HttpRouter
109
+ - ✓ Removed all legacy compatibility methods from HttpRouter (~130 lines)
110
+ - ✓ Removed LegacyRouterResult interface
111
+ - ✓ Removed ProxyRouter and RouteRouter aliases
112
+ - ✓ Updated RequestHandler to remove legacyRouter parameter and legacy routing fallback (~80 lines)
113
+ - ✓ Updated WebSocketHandler to remove legacyRouter parameter and legacy routing fallback
114
+ - ✓ Updated HttpProxy to use only unified HttpRouter
115
+ - ✓ Removed IReverseProxyConfig interface (deprecated legacy interface)
116
+ - ✓ Removed useExternalPort80Handler deprecated option
117
+ - ✓ Removed backward compatibility exports from index.ts
118
+ - ✓ Removed all deprecated functions from route-utils.ts (~50 lines)
119
+ - ✓ Clean build with no legacy code
120
+
121
+ ### Files Updated
122
+ 1. `ts/core/utils/route-utils.ts` - Replaced all matching logic with unified matchers
123
+ 2. `ts/core/utils/security-utils.ts` - Updated to use IpMatcher directly
124
+ 3. `ts/proxies/smart-proxy/smart-proxy.ts` - Using SharedRouteManager with logger adapter
125
+ 4. `ts/proxies/smart-proxy/route-connection-handler.ts` - Updated to use SharedRouteManager
126
+ 5. `ts/proxies/smart-proxy/index.ts` - Exporting SharedRouteManager as RouteManager
127
+ 6. `ts/core/routing/matchers/header.ts` - Fixed type handling for array header values
128
+ 7. `ts/core/utils/route-manager.ts` - Removed unused ipToNumber import
129
+ 8. `ts/proxies/http-proxy/http-proxy.ts` - Updated imports to use unified router
130
+ 9. `ts/proxies/http-proxy/request-handler.ts` - Updated to use routeReqLegacy()
131
+ 10. `ts/proxies/http-proxy/websocket-handler.ts` - Updated to use routeReqLegacy()
132
+ 11. `ts/routing/router/index.ts` - Export unified HttpRouter with aliases
133
+ 12. `ts/proxies/smart-proxy/utils/route-utils.ts` - Updated to use unified matchers directly
134
+ 13. `ts/proxies/http-proxy/request-handler.ts` - Fixed findMatchingRoute usage
135
+ 14. `ts/proxies/http-proxy/models/types.ts` - Removed duplicate RouteManager class
136
+ 15. `ts/index.ts` - Updated exports to use SharedRouteManager aliases
137
+ 16. `ts/proxies/index.ts` - Updated exports to use SharedRouteManager aliases
138
+ 17. `test/test.acme-route-creation.ts` - Fixed getAllRoutes → getRoutes method call
139
+
140
+ ### Files Created
141
+ 1. `ts/core/routing/matchers/domain.ts` - Unified domain matcher
142
+ 2. `ts/core/routing/matchers/path.ts` - Unified path matcher
143
+ 3. `ts/core/routing/matchers/ip.ts` - Unified IP matcher
144
+ 4. `ts/core/routing/matchers/header.ts` - Unified header matcher
145
+ 5. `ts/core/routing/matchers/index.ts` - Matcher exports
146
+ 6. `ts/core/routing/types.ts` - Core routing types
147
+ 7. `ts/core/routing/specificity.ts` - Route specificity calculator
148
+ 8. `ts/core/routing/index.ts` - Main routing exports
149
+ 9. `ts/routing/router/http-router.ts` - Unified HTTP router
150
+
151
+ ### Lines of Code Removed
152
+ - Target: ~1,500 lines
153
+ - Actual: ~2,332 lines (Target exceeded by 55%!)
154
+ - Phase 1: ~200 lines (matching logic)
155
+ - Phase 2: 553 lines (SmartProxy RouteManager)
156
+ - Phase 3: 919 lines (ProxyRouter + RouteRouter)
157
+ - Phase 4: ~200 lines (Duplicate RouteManager from http-proxy)
158
+ - Phase 5: ~460 lines (Legacy compatibility code)
159
+
160
+ ## Unified Routing Architecture Summary
161
+
162
+ The routing unification effort has successfully:
163
+ 1. **Created unified matchers** - Consistent matching logic across all route types
164
+ - DomainMatcher: Wildcard domain matching with specificity calculation
165
+ - PathMatcher: Path pattern matching with parameter extraction
166
+ - IpMatcher: IP address and CIDR notation matching
167
+ - HeaderMatcher: HTTP header matching with regex support
168
+ 2. **Consolidated route managers** - Single SharedRouteManager for all proxies
169
+ 3. **Unified routers** - Single HttpRouter for all HTTP routing needs
170
+ 4. **Removed ~2,332 lines of code** - Exceeded target by 55%
171
+ 5. **Clean modern architecture** - No legacy code, no backward compatibility layers
172
+
173
+ ## Safety Checklist Before Deletion
174
+
175
+ Before deleting any code:
176
+ 1. ✓ All tests pass
177
+ 2. ✓ No references to deleted code remain
178
+ 3. ✓ Migration path tested
179
+ 4. ✓ Performance benchmarks show no regression
180
+ 5. ✓ Documentation updated
181
+
182
+ ## Rollback Plan
183
+
184
+ If issues arise after deletion:
185
+ 1. Git history preserves all deleted code
186
+ 2. Each phase can be reverted independently
187
+ 3. Feature flags can disable new code if needed
package/readme.hints.md CHANGED
@@ -661,4 +661,199 @@ Updated all forwarding handlers to use the new centralized socket utilities:
661
661
  - Reduced code duplication
662
662
 
663
663
  ### Migration Notes
664
- No user-facing changes. All forwarding handlers now use the same robust socket handling as the main SmartProxy connection handler.
664
+ No user-facing changes. All forwarding handlers now use the same robust socket handling as the main SmartProxy connection handler.
665
+
666
+ ## WrappedSocket Class Evaluation for PROXY Protocol (v19.5.19+)
667
+
668
+ ### Current Socket Handling Architecture
669
+ - Sockets are handled directly as `net.Socket` instances throughout the codebase
670
+ - Socket augmentation via TypeScript module augmentation for TLS properties
671
+ - Metadata tracked separately in `IConnectionRecord` objects
672
+ - Socket utilities provide helper functions but don't encapsulate the socket
673
+ - Connection records track extensive metadata (IDs, timestamps, byte counters, TLS state, etc.)
674
+
675
+ ### Evaluation: Should We Introduce a WrappedSocket Class?
676
+
677
+ **Yes, a WrappedSocket class would make sense**, particularly for PROXY protocol implementation and future extensibility.
678
+
679
+ ### Design Considerations for WrappedSocket
680
+
681
+ ```typescript
682
+ class WrappedSocket {
683
+ private socket: net.Socket;
684
+ private connectionId: string;
685
+ private metadata: {
686
+ realClientIP?: string; // From PROXY protocol
687
+ realClientPort?: number; // From PROXY protocol
688
+ proxyIP?: string; // Immediate connection IP
689
+ proxyPort?: number; // Immediate connection port
690
+ bytesReceived: number;
691
+ bytesSent: number;
692
+ lastActivity: number;
693
+ isTLS: boolean;
694
+ // ... other metadata
695
+ };
696
+
697
+ // PROXY protocol handling
698
+ private proxyProtocolParsed: boolean = false;
699
+ private pendingData: Buffer[] = [];
700
+
701
+ constructor(socket: net.Socket) {
702
+ this.socket = socket;
703
+ this.setupHandlers();
704
+ }
705
+
706
+ // Getters for clean access
707
+ get remoteAddress(): string {
708
+ return this.metadata.realClientIP || this.socket.remoteAddress || '';
709
+ }
710
+
711
+ get remotePort(): number {
712
+ return this.metadata.realClientPort || this.socket.remotePort || 0;
713
+ }
714
+
715
+ get isFromTrustedProxy(): boolean {
716
+ return !!this.metadata.realClientIP;
717
+ }
718
+
719
+ // PROXY protocol parsing
720
+ async parseProxyProtocol(trustedProxies: string[]): Promise<boolean> {
721
+ // Implementation here
722
+ }
723
+
724
+ // Delegate socket methods
725
+ write(data: any): boolean {
726
+ this.metadata.bytesSent += Buffer.byteLength(data);
727
+ return this.socket.write(data);
728
+ }
729
+
730
+ destroy(error?: Error): void {
731
+ this.socket.destroy(error);
732
+ }
733
+
734
+ // Event forwarding
735
+ on(event: string, listener: Function): this {
736
+ this.socket.on(event, listener);
737
+ return this;
738
+ }
739
+ }
740
+ ```
741
+
742
+ ### Implementation Benefits
743
+
744
+ 1. **Encapsulation**: Bundle socket + metadata + behavior in one place
745
+ 2. **PROXY Protocol Integration**: Cleaner handling without modifying existing socket code
746
+ 3. **State Management**: Centralized socket state tracking and validation
747
+ 4. **API Consistency**: Uniform interface for all socket operations
748
+ 5. **Future Extensibility**: Easy to add new socket-level features (compression, encryption, etc.)
749
+ 6. **Type Safety**: Better TypeScript support without module augmentation
750
+ 7. **Testing**: Easier to mock and test socket behavior
751
+
752
+ ### Implementation Drawbacks
753
+
754
+ 1. **Major Refactoring**: Would require changes throughout the codebase
755
+ 2. **Performance Overhead**: Additional abstraction layer (minimal but present)
756
+ 3. **Compatibility**: Need to maintain event emitter compatibility
757
+ 4. **Learning Curve**: Developers need to understand the wrapper
758
+
759
+ ### Recommended Approach: Phased Implementation
760
+
761
+ **Phase 1: PROXY Protocol Only** (Immediate)
762
+ - Create minimal `ProxyProtocolSocket` wrapper for new connections from trusted proxies
763
+ - Use in connection handler when receiving from trusted proxy IPs
764
+ - Minimal disruption to existing code
765
+
766
+ ```typescript
767
+ class ProxyProtocolSocket {
768
+ constructor(
769
+ public socket: net.Socket,
770
+ public realClientIP?: string,
771
+ public realClientPort?: number
772
+ ) {}
773
+
774
+ get remoteAddress(): string {
775
+ return this.realClientIP || this.socket.remoteAddress || '';
776
+ }
777
+
778
+ get remotePort(): number {
779
+ return this.realClientPort || this.socket.remotePort || 0;
780
+ }
781
+ }
782
+ ```
783
+
784
+ **Phase 2: Gradual Migration** (Future)
785
+ - Extend wrapper with more functionality
786
+ - Migrate critical paths to use wrapper
787
+ - Add performance monitoring
788
+
789
+ **Phase 3: Full Adoption** (Long-term)
790
+ - Complete migration to WrappedSocket
791
+ - Remove socket augmentation
792
+ - Standardize all socket handling
793
+
794
+ ### Decision Summary
795
+
796
+ ✅ **Implement minimal ProxyProtocolSocket for immediate PROXY protocol support**
797
+ - Low risk, high value
798
+ - Solves the immediate proxy chain connection limit issue
799
+ - Sets foundation for future improvements
800
+ - Can be implemented alongside existing code
801
+
802
+ 📋 **Consider full WrappedSocket for future major version**
803
+ - Cleaner architecture
804
+ - Better maintainability
805
+ - But requires significant refactoring
806
+
807
+ ## WrappedSocket Implementation (PROXY Protocol Phase 1) - v19.5.19+
808
+
809
+ The WrappedSocket class has been implemented as the foundation for PROXY protocol support:
810
+
811
+ ### Implementation Details
812
+
813
+ 1. **Design Approach**: Uses JavaScript Proxy to delegate all Socket methods/properties to the underlying socket while allowing override of specific properties (remoteAddress, remotePort).
814
+
815
+ 2. **Key Design Decisions**:
816
+ - NOT a Duplex stream - Initially tried this approach but it created infinite loops
817
+ - Simple wrapper using Proxy pattern for transparent delegation
818
+ - All sockets are wrapped, not just those from trusted proxies
819
+ - Trusted proxy detection happens after wrapping
820
+
821
+ 3. **Usage Pattern**:
822
+ ```typescript
823
+ // In RouteConnectionHandler.handleConnection()
824
+ const wrappedSocket = new WrappedSocket(socket);
825
+ // Pass wrappedSocket throughout the flow
826
+
827
+ // When calling socket-utils functions, extract underlying socket:
828
+ const underlyingSocket = getUnderlyingSocket(socket);
829
+ setupBidirectionalForwarding(underlyingSocket, targetSocket, {...});
830
+ ```
831
+
832
+ 4. **Important Implementation Notes**:
833
+ - Socket utility functions (setupBidirectionalForwarding, cleanupSocket) expect raw net.Socket
834
+ - Always extract underlying socket before passing to these utilities using `getUnderlyingSocket()`
835
+ - WrappedSocket preserves all Socket functionality through Proxy delegation
836
+ - TypeScript typing handled via index signature: `[key: string]: any`
837
+
838
+ 5. **Files Modified**:
839
+ - `ts/core/models/wrapped-socket.ts` - The WrappedSocket implementation
840
+ - `ts/core/models/socket-types.ts` - Helper functions and type guards
841
+ - `ts/proxies/smart-proxy/route-connection-handler.ts` - Updated to wrap all incoming sockets
842
+ - `ts/proxies/smart-proxy/connection-manager.ts` - Updated to accept WrappedSocket
843
+ - `ts/proxies/smart-proxy/http-proxy-bridge.ts` - Updated to handle WrappedSocket
844
+
845
+ 6. **Test Coverage**:
846
+ - `test/test.wrapped-socket-forwarding.ts` - Verifies data forwarding through wrapped sockets
847
+
848
+ ### Next Steps for PROXY Protocol
849
+ - Phase 2: Parse PROXY protocol header from trusted proxies
850
+ - Phase 3: Update real client IP/port after parsing
851
+ - Phase 4: Test with HAProxy and AWS ELB
852
+ - Phase 5: Documentation and configuration
853
+
854
+ ## Proxy Protocol Documentation
855
+
856
+ For detailed information about proxy protocol implementation and proxy chaining:
857
+ - **[Proxy Protocol Guide](./readme.proxy-protocol.md)** - Complete implementation details and configuration
858
+ - **[Proxy Protocol Examples](./readme.proxy-protocol-example.md)** - Code examples and conceptual implementation
859
+ - **[Proxy Chain Summary](./readme.proxy-chain-summary.md)** - Quick reference for proxy chaining setup