@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.
- package/changelog.md +26 -0
- package/dist_ts/core/utils/proxy-protocol.d.ts +5 -17
- package/dist_ts/core/utils/proxy-protocol.js +13 -97
- package/dist_ts/core/utils/websocket-utils.d.ts +6 -7
- package/dist_ts/core/utils/websocket-utils.js +10 -66
- package/dist_ts/detection/detectors/http-detector-v2.d.ts +33 -0
- package/dist_ts/detection/detectors/http-detector-v2.js +87 -0
- package/dist_ts/detection/detectors/http-detector.d.ts +33 -0
- package/dist_ts/detection/detectors/http-detector.js +89 -0
- package/dist_ts/detection/detectors/quick-detector.d.ts +28 -0
- package/dist_ts/detection/detectors/quick-detector.js +131 -0
- package/dist_ts/detection/detectors/routing-extractor.d.ts +28 -0
- package/dist_ts/detection/detectors/routing-extractor.js +122 -0
- package/dist_ts/detection/detectors/tls-detector-v2.d.ts +33 -0
- package/dist_ts/detection/detectors/tls-detector-v2.js +80 -0
- package/dist_ts/detection/detectors/tls-detector.d.ts +33 -0
- package/dist_ts/detection/detectors/tls-detector.js +106 -0
- package/dist_ts/detection/index.d.ts +17 -0
- package/dist_ts/detection/index.js +22 -0
- package/dist_ts/detection/models/detection-types.d.ts +87 -0
- package/dist_ts/detection/models/detection-types.js +5 -0
- package/dist_ts/detection/models/interfaces.d.ts +97 -0
- package/dist_ts/detection/models/interfaces.js +5 -0
- package/dist_ts/detection/protocol-detector-v2.d.ts +46 -0
- package/dist_ts/detection/protocol-detector-v2.js +116 -0
- package/dist_ts/detection/protocol-detector.d.ts +74 -0
- package/dist_ts/detection/protocol-detector.js +173 -0
- package/dist_ts/detection/utils/buffer-utils.d.ts +61 -0
- package/dist_ts/detection/utils/buffer-utils.js +127 -0
- package/dist_ts/detection/utils/fragment-manager.d.ts +31 -0
- package/dist_ts/detection/utils/fragment-manager.js +53 -0
- package/dist_ts/detection/utils/parser-utils.d.ts +42 -0
- package/dist_ts/detection/utils/parser-utils.js +63 -0
- package/dist_ts/index.d.ts +2 -1
- package/dist_ts/index.js +3 -2
- package/dist_ts/protocols/common/fragment-handler.d.ts +73 -0
- package/dist_ts/protocols/common/fragment-handler.js +117 -0
- package/dist_ts/protocols/common/index.d.ts +7 -0
- package/dist_ts/protocols/common/index.js +8 -0
- package/dist_ts/protocols/common/types.d.ts +68 -0
- package/dist_ts/protocols/common/types.js +7 -0
- package/dist_ts/protocols/http/constants.d.ts +119 -0
- package/dist_ts/protocols/http/constants.js +200 -0
- package/dist_ts/protocols/http/index.d.ts +7 -0
- package/dist_ts/protocols/http/index.js +8 -0
- package/dist_ts/protocols/http/parser.d.ts +58 -0
- package/dist_ts/protocols/http/parser.js +184 -0
- package/dist_ts/protocols/http/types.d.ts +62 -0
- package/dist_ts/protocols/http/types.js +5 -0
- package/dist_ts/protocols/index.d.ts +11 -0
- package/dist_ts/protocols/index.js +12 -0
- package/dist_ts/protocols/proxy/index.d.ts +6 -0
- package/dist_ts/protocols/proxy/index.js +7 -0
- package/dist_ts/protocols/proxy/parser.d.ts +44 -0
- package/dist_ts/protocols/proxy/parser.js +153 -0
- package/dist_ts/protocols/proxy/types.d.ts +47 -0
- package/dist_ts/protocols/proxy/types.js +6 -0
- package/dist_ts/protocols/tls/alerts/index.d.ts +4 -0
- package/dist_ts/protocols/tls/alerts/index.js +5 -0
- package/dist_ts/protocols/tls/alerts/tls-alert.d.ts +150 -0
- package/dist_ts/protocols/tls/alerts/tls-alert.js +226 -0
- package/dist_ts/protocols/tls/constants.d.ts +122 -0
- package/dist_ts/protocols/tls/constants.js +135 -0
- package/dist_ts/protocols/tls/index.d.ts +12 -0
- package/dist_ts/protocols/tls/index.js +27 -0
- package/dist_ts/protocols/tls/parser.d.ts +53 -0
- package/dist_ts/protocols/tls/parser.js +294 -0
- package/dist_ts/protocols/tls/sni/client-hello-parser.d.ts +100 -0
- package/dist_ts/protocols/tls/sni/client-hello-parser.js +463 -0
- package/dist_ts/protocols/tls/sni/index.d.ts +5 -0
- package/dist_ts/protocols/tls/sni/index.js +6 -0
- package/dist_ts/protocols/tls/sni/sni-extraction.d.ts +58 -0
- package/dist_ts/protocols/tls/sni/sni-extraction.js +275 -0
- package/dist_ts/protocols/tls/types.d.ts +65 -0
- package/dist_ts/protocols/tls/types.js +5 -0
- package/dist_ts/protocols/tls/utils/index.d.ts +4 -0
- package/dist_ts/protocols/tls/utils/index.js +5 -0
- package/dist_ts/protocols/tls/utils/tls-utils.d.ts +158 -0
- package/dist_ts/protocols/tls/utils/tls-utils.js +187 -0
- package/dist_ts/protocols/websocket/constants.d.ts +55 -0
- package/dist_ts/protocols/websocket/constants.js +58 -0
- package/dist_ts/protocols/websocket/index.d.ts +7 -0
- package/dist_ts/protocols/websocket/index.js +8 -0
- package/dist_ts/protocols/websocket/types.d.ts +47 -0
- package/dist_ts/protocols/websocket/types.js +5 -0
- package/dist_ts/protocols/websocket/utils.d.ts +25 -0
- package/dist_ts/protocols/websocket/utils.js +103 -0
- package/dist_ts/proxies/http-proxy/models/http-types.d.ts +25 -27
- package/dist_ts/proxies/http-proxy/models/http-types.js +24 -44
- package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +5 -0
- package/dist_ts/proxies/smart-proxy/models/route-types.js +1 -1
- package/dist_ts/proxies/smart-proxy/route-connection-handler.js +81 -61
- package/dist_ts/proxies/smart-proxy/tls-manager.js +2 -1
- package/dist_ts/proxies/smart-proxy/utils/index.d.ts +1 -2
- package/dist_ts/proxies/smart-proxy/utils/index.js +3 -4
- package/dist_ts/proxies/smart-proxy/utils/route-helpers.d.ts +112 -8
- package/dist_ts/proxies/smart-proxy/utils/route-helpers.js +231 -76
- package/dist_ts/tls/index.d.ts +5 -7
- package/dist_ts/tls/index.js +8 -11
- package/dist_ts/tls/sni/client-hello-parser.js +3 -2
- package/dist_ts/tls/sni/sni-handler.js +4 -4
- package/dist_ts/tls/utils/tls-utils.d.ts +1 -110
- package/dist_ts/tls/utils/tls-utils.js +4 -116
- package/package.json +17 -8
- package/readme.md +471 -2345
- package/readme.plan.md +0 -0
- package/ts/core/utils/proxy-protocol.ts +14 -131
- package/ts/core/utils/websocket-utils.ts +12 -60
- package/ts/detection/detectors/http-detector.ts +114 -0
- package/ts/detection/detectors/quick-detector.ts +148 -0
- package/ts/detection/detectors/routing-extractor.ts +147 -0
- package/ts/detection/detectors/tls-detector.ts +120 -0
- package/ts/detection/index.ts +25 -0
- package/ts/detection/models/detection-types.ts +102 -0
- package/ts/detection/models/interfaces.ts +115 -0
- package/ts/detection/protocol-detector.ts +230 -0
- package/ts/detection/utils/buffer-utils.ts +141 -0
- package/ts/detection/utils/fragment-manager.ts +64 -0
- package/ts/detection/utils/parser-utils.ts +77 -0
- package/ts/index.ts +3 -2
- package/ts/protocols/common/fragment-handler.ts +163 -0
- package/ts/protocols/common/index.ts +8 -0
- package/ts/protocols/common/types.ts +76 -0
- package/ts/protocols/http/constants.ts +219 -0
- package/ts/protocols/http/index.ts +8 -0
- package/ts/protocols/http/parser.ts +219 -0
- package/ts/protocols/http/types.ts +70 -0
- package/ts/protocols/index.ts +12 -0
- package/ts/protocols/proxy/index.ts +7 -0
- package/ts/protocols/proxy/parser.ts +183 -0
- package/ts/protocols/proxy/types.ts +53 -0
- package/ts/{tls → protocols/tls}/alerts/tls-alert.ts +1 -1
- package/ts/protocols/tls/index.ts +37 -0
- package/ts/protocols/tls/sni/index.ts +6 -0
- package/ts/{tls → protocols/tls}/utils/tls-utils.ts +1 -1
- package/ts/protocols/websocket/constants.ts +60 -0
- package/ts/protocols/websocket/index.ts +8 -0
- package/ts/protocols/websocket/types.ts +53 -0
- package/ts/protocols/websocket/utils.ts +98 -0
- package/ts/proxies/http-proxy/models/http-types.ts +29 -46
- package/ts/proxies/smart-proxy/models/interfaces.ts +7 -1
- package/ts/proxies/smart-proxy/models/route-types.ts +0 -1
- package/ts/proxies/smart-proxy/route-connection-handler.ts +91 -68
- package/ts/proxies/smart-proxy/tls-manager.ts +1 -0
- package/ts/proxies/smart-proxy/utils/index.ts +2 -13
- package/ts/proxies/smart-proxy/utils/route-helpers.ts +323 -86
- package/ts/tls/index.ts +8 -12
- package/ts/tls/sni/sni-handler.ts +3 -3
- package/ts/forwarding/config/forwarding-types.ts +0 -76
- package/ts/forwarding/config/index.ts +0 -26
- package/ts/forwarding/factory/forwarding-factory.ts +0 -189
- package/ts/forwarding/factory/index.ts +0 -5
- package/ts/forwarding/handlers/base-handler.ts +0 -155
- package/ts/forwarding/handlers/http-handler.ts +0 -163
- package/ts/forwarding/handlers/https-passthrough-handler.ts +0 -185
- package/ts/forwarding/handlers/https-terminate-to-http-handler.ts +0 -312
- package/ts/forwarding/handlers/https-terminate-to-https-handler.ts +0 -297
- package/ts/forwarding/handlers/index.ts +0 -9
- package/ts/forwarding/index.ts +0 -35
- package/ts/proxies/smart-proxy/utils/route-patterns.ts +0 -403
- /package/ts/{tls → protocols/tls}/alerts/index.ts +0 -0
- /package/ts/{tls → protocols/tls}/sni/client-hello-parser.ts +0 -0
- /package/ts/{tls → protocols/tls}/sni/sni-extraction.ts +0 -0
- /package/ts/{tls → protocols/tls}/utils/index.ts +0 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TLS Protocol Detector
|
|
3
|
+
*
|
|
4
|
+
* Simplified TLS detection using the new architecture
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { IProtocolDetector } from '../models/interfaces.js';
|
|
8
|
+
import type { IDetectionResult, IDetectionOptions } from '../models/detection-types.js';
|
|
9
|
+
import type { IProtocolDetectionResult, IConnectionContext } from '../../protocols/common/types.js';
|
|
10
|
+
import { QuickProtocolDetector } from './quick-detector.js';
|
|
11
|
+
import { RoutingExtractor } from './routing-extractor.js';
|
|
12
|
+
import { DetectionFragmentManager } from '../utils/fragment-manager.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Simplified TLS detector
|
|
16
|
+
*/
|
|
17
|
+
export class TlsDetector implements IProtocolDetector {
|
|
18
|
+
private quickDetector = new QuickProtocolDetector();
|
|
19
|
+
private fragmentManager: DetectionFragmentManager;
|
|
20
|
+
|
|
21
|
+
constructor(fragmentManager?: DetectionFragmentManager) {
|
|
22
|
+
this.fragmentManager = fragmentManager || new DetectionFragmentManager();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Check if buffer can be handled by this detector
|
|
27
|
+
*/
|
|
28
|
+
canHandle(buffer: Buffer): boolean {
|
|
29
|
+
const result = this.quickDetector.quickDetect(buffer);
|
|
30
|
+
return result.protocol === 'tls' && result.confidence > 50;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get minimum bytes needed for detection
|
|
35
|
+
*/
|
|
36
|
+
getMinimumBytes(): number {
|
|
37
|
+
return 5; // TLS record header
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Detect TLS protocol from buffer
|
|
42
|
+
*/
|
|
43
|
+
detect(buffer: Buffer, options?: IDetectionOptions): IDetectionResult | null {
|
|
44
|
+
// Quick detection first
|
|
45
|
+
const quickResult = this.quickDetector.quickDetect(buffer);
|
|
46
|
+
|
|
47
|
+
if (quickResult.protocol !== 'tls' || quickResult.confidence < 50) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Extract TLS version for compatibility
|
|
52
|
+
let tlsVersion: string | undefined;
|
|
53
|
+
if (buffer.length >= 3) {
|
|
54
|
+
const versionByte = buffer[2];
|
|
55
|
+
switch (versionByte) {
|
|
56
|
+
case 0x00: tlsVersion = 'SSLv3'; break;
|
|
57
|
+
case 0x01: tlsVersion = 'TLSv1.0'; break;
|
|
58
|
+
case 0x02: tlsVersion = 'TLSv1.1'; break;
|
|
59
|
+
case 0x03: tlsVersion = 'TLSv1.2'; break;
|
|
60
|
+
case 0x04: tlsVersion = 'TLSv1.3'; break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// If we don't need domain extraction, we can return early
|
|
65
|
+
if (quickResult.confidence >= 95 && !options?.extractFullHeaders) {
|
|
66
|
+
return {
|
|
67
|
+
protocol: 'tls',
|
|
68
|
+
connectionInfo: {
|
|
69
|
+
protocol: 'tls',
|
|
70
|
+
tlsVersion: tlsVersion as any
|
|
71
|
+
},
|
|
72
|
+
isComplete: true
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Extract routing information if needed
|
|
77
|
+
const routing = RoutingExtractor.extract(buffer, 'tls');
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
protocol: 'tls',
|
|
81
|
+
connectionInfo: {
|
|
82
|
+
protocol: 'tls',
|
|
83
|
+
domain: routing?.domain,
|
|
84
|
+
sni: routing?.domain,
|
|
85
|
+
tlsVersion: tlsVersion as any
|
|
86
|
+
},
|
|
87
|
+
isComplete: !quickResult.requiresMoreData,
|
|
88
|
+
bytesNeeded: quickResult.requiresMoreData ? buffer.length + 100 : undefined
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Handle fragmented detection
|
|
94
|
+
*/
|
|
95
|
+
detectWithContext(
|
|
96
|
+
buffer: Buffer,
|
|
97
|
+
context: IConnectionContext,
|
|
98
|
+
options?: IDetectionOptions
|
|
99
|
+
): IDetectionResult | null {
|
|
100
|
+
const handler = this.fragmentManager.getHandler('tls');
|
|
101
|
+
const connectionId = DetectionFragmentManager.createConnectionId(context);
|
|
102
|
+
|
|
103
|
+
// Add fragment
|
|
104
|
+
const result = handler.addFragment(connectionId, buffer);
|
|
105
|
+
|
|
106
|
+
if (result.error) {
|
|
107
|
+
handler.complete(connectionId);
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Try detection on accumulated buffer
|
|
112
|
+
const detectResult = this.detect(result.buffer!, options);
|
|
113
|
+
|
|
114
|
+
if (detectResult && detectResult.isComplete) {
|
|
115
|
+
handler.complete(connectionId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return detectResult;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized Protocol Detection Module
|
|
3
|
+
*
|
|
4
|
+
* This module provides unified protocol detection capabilities for
|
|
5
|
+
* both TLS and HTTP protocols, extracting connection information
|
|
6
|
+
* without consuming the data stream.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// Main detector
|
|
10
|
+
export * from './protocol-detector.js';
|
|
11
|
+
|
|
12
|
+
// Models
|
|
13
|
+
export * from './models/detection-types.js';
|
|
14
|
+
export * from './models/interfaces.js';
|
|
15
|
+
|
|
16
|
+
// Individual detectors
|
|
17
|
+
export * from './detectors/tls-detector.js';
|
|
18
|
+
export * from './detectors/http-detector.js';
|
|
19
|
+
export * from './detectors/quick-detector.js';
|
|
20
|
+
export * from './detectors/routing-extractor.js';
|
|
21
|
+
|
|
22
|
+
// Utilities
|
|
23
|
+
export * from './utils/buffer-utils.js';
|
|
24
|
+
export * from './utils/parser-utils.js';
|
|
25
|
+
export * from './utils/fragment-manager.js';
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
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
|
+
|
|
22
|
+
constructor() {
|
|
23
|
+
this.fragmentManager = new DetectionFragmentManager();
|
|
24
|
+
this.tlsDetector = new TlsDetector(this.fragmentManager);
|
|
25
|
+
this.httpDetector = new HttpDetector(this.fragmentManager);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private static getInstance(): ProtocolDetector {
|
|
29
|
+
if (!this.instance) {
|
|
30
|
+
this.instance = new ProtocolDetector();
|
|
31
|
+
}
|
|
32
|
+
return this.instance;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Detect protocol from buffer data
|
|
37
|
+
*/
|
|
38
|
+
static async detect(buffer: Buffer, options?: IDetectionOptions): Promise<IDetectionResult> {
|
|
39
|
+
return this.getInstance().detectInstance(buffer, options);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private async detectInstance(buffer: Buffer, options?: IDetectionOptions): Promise<IDetectionResult> {
|
|
43
|
+
// Quick sanity check
|
|
44
|
+
if (!buffer || buffer.length === 0) {
|
|
45
|
+
return {
|
|
46
|
+
protocol: 'unknown',
|
|
47
|
+
connectionInfo: { protocol: 'unknown' },
|
|
48
|
+
isComplete: true
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Try TLS detection first (more specific)
|
|
53
|
+
if (this.tlsDetector.canHandle(buffer)) {
|
|
54
|
+
const tlsResult = this.tlsDetector.detect(buffer, options);
|
|
55
|
+
if (tlsResult) {
|
|
56
|
+
return tlsResult;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Try HTTP detection
|
|
61
|
+
if (this.httpDetector.canHandle(buffer)) {
|
|
62
|
+
const httpResult = this.httpDetector.detect(buffer, options);
|
|
63
|
+
if (httpResult) {
|
|
64
|
+
return httpResult;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Neither TLS nor HTTP
|
|
69
|
+
return {
|
|
70
|
+
protocol: 'unknown',
|
|
71
|
+
connectionInfo: { protocol: 'unknown' },
|
|
72
|
+
isComplete: true
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Detect protocol with connection tracking for fragmented data
|
|
78
|
+
* @deprecated Use detectWithContext instead
|
|
79
|
+
*/
|
|
80
|
+
static async detectWithConnectionTracking(
|
|
81
|
+
buffer: Buffer,
|
|
82
|
+
connectionId: string,
|
|
83
|
+
options?: IDetectionOptions
|
|
84
|
+
): Promise<IDetectionResult> {
|
|
85
|
+
// Convert connection ID to context
|
|
86
|
+
const context: IConnectionContext = {
|
|
87
|
+
id: connectionId,
|
|
88
|
+
sourceIp: 'unknown',
|
|
89
|
+
sourcePort: 0,
|
|
90
|
+
destIp: 'unknown',
|
|
91
|
+
destPort: 0,
|
|
92
|
+
timestamp: Date.now()
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return this.getInstance().detectWithContextInstance(buffer, context, options);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Detect protocol with connection context for fragmented data
|
|
100
|
+
*/
|
|
101
|
+
static async detectWithContext(
|
|
102
|
+
buffer: Buffer,
|
|
103
|
+
context: IConnectionContext,
|
|
104
|
+
options?: IDetectionOptions
|
|
105
|
+
): Promise<IDetectionResult> {
|
|
106
|
+
return this.getInstance().detectWithContextInstance(buffer, context, options);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private async detectWithContextInstance(
|
|
110
|
+
buffer: Buffer,
|
|
111
|
+
context: IConnectionContext,
|
|
112
|
+
options?: IDetectionOptions
|
|
113
|
+
): Promise<IDetectionResult> {
|
|
114
|
+
// Quick sanity check
|
|
115
|
+
if (!buffer || buffer.length === 0) {
|
|
116
|
+
return {
|
|
117
|
+
protocol: 'unknown',
|
|
118
|
+
connectionInfo: { protocol: 'unknown' },
|
|
119
|
+
isComplete: true
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// First peek to determine protocol type
|
|
124
|
+
if (this.tlsDetector.canHandle(buffer)) {
|
|
125
|
+
const result = this.tlsDetector.detectWithContext(buffer, context, options);
|
|
126
|
+
if (result) {
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (this.httpDetector.canHandle(buffer)) {
|
|
132
|
+
const result = this.httpDetector.detectWithContext(buffer, context, options);
|
|
133
|
+
if (result) {
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Can't determine protocol
|
|
139
|
+
return {
|
|
140
|
+
protocol: 'unknown',
|
|
141
|
+
connectionInfo: { protocol: 'unknown' },
|
|
142
|
+
isComplete: false,
|
|
143
|
+
bytesNeeded: Math.max(
|
|
144
|
+
this.tlsDetector.getMinimumBytes(),
|
|
145
|
+
this.httpDetector.getMinimumBytes()
|
|
146
|
+
)
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Clean up resources
|
|
152
|
+
*/
|
|
153
|
+
static cleanup(): void {
|
|
154
|
+
this.getInstance().cleanupInstance();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private cleanupInstance(): void {
|
|
158
|
+
this.fragmentManager.cleanup();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Destroy detector instance
|
|
163
|
+
*/
|
|
164
|
+
static destroy(): void {
|
|
165
|
+
this.getInstance().destroyInstance();
|
|
166
|
+
this.instance = null as any;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private destroyInstance(): void {
|
|
170
|
+
this.fragmentManager.destroy();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Clean up old connection tracking entries
|
|
175
|
+
*
|
|
176
|
+
* @param maxAge Maximum age in milliseconds (default: 30 seconds)
|
|
177
|
+
*/
|
|
178
|
+
static cleanupConnections(maxAge: number = 30000): void {
|
|
179
|
+
// Cleanup is now handled internally by the fragment manager
|
|
180
|
+
this.getInstance().fragmentManager.cleanup();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Extract domain from connection info
|
|
185
|
+
*/
|
|
186
|
+
static extractDomain(connectionInfo: any): string | undefined {
|
|
187
|
+
return connectionInfo.domain || connectionInfo.sni || connectionInfo.host;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Create a connection ID from connection parameters
|
|
192
|
+
* @deprecated Use createConnectionContext instead
|
|
193
|
+
*/
|
|
194
|
+
static createConnectionId(params: {
|
|
195
|
+
sourceIp?: string;
|
|
196
|
+
sourcePort?: number;
|
|
197
|
+
destIp?: string;
|
|
198
|
+
destPort?: number;
|
|
199
|
+
socketId?: string;
|
|
200
|
+
}): string {
|
|
201
|
+
// If socketId is provided, use it
|
|
202
|
+
if (params.socketId) {
|
|
203
|
+
return params.socketId;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Otherwise create from connection tuple
|
|
207
|
+
const { sourceIp = 'unknown', sourcePort = 0, destIp = 'unknown', destPort = 0 } = params;
|
|
208
|
+
return `${sourceIp}:${sourcePort}-${destIp}:${destPort}`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Create a connection context from parameters
|
|
213
|
+
*/
|
|
214
|
+
static createConnectionContext(params: {
|
|
215
|
+
sourceIp?: string;
|
|
216
|
+
sourcePort?: number;
|
|
217
|
+
destIp?: string;
|
|
218
|
+
destPort?: number;
|
|
219
|
+
socketId?: string;
|
|
220
|
+
}): IConnectionContext {
|
|
221
|
+
return {
|
|
222
|
+
id: params.socketId,
|
|
223
|
+
sourceIp: params.sourceIp || 'unknown',
|
|
224
|
+
sourcePort: params.sourcePort || 0,
|
|
225
|
+
destIp: params.destIp || 'unknown',
|
|
226
|
+
destPort: params.destPort || 0,
|
|
227
|
+
timestamp: Date.now()
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|