@push.rocks/smartproxy 20.0.1 → 21.1.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 (164) hide show
  1. package/changelog.md +26 -0
  2. package/dist_ts/core/utils/proxy-protocol.d.ts +5 -17
  3. package/dist_ts/core/utils/proxy-protocol.js +13 -97
  4. package/dist_ts/core/utils/websocket-utils.d.ts +6 -7
  5. package/dist_ts/core/utils/websocket-utils.js +10 -66
  6. package/dist_ts/detection/detectors/http-detector-v2.d.ts +33 -0
  7. package/dist_ts/detection/detectors/http-detector-v2.js +87 -0
  8. package/dist_ts/detection/detectors/http-detector.d.ts +33 -0
  9. package/dist_ts/detection/detectors/http-detector.js +89 -0
  10. package/dist_ts/detection/detectors/quick-detector.d.ts +28 -0
  11. package/dist_ts/detection/detectors/quick-detector.js +131 -0
  12. package/dist_ts/detection/detectors/routing-extractor.d.ts +28 -0
  13. package/dist_ts/detection/detectors/routing-extractor.js +122 -0
  14. package/dist_ts/detection/detectors/tls-detector-v2.d.ts +33 -0
  15. package/dist_ts/detection/detectors/tls-detector-v2.js +80 -0
  16. package/dist_ts/detection/detectors/tls-detector.d.ts +33 -0
  17. package/dist_ts/detection/detectors/tls-detector.js +106 -0
  18. package/dist_ts/detection/index.d.ts +17 -0
  19. package/dist_ts/detection/index.js +22 -0
  20. package/dist_ts/detection/models/detection-types.d.ts +87 -0
  21. package/dist_ts/detection/models/detection-types.js +5 -0
  22. package/dist_ts/detection/models/interfaces.d.ts +97 -0
  23. package/dist_ts/detection/models/interfaces.js +5 -0
  24. package/dist_ts/detection/protocol-detector-v2.d.ts +46 -0
  25. package/dist_ts/detection/protocol-detector-v2.js +116 -0
  26. package/dist_ts/detection/protocol-detector.d.ts +74 -0
  27. package/dist_ts/detection/protocol-detector.js +173 -0
  28. package/dist_ts/detection/utils/buffer-utils.d.ts +61 -0
  29. package/dist_ts/detection/utils/buffer-utils.js +127 -0
  30. package/dist_ts/detection/utils/fragment-manager.d.ts +31 -0
  31. package/dist_ts/detection/utils/fragment-manager.js +53 -0
  32. package/dist_ts/detection/utils/parser-utils.d.ts +42 -0
  33. package/dist_ts/detection/utils/parser-utils.js +63 -0
  34. package/dist_ts/index.d.ts +2 -1
  35. package/dist_ts/index.js +3 -2
  36. package/dist_ts/protocols/common/fragment-handler.d.ts +73 -0
  37. package/dist_ts/protocols/common/fragment-handler.js +117 -0
  38. package/dist_ts/protocols/common/index.d.ts +7 -0
  39. package/dist_ts/protocols/common/index.js +8 -0
  40. package/dist_ts/protocols/common/types.d.ts +68 -0
  41. package/dist_ts/protocols/common/types.js +7 -0
  42. package/dist_ts/protocols/http/constants.d.ts +119 -0
  43. package/dist_ts/protocols/http/constants.js +200 -0
  44. package/dist_ts/protocols/http/index.d.ts +7 -0
  45. package/dist_ts/protocols/http/index.js +8 -0
  46. package/dist_ts/protocols/http/parser.d.ts +58 -0
  47. package/dist_ts/protocols/http/parser.js +184 -0
  48. package/dist_ts/protocols/http/types.d.ts +62 -0
  49. package/dist_ts/protocols/http/types.js +5 -0
  50. package/dist_ts/protocols/index.d.ts +11 -0
  51. package/dist_ts/protocols/index.js +12 -0
  52. package/dist_ts/protocols/proxy/index.d.ts +6 -0
  53. package/dist_ts/protocols/proxy/index.js +7 -0
  54. package/dist_ts/protocols/proxy/parser.d.ts +44 -0
  55. package/dist_ts/protocols/proxy/parser.js +153 -0
  56. package/dist_ts/protocols/proxy/types.d.ts +47 -0
  57. package/dist_ts/protocols/proxy/types.js +6 -0
  58. package/dist_ts/protocols/tls/alerts/index.d.ts +4 -0
  59. package/dist_ts/protocols/tls/alerts/index.js +5 -0
  60. package/dist_ts/protocols/tls/alerts/tls-alert.d.ts +150 -0
  61. package/dist_ts/protocols/tls/alerts/tls-alert.js +226 -0
  62. package/dist_ts/protocols/tls/constants.d.ts +122 -0
  63. package/dist_ts/protocols/tls/constants.js +135 -0
  64. package/dist_ts/protocols/tls/index.d.ts +12 -0
  65. package/dist_ts/protocols/tls/index.js +27 -0
  66. package/dist_ts/protocols/tls/parser.d.ts +53 -0
  67. package/dist_ts/protocols/tls/parser.js +294 -0
  68. package/dist_ts/protocols/tls/sni/client-hello-parser.d.ts +100 -0
  69. package/dist_ts/protocols/tls/sni/client-hello-parser.js +463 -0
  70. package/dist_ts/protocols/tls/sni/index.d.ts +5 -0
  71. package/dist_ts/protocols/tls/sni/index.js +6 -0
  72. package/dist_ts/protocols/tls/sni/sni-extraction.d.ts +58 -0
  73. package/dist_ts/protocols/tls/sni/sni-extraction.js +275 -0
  74. package/dist_ts/protocols/tls/types.d.ts +65 -0
  75. package/dist_ts/protocols/tls/types.js +5 -0
  76. package/dist_ts/protocols/tls/utils/index.d.ts +4 -0
  77. package/dist_ts/protocols/tls/utils/index.js +5 -0
  78. package/dist_ts/protocols/tls/utils/tls-utils.d.ts +158 -0
  79. package/dist_ts/protocols/tls/utils/tls-utils.js +187 -0
  80. package/dist_ts/protocols/websocket/constants.d.ts +55 -0
  81. package/dist_ts/protocols/websocket/constants.js +58 -0
  82. package/dist_ts/protocols/websocket/index.d.ts +7 -0
  83. package/dist_ts/protocols/websocket/index.js +8 -0
  84. package/dist_ts/protocols/websocket/types.d.ts +47 -0
  85. package/dist_ts/protocols/websocket/types.js +5 -0
  86. package/dist_ts/protocols/websocket/utils.d.ts +25 -0
  87. package/dist_ts/protocols/websocket/utils.js +103 -0
  88. package/dist_ts/proxies/http-proxy/models/http-types.d.ts +25 -27
  89. package/dist_ts/proxies/http-proxy/models/http-types.js +24 -44
  90. package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +5 -0
  91. package/dist_ts/proxies/smart-proxy/models/route-types.js +1 -1
  92. package/dist_ts/proxies/smart-proxy/route-connection-handler.js +81 -61
  93. package/dist_ts/proxies/smart-proxy/tls-manager.js +2 -1
  94. package/dist_ts/proxies/smart-proxy/utils/index.d.ts +1 -2
  95. package/dist_ts/proxies/smart-proxy/utils/index.js +3 -4
  96. package/dist_ts/proxies/smart-proxy/utils/route-helpers.d.ts +112 -8
  97. package/dist_ts/proxies/smart-proxy/utils/route-helpers.js +231 -76
  98. package/dist_ts/tls/index.d.ts +5 -7
  99. package/dist_ts/tls/index.js +8 -11
  100. package/dist_ts/tls/sni/client-hello-parser.js +3 -2
  101. package/dist_ts/tls/sni/sni-handler.js +4 -4
  102. package/dist_ts/tls/utils/tls-utils.d.ts +1 -110
  103. package/dist_ts/tls/utils/tls-utils.js +4 -116
  104. package/package.json +17 -8
  105. package/readme.md +471 -2345
  106. package/readme.plan.md +0 -0
  107. package/ts/core/utils/proxy-protocol.ts +14 -131
  108. package/ts/core/utils/websocket-utils.ts +12 -60
  109. package/ts/detection/detectors/http-detector.ts +114 -0
  110. package/ts/detection/detectors/quick-detector.ts +148 -0
  111. package/ts/detection/detectors/routing-extractor.ts +147 -0
  112. package/ts/detection/detectors/tls-detector.ts +120 -0
  113. package/ts/detection/index.ts +25 -0
  114. package/ts/detection/models/detection-types.ts +102 -0
  115. package/ts/detection/models/interfaces.ts +115 -0
  116. package/ts/detection/protocol-detector.ts +230 -0
  117. package/ts/detection/utils/buffer-utils.ts +141 -0
  118. package/ts/detection/utils/fragment-manager.ts +64 -0
  119. package/ts/detection/utils/parser-utils.ts +77 -0
  120. package/ts/index.ts +3 -2
  121. package/ts/protocols/common/fragment-handler.ts +163 -0
  122. package/ts/protocols/common/index.ts +8 -0
  123. package/ts/protocols/common/types.ts +76 -0
  124. package/ts/protocols/http/constants.ts +219 -0
  125. package/ts/protocols/http/index.ts +8 -0
  126. package/ts/protocols/http/parser.ts +219 -0
  127. package/ts/protocols/http/types.ts +70 -0
  128. package/ts/protocols/index.ts +12 -0
  129. package/ts/protocols/proxy/index.ts +7 -0
  130. package/ts/protocols/proxy/parser.ts +183 -0
  131. package/ts/protocols/proxy/types.ts +53 -0
  132. package/ts/{tls → protocols/tls}/alerts/tls-alert.ts +1 -1
  133. package/ts/protocols/tls/index.ts +37 -0
  134. package/ts/protocols/tls/sni/index.ts +6 -0
  135. package/ts/{tls → protocols/tls}/utils/tls-utils.ts +1 -1
  136. package/ts/protocols/websocket/constants.ts +60 -0
  137. package/ts/protocols/websocket/index.ts +8 -0
  138. package/ts/protocols/websocket/types.ts +53 -0
  139. package/ts/protocols/websocket/utils.ts +98 -0
  140. package/ts/proxies/http-proxy/models/http-types.ts +29 -46
  141. package/ts/proxies/smart-proxy/models/interfaces.ts +7 -1
  142. package/ts/proxies/smart-proxy/models/route-types.ts +0 -1
  143. package/ts/proxies/smart-proxy/route-connection-handler.ts +91 -68
  144. package/ts/proxies/smart-proxy/tls-manager.ts +1 -0
  145. package/ts/proxies/smart-proxy/utils/index.ts +2 -13
  146. package/ts/proxies/smart-proxy/utils/route-helpers.ts +323 -86
  147. package/ts/tls/index.ts +8 -12
  148. package/ts/tls/sni/sni-handler.ts +3 -3
  149. package/ts/forwarding/config/forwarding-types.ts +0 -76
  150. package/ts/forwarding/config/index.ts +0 -26
  151. package/ts/forwarding/factory/forwarding-factory.ts +0 -189
  152. package/ts/forwarding/factory/index.ts +0 -5
  153. package/ts/forwarding/handlers/base-handler.ts +0 -155
  154. package/ts/forwarding/handlers/http-handler.ts +0 -163
  155. package/ts/forwarding/handlers/https-passthrough-handler.ts +0 -185
  156. package/ts/forwarding/handlers/https-terminate-to-http-handler.ts +0 -312
  157. package/ts/forwarding/handlers/https-terminate-to-https-handler.ts +0 -297
  158. package/ts/forwarding/handlers/index.ts +0 -9
  159. package/ts/forwarding/index.ts +0 -35
  160. package/ts/proxies/smart-proxy/utils/route-patterns.ts +0 -403
  161. /package/ts/{tls → protocols/tls}/alerts/index.ts +0 -0
  162. /package/ts/{tls → protocols/tls}/sni/client-hello-parser.ts +0 -0
  163. /package/ts/{tls → protocols/tls}/sni/sni-extraction.ts +0 -0
  164. /package/ts/{tls → protocols/tls}/utils/index.ts +0 -0
@@ -1,297 +0,0 @@
1
- import * as plugins from '../../plugins.js';
2
- import { ForwardingHandler } from './base-handler.js';
3
- import type { IForwardConfig } from '../config/forwarding-types.js';
4
- import { ForwardingHandlerEvents } from '../config/forwarding-types.js';
5
- import { setupSocketHandlers, createSocketWithErrorHandler, setupBidirectionalForwarding } from '../../core/utils/socket-utils.js';
6
-
7
- /**
8
- * Handler for HTTPS termination with HTTPS backend
9
- */
10
- export class HttpsTerminateToHttpsHandler extends ForwardingHandler {
11
- private secureContext: plugins.tls.SecureContext | null = null;
12
-
13
- /**
14
- * Create a new HTTPS termination with HTTPS backend handler
15
- * @param config The forwarding configuration
16
- */
17
- constructor(config: IForwardConfig) {
18
- super(config);
19
-
20
- // Validate that this is an HTTPS terminate to HTTPS configuration
21
- if (config.type !== 'https-terminate-to-https') {
22
- throw new Error(`Invalid configuration type for HttpsTerminateToHttpsHandler: ${config.type}`);
23
- }
24
- }
25
-
26
- /**
27
- * Initialize the handler, setting up TLS context
28
- */
29
- public async initialize(): Promise<void> {
30
- // We need to load or create TLS certificates for termination
31
- if (this.config.https?.customCert) {
32
- // Use custom certificate from configuration
33
- this.secureContext = plugins.tls.createSecureContext({
34
- key: this.config.https.customCert.key,
35
- cert: this.config.https.customCert.cert
36
- });
37
-
38
- this.emit(ForwardingHandlerEvents.CERTIFICATE_LOADED, {
39
- source: 'config',
40
- domain: this.config.target.host
41
- });
42
- } else if (this.config.acme?.enabled) {
43
- // Request certificate through ACME if needed
44
- this.emit(ForwardingHandlerEvents.CERTIFICATE_NEEDED, {
45
- domain: Array.isArray(this.config.target.host)
46
- ? this.config.target.host[0]
47
- : this.config.target.host,
48
- useProduction: this.config.acme.production || false
49
- });
50
-
51
- // In a real implementation, we would wait for the certificate to be issued
52
- // For now, we'll use a dummy context
53
- this.secureContext = plugins.tls.createSecureContext({
54
- key: '-----BEGIN PRIVATE KEY-----\nDummy key\n-----END PRIVATE KEY-----',
55
- cert: '-----BEGIN CERTIFICATE-----\nDummy cert\n-----END CERTIFICATE-----'
56
- });
57
- } else {
58
- throw new Error('HTTPS termination requires either a custom certificate or ACME enabled');
59
- }
60
- }
61
-
62
- /**
63
- * Set the secure context for TLS termination
64
- * Called when a certificate is available
65
- * @param context The secure context
66
- */
67
- public setSecureContext(context: plugins.tls.SecureContext): void {
68
- this.secureContext = context;
69
- }
70
-
71
- /**
72
- * Handle a TLS/SSL socket connection by terminating TLS and creating a new TLS connection to backend
73
- * @param clientSocket The incoming socket from the client
74
- */
75
- public handleConnection(clientSocket: plugins.net.Socket): void {
76
- // Make sure we have a secure context
77
- if (!this.secureContext) {
78
- clientSocket.destroy(new Error('TLS secure context not initialized'));
79
- return;
80
- }
81
-
82
- const remoteAddress = clientSocket.remoteAddress || 'unknown';
83
- const remotePort = clientSocket.remotePort || 0;
84
-
85
- // Create a TLS socket using our secure context
86
- const tlsSocket = new plugins.tls.TLSSocket(clientSocket, {
87
- secureContext: this.secureContext,
88
- isServer: true
89
- });
90
-
91
- this.emit(ForwardingHandlerEvents.CONNECTED, {
92
- remoteAddress,
93
- remotePort,
94
- tls: true
95
- });
96
-
97
- // Variable to track backend socket
98
- let backendSocket: plugins.tls.TLSSocket | null = null;
99
- let isConnectedToBackend = false;
100
-
101
- // Set up initial error handling for TLS socket
102
- const tlsCleanupHandler = (reason: string) => {
103
- if (!isConnectedToBackend) {
104
- // If backend not connected yet, just emit disconnected event
105
- this.emit(ForwardingHandlerEvents.DISCONNECTED, {
106
- remoteAddress,
107
- reason
108
- });
109
-
110
- // Cleanup TLS socket if needed
111
- if (!tlsSocket.destroyed) {
112
- tlsSocket.destroy();
113
- }
114
- }
115
- // If connected to backend, setupBidirectionalForwarding will handle cleanup
116
- };
117
-
118
- setupSocketHandlers(tlsSocket, tlsCleanupHandler, undefined, 'tls');
119
-
120
- // Set timeout
121
- const timeout = this.getTimeout();
122
- tlsSocket.setTimeout(timeout);
123
-
124
- tlsSocket.on('timeout', () => {
125
- this.emit(ForwardingHandlerEvents.ERROR, {
126
- remoteAddress,
127
- error: 'TLS connection timeout'
128
- });
129
- tlsCleanupHandler('timeout');
130
- });
131
-
132
- // Get the target from configuration
133
- const target = this.getTargetFromConfig();
134
-
135
- // Set up the connection to the HTTPS backend
136
- const connectToBackend = () => {
137
- backendSocket = plugins.tls.connect({
138
- host: target.host,
139
- port: target.port,
140
- // In a real implementation, we would configure TLS options
141
- rejectUnauthorized: false // For testing only, never use in production
142
- }, () => {
143
- isConnectedToBackend = true;
144
-
145
- this.emit(ForwardingHandlerEvents.DATA_FORWARDED, {
146
- direction: 'outbound',
147
- target: `${target.host}:${target.port}`,
148
- tls: true
149
- });
150
-
151
- // Set up bidirectional forwarding with proper cleanup
152
- setupBidirectionalForwarding(tlsSocket, backendSocket!, {
153
- onCleanup: (reason) => {
154
- this.emit(ForwardingHandlerEvents.DISCONNECTED, {
155
- remoteAddress,
156
- reason
157
- });
158
- },
159
- enableHalfOpen: false // Close both when one closes
160
- });
161
-
162
- // Set timeout for backend socket
163
- backendSocket!.setTimeout(timeout);
164
-
165
- backendSocket!.on('timeout', () => {
166
- this.emit(ForwardingHandlerEvents.ERROR, {
167
- remoteAddress,
168
- error: 'Backend connection timeout'
169
- });
170
- // Let setupBidirectionalForwarding handle the cleanup
171
- });
172
- });
173
-
174
- // Handle backend connection errors
175
- backendSocket.on('error', (error) => {
176
- this.emit(ForwardingHandlerEvents.ERROR, {
177
- remoteAddress,
178
- error: `Backend connection error: ${error.message}`
179
- });
180
-
181
- if (!isConnectedToBackend) {
182
- // Connection failed, clean up TLS socket
183
- if (!tlsSocket.destroyed) {
184
- tlsSocket.destroy();
185
- }
186
- this.emit(ForwardingHandlerEvents.DISCONNECTED, {
187
- remoteAddress,
188
- reason: `backend_connection_failed: ${error.message}`
189
- });
190
- }
191
- // If connected, let setupBidirectionalForwarding handle cleanup
192
- });
193
- };
194
-
195
- // Wait for the TLS handshake to complete before connecting to backend
196
- tlsSocket.on('secure', () => {
197
- connectToBackend();
198
- });
199
- }
200
-
201
- /**
202
- * Handle an HTTP request by forwarding to the HTTPS backend
203
- * @param req The HTTP request
204
- * @param res The HTTP response
205
- */
206
- public handleHttpRequest(req: plugins.http.IncomingMessage, res: plugins.http.ServerResponse): void {
207
- // Check if we should redirect to HTTPS
208
- if (this.config.http?.redirectToHttps) {
209
- this.redirectToHttps(req, res);
210
- return;
211
- }
212
-
213
- // Get the target from configuration
214
- const target = this.getTargetFromConfig();
215
-
216
- // Create custom headers with variable substitution
217
- const variables = {
218
- clientIp: req.socket.remoteAddress || 'unknown'
219
- };
220
-
221
- // Prepare headers, merging with any custom headers from config
222
- const headers = this.applyCustomHeaders(req.headers, variables);
223
-
224
- // Create the proxy request options
225
- const options = {
226
- hostname: target.host,
227
- port: target.port,
228
- path: req.url,
229
- method: req.method,
230
- headers,
231
- // In a real implementation, we would configure TLS options
232
- rejectUnauthorized: false // For testing only, never use in production
233
- };
234
-
235
- // Create the proxy request using HTTPS
236
- const proxyReq = plugins.https.request(options, (proxyRes) => {
237
- // Copy status code and headers from the proxied response
238
- res.writeHead(proxyRes.statusCode || 500, proxyRes.headers);
239
-
240
- // Pipe the proxy response to the client response
241
- proxyRes.pipe(res);
242
-
243
- // Track response size for logging
244
- let responseSize = 0;
245
- proxyRes.on('data', (chunk) => {
246
- responseSize += chunk.length;
247
- });
248
-
249
- proxyRes.on('end', () => {
250
- this.emit(ForwardingHandlerEvents.HTTP_RESPONSE, {
251
- statusCode: proxyRes.statusCode,
252
- headers: proxyRes.headers,
253
- size: responseSize
254
- });
255
- });
256
- });
257
-
258
- // Handle errors in the proxy request
259
- proxyReq.on('error', (error) => {
260
- this.emit(ForwardingHandlerEvents.ERROR, {
261
- remoteAddress: req.socket.remoteAddress,
262
- error: `Proxy request error: ${error.message}`
263
- });
264
-
265
- // Send an error response if headers haven't been sent yet
266
- if (!res.headersSent) {
267
- res.writeHead(502, { 'Content-Type': 'text/plain' });
268
- res.end(`Error forwarding request: ${error.message}`);
269
- } else {
270
- // Just end the response if headers have already been sent
271
- res.end();
272
- }
273
- });
274
-
275
- // Track request details for logging
276
- let requestSize = 0;
277
- req.on('data', (chunk) => {
278
- requestSize += chunk.length;
279
- });
280
-
281
- // Log the request
282
- this.emit(ForwardingHandlerEvents.HTTP_REQUEST, {
283
- method: req.method,
284
- url: req.url,
285
- headers: req.headers,
286
- remoteAddress: req.socket.remoteAddress,
287
- target: `${target.host}:${target.port}`
288
- });
289
-
290
- // Pipe the client request to the proxy request
291
- if (req.readable) {
292
- req.pipe(proxyReq);
293
- } else {
294
- proxyReq.end();
295
- }
296
- }
297
- }
@@ -1,9 +0,0 @@
1
- /**
2
- * Forwarding handler implementations
3
- */
4
-
5
- export { ForwardingHandler } from './base-handler.js';
6
- export { HttpForwardingHandler } from './http-handler.js';
7
- export { HttpsPassthroughHandler } from './https-passthrough-handler.js';
8
- export { HttpsTerminateToHttpHandler } from './https-terminate-to-http-handler.js';
9
- export { HttpsTerminateToHttpsHandler } from './https-terminate-to-https-handler.js';
@@ -1,35 +0,0 @@
1
- /**
2
- * Forwarding system module
3
- * Provides a flexible and type-safe way to configure and manage various forwarding strategies
4
- */
5
-
6
- // Export handlers
7
- export { ForwardingHandler } from './handlers/base-handler.js';
8
- export * from './handlers/http-handler.js';
9
- export * from './handlers/https-passthrough-handler.js';
10
- export * from './handlers/https-terminate-to-http-handler.js';
11
- export * from './handlers/https-terminate-to-https-handler.js';
12
-
13
- // Export factory
14
- export * from './factory/forwarding-factory.js';
15
-
16
- // Export types - these include TForwardingType and IForwardConfig
17
- export type {
18
- TForwardingType,
19
- IForwardConfig,
20
- IForwardingHandler
21
- } from './config/forwarding-types.js';
22
-
23
- export {
24
- ForwardingHandlerEvents
25
- } from './config/forwarding-types.js';
26
-
27
- // Export route helpers directly from route-patterns
28
- export {
29
- createHttpRoute,
30
- createHttpsTerminateRoute,
31
- createHttpsPassthroughRoute,
32
- createHttpToHttpsRedirect,
33
- createCompleteHttpsServer,
34
- createLoadBalancerRoute
35
- } from '../proxies/smart-proxy/utils/route-patterns.js';