Skip to content

Commit 6525df7

Browse files
committed
Update to the latest read-tls-client-hello
1 parent f437cde commit 6525df7

6 files changed

Lines changed: 28 additions & 17 deletions

File tree

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"dns-packet": "^5.6.1",
4141
"lodash": "^4.17.23",
4242
"parse-multipart-data": "^1.5.0",
43-
"read-tls-client-hello": "^1.1.0",
43+
"read-tls-client-hello": "^2.0.0",
4444
"tsx": "^4.19.3",
4545
"ws": "^8.19.0"
4646
},

src/http-handler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as http from 'http';
22
import * as http2 from 'http2';
33
import { MaybePromise, StatusError } from '@httptoolkit/util';
4+
import { getExtensionData } from 'read-tls-client-hello';
45

56
import { httpEndpoints, tlsEndpoints } from './endpoints/endpoint-index.js';
67
import { HttpRequest, HttpResponse } from './endpoints/http-index.js';
@@ -75,7 +76,11 @@ function createHttpRequestHandler(options: {
7576
const authority = req.headers[':authority']?.toString();
7677
if (authority) {
7778
const hostWithoutPort = authority.replace(/:\d+$/, '').toLowerCase();
78-
const sni = socket.stream?.[TLS_CLIENT_HELLO]?.serverName?.toLowerCase();
79+
const tlsClientHello = socket.stream?.[TLS_CLIENT_HELLO];
80+
const sni = (tlsClientHello
81+
? getExtensionData(tlsClientHello, 'sni')?.serverName
82+
: undefined
83+
)?.toLowerCase();
7984

8085
if (sni && sni !== hostWithoutPort) {
8186
res.writeHead(421, { 'content-type': 'text/plain' });

src/server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as net from 'net';
22

33
import {
44
readTlsClientHello,
5-
calculateJa3FromFingerprintData,
6-
calculateJa4FromHelloData,
5+
calculateJa3,
6+
calculateJa4,
77
} from 'read-tls-client-hello';
88

99
import { createHttp1Handler, createHttp2Handler } from './http-handler.js';
@@ -176,8 +176,8 @@ const createTcpHandler = async (options: ServerOptions = {}) => {
176176
const helloData = await readTlsClientHello(conn);
177177
conn[TLS_CLIENT_HELLO] = {
178178
...helloData,
179-
ja3: calculateJa3FromFingerprintData(helloData.fingerprintData),
180-
ja4: calculateJa4FromHelloData(helloData)
179+
ja3: calculateJa3(helloData),
180+
ja4: calculateJa4(helloData)
181181
};
182182
} catch (e) {
183183
// Non-TLS traffic or malformed client hello - continue without fingerprint

src/tls-client-hello.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { TlsHelloData } from 'read-tls-client-hello';
1+
import type { TlsClientHelloMessage } from 'read-tls-client-hello';
22

33
export const TLS_CLIENT_HELLO: unique symbol = Symbol('tlsClientHello');
44

5-
export interface TlsClientHelloData extends TlsHelloData {
5+
export interface TlsClientHelloData extends TlsClientHelloMessage {
66
ja3: string;
77
ja4: string;
88
}

src/tls-handler.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import * as crypto from 'node:crypto';
33
import * as stream from 'stream';
44
import { EventEmitter } from 'events';
55

6+
import { ErrorLike } from '@httptoolkit/util';
7+
import { getExtensionData } from 'read-tls-client-hello';
8+
69
import { ConnectionProcessor } from './process-connection.js';
710
import { LocalCA } from './tls-certificates/local-ca.js';
811
import { CertOptions, calculateCertCacheKey } from './tls-certificates/cert-definitions.js';
912
import { SecureContextCache } from './tls-certificates/secure-context-cache.js';
1013
import { tlsEndpoints } from './endpoints/endpoint-index.js';
11-
import { ErrorLike } from '@httptoolkit/util';
1214
import { PROXY_PROTOCOL } from './proxy-protocol.js';
1315
import { TLS_CLIENT_HELLO } from './tls-client-hello.js';
1416

@@ -137,7 +139,10 @@ class TlsConnectionHandler {
137139

138140
async handleConnection(rawSocket: stream.Duplex) {
139141
try {
140-
const serverName = rawSocket[TLS_CLIENT_HELLO]?.serverName;
142+
const tlsClientHello = rawSocket[TLS_CLIENT_HELLO];
143+
const serverName = tlsClientHello
144+
? getExtensionData(tlsClientHello, 'sni')?.serverName
145+
: undefined;
141146
const domain = serverName || this.tlsConfig.rootDomain;
142147

143148
const serverNameParts = getSNIPrefixParts(domain, this.tlsConfig.rootDomain);
@@ -185,8 +190,9 @@ class TlsConnectionHandler {
185190
: DEFAULT_ALPN_PROTOCOLS;
186191

187192
// Check if client requested OCSP stapling (extension 5 = status_request)
188-
const clientExtensions = rawSocket[TLS_CLIENT_HELLO]?.fingerprintData?.[2];
189-
const clientRequestedOCSP = clientExtensions?.includes(5) ?? false;
193+
const clientRequestedOCSP = tlsClientHello
194+
? !!getExtensionData(tlsClientHello, 'status_request')
195+
: false;
190196

191197
const tlsSocket = new tls.TLSSocket(rawSocket, {
192198
isServer: true,

0 commit comments

Comments
 (0)