@push.rocks/smartproxy 25.17.9 → 26.0.0

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 (179) hide show
  1. package/changelog.md +16 -0
  2. package/dist_rust/rustproxy_linux_amd64 +0 -0
  3. package/dist_rust/rustproxy_linux_arm64 +0 -0
  4. package/dist_ts/00_commitinfo_data.js +2 -2
  5. package/dist_ts/core/index.d.ts +0 -1
  6. package/dist_ts/core/index.js +1 -2
  7. package/dist_ts/core/models/index.d.ts +0 -1
  8. package/dist_ts/core/models/index.js +1 -2
  9. package/dist_ts/core/utils/index.d.ts +0 -12
  10. package/dist_ts/core/utils/index.js +1 -13
  11. package/dist_ts/index.d.ts +0 -3
  12. package/dist_ts/index.js +2 -7
  13. package/dist_ts/protocols/http/index.d.ts +0 -1
  14. package/dist_ts/protocols/http/index.js +1 -2
  15. package/dist_ts/protocols/index.d.ts +0 -7
  16. package/dist_ts/protocols/index.js +1 -8
  17. package/dist_ts/proxies/smart-proxy/socket-handler-server.js +6 -1
  18. package/dist_ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.d.ts +0 -7
  19. package/dist_ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.js +50 -51
  20. package/dist_ts/routing/index.d.ts +0 -1
  21. package/dist_ts/routing/index.js +1 -3
  22. package/package.json +1 -1
  23. package/ts/00_commitinfo_data.ts +1 -1
  24. package/ts/core/index.ts +0 -1
  25. package/ts/core/models/index.ts +0 -1
  26. package/ts/core/utils/index.ts +0 -12
  27. package/ts/index.ts +1 -7
  28. package/ts/protocols/http/index.ts +1 -2
  29. package/ts/protocols/index.ts +0 -7
  30. package/ts/proxies/smart-proxy/socket-handler-server.ts +6 -0
  31. package/ts/proxies/smart-proxy/utils/route-helpers/socket-handlers.ts +60 -59
  32. package/ts/routing/index.ts +0 -3
  33. package/dist_ts/core/events/index.d.ts +0 -4
  34. package/dist_ts/core/events/index.js +0 -5
  35. package/dist_ts/core/models/socket-augmentation.d.ts +0 -15
  36. package/dist_ts/core/models/socket-augmentation.js +0 -18
  37. package/dist_ts/core/utils/async-utils.d.ts +0 -81
  38. package/dist_ts/core/utils/async-utils.js +0 -216
  39. package/dist_ts/core/utils/binary-heap.d.ts +0 -73
  40. package/dist_ts/core/utils/binary-heap.js +0 -193
  41. package/dist_ts/core/utils/enhanced-connection-pool.d.ts +0 -110
  42. package/dist_ts/core/utils/enhanced-connection-pool.js +0 -325
  43. package/dist_ts/core/utils/fs-utils.d.ts +0 -144
  44. package/dist_ts/core/utils/fs-utils.js +0 -252
  45. package/dist_ts/core/utils/ip-utils.d.ts +0 -69
  46. package/dist_ts/core/utils/ip-utils.js +0 -270
  47. package/dist_ts/core/utils/lifecycle-component.d.ts +0 -59
  48. package/dist_ts/core/utils/lifecycle-component.js +0 -211
  49. package/dist_ts/core/utils/log-deduplicator.d.ts +0 -39
  50. package/dist_ts/core/utils/log-deduplicator.js +0 -305
  51. package/dist_ts/core/utils/security-utils.d.ts +0 -111
  52. package/dist_ts/core/utils/security-utils.js +0 -212
  53. package/dist_ts/core/utils/shared-security-manager.d.ts +0 -128
  54. package/dist_ts/core/utils/shared-security-manager.js +0 -362
  55. package/dist_ts/core/utils/socket-utils.d.ts +0 -63
  56. package/dist_ts/core/utils/socket-utils.js +0 -249
  57. package/dist_ts/core/utils/template-utils.d.ts +0 -37
  58. package/dist_ts/core/utils/template-utils.js +0 -104
  59. package/dist_ts/core/utils/validation-utils.d.ts +0 -61
  60. package/dist_ts/core/utils/validation-utils.js +0 -149
  61. package/dist_ts/core/utils/websocket-utils.d.ts +0 -22
  62. package/dist_ts/core/utils/websocket-utils.js +0 -30
  63. package/dist_ts/detection/detectors/http-detector.d.ts +0 -33
  64. package/dist_ts/detection/detectors/http-detector.js +0 -101
  65. package/dist_ts/detection/detectors/quick-detector.d.ts +0 -28
  66. package/dist_ts/detection/detectors/quick-detector.js +0 -131
  67. package/dist_ts/detection/detectors/routing-extractor.d.ts +0 -28
  68. package/dist_ts/detection/detectors/routing-extractor.js +0 -122
  69. package/dist_ts/detection/detectors/tls-detector.d.ts +0 -47
  70. package/dist_ts/detection/detectors/tls-detector.js +0 -183
  71. package/dist_ts/detection/index.d.ts +0 -17
  72. package/dist_ts/detection/index.js +0 -22
  73. package/dist_ts/detection/models/detection-types.d.ts +0 -87
  74. package/dist_ts/detection/models/detection-types.js +0 -5
  75. package/dist_ts/detection/models/interfaces.d.ts +0 -97
  76. package/dist_ts/detection/models/interfaces.js +0 -5
  77. package/dist_ts/detection/protocol-detector.d.ts +0 -79
  78. package/dist_ts/detection/protocol-detector.js +0 -253
  79. package/dist_ts/detection/utils/buffer-utils.d.ts +0 -61
  80. package/dist_ts/detection/utils/buffer-utils.js +0 -127
  81. package/dist_ts/detection/utils/fragment-manager.d.ts +0 -31
  82. package/dist_ts/detection/utils/fragment-manager.js +0 -53
  83. package/dist_ts/detection/utils/parser-utils.d.ts +0 -42
  84. package/dist_ts/detection/utils/parser-utils.js +0 -63
  85. package/dist_ts/protocols/common/fragment-handler.d.ts +0 -73
  86. package/dist_ts/protocols/common/fragment-handler.js +0 -121
  87. package/dist_ts/protocols/common/index.d.ts +0 -7
  88. package/dist_ts/protocols/common/index.js +0 -8
  89. package/dist_ts/protocols/common/types.d.ts +0 -68
  90. package/dist_ts/protocols/common/types.js +0 -7
  91. package/dist_ts/protocols/http/parser.d.ts +0 -58
  92. package/dist_ts/protocols/http/parser.js +0 -184
  93. package/dist_ts/protocols/proxy/index.d.ts +0 -5
  94. package/dist_ts/protocols/proxy/index.js +0 -6
  95. package/dist_ts/protocols/proxy/types.d.ts +0 -47
  96. package/dist_ts/protocols/proxy/types.js +0 -6
  97. package/dist_ts/protocols/tls/alerts/index.d.ts +0 -4
  98. package/dist_ts/protocols/tls/alerts/index.js +0 -5
  99. package/dist_ts/protocols/tls/alerts/tls-alert.d.ts +0 -150
  100. package/dist_ts/protocols/tls/alerts/tls-alert.js +0 -226
  101. package/dist_ts/protocols/tls/index.d.ts +0 -12
  102. package/dist_ts/protocols/tls/index.js +0 -27
  103. package/dist_ts/protocols/tls/sni/client-hello-parser.d.ts +0 -100
  104. package/dist_ts/protocols/tls/sni/client-hello-parser.js +0 -463
  105. package/dist_ts/protocols/tls/sni/index.d.ts +0 -5
  106. package/dist_ts/protocols/tls/sni/index.js +0 -6
  107. package/dist_ts/protocols/tls/sni/sni-extraction.d.ts +0 -58
  108. package/dist_ts/protocols/tls/sni/sni-extraction.js +0 -275
  109. package/dist_ts/protocols/tls/utils/index.d.ts +0 -4
  110. package/dist_ts/protocols/tls/utils/index.js +0 -5
  111. package/dist_ts/protocols/tls/utils/tls-utils.d.ts +0 -158
  112. package/dist_ts/protocols/tls/utils/tls-utils.js +0 -187
  113. package/dist_ts/protocols/websocket/constants.d.ts +0 -55
  114. package/dist_ts/protocols/websocket/constants.js +0 -58
  115. package/dist_ts/protocols/websocket/index.d.ts +0 -7
  116. package/dist_ts/protocols/websocket/index.js +0 -8
  117. package/dist_ts/protocols/websocket/types.d.ts +0 -47
  118. package/dist_ts/protocols/websocket/types.js +0 -5
  119. package/dist_ts/protocols/websocket/utils.d.ts +0 -25
  120. package/dist_ts/protocols/websocket/utils.js +0 -103
  121. package/dist_ts/routing/router/http-router.d.ts +0 -89
  122. package/dist_ts/routing/router/http-router.js +0 -205
  123. package/dist_ts/routing/router/index.d.ts +0 -5
  124. package/dist_ts/routing/router/index.js +0 -6
  125. package/dist_ts/tls/index.d.ts +0 -16
  126. package/dist_ts/tls/index.js +0 -24
  127. package/dist_ts/tls/sni/index.d.ts +0 -4
  128. package/dist_ts/tls/sni/index.js +0 -5
  129. package/dist_ts/tls/sni/sni-handler.d.ts +0 -154
  130. package/dist_ts/tls/sni/sni-handler.js +0 -191
  131. package/ts/core/events/index.ts +0 -3
  132. package/ts/core/models/socket-augmentation.ts +0 -38
  133. package/ts/core/utils/async-utils.ts +0 -275
  134. package/ts/core/utils/binary-heap.ts +0 -225
  135. package/ts/core/utils/enhanced-connection-pool.ts +0 -425
  136. package/ts/core/utils/fs-utils.ts +0 -270
  137. package/ts/core/utils/ip-utils.ts +0 -303
  138. package/ts/core/utils/lifecycle-component.ts +0 -251
  139. package/ts/core/utils/log-deduplicator.ts +0 -370
  140. package/ts/core/utils/security-utils.ts +0 -305
  141. package/ts/core/utils/shared-security-manager.ts +0 -470
  142. package/ts/core/utils/socket-utils.ts +0 -322
  143. package/ts/core/utils/template-utils.ts +0 -124
  144. package/ts/core/utils/validation-utils.ts +0 -177
  145. package/ts/core/utils/websocket-utils.ts +0 -33
  146. package/ts/detection/detectors/http-detector.ts +0 -127
  147. package/ts/detection/detectors/quick-detector.ts +0 -148
  148. package/ts/detection/detectors/routing-extractor.ts +0 -147
  149. package/ts/detection/detectors/tls-detector.ts +0 -223
  150. package/ts/detection/index.ts +0 -25
  151. package/ts/detection/models/detection-types.ts +0 -102
  152. package/ts/detection/models/interfaces.ts +0 -115
  153. package/ts/detection/protocol-detector.ts +0 -319
  154. package/ts/detection/utils/buffer-utils.ts +0 -141
  155. package/ts/detection/utils/fragment-manager.ts +0 -64
  156. package/ts/detection/utils/parser-utils.ts +0 -77
  157. package/ts/protocols/common/fragment-handler.ts +0 -167
  158. package/ts/protocols/common/index.ts +0 -8
  159. package/ts/protocols/common/types.ts +0 -76
  160. package/ts/protocols/http/parser.ts +0 -219
  161. package/ts/protocols/proxy/index.ts +0 -6
  162. package/ts/protocols/proxy/types.ts +0 -53
  163. package/ts/protocols/tls/alerts/index.ts +0 -3
  164. package/ts/protocols/tls/alerts/tls-alert.ts +0 -259
  165. package/ts/protocols/tls/index.ts +0 -37
  166. package/ts/protocols/tls/sni/client-hello-parser.ts +0 -629
  167. package/ts/protocols/tls/sni/index.ts +0 -6
  168. package/ts/protocols/tls/sni/sni-extraction.ts +0 -353
  169. package/ts/protocols/tls/utils/index.ts +0 -3
  170. package/ts/protocols/tls/utils/tls-utils.ts +0 -201
  171. package/ts/protocols/websocket/constants.ts +0 -60
  172. package/ts/protocols/websocket/index.ts +0 -8
  173. package/ts/protocols/websocket/types.ts +0 -53
  174. package/ts/protocols/websocket/utils.ts +0 -98
  175. package/ts/routing/router/http-router.ts +0 -266
  176. package/ts/routing/router/index.ts +0 -7
  177. package/ts/tls/index.ts +0 -29
  178. package/ts/tls/sni/index.ts +0 -3
  179. package/ts/tls/sni/sni-handler.ts +0 -264
@@ -1,102 +0,0 @@
1
- /**
2
- * Type definitions for protocol detection
3
- */
4
-
5
- /**
6
- * Supported protocol types that can be detected
7
- */
8
- export type TProtocolType = 'tls' | 'http' | 'unknown';
9
-
10
- /**
11
- * HTTP method types
12
- */
13
- export type THttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE';
14
-
15
- /**
16
- * TLS version identifiers
17
- */
18
- export type TTlsVersion = 'SSLv3' | 'TLSv1.0' | 'TLSv1.1' | 'TLSv1.2' | 'TLSv1.3';
19
-
20
- /**
21
- * Connection information extracted from protocol detection
22
- */
23
- export interface IConnectionInfo {
24
- /**
25
- * The detected protocol type
26
- */
27
- protocol: TProtocolType;
28
-
29
- /**
30
- * Domain/hostname extracted from the connection
31
- * - For TLS: from SNI extension
32
- * - For HTTP: from Host header
33
- */
34
- domain?: string;
35
-
36
- /**
37
- * HTTP-specific fields
38
- */
39
- method?: THttpMethod;
40
- path?: string;
41
- httpVersion?: string;
42
- headers?: Record<string, string>;
43
-
44
- /**
45
- * TLS-specific fields
46
- */
47
- tlsVersion?: TTlsVersion;
48
- sni?: string;
49
- alpn?: string[];
50
- cipherSuites?: number[];
51
- }
52
-
53
- /**
54
- * Result of protocol detection
55
- */
56
- export interface IDetectionResult {
57
- /**
58
- * The detected protocol type
59
- */
60
- protocol: TProtocolType;
61
-
62
- /**
63
- * Extracted connection information
64
- */
65
- connectionInfo: IConnectionInfo;
66
-
67
- /**
68
- * Any remaining buffer data after detection headers
69
- * This can be used to continue processing the stream
70
- */
71
- remainingBuffer?: Buffer;
72
-
73
- /**
74
- * Whether the detection is complete or needs more data
75
- */
76
- isComplete: boolean;
77
-
78
- /**
79
- * Minimum bytes needed for complete detection (if incomplete)
80
- */
81
- bytesNeeded?: number;
82
- }
83
-
84
- /**
85
- * Options for protocol detection
86
- */
87
- export interface IDetectionOptions {
88
- /**
89
- * Maximum bytes to buffer for detection (default: 8192)
90
- */
91
- maxBufferSize?: number;
92
-
93
- /**
94
- * Timeout for detection in milliseconds (default: 5000)
95
- */
96
- timeout?: number;
97
-
98
- /**
99
- * Whether to extract full headers or just essential info
100
- */
101
- extractFullHeaders?: boolean;
102
- }
@@ -1,115 +0,0 @@
1
- /**
2
- * Interface definitions for protocol detection components
3
- */
4
-
5
- import type { IDetectionResult, IDetectionOptions } from './detection-types.js';
6
-
7
- /**
8
- * Interface for protocol detectors
9
- */
10
- export interface IProtocolDetector {
11
- /**
12
- * Detect protocol from buffer data
13
- * @param buffer The buffer to analyze
14
- * @param options Detection options
15
- * @returns Detection result or null if protocol cannot be determined
16
- */
17
- detect(buffer: Buffer, options?: IDetectionOptions): IDetectionResult | null;
18
-
19
- /**
20
- * Check if buffer potentially contains this protocol
21
- * @param buffer The buffer to check
22
- * @returns True if buffer might contain this protocol
23
- */
24
- canHandle(buffer: Buffer): boolean;
25
-
26
- /**
27
- * Get the minimum bytes needed for detection
28
- */
29
- getMinimumBytes(): number;
30
- }
31
-
32
- /**
33
- * Interface for connection tracking during fragmented detection
34
- */
35
- export interface IConnectionTracker {
36
- /**
37
- * Connection identifier
38
- */
39
- id: string;
40
-
41
- /**
42
- * Accumulated buffer data
43
- */
44
- buffer: Buffer;
45
-
46
- /**
47
- * Timestamp of first data
48
- */
49
- startTime: number;
50
-
51
- /**
52
- * Current detection state
53
- */
54
- state: 'detecting' | 'complete' | 'failed';
55
-
56
- /**
57
- * Partial detection result (if any)
58
- */
59
- partialResult?: Partial<IDetectionResult>;
60
- }
61
-
62
- /**
63
- * Interface for buffer accumulator (handles fragmented data)
64
- */
65
- export interface IBufferAccumulator {
66
- /**
67
- * Add data to accumulator
68
- */
69
- append(data: Buffer): void;
70
-
71
- /**
72
- * Get accumulated buffer
73
- */
74
- getBuffer(): Buffer;
75
-
76
- /**
77
- * Get buffer length
78
- */
79
- length(): number;
80
-
81
- /**
82
- * Clear accumulated data
83
- */
84
- clear(): void;
85
-
86
- /**
87
- * Check if accumulator has enough data
88
- */
89
- hasMinimumBytes(minBytes: number): boolean;
90
- }
91
-
92
- /**
93
- * Detection events
94
- */
95
- export interface IDetectionEvents {
96
- /**
97
- * Emitted when protocol is successfully detected
98
- */
99
- detected: (result: IDetectionResult) => void;
100
-
101
- /**
102
- * Emitted when detection fails
103
- */
104
- failed: (error: Error) => void;
105
-
106
- /**
107
- * Emitted when detection times out
108
- */
109
- timeout: () => void;
110
-
111
- /**
112
- * Emitted when more data is needed
113
- */
114
- needMoreData: (bytesNeeded: number) => void;
115
- }
@@ -1,319 +0,0 @@
1
- /**
2
- * Protocol Detector
3
- *
4
- * Simplified protocol detection using the new architecture
5
- */
6
-
7
- import type { IDetectionResult, IDetectionOptions } from './models/detection-types.js';
8
- import type { IConnectionContext } from '../protocols/common/types.js';
9
- import { TlsDetector } from './detectors/tls-detector.js';
10
- import { HttpDetector } from './detectors/http-detector.js';
11
- import { DetectionFragmentManager } from './utils/fragment-manager.js';
12
-
13
- /**
14
- * Main protocol detector class
15
- */
16
- export class ProtocolDetector {
17
- private static instance: ProtocolDetector;
18
- private fragmentManager: DetectionFragmentManager;
19
- private tlsDetector: TlsDetector;
20
- private httpDetector: HttpDetector;
21
- private connectionProtocols: Map<string, { protocol: 'tls' | 'http'; createdAt: number }> = new Map();
22
-
23
- constructor() {
24
- this.fragmentManager = new DetectionFragmentManager();
25
- this.tlsDetector = new TlsDetector();
26
- this.httpDetector = new HttpDetector(this.fragmentManager);
27
- }
28
-
29
- private static getInstance(): ProtocolDetector {
30
- if (!this.instance) {
31
- this.instance = new ProtocolDetector();
32
- }
33
- return this.instance;
34
- }
35
-
36
- /**
37
- * Detect protocol from buffer data
38
- */
39
- static async detect(buffer: Buffer, options?: IDetectionOptions): Promise<IDetectionResult> {
40
- return this.getInstance().detectInstance(buffer, options);
41
- }
42
-
43
- private async detectInstance(buffer: Buffer, options?: IDetectionOptions): Promise<IDetectionResult> {
44
- // Quick sanity check
45
- if (!buffer || buffer.length === 0) {
46
- return {
47
- protocol: 'unknown',
48
- connectionInfo: { protocol: 'unknown' },
49
- isComplete: true
50
- };
51
- }
52
-
53
- // Try TLS detection first (more specific)
54
- if (this.tlsDetector.canHandle(buffer)) {
55
- const tlsResult = this.tlsDetector.detect(buffer, options);
56
- if (tlsResult) {
57
- return tlsResult;
58
- }
59
- }
60
-
61
- // Try HTTP detection
62
- if (this.httpDetector.canHandle(buffer)) {
63
- const httpResult = this.httpDetector.detect(buffer, options);
64
- if (httpResult) {
65
- return httpResult;
66
- }
67
- }
68
-
69
- // Neither TLS nor HTTP
70
- return {
71
- protocol: 'unknown',
72
- connectionInfo: { protocol: 'unknown' },
73
- isComplete: true
74
- };
75
- }
76
-
77
- /**
78
- * Detect protocol with connection tracking for fragmented data
79
- * @deprecated Use detectWithContext instead
80
- */
81
- static async detectWithConnectionTracking(
82
- buffer: Buffer,
83
- connectionId: string,
84
- options?: IDetectionOptions
85
- ): Promise<IDetectionResult> {
86
- // Convert connection ID to context
87
- const context: IConnectionContext = {
88
- id: connectionId,
89
- sourceIp: 'unknown',
90
- sourcePort: 0,
91
- destIp: 'unknown',
92
- destPort: 0,
93
- timestamp: Date.now()
94
- };
95
-
96
- return this.getInstance().detectWithContextInstance(buffer, context, options);
97
- }
98
-
99
- /**
100
- * Detect protocol with connection context for fragmented data
101
- */
102
- static async detectWithContext(
103
- buffer: Buffer,
104
- context: IConnectionContext,
105
- options?: IDetectionOptions
106
- ): Promise<IDetectionResult> {
107
- return this.getInstance().detectWithContextInstance(buffer, context, options);
108
- }
109
-
110
- private async detectWithContextInstance(
111
- buffer: Buffer,
112
- context: IConnectionContext,
113
- options?: IDetectionOptions
114
- ): Promise<IDetectionResult> {
115
- // Quick sanity check
116
- if (!buffer || buffer.length === 0) {
117
- return {
118
- protocol: 'unknown',
119
- connectionInfo: { protocol: 'unknown' },
120
- isComplete: true
121
- };
122
- }
123
-
124
- const connectionId = DetectionFragmentManager.createConnectionId(context);
125
-
126
- // Check if we already know the protocol for this connection
127
- const knownEntry = this.connectionProtocols.get(connectionId);
128
- const knownProtocol = knownEntry?.protocol;
129
-
130
- if (knownProtocol === 'http') {
131
- const result = this.httpDetector.detectWithContext(buffer, context, options);
132
- if (result) {
133
- if (result.isComplete) {
134
- this.connectionProtocols.delete(connectionId);
135
- }
136
- return result;
137
- }
138
- } else if (knownProtocol === 'tls') {
139
- // Handle TLS with fragment accumulation
140
- const handler = this.fragmentManager.getHandler('tls');
141
- const fragmentResult = handler.addFragment(connectionId, buffer);
142
-
143
- if (fragmentResult.error) {
144
- handler.complete(connectionId);
145
- this.connectionProtocols.delete(connectionId);
146
- return {
147
- protocol: 'unknown',
148
- connectionInfo: { protocol: 'unknown' },
149
- isComplete: true
150
- };
151
- }
152
-
153
- const result = this.tlsDetector.detect(fragmentResult.buffer!, options);
154
- if (result) {
155
- if (result.isComplete) {
156
- handler.complete(connectionId);
157
- this.connectionProtocols.delete(connectionId);
158
- }
159
- return result;
160
- }
161
- }
162
-
163
- // If we don't know the protocol yet, try to detect it
164
- if (!knownProtocol) {
165
- // First peek to determine protocol type
166
- if (this.tlsDetector.canHandle(buffer)) {
167
- this.connectionProtocols.set(connectionId, { protocol: 'tls', createdAt: Date.now() });
168
- // Handle TLS with fragment accumulation
169
- const handler = this.fragmentManager.getHandler('tls');
170
- const fragmentResult = handler.addFragment(connectionId, buffer);
171
-
172
- if (fragmentResult.error) {
173
- handler.complete(connectionId);
174
- this.connectionProtocols.delete(connectionId);
175
- return {
176
- protocol: 'unknown',
177
- connectionInfo: { protocol: 'unknown' },
178
- isComplete: true
179
- };
180
- }
181
-
182
- const result = this.tlsDetector.detect(fragmentResult.buffer!, options);
183
- if (result) {
184
- if (result.isComplete) {
185
- handler.complete(connectionId);
186
- this.connectionProtocols.delete(connectionId);
187
- }
188
- return result;
189
- }
190
- }
191
-
192
- if (this.httpDetector.canHandle(buffer)) {
193
- this.connectionProtocols.set(connectionId, { protocol: 'http', createdAt: Date.now() });
194
- const result = this.httpDetector.detectWithContext(buffer, context, options);
195
- if (result) {
196
- if (result.isComplete) {
197
- this.connectionProtocols.delete(connectionId);
198
- }
199
- return result;
200
- }
201
- }
202
- }
203
-
204
- // Can't determine protocol
205
- return {
206
- protocol: 'unknown',
207
- connectionInfo: { protocol: 'unknown' },
208
- isComplete: false,
209
- bytesNeeded: Math.max(
210
- this.tlsDetector.getMinimumBytes(),
211
- this.httpDetector.getMinimumBytes()
212
- )
213
- };
214
- }
215
-
216
- /**
217
- * Clean up resources
218
- */
219
- static cleanup(): void {
220
- this.getInstance().cleanupInstance();
221
- }
222
-
223
- private cleanupInstance(): void {
224
- this.fragmentManager.cleanup();
225
- // Remove stale connectionProtocols entries (abandoned handshakes, port scanners)
226
- const maxAge = 30_000; // 30 seconds
227
- const now = Date.now();
228
- for (const [id, entry] of this.connectionProtocols) {
229
- if (now - entry.createdAt > maxAge) {
230
- this.connectionProtocols.delete(id);
231
- }
232
- }
233
- }
234
-
235
- /**
236
- * Destroy detector instance
237
- */
238
- static destroy(): void {
239
- this.getInstance().destroyInstance();
240
- this.instance = null as any;
241
- }
242
-
243
- private destroyInstance(): void {
244
- this.fragmentManager.destroy();
245
- this.connectionProtocols.clear();
246
- }
247
-
248
- /**
249
- * Clean up old connection tracking entries
250
- *
251
- * @param _maxAge Maximum age in milliseconds (default: 30 seconds)
252
- */
253
- static cleanupConnections(_maxAge: number = 30000): void {
254
- this.getInstance().cleanupInstance();
255
- }
256
-
257
- /**
258
- * Clean up fragments for a specific connection
259
- */
260
- static cleanupConnection(context: IConnectionContext): void {
261
- const instance = this.getInstance();
262
- const connectionId = DetectionFragmentManager.createConnectionId(context);
263
-
264
- // Clean up both TLS and HTTP fragments for this connection
265
- instance.fragmentManager.getHandler('tls').complete(connectionId);
266
- instance.fragmentManager.getHandler('http').complete(connectionId);
267
-
268
- // Remove from connection protocols tracking
269
- instance.connectionProtocols.delete(connectionId);
270
- }
271
-
272
- /**
273
- * Extract domain from connection info
274
- */
275
- static extractDomain(connectionInfo: any): string | undefined {
276
- return connectionInfo.domain || connectionInfo.sni || connectionInfo.host;
277
- }
278
-
279
- /**
280
- * Create a connection ID from connection parameters
281
- * @deprecated Use createConnectionContext instead
282
- */
283
- static createConnectionId(params: {
284
- sourceIp?: string;
285
- sourcePort?: number;
286
- destIp?: string;
287
- destPort?: number;
288
- socketId?: string;
289
- }): string {
290
- // If socketId is provided, use it
291
- if (params.socketId) {
292
- return params.socketId;
293
- }
294
-
295
- // Otherwise create from connection tuple
296
- const { sourceIp = 'unknown', sourcePort = 0, destIp = 'unknown', destPort = 0 } = params;
297
- return `${sourceIp}:${sourcePort}-${destIp}:${destPort}`;
298
- }
299
-
300
- /**
301
- * Create a connection context from parameters
302
- */
303
- static createConnectionContext(params: {
304
- sourceIp?: string;
305
- sourcePort?: number;
306
- destIp?: string;
307
- destPort?: number;
308
- socketId?: string;
309
- }): IConnectionContext {
310
- return {
311
- id: params.socketId,
312
- sourceIp: params.sourceIp || 'unknown',
313
- sourcePort: params.sourcePort || 0,
314
- destIp: params.destIp || 'unknown',
315
- destPort: params.destPort || 0,
316
- timestamp: Date.now()
317
- };
318
- }
319
- }
@@ -1,141 +0,0 @@
1
- /**
2
- * Buffer manipulation utilities for protocol detection
3
- */
4
-
5
- // Import from protocols
6
- import { HttpParser } from '../../protocols/http/index.js';
7
-
8
- /**
9
- * BufferAccumulator class for handling fragmented data
10
- */
11
- export class BufferAccumulator {
12
- private chunks: Buffer[] = [];
13
- private totalLength = 0;
14
-
15
- /**
16
- * Append data to the accumulator
17
- */
18
- append(data: Buffer): void {
19
- this.chunks.push(data);
20
- this.totalLength += data.length;
21
- }
22
-
23
- /**
24
- * Get the accumulated buffer
25
- */
26
- getBuffer(): Buffer {
27
- if (this.chunks.length === 0) {
28
- return Buffer.alloc(0);
29
- }
30
- if (this.chunks.length === 1) {
31
- return this.chunks[0];
32
- }
33
- return Buffer.concat(this.chunks, this.totalLength);
34
- }
35
-
36
- /**
37
- * Get current buffer length
38
- */
39
- length(): number {
40
- return this.totalLength;
41
- }
42
-
43
- /**
44
- * Clear all accumulated data
45
- */
46
- clear(): void {
47
- this.chunks = [];
48
- this.totalLength = 0;
49
- }
50
-
51
- /**
52
- * Check if accumulator has minimum bytes
53
- */
54
- hasMinimumBytes(minBytes: number): boolean {
55
- return this.totalLength >= minBytes;
56
- }
57
- }
58
-
59
- /**
60
- * Read a big-endian 16-bit integer from buffer
61
- */
62
- export function readUInt16BE(buffer: Buffer, offset: number): number {
63
- if (offset + 2 > buffer.length) {
64
- throw new Error('Buffer too short for UInt16BE read');
65
- }
66
- return (buffer[offset] << 8) | buffer[offset + 1];
67
- }
68
-
69
- /**
70
- * Read a big-endian 24-bit integer from buffer
71
- */
72
- export function readUInt24BE(buffer: Buffer, offset: number): number {
73
- if (offset + 3 > buffer.length) {
74
- throw new Error('Buffer too short for UInt24BE read');
75
- }
76
- return (buffer[offset] << 16) | (buffer[offset + 1] << 8) | buffer[offset + 2];
77
- }
78
-
79
- /**
80
- * Find a byte sequence in a buffer
81
- */
82
- export function findSequence(buffer: Buffer, sequence: Buffer, startOffset = 0): number {
83
- if (sequence.length === 0) {
84
- return startOffset;
85
- }
86
-
87
- const searchLength = buffer.length - sequence.length + 1;
88
- for (let i = startOffset; i < searchLength; i++) {
89
- let found = true;
90
- for (let j = 0; j < sequence.length; j++) {
91
- if (buffer[i + j] !== sequence[j]) {
92
- found = false;
93
- break;
94
- }
95
- }
96
- if (found) {
97
- return i;
98
- }
99
- }
100
- return -1;
101
- }
102
-
103
- /**
104
- * Extract a line from buffer (up to CRLF or LF)
105
- */
106
- export function extractLine(buffer: Buffer, startOffset = 0): { line: string; nextOffset: number } | null {
107
- // Delegate to protocol parser
108
- return HttpParser.extractLine(buffer, startOffset);
109
- }
110
-
111
- /**
112
- * Check if buffer starts with a string (case-insensitive)
113
- */
114
- export function startsWithString(buffer: Buffer, str: string, offset = 0): boolean {
115
- if (offset + str.length > buffer.length) {
116
- return false;
117
- }
118
-
119
- const bufferStr = buffer.slice(offset, offset + str.length).toString('utf8');
120
- return bufferStr.toLowerCase() === str.toLowerCase();
121
- }
122
-
123
- /**
124
- * Safe buffer slice that doesn't throw on out-of-bounds
125
- */
126
- export function safeSlice(buffer: Buffer, start: number, end?: number): Buffer {
127
- const safeStart = Math.max(0, Math.min(start, buffer.length));
128
- const safeEnd = end === undefined
129
- ? buffer.length
130
- : Math.max(safeStart, Math.min(end, buffer.length));
131
-
132
- return buffer.slice(safeStart, safeEnd);
133
- }
134
-
135
- /**
136
- * Check if buffer contains printable ASCII
137
- */
138
- export function isPrintableAscii(buffer: Buffer, length?: number): boolean {
139
- // Delegate to protocol parser
140
- return HttpParser.isPrintableAscii(buffer, length);
141
- }