Skip to content

Commit 6224214

Browse files
committed
pre-compute URLPatterns
1 parent 95f5cae commit 6224214

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/server/http-combo-server.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ function analyzeAndMaybePassThroughTls(
381381
if (passthroughList && interceptOnlyList){
382382
throw new Error('Cannot use both tlsPassthrough and tlsInterceptOnly options at the same time.');
383383
}
384-
const passThroughHostnames = passthroughList?.map(({ hostname }) => hostname) ?? [];
385-
const interceptOnlyHostnames = interceptOnlyList?.map(({ hostname }) => hostname);
384+
const passThroughPatterns = passthroughList?.map(({ hostname }) => new URLPattern(`https://${hostname}`)) ?? [];
385+
const interceptOnlyPatterns = interceptOnlyList?.map(({ hostname }) => new URLPattern(`https://${hostname}`));
386386

387387
const tlsConnectionListener = server.listeners('connection')[0] as (socket: net.Socket) => {};
388388
server.removeListener('connection', tlsConnectionListener);
@@ -401,11 +401,11 @@ function analyzeAndMaybePassThroughTls(
401401
ja3Fingerprint: calculateJa3FromFingerprintData(helloData.fingerprintData)
402402
};
403403

404-
if (shouldPassThrough(connectHostname, passThroughHostnames, interceptOnlyHostnames)) {
404+
if (shouldPassThrough(connectHostname, passThroughPatterns, interceptOnlyPatterns)) {
405405
const upstreamPort = connectPort ? parseInt(connectPort, 10) : undefined;
406406
passthroughListener(socket, connectHostname, upstreamPort);
407407
return; // Do not continue with TLS
408-
} else if (shouldPassThrough(sniHostname, passThroughHostnames, interceptOnlyHostnames)) {
408+
} else if (shouldPassThrough(sniHostname, passThroughPatterns, interceptOnlyPatterns)) {
409409
passthroughListener(socket, sniHostname!); // Can't guess the port - not included in SNI
410410
return; // Do not continue with TLS
411411
}
@@ -425,18 +425,18 @@ function analyzeAndMaybePassThroughTls(
425425
export function shouldPassThrough(
426426
hostname: string | undefined,
427427
// Only one of these two should have values (validated above):
428-
passThroughHostnames: string[],
429-
interceptOnlyHostnames: string[] | undefined
428+
passThroughPatterns: URLPattern[],
429+
interceptOnlyPatterns: URLPattern[] | undefined
430430
): boolean {
431431
if (!hostname) return false;
432432

433-
if (interceptOnlyHostnames) {
434-
return !interceptOnlyHostnames.some((hn) =>
435-
new URLPattern(`https://${hn}`).test(`https://${hostname}`)
433+
if (interceptOnlyPatterns) {
434+
return !interceptOnlyPatterns.some((pattern) =>
435+
pattern.test(`https://${hostname}`)
436436
);
437437
}
438438

439-
return passThroughHostnames.some((hn) =>
440-
new URLPattern(`https://${hn}`).test(`https://${hostname}`)
439+
return passThroughPatterns.some((pattern) =>
440+
pattern.test(`https://${hostname}`)
441441
);
442442
}

test/http-combo-server.spec.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { URLPattern } from "urlpattern-polyfill";
12
import { shouldPassThrough } from "../src/server/http-combo-server";
23
import { expect } from "./test-utils";
34

@@ -21,7 +22,7 @@ describe("shouldPassThrough", () => {
2122
it("should return true when hostname is in passThroughHostnames", () => {
2223
const should = shouldPassThrough(
2324
"example.org",
24-
["example.org"],
25+
[new URLPattern("https://example.org")],
2526
undefined
2627
);
2728
expect(should).to.be.true;
@@ -30,30 +31,46 @@ describe("shouldPassThrough", () => {
3031
it("should return false when hostname is not in passThroughHostnames", () => {
3132
const should = shouldPassThrough(
3233
"example.org",
33-
["example.com"],
34+
[new URLPattern("https://example.com")],
3435
undefined
3536
);
3637
expect(should).to.be.false;
3738
});
3839

3940
it("should return true when hostname match a wildcard", () => {
40-
const should = shouldPassThrough("example.org", ["*.org"], undefined);
41+
const should = shouldPassThrough(
42+
"example.org",
43+
[new URLPattern("https://*.org")],
44+
undefined
45+
);
4146
expect(should).to.be.true;
4247
});
4348
});
4449
describe("interceptOnlyHostnames", () => {
4550
it("should return false when hostname is in interceptOnlyHostnames", () => {
46-
const should = shouldPassThrough("example.org", [], ["example.org"]);
51+
const should = shouldPassThrough(
52+
"example.org",
53+
[],
54+
[new URLPattern("https://example.org")]
55+
);
4756
expect(should).to.be.false;
4857
});
4958

5059
it("should return true when hostname is not in interceptOnlyHostnames", () => {
51-
const should = shouldPassThrough("example.org", [], ["example.com"]);
60+
const should = shouldPassThrough(
61+
"example.org",
62+
[],
63+
[new URLPattern("https://example.com")]
64+
);
5265
expect(should).to.be.true;
5366
});
5467

5568
it("should return false when hostname match a wildcard", () => {
56-
const should = shouldPassThrough("example.org", [], ["*.org"]);
69+
const should = shouldPassThrough(
70+
"example.org",
71+
[],
72+
[new URLPattern("https://*.org")]
73+
);
5774
expect(should).to.be.false;
5875
});
5976
});

0 commit comments

Comments
 (0)