@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
package/readme.plan.md
CHANGED
|
Binary file
|
|
@@ -1,161 +1,44 @@
|
|
|
1
1
|
import * as plugins from '../../plugins.js';
|
|
2
2
|
import { logger } from './logger.js';
|
|
3
|
+
import { ProxyProtocolParser as ProtocolParser, type IProxyInfo, type IProxyParseResult } from '../../protocols/proxy/index.js';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*/
|
|
7
|
-
export interface IProxyInfo {
|
|
8
|
-
protocol: 'TCP4' | 'TCP6' | 'UNKNOWN';
|
|
9
|
-
sourceIP: string;
|
|
10
|
-
sourcePort: number;
|
|
11
|
-
destinationIP: string;
|
|
12
|
-
destinationPort: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Interface for parse result including remaining data
|
|
17
|
-
*/
|
|
18
|
-
export interface IProxyParseResult {
|
|
19
|
-
proxyInfo: IProxyInfo | null;
|
|
20
|
-
remainingData: Buffer;
|
|
21
|
-
}
|
|
5
|
+
// Re-export types from protocols for backward compatibility
|
|
6
|
+
export type { IProxyInfo, IProxyParseResult } from '../../protocols/proxy/index.js';
|
|
22
7
|
|
|
23
8
|
/**
|
|
24
9
|
* Parser for PROXY protocol v1 (text format)
|
|
25
10
|
* Spec: https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
|
|
11
|
+
*
|
|
12
|
+
* This class now delegates to the protocol parser but adds
|
|
13
|
+
* smartproxy-specific features like socket reading and logging
|
|
26
14
|
*/
|
|
27
15
|
export class ProxyProtocolParser {
|
|
28
|
-
static readonly PROXY_V1_SIGNATURE =
|
|
29
|
-
static readonly MAX_HEADER_LENGTH =
|
|
30
|
-
static readonly HEADER_TERMINATOR =
|
|
16
|
+
static readonly PROXY_V1_SIGNATURE = ProtocolParser.PROXY_V1_SIGNATURE;
|
|
17
|
+
static readonly MAX_HEADER_LENGTH = ProtocolParser.MAX_HEADER_LENGTH;
|
|
18
|
+
static readonly HEADER_TERMINATOR = ProtocolParser.HEADER_TERMINATOR;
|
|
31
19
|
|
|
32
20
|
/**
|
|
33
21
|
* Parse PROXY protocol v1 header from buffer
|
|
34
22
|
* Returns proxy info and remaining data after header
|
|
35
23
|
*/
|
|
36
24
|
static parse(data: Buffer): IProxyParseResult {
|
|
37
|
-
//
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
proxyInfo: null,
|
|
41
|
-
remainingData: data
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Find header terminator
|
|
46
|
-
const headerEndIndex = data.indexOf(this.HEADER_TERMINATOR);
|
|
47
|
-
if (headerEndIndex === -1) {
|
|
48
|
-
// Header incomplete, need more data
|
|
49
|
-
if (data.length > this.MAX_HEADER_LENGTH) {
|
|
50
|
-
// Header too long, invalid
|
|
51
|
-
throw new Error('PROXY protocol header exceeds maximum length');
|
|
52
|
-
}
|
|
53
|
-
return {
|
|
54
|
-
proxyInfo: null,
|
|
55
|
-
remainingData: data
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Extract header line
|
|
60
|
-
const headerLine = data.toString('ascii', 0, headerEndIndex);
|
|
61
|
-
const remainingData = data.slice(headerEndIndex + 2); // Skip \r\n
|
|
62
|
-
|
|
63
|
-
// Parse header
|
|
64
|
-
const parts = headerLine.split(' ');
|
|
65
|
-
|
|
66
|
-
if (parts.length < 2) {
|
|
67
|
-
throw new Error(`Invalid PROXY protocol header format: ${headerLine}`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const [signature, protocol] = parts;
|
|
71
|
-
|
|
72
|
-
// Validate protocol
|
|
73
|
-
if (!['TCP4', 'TCP6', 'UNKNOWN'].includes(protocol)) {
|
|
74
|
-
throw new Error(`Invalid PROXY protocol: ${protocol}`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// For UNKNOWN protocol, ignore addresses
|
|
78
|
-
if (protocol === 'UNKNOWN') {
|
|
79
|
-
return {
|
|
80
|
-
proxyInfo: {
|
|
81
|
-
protocol: 'UNKNOWN',
|
|
82
|
-
sourceIP: '',
|
|
83
|
-
sourcePort: 0,
|
|
84
|
-
destinationIP: '',
|
|
85
|
-
destinationPort: 0
|
|
86
|
-
},
|
|
87
|
-
remainingData
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// For TCP4/TCP6, we need all 6 parts
|
|
92
|
-
if (parts.length !== 6) {
|
|
93
|
-
throw new Error(`Invalid PROXY protocol header format: ${headerLine}`);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const [, , srcIP, dstIP, srcPort, dstPort] = parts;
|
|
97
|
-
|
|
98
|
-
// Validate and parse ports
|
|
99
|
-
const sourcePort = parseInt(srcPort, 10);
|
|
100
|
-
const destinationPort = parseInt(dstPort, 10);
|
|
101
|
-
|
|
102
|
-
if (isNaN(sourcePort) || sourcePort < 0 || sourcePort > 65535) {
|
|
103
|
-
throw new Error(`Invalid source port: ${srcPort}`);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (isNaN(destinationPort) || destinationPort < 0 || destinationPort > 65535) {
|
|
107
|
-
throw new Error(`Invalid destination port: ${dstPort}`);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Validate IP addresses
|
|
111
|
-
const protocolType = protocol as 'TCP4' | 'TCP6' | 'UNKNOWN';
|
|
112
|
-
if (!this.isValidIP(srcIP, protocolType)) {
|
|
113
|
-
throw new Error(`Invalid source IP for ${protocol}: ${srcIP}`);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (!this.isValidIP(dstIP, protocolType)) {
|
|
117
|
-
throw new Error(`Invalid destination IP for ${protocol}: ${dstIP}`);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return {
|
|
121
|
-
proxyInfo: {
|
|
122
|
-
protocol: protocol as 'TCP4' | 'TCP6',
|
|
123
|
-
sourceIP: srcIP,
|
|
124
|
-
sourcePort,
|
|
125
|
-
destinationIP: dstIP,
|
|
126
|
-
destinationPort
|
|
127
|
-
},
|
|
128
|
-
remainingData
|
|
129
|
-
};
|
|
25
|
+
// Delegate to protocol parser
|
|
26
|
+
return ProtocolParser.parse(data);
|
|
130
27
|
}
|
|
131
28
|
|
|
132
29
|
/**
|
|
133
30
|
* Generate PROXY protocol v1 header
|
|
134
31
|
*/
|
|
135
32
|
static generate(info: IProxyInfo): Buffer {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const header = `PROXY ${info.protocol} ${info.sourceIP} ${info.destinationIP} ${info.sourcePort} ${info.destinationPort}\r\n`;
|
|
141
|
-
|
|
142
|
-
if (header.length > this.MAX_HEADER_LENGTH) {
|
|
143
|
-
throw new Error('Generated PROXY protocol header exceeds maximum length');
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return Buffer.from(header, 'ascii');
|
|
33
|
+
// Delegate to protocol parser
|
|
34
|
+
return ProtocolParser.generate(info);
|
|
147
35
|
}
|
|
148
36
|
|
|
149
37
|
/**
|
|
150
38
|
* Validate IP address format
|
|
151
39
|
*/
|
|
152
40
|
private static isValidIP(ip: string, protocol: 'TCP4' | 'TCP6' | 'UNKNOWN'): boolean {
|
|
153
|
-
|
|
154
|
-
return plugins.net.isIPv4(ip);
|
|
155
|
-
} else if (protocol === 'TCP6') {
|
|
156
|
-
return plugins.net.isIPv6(ip);
|
|
157
|
-
}
|
|
158
|
-
return false;
|
|
41
|
+
return ProtocolParser.isValidIP(ip, protocol);
|
|
159
42
|
}
|
|
160
43
|
|
|
161
44
|
/**
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WebSocket utility functions
|
|
3
|
+
*
|
|
4
|
+
* This module provides smartproxy-specific WebSocket utilities
|
|
5
|
+
* and re-exports protocol utilities from the protocols module
|
|
3
6
|
*/
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*/
|
|
9
|
-
export type RawData = Buffer | ArrayBuffer | Buffer[] | any;
|
|
8
|
+
// Import and re-export from protocols
|
|
9
|
+
import { getMessageSize as protocolGetMessageSize, toBuffer as protocolToBuffer } from '../../protocols/websocket/index.js';
|
|
10
|
+
export type { RawData } from '../../protocols/websocket/index.js';
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Get the length of a WebSocket message regardless of its type
|
|
@@ -15,35 +16,9 @@ export type RawData = Buffer | ArrayBuffer | Buffer[] | any;
|
|
|
15
16
|
* @param data - The data message from WebSocket (could be any RawData type)
|
|
16
17
|
* @returns The length of the data in bytes
|
|
17
18
|
*/
|
|
18
|
-
export function getMessageSize(data: RawData): number {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return Buffer.from(data, 'utf8').length;
|
|
22
|
-
} else if (data instanceof Buffer) {
|
|
23
|
-
// For Node.js Buffer
|
|
24
|
-
return data.length;
|
|
25
|
-
} else if (data instanceof ArrayBuffer) {
|
|
26
|
-
// For ArrayBuffer
|
|
27
|
-
return data.byteLength;
|
|
28
|
-
} else if (Array.isArray(data)) {
|
|
29
|
-
// For array of buffers, sum their lengths
|
|
30
|
-
return data.reduce((sum, chunk) => {
|
|
31
|
-
if (chunk instanceof Buffer) {
|
|
32
|
-
return sum + chunk.length;
|
|
33
|
-
} else if (chunk instanceof ArrayBuffer) {
|
|
34
|
-
return sum + chunk.byteLength;
|
|
35
|
-
}
|
|
36
|
-
return sum;
|
|
37
|
-
}, 0);
|
|
38
|
-
} else {
|
|
39
|
-
// For other types, try to determine the size or return 0
|
|
40
|
-
try {
|
|
41
|
-
return Buffer.from(data).length;
|
|
42
|
-
} catch (e) {
|
|
43
|
-
console.warn('Could not determine message size', e);
|
|
44
|
-
return 0;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
19
|
+
export function getMessageSize(data: import('../../protocols/websocket/index.js').RawData): number {
|
|
20
|
+
// Delegate to protocol implementation
|
|
21
|
+
return protocolGetMessageSize(data);
|
|
47
22
|
}
|
|
48
23
|
|
|
49
24
|
/**
|
|
@@ -52,30 +27,7 @@ export function getMessageSize(data: RawData): number {
|
|
|
52
27
|
* @param data - The data message from WebSocket (could be any RawData type)
|
|
53
28
|
* @returns A Buffer containing the data
|
|
54
29
|
*/
|
|
55
|
-
export function toBuffer(data: RawData): Buffer {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
} else if (data instanceof Buffer) {
|
|
59
|
-
return data;
|
|
60
|
-
} else if (data instanceof ArrayBuffer) {
|
|
61
|
-
return Buffer.from(data);
|
|
62
|
-
} else if (Array.isArray(data)) {
|
|
63
|
-
// For array of buffers, concatenate them
|
|
64
|
-
return Buffer.concat(data.map(chunk => {
|
|
65
|
-
if (chunk instanceof Buffer) {
|
|
66
|
-
return chunk;
|
|
67
|
-
} else if (chunk instanceof ArrayBuffer) {
|
|
68
|
-
return Buffer.from(chunk);
|
|
69
|
-
}
|
|
70
|
-
return Buffer.from(chunk);
|
|
71
|
-
}));
|
|
72
|
-
} else {
|
|
73
|
-
// For other types, try to convert to Buffer or return empty Buffer
|
|
74
|
-
try {
|
|
75
|
-
return Buffer.from(data);
|
|
76
|
-
} catch (e) {
|
|
77
|
-
console.warn('Could not convert message to Buffer', e);
|
|
78
|
-
return Buffer.alloc(0);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
30
|
+
export function toBuffer(data: import('../../protocols/websocket/index.js').RawData): Buffer {
|
|
31
|
+
// Delegate to protocol implementation
|
|
32
|
+
return protocolToBuffer(data);
|
|
81
33
|
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Protocol Detector
|
|
3
|
+
*
|
|
4
|
+
* Simplified HTTP 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 type { THttpMethod } from '../../protocols/http/index.js';
|
|
11
|
+
import { QuickProtocolDetector } from './quick-detector.js';
|
|
12
|
+
import { RoutingExtractor } from './routing-extractor.js';
|
|
13
|
+
import { DetectionFragmentManager } from '../utils/fragment-manager.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Simplified HTTP detector
|
|
17
|
+
*/
|
|
18
|
+
export class HttpDetector implements IProtocolDetector {
|
|
19
|
+
private quickDetector = new QuickProtocolDetector();
|
|
20
|
+
private fragmentManager: DetectionFragmentManager;
|
|
21
|
+
|
|
22
|
+
constructor(fragmentManager?: DetectionFragmentManager) {
|
|
23
|
+
this.fragmentManager = fragmentManager || new DetectionFragmentManager();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Check if buffer can be handled by this detector
|
|
28
|
+
*/
|
|
29
|
+
canHandle(buffer: Buffer): boolean {
|
|
30
|
+
const result = this.quickDetector.quickDetect(buffer);
|
|
31
|
+
return result.protocol === 'http' && result.confidence > 50;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Get minimum bytes needed for detection
|
|
36
|
+
*/
|
|
37
|
+
getMinimumBytes(): number {
|
|
38
|
+
return 4; // "GET " minimum
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Detect HTTP protocol from buffer
|
|
43
|
+
*/
|
|
44
|
+
detect(buffer: Buffer, options?: IDetectionOptions): IDetectionResult | null {
|
|
45
|
+
// Quick detection first
|
|
46
|
+
const quickResult = this.quickDetector.quickDetect(buffer);
|
|
47
|
+
|
|
48
|
+
if (quickResult.protocol !== 'http' || quickResult.confidence < 50) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Extract routing information
|
|
53
|
+
const routing = RoutingExtractor.extract(buffer, 'http');
|
|
54
|
+
|
|
55
|
+
// If we don't need full headers, we can return early
|
|
56
|
+
if (quickResult.confidence >= 95 && !options?.extractFullHeaders) {
|
|
57
|
+
return {
|
|
58
|
+
protocol: 'http',
|
|
59
|
+
connectionInfo: {
|
|
60
|
+
protocol: 'http',
|
|
61
|
+
method: quickResult.metadata?.method as THttpMethod,
|
|
62
|
+
domain: routing?.domain,
|
|
63
|
+
path: routing?.path
|
|
64
|
+
},
|
|
65
|
+
isComplete: true
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Check if we have complete headers
|
|
70
|
+
const headersEnd = buffer.indexOf('\r\n\r\n');
|
|
71
|
+
const isComplete = headersEnd !== -1;
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
protocol: 'http',
|
|
75
|
+
connectionInfo: {
|
|
76
|
+
protocol: 'http',
|
|
77
|
+
domain: routing?.domain,
|
|
78
|
+
path: routing?.path,
|
|
79
|
+
method: quickResult.metadata?.method as THttpMethod
|
|
80
|
+
},
|
|
81
|
+
isComplete,
|
|
82
|
+
bytesNeeded: isComplete ? undefined : buffer.length + 512 // Need more for headers
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Handle fragmented detection
|
|
88
|
+
*/
|
|
89
|
+
detectWithContext(
|
|
90
|
+
buffer: Buffer,
|
|
91
|
+
context: IConnectionContext,
|
|
92
|
+
options?: IDetectionOptions
|
|
93
|
+
): IDetectionResult | null {
|
|
94
|
+
const handler = this.fragmentManager.getHandler('http');
|
|
95
|
+
const connectionId = DetectionFragmentManager.createConnectionId(context);
|
|
96
|
+
|
|
97
|
+
// Add fragment
|
|
98
|
+
const result = handler.addFragment(connectionId, buffer);
|
|
99
|
+
|
|
100
|
+
if (result.error) {
|
|
101
|
+
handler.complete(connectionId);
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Try detection on accumulated buffer
|
|
106
|
+
const detectResult = this.detect(result.buffer!, options);
|
|
107
|
+
|
|
108
|
+
if (detectResult && detectResult.isComplete) {
|
|
109
|
+
handler.complete(connectionId);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return detectResult;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quick Protocol Detector
|
|
3
|
+
*
|
|
4
|
+
* Lightweight protocol identification based on minimal bytes
|
|
5
|
+
* No parsing, just identification
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { IProtocolDetector, IProtocolDetectionResult } from '../../protocols/common/types.js';
|
|
9
|
+
import { TlsRecordType } from '../../protocols/tls/index.js';
|
|
10
|
+
import { HttpParser } from '../../protocols/http/index.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Quick protocol detector for fast identification
|
|
14
|
+
*/
|
|
15
|
+
export class QuickProtocolDetector implements IProtocolDetector {
|
|
16
|
+
/**
|
|
17
|
+
* Check if this detector can handle the data
|
|
18
|
+
*/
|
|
19
|
+
canHandle(data: Buffer): boolean {
|
|
20
|
+
return data.length >= 1;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Perform quick detection based on first few bytes
|
|
25
|
+
*/
|
|
26
|
+
quickDetect(data: Buffer): IProtocolDetectionResult {
|
|
27
|
+
if (data.length === 0) {
|
|
28
|
+
return {
|
|
29
|
+
protocol: 'unknown',
|
|
30
|
+
confidence: 0,
|
|
31
|
+
requiresMoreData: true
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Check for TLS
|
|
36
|
+
const tlsResult = this.checkTls(data);
|
|
37
|
+
if (tlsResult.confidence > 80) {
|
|
38
|
+
return tlsResult;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Check for HTTP
|
|
42
|
+
const httpResult = this.checkHttp(data);
|
|
43
|
+
if (httpResult.confidence > 80) {
|
|
44
|
+
return httpResult;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Need more data or unknown
|
|
48
|
+
return {
|
|
49
|
+
protocol: 'unknown',
|
|
50
|
+
confidence: 0,
|
|
51
|
+
requiresMoreData: data.length < 20
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Check if data looks like TLS
|
|
57
|
+
*/
|
|
58
|
+
private checkTls(data: Buffer): IProtocolDetectionResult {
|
|
59
|
+
if (data.length < 3) {
|
|
60
|
+
return {
|
|
61
|
+
protocol: 'tls',
|
|
62
|
+
confidence: 0,
|
|
63
|
+
requiresMoreData: true
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const firstByte = data[0];
|
|
68
|
+
const secondByte = data[1];
|
|
69
|
+
|
|
70
|
+
// Check for valid TLS record type
|
|
71
|
+
const validRecordTypes = [
|
|
72
|
+
TlsRecordType.CHANGE_CIPHER_SPEC,
|
|
73
|
+
TlsRecordType.ALERT,
|
|
74
|
+
TlsRecordType.HANDSHAKE,
|
|
75
|
+
TlsRecordType.APPLICATION_DATA,
|
|
76
|
+
TlsRecordType.HEARTBEAT
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
if (!validRecordTypes.includes(firstByte)) {
|
|
80
|
+
return {
|
|
81
|
+
protocol: 'tls',
|
|
82
|
+
confidence: 0
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Check TLS version byte (0x03 for all TLS/SSL versions)
|
|
87
|
+
if (secondByte !== 0x03) {
|
|
88
|
+
return {
|
|
89
|
+
protocol: 'tls',
|
|
90
|
+
confidence: 0
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// High confidence it's TLS
|
|
95
|
+
return {
|
|
96
|
+
protocol: 'tls',
|
|
97
|
+
confidence: 95,
|
|
98
|
+
metadata: {
|
|
99
|
+
recordType: firstByte
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Check if data looks like HTTP
|
|
106
|
+
*/
|
|
107
|
+
private checkHttp(data: Buffer): IProtocolDetectionResult {
|
|
108
|
+
if (data.length < 3) {
|
|
109
|
+
return {
|
|
110
|
+
protocol: 'http',
|
|
111
|
+
confidence: 0,
|
|
112
|
+
requiresMoreData: true
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Quick check for HTTP methods
|
|
117
|
+
const start = data.subarray(0, Math.min(10, data.length)).toString('ascii');
|
|
118
|
+
|
|
119
|
+
// Check common HTTP methods
|
|
120
|
+
const httpMethods = ['GET ', 'POST ', 'PUT ', 'DELETE ', 'HEAD ', 'OPTIONS', 'PATCH ', 'CONNECT', 'TRACE '];
|
|
121
|
+
for (const method of httpMethods) {
|
|
122
|
+
if (start.startsWith(method)) {
|
|
123
|
+
return {
|
|
124
|
+
protocol: 'http',
|
|
125
|
+
confidence: 95,
|
|
126
|
+
metadata: {
|
|
127
|
+
method: method.trim()
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Check if it might be HTTP but need more data
|
|
134
|
+
if (HttpParser.isPrintableAscii(data, Math.min(20, data.length))) {
|
|
135
|
+
// Could be HTTP, but not sure
|
|
136
|
+
return {
|
|
137
|
+
protocol: 'http',
|
|
138
|
+
confidence: 30,
|
|
139
|
+
requiresMoreData: data.length < 20
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
protocol: 'http',
|
|
145
|
+
confidence: 0
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Routing Information Extractor
|
|
3
|
+
*
|
|
4
|
+
* Extracts minimal routing information from protocols
|
|
5
|
+
* without full parsing
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { IRoutingInfo, IConnectionContext, TProtocolType } from '../../protocols/common/types.js';
|
|
9
|
+
import { SniExtraction } from '../../protocols/tls/sni/sni-extraction.js';
|
|
10
|
+
import { HttpParser } from '../../protocols/http/index.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Extracts routing information from protocol data
|
|
14
|
+
*/
|
|
15
|
+
export class RoutingExtractor {
|
|
16
|
+
/**
|
|
17
|
+
* Extract routing info based on protocol type
|
|
18
|
+
*/
|
|
19
|
+
static extract(
|
|
20
|
+
data: Buffer,
|
|
21
|
+
protocol: TProtocolType,
|
|
22
|
+
context?: IConnectionContext
|
|
23
|
+
): IRoutingInfo | null {
|
|
24
|
+
switch (protocol) {
|
|
25
|
+
case 'tls':
|
|
26
|
+
case 'https':
|
|
27
|
+
return this.extractTlsRouting(data, context);
|
|
28
|
+
|
|
29
|
+
case 'http':
|
|
30
|
+
return this.extractHttpRouting(data);
|
|
31
|
+
|
|
32
|
+
default:
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Extract routing from TLS ClientHello (SNI)
|
|
39
|
+
*/
|
|
40
|
+
private static extractTlsRouting(
|
|
41
|
+
data: Buffer,
|
|
42
|
+
context?: IConnectionContext
|
|
43
|
+
): IRoutingInfo | null {
|
|
44
|
+
try {
|
|
45
|
+
// Quick SNI extraction without full parsing
|
|
46
|
+
const sni = SniExtraction.extractSNI(data);
|
|
47
|
+
|
|
48
|
+
if (sni) {
|
|
49
|
+
return {
|
|
50
|
+
domain: sni,
|
|
51
|
+
protocol: 'tls',
|
|
52
|
+
port: 443 // Default HTTPS port
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return null;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
// Extraction failed, return null
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Extract routing from HTTP headers (Host header)
|
|
65
|
+
*/
|
|
66
|
+
private static extractHttpRouting(data: Buffer): IRoutingInfo | null {
|
|
67
|
+
try {
|
|
68
|
+
// Look for first line
|
|
69
|
+
const firstLineEnd = data.indexOf('\n');
|
|
70
|
+
if (firstLineEnd === -1) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Parse request line
|
|
75
|
+
const firstLine = data.subarray(0, firstLineEnd).toString('ascii').trim();
|
|
76
|
+
const requestLine = HttpParser.parseRequestLine(firstLine);
|
|
77
|
+
|
|
78
|
+
if (!requestLine) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Look for Host header
|
|
83
|
+
let pos = firstLineEnd + 1;
|
|
84
|
+
const maxSearch = Math.min(data.length, 4096); // Don't search too far
|
|
85
|
+
|
|
86
|
+
while (pos < maxSearch) {
|
|
87
|
+
const lineEnd = data.indexOf('\n', pos);
|
|
88
|
+
if (lineEnd === -1) break;
|
|
89
|
+
|
|
90
|
+
const line = data.subarray(pos, lineEnd).toString('ascii').trim();
|
|
91
|
+
|
|
92
|
+
// Empty line means end of headers
|
|
93
|
+
if (line.length === 0) break;
|
|
94
|
+
|
|
95
|
+
// Check for Host header
|
|
96
|
+
if (line.toLowerCase().startsWith('host:')) {
|
|
97
|
+
const hostValue = line.substring(5).trim();
|
|
98
|
+
const domain = HttpParser.extractDomainFromHost(hostValue);
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
domain,
|
|
102
|
+
path: requestLine.path,
|
|
103
|
+
protocol: 'http',
|
|
104
|
+
port: 80 // Default HTTP port
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
pos = lineEnd + 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// No Host header found, but we have the path
|
|
112
|
+
return {
|
|
113
|
+
path: requestLine.path,
|
|
114
|
+
protocol: 'http',
|
|
115
|
+
port: 80
|
|
116
|
+
};
|
|
117
|
+
} catch (error) {
|
|
118
|
+
// Extraction failed
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Try to extract domain from any protocol
|
|
125
|
+
*/
|
|
126
|
+
static extractDomain(data: Buffer, hint?: TProtocolType): string | null {
|
|
127
|
+
// If we have a hint, use it
|
|
128
|
+
if (hint) {
|
|
129
|
+
const routing = this.extract(data, hint);
|
|
130
|
+
return routing?.domain || null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Try TLS first (more specific)
|
|
134
|
+
const tlsRouting = this.extractTlsRouting(data);
|
|
135
|
+
if (tlsRouting?.domain) {
|
|
136
|
+
return tlsRouting.domain;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Try HTTP
|
|
140
|
+
const httpRouting = this.extractHttpRouting(data);
|
|
141
|
+
if (httpRouting?.domain) {
|
|
142
|
+
return httpRouting.domain;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|