@push.rocks/smartproxy 25.17.10 → 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 +8 -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,98 +0,0 @@
1
- /**
2
- * WebSocket Protocol Utilities
3
- */
4
-
5
- import * as crypto from 'node:crypto';
6
- import { WEBSOCKET_MAGIC_STRING } from './constants.js';
7
- import type { RawData } from './types.js';
8
-
9
- /**
10
- * Get the length of a WebSocket message regardless of its type
11
- * (handles all possible WebSocket message data types)
12
- */
13
- export function getMessageSize(data: RawData): number {
14
- if (typeof data === 'string') {
15
- // For string data, get the byte length
16
- return Buffer.from(data, 'utf8').length;
17
- } else if (data instanceof Buffer) {
18
- // For Node.js Buffer
19
- return data.length;
20
- } else if (data instanceof ArrayBuffer) {
21
- // For ArrayBuffer
22
- return data.byteLength;
23
- } else if (Array.isArray(data)) {
24
- // For array of buffers, sum their lengths
25
- return data.reduce((sum, chunk) => {
26
- if (chunk instanceof Buffer) {
27
- return sum + chunk.length;
28
- } else if (chunk instanceof ArrayBuffer) {
29
- return sum + chunk.byteLength;
30
- }
31
- return sum;
32
- }, 0);
33
- } else {
34
- // For other types, try to determine the size or return 0
35
- try {
36
- return Buffer.from(data).length;
37
- } catch (e) {
38
- return 0;
39
- }
40
- }
41
- }
42
-
43
- /**
44
- * Convert any raw WebSocket data to Buffer for consistent handling
45
- */
46
- export function toBuffer(data: RawData): Buffer {
47
- if (typeof data === 'string') {
48
- return Buffer.from(data, 'utf8');
49
- } else if (data instanceof Buffer) {
50
- return data;
51
- } else if (data instanceof ArrayBuffer) {
52
- return Buffer.from(data);
53
- } else if (Array.isArray(data)) {
54
- // For array of buffers, concatenate them
55
- return Buffer.concat(data.map(chunk => {
56
- if (chunk instanceof Buffer) {
57
- return chunk;
58
- } else if (chunk instanceof ArrayBuffer) {
59
- return Buffer.from(chunk);
60
- }
61
- return Buffer.from(chunk);
62
- }));
63
- } else {
64
- // For other types, try to convert to Buffer or return empty Buffer
65
- try {
66
- return Buffer.from(data);
67
- } catch (e) {
68
- return Buffer.alloc(0);
69
- }
70
- }
71
- }
72
-
73
- /**
74
- * Generate WebSocket accept key from client key
75
- */
76
- export function generateAcceptKey(clientKey: string): string {
77
- const hash = crypto.createHash('sha1');
78
- hash.update(clientKey + WEBSOCKET_MAGIC_STRING);
79
- return hash.digest('base64');
80
- }
81
-
82
- /**
83
- * Validate WebSocket upgrade request
84
- */
85
- export function isWebSocketUpgrade(headers: Record<string, string>): boolean {
86
- const upgrade = headers['upgrade'];
87
- const connection = headers['connection'];
88
-
89
- return upgrade?.toLowerCase() === 'websocket' &&
90
- connection?.toLowerCase().includes('upgrade');
91
- }
92
-
93
- /**
94
- * Generate random WebSocket key for client handshake
95
- */
96
- export function generateWebSocketKey(): string {
97
- return crypto.randomBytes(16).toString('base64');
98
- }
@@ -1,266 +0,0 @@
1
- import * as plugins from '../../plugins.js';
2
- import type { IRouteConfig } from '../../proxies/smart-proxy/models/route-types.js';
3
- import { DomainMatcher, PathMatcher } from '../../core/routing/matchers/index.js';
4
-
5
- /**
6
- * Interface for router result with additional metadata
7
- */
8
- export interface RouterResult {
9
- route: IRouteConfig;
10
- pathMatch?: string;
11
- pathParams?: Record<string, string>;
12
- pathRemainder?: string;
13
- }
14
-
15
-
16
- /**
17
- * Logger interface for HttpRouter
18
- */
19
- export interface ILogger {
20
- debug?: (message: string, data?: any) => void;
21
- info: (message: string, data?: any) => void;
22
- warn: (message: string, data?: any) => void;
23
- error: (message: string, data?: any) => void;
24
- }
25
-
26
- /**
27
- * Unified HTTP Router for reverse proxy requests
28
- *
29
- * Domain matching patterns:
30
- * - Exact matches: "example.com"
31
- * - Wildcard subdomains: "*.example.com" (matches any subdomain of example.com)
32
- * - TLD wildcards: "example.*" (matches example.com, example.org, etc.)
33
- * - Complex wildcards: "*.lossless*" (matches any subdomain of any lossless domain)
34
- * - Default fallback: "*" (matches any unmatched domain)
35
- *
36
- * Path pattern matching:
37
- * - Exact path: "/api/users"
38
- * - Wildcard paths: "/api/*"
39
- * - Path parameters: "/users/:id/profile"
40
- */
41
- export class HttpRouter {
42
- // Store routes sorted by priority
43
- private routes: IRouteConfig[] = [];
44
- // Default route to use when no match is found (optional)
45
- private defaultRoute?: IRouteConfig;
46
- // Logger interface
47
- private logger: ILogger;
48
-
49
- constructor(
50
- routes?: IRouteConfig[],
51
- logger?: ILogger
52
- ) {
53
- this.logger = logger || {
54
- error: console.error.bind(console),
55
- warn: console.warn.bind(console),
56
- info: console.info.bind(console),
57
- debug: console.debug?.bind(console)
58
- };
59
-
60
- if (routes) {
61
- this.setRoutes(routes);
62
- }
63
- }
64
-
65
- /**
66
- * Sets a new set of routes
67
- * @param routes Array of route configurations
68
- */
69
- public setRoutes(routes: IRouteConfig[]): void {
70
- this.routes = [...routes];
71
-
72
- // Sort routes by priority (higher priority first)
73
- this.routes.sort((a, b) => {
74
- const priorityA = a.priority ?? 0;
75
- const priorityB = b.priority ?? 0;
76
- return priorityB - priorityA;
77
- });
78
-
79
- // Find default route if any (route with "*" as domain)
80
- this.defaultRoute = this.routes.find(route => {
81
- const domains = Array.isArray(route.match.domains)
82
- ? route.match.domains
83
- : route.match.domains ? [route.match.domains] : [];
84
- return domains.includes('*');
85
- });
86
-
87
- const uniqueDomains = this.getHostnames();
88
- this.logger.info(`HttpRouter initialized with ${this.routes.length} routes (${uniqueDomains.length} unique hosts)`);
89
- }
90
-
91
- /**
92
- * Routes a request based on hostname and path
93
- * @param req The incoming HTTP request
94
- * @returns The matching route or undefined if no match found
95
- */
96
- public routeReq(req: plugins.http.IncomingMessage): IRouteConfig | undefined {
97
- const result = this.routeReqWithDetails(req);
98
- return result ? result.route : undefined;
99
- }
100
-
101
- /**
102
- * Routes a request with detailed matching information
103
- * @param req The incoming HTTP request
104
- * @returns Detailed routing result including matched route and path information
105
- */
106
- public routeReqWithDetails(req: plugins.http.IncomingMessage): RouterResult | undefined {
107
- // Extract and validate host header
108
- const originalHost = req.headers.host;
109
- if (!originalHost) {
110
- this.logger.error('No host header found in request');
111
- return this.defaultRoute ? { route: this.defaultRoute } : undefined;
112
- }
113
-
114
- // Parse URL for path matching
115
- const parsedUrl = plugins.url.parse(req.url || '/');
116
- const urlPath = parsedUrl.pathname || '/';
117
-
118
- // Extract hostname without port
119
- const hostWithoutPort = originalHost.split(':')[0].toLowerCase();
120
-
121
- // Find matching route
122
- const matchingRoute = this.findMatchingRoute(hostWithoutPort, urlPath);
123
-
124
- if (matchingRoute) {
125
- return matchingRoute;
126
- }
127
-
128
- // Fall back to default route if available
129
- if (this.defaultRoute) {
130
- this.logger.warn(`No specific route found for host: ${hostWithoutPort}, using default`);
131
- return { route: this.defaultRoute };
132
- }
133
-
134
- this.logger.error(`No route found for host: ${hostWithoutPort}`);
135
- return undefined;
136
- }
137
-
138
- /**
139
- * Find the best matching route for a given hostname and path
140
- */
141
- private findMatchingRoute(hostname: string, path: string): RouterResult | undefined {
142
- // Try each route in priority order
143
- for (const route of this.routes) {
144
- // Skip disabled routes
145
- if (route.enabled === false) {
146
- continue;
147
- }
148
-
149
- // Check domain match
150
- if (route.match.domains) {
151
- const domains = Array.isArray(route.match.domains)
152
- ? route.match.domains
153
- : [route.match.domains];
154
-
155
- // Check if any domain pattern matches
156
- const domainMatches = domains.some(domain =>
157
- DomainMatcher.match(domain, hostname)
158
- );
159
-
160
- if (!domainMatches) {
161
- continue;
162
- }
163
- }
164
-
165
- // Check path match if specified
166
- if (route.match.path) {
167
- const pathResult = PathMatcher.match(route.match.path, path);
168
- if (pathResult.matches) {
169
- return {
170
- route,
171
- pathMatch: pathResult.pathMatch || path,
172
- pathParams: pathResult.params,
173
- pathRemainder: pathResult.pathRemainder
174
- };
175
- }
176
- } else {
177
- // No path specified, so domain match is sufficient
178
- return { route };
179
- }
180
- }
181
-
182
- return undefined;
183
- }
184
-
185
- /**
186
- * Gets all currently active route configurations
187
- * @returns Array of all active routes
188
- */
189
- public getRoutes(): IRouteConfig[] {
190
- return [...this.routes];
191
- }
192
-
193
- /**
194
- * Gets all hostnames that this router is configured to handle
195
- * @returns Array of unique hostnames
196
- */
197
- public getHostnames(): string[] {
198
- const hostnames = new Set<string>();
199
- for (const route of this.routes) {
200
- if (!route.match.domains) continue;
201
-
202
- const domains = Array.isArray(route.match.domains)
203
- ? route.match.domains
204
- : [route.match.domains];
205
-
206
- for (const domain of domains) {
207
- if (domain !== '*') {
208
- hostnames.add(domain.toLowerCase());
209
- }
210
- }
211
- }
212
- return Array.from(hostnames);
213
- }
214
-
215
- /**
216
- * Adds a single new route configuration
217
- * @param route The route configuration to add
218
- */
219
- public addRoute(route: IRouteConfig): void {
220
- this.routes.push(route);
221
-
222
- // Re-sort routes by priority
223
- this.routes.sort((a, b) => {
224
- const priorityA = a.priority ?? 0;
225
- const priorityB = b.priority ?? 0;
226
- return priorityB - priorityA;
227
- });
228
- }
229
-
230
- /**
231
- * Removes routes by domain pattern
232
- * @param domain The domain pattern to remove routes for
233
- * @returns Boolean indicating whether any routes were removed
234
- */
235
- public removeRoutesByDomain(domain: string): boolean {
236
- const initialCount = this.routes.length;
237
-
238
- // Filter out routes that match the domain
239
- this.routes = this.routes.filter(route => {
240
- if (!route.match.domains) return true;
241
-
242
- const domains = Array.isArray(route.match.domains)
243
- ? route.match.domains
244
- : [route.match.domains];
245
-
246
- return !domains.includes(domain);
247
- });
248
-
249
- return this.routes.length !== initialCount;
250
- }
251
-
252
- /**
253
- * Remove a specific route by reference
254
- * @param route The route to remove
255
- * @returns Boolean indicating if the route was found and removed
256
- */
257
- public removeRoute(route: IRouteConfig): boolean {
258
- const index = this.routes.indexOf(route);
259
- if (index !== -1) {
260
- this.routes.splice(index, 1);
261
- return true;
262
- }
263
- return false;
264
- }
265
-
266
- }
@@ -1,7 +0,0 @@
1
- /**
2
- * HTTP routing
3
- */
4
-
5
- // Export the unified HttpRouter
6
- export { HttpRouter } from './http-router.js';
7
- export type { RouterResult, ILogger } from './http-router.js';
package/ts/tls/index.ts DELETED
@@ -1,29 +0,0 @@
1
- /**
2
- * TLS module for smartproxy
3
- * Re-exports protocol components and provides smartproxy-specific functionality
4
- */
5
-
6
- // Re-export all protocol components from protocols/tls
7
- export * from '../protocols/tls/index.js';
8
-
9
- // Export smartproxy-specific SNI handler
10
- export * from './sni/sni-handler.js';
11
-
12
- // Create a namespace for SNI utilities
13
- import { SniHandler } from './sni/sni-handler.js';
14
- import { SniExtraction } from '../protocols/tls/sni/sni-extraction.js';
15
- import { ClientHelloParser } from '../protocols/tls/sni/client-hello-parser.js';
16
-
17
- // Export utility objects for convenience
18
- export const SNI = {
19
- // Main handler class (for backward compatibility)
20
- Handler: SniHandler,
21
-
22
- // Utility classes
23
- Extraction: SniExtraction,
24
- Parser: ClientHelloParser,
25
-
26
- // Convenience functions
27
- extractSNI: SniHandler.extractSNI,
28
- processTlsPacket: SniHandler.processTlsPacket,
29
- };
@@ -1,3 +0,0 @@
1
- /**
2
- * SNI handling
3
- */
@@ -1,264 +0,0 @@
1
- import { Buffer } from 'node:buffer';
2
- import {
3
- TlsRecordType,
4
- TlsHandshakeType,
5
- TlsExtensionType,
6
- TlsUtils
7
- } from '../../protocols/tls/utils/tls-utils.js';
8
- import {
9
- ClientHelloParser,
10
- type LoggerFunction
11
- } from '../../protocols/tls/sni/client-hello-parser.js';
12
- import {
13
- SniExtraction,
14
- type ConnectionInfo
15
- } from '../../protocols/tls/sni/sni-extraction.js';
16
-
17
- /**
18
- * SNI (Server Name Indication) handler for TLS connections.
19
- * Provides robust extraction of SNI values from TLS ClientHello messages
20
- * with support for fragmented packets, TLS 1.3 resumption, Chrome-specific
21
- * connection behaviors, and tab hibernation/reactivation scenarios.
22
- *
23
- * This class retains the original API but leverages the new modular implementation
24
- * for better maintainability and testability.
25
- */
26
- export class SniHandler {
27
- // Re-export constants for backward compatibility
28
- private static readonly TLS_HANDSHAKE_RECORD_TYPE = TlsRecordType.HANDSHAKE;
29
- private static readonly TLS_APPLICATION_DATA_TYPE = TlsRecordType.APPLICATION_DATA;
30
- private static readonly TLS_CLIENT_HELLO_HANDSHAKE_TYPE = TlsHandshakeType.CLIENT_HELLO;
31
- private static readonly TLS_SNI_EXTENSION_TYPE = TlsExtensionType.SERVER_NAME;
32
- private static readonly TLS_SESSION_TICKET_EXTENSION_TYPE = TlsExtensionType.SESSION_TICKET;
33
- private static readonly TLS_SNI_HOST_NAME_TYPE = 0; // NameType.HOST_NAME in RFC 6066
34
- private static readonly TLS_PSK_EXTENSION_TYPE = TlsExtensionType.PRE_SHARED_KEY;
35
- private static readonly TLS_PSK_KE_MODES_EXTENSION_TYPE = TlsExtensionType.PSK_KEY_EXCHANGE_MODES;
36
- private static readonly TLS_EARLY_DATA_EXTENSION_TYPE = TlsExtensionType.EARLY_DATA;
37
-
38
- /**
39
- * Checks if a buffer contains a TLS handshake message (record type 22)
40
- * @param buffer - The buffer to check
41
- * @returns true if the buffer starts with a TLS handshake record type
42
- */
43
- public static isTlsHandshake(buffer: Buffer): boolean {
44
- return TlsUtils.isTlsHandshake(buffer);
45
- }
46
-
47
- /**
48
- * Checks if a buffer contains TLS application data (record type 23)
49
- * @param buffer - The buffer to check
50
- * @returns true if the buffer starts with a TLS application data record type
51
- */
52
- public static isTlsApplicationData(buffer: Buffer): boolean {
53
- return TlsUtils.isTlsApplicationData(buffer);
54
- }
55
-
56
- /**
57
- * Creates a connection ID based on source/destination information
58
- * Used to track fragmented ClientHello messages across multiple packets
59
- *
60
- * @param connectionInfo - Object containing connection identifiers (IP/port)
61
- * @returns A string ID for the connection
62
- */
63
- public static createConnectionId(connectionInfo: {
64
- sourceIp?: string;
65
- sourcePort?: number;
66
- destIp?: string;
67
- destPort?: number;
68
- }): string {
69
- return TlsUtils.createConnectionId(connectionInfo);
70
- }
71
-
72
- /**
73
- * Handles potential fragmented ClientHello messages by buffering and reassembling
74
- * TLS record fragments that might span multiple TCP packets.
75
- *
76
- * @param buffer - The current buffer fragment
77
- * @param connectionId - Unique identifier for the connection
78
- * @param enableLogging - Whether to enable logging
79
- * @returns A complete buffer if reassembly is successful, or undefined if more fragments are needed
80
- */
81
- public static handleFragmentedClientHello(
82
- buffer: Buffer,
83
- connectionId: string,
84
- enableLogging: boolean = false
85
- ): Buffer | undefined {
86
- const logger = enableLogging ?
87
- (message: string) => console.log(`[SNI Fragment] ${message}`) :
88
- undefined;
89
-
90
- return ClientHelloParser.handleFragmentedClientHello(buffer, connectionId, logger);
91
- }
92
-
93
- /**
94
- * Checks if a buffer contains a TLS ClientHello message
95
- * @param buffer - The buffer to check
96
- * @returns true if the buffer appears to be a ClientHello message
97
- */
98
- public static isClientHello(buffer: Buffer): boolean {
99
- return TlsUtils.isClientHello(buffer);
100
- }
101
-
102
- /**
103
- * Checks if a ClientHello message contains session resumption indicators
104
- * such as session tickets or PSK (Pre-Shared Key) extensions.
105
- *
106
- * @param buffer - The buffer containing a ClientHello message
107
- * @param enableLogging - Whether to enable logging
108
- * @returns Object containing details about session resumption and SNI presence
109
- */
110
- public static hasSessionResumption(
111
- buffer: Buffer,
112
- enableLogging: boolean = false
113
- ): { isResumption: boolean; hasSNI: boolean } {
114
- const logger = enableLogging ?
115
- (message: string) => console.log(`[Session Resumption] ${message}`) :
116
- undefined;
117
-
118
- return ClientHelloParser.hasSessionResumption(buffer, logger);
119
- }
120
-
121
- /**
122
- * Detects characteristics of a tab reactivation TLS handshake
123
- * These often have specific patterns in Chrome and other browsers
124
- *
125
- * @param buffer - The buffer containing a ClientHello message
126
- * @param enableLogging - Whether to enable logging
127
- * @returns true if this appears to be a tab reactivation handshake
128
- */
129
- public static isTabReactivationHandshake(
130
- buffer: Buffer,
131
- enableLogging: boolean = false
132
- ): boolean {
133
- const logger = enableLogging ?
134
- (message: string) => console.log(`[Tab Reactivation] ${message}`) :
135
- undefined;
136
-
137
- return ClientHelloParser.isTabReactivationHandshake(buffer, logger);
138
- }
139
-
140
- /**
141
- * Extracts the SNI (Server Name Indication) from a TLS ClientHello message.
142
- * Implements robust parsing with support for session resumption edge cases.
143
- *
144
- * @param buffer - The buffer containing the TLS ClientHello message
145
- * @param enableLogging - Whether to enable detailed debug logging
146
- * @returns The extracted server name or undefined if not found
147
- */
148
- public static extractSNI(buffer: Buffer, enableLogging: boolean = false): string | undefined {
149
- const logger = enableLogging ?
150
- (message: string) => console.log(`[SNI Extraction] ${message}`) :
151
- undefined;
152
-
153
- return SniExtraction.extractSNI(buffer, logger);
154
- }
155
-
156
- /**
157
- * Attempts to extract SNI from the PSK extension in a TLS 1.3 ClientHello.
158
- *
159
- * In TLS 1.3, when a client attempts to resume a session, it may include
160
- * the server name in the PSK identity hint rather than in the SNI extension.
161
- *
162
- * @param buffer - The buffer containing the TLS ClientHello message
163
- * @param enableLogging - Whether to enable detailed debug logging
164
- * @returns The extracted server name or undefined if not found
165
- */
166
- public static extractSNIFromPSKExtension(
167
- buffer: Buffer,
168
- enableLogging: boolean = false
169
- ): string | undefined {
170
- const logger = enableLogging ?
171
- (message: string) => console.log(`[PSK-SNI Extraction] ${message}`) :
172
- undefined;
173
-
174
- return SniExtraction.extractSNIFromPSKExtension(buffer, logger);
175
- }
176
-
177
- /**
178
- * Checks if the buffer contains TLS 1.3 early data (0-RTT)
179
- * @param buffer - The buffer to check
180
- * @param enableLogging - Whether to enable logging
181
- * @returns true if early data is detected
182
- */
183
- public static hasEarlyData(buffer: Buffer, enableLogging: boolean = false): boolean {
184
- // This functionality has been moved to ClientHelloParser
185
- // We can implement it in terms of the parse result if needed
186
- const logger = enableLogging ?
187
- (message: string) => console.log(`[Early Data] ${message}`) :
188
- undefined;
189
-
190
- const parseResult = ClientHelloParser.parseClientHello(buffer, logger);
191
- return parseResult.isValid && parseResult.hasEarlyData;
192
- }
193
-
194
- /**
195
- * Attempts to extract SNI from an initial ClientHello packet and handles
196
- * session resumption edge cases more robustly than the standard extraction.
197
- *
198
- * This method handles:
199
- * 1. Standard SNI extraction
200
- * 2. TLS 1.3 PSK-based resumption (Chrome, Firefox, etc.)
201
- * 3. Session ticket-based resumption
202
- * 4. Fragmented ClientHello messages
203
- * 5. TLS 1.3 Early Data (0-RTT)
204
- * 6. Chrome's connection racing behaviors
205
- *
206
- * @param buffer - The buffer containing the TLS ClientHello message
207
- * @param connectionInfo - Optional connection information for fragment handling
208
- * @param enableLogging - Whether to enable detailed debug logging
209
- * @returns The extracted server name or undefined if not found or more data needed
210
- */
211
- public static extractSNIWithResumptionSupport(
212
- buffer: Buffer,
213
- connectionInfo?: {
214
- sourceIp?: string;
215
- sourcePort?: number;
216
- destIp?: string;
217
- destPort?: number;
218
- },
219
- enableLogging: boolean = false
220
- ): string | undefined {
221
- const logger = enableLogging ?
222
- (message: string) => console.log(`[SNI Extraction] ${message}`) :
223
- undefined;
224
-
225
- return SniExtraction.extractSNIWithResumptionSupport(
226
- buffer,
227
- connectionInfo as ConnectionInfo,
228
- logger
229
- );
230
- }
231
-
232
- /**
233
- * Main entry point for SNI extraction that handles all edge cases.
234
- * This should be called for each TLS packet received from a client.
235
- *
236
- * The method uses connection tracking to handle fragmented ClientHello
237
- * messages and various TLS 1.3 behaviors, including Chrome's connection
238
- * racing patterns and tab reactivation behaviors.
239
- *
240
- * @param buffer - The buffer containing TLS data
241
- * @param connectionInfo - Connection metadata (IPs and ports)
242
- * @param enableLogging - Whether to enable detailed debug logging
243
- * @param cachedSni - Optional cached SNI from previous connections (for racing detection)
244
- * @returns The extracted server name or undefined if not found or more data needed
245
- */
246
- public static processTlsPacket(
247
- buffer: Buffer,
248
- connectionInfo: {
249
- sourceIp: string;
250
- sourcePort: number;
251
- destIp: string;
252
- destPort: number;
253
- timestamp?: number;
254
- },
255
- enableLogging: boolean = false,
256
- cachedSni?: string
257
- ): string | undefined {
258
- const logger = enableLogging ?
259
- (message: string) => console.log(`[TLS Packet] ${message}`) :
260
- undefined;
261
-
262
- return SniExtraction.processTlsPacket(buffer, connectionInfo, logger, cachedSni);
263
- }
264
- }