Skip to content

Commit 95f5cae

Browse files
committed
add support for wildcards in shouldPassThrough
1 parent 7366d08 commit 95f5cae

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
"semver": "^7.5.3",
198198
"socks-proxy-agent": "^7.0.0",
199199
"typed-error": "^3.0.2",
200+
"urlpattern-polyfill": "^10.0.0",
200201
"uuid": "^8.3.2",
201202
"ws": "^8.8.0"
202203
}

src/server/http-combo-server.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
NonTlsError,
1515
readTlsClientHello
1616
} from 'read-tls-client-hello';
17+
import { URLPattern } from "urlpattern-polyfill";
1718

1819
import { TlsHandshakeFailure } from '../types';
1920
import { getCA } from '../util/tls';
@@ -426,12 +427,16 @@ export function shouldPassThrough(
426427
// Only one of these two should have values (validated above):
427428
passThroughHostnames: string[],
428429
interceptOnlyHostnames: string[] | undefined
429-
): boolean {
430+
): boolean {
430431
if (!hostname) return false;
431-
432+
432433
if (interceptOnlyHostnames) {
433-
return !interceptOnlyHostnames.includes(hostname);
434+
return !interceptOnlyHostnames.some((hn) =>
435+
new URLPattern(`https://${hn}`).test(`https://${hostname}`)
436+
);
434437
}
435-
436-
return passThroughHostnames.includes(hostname);
437-
}
438+
439+
return passThroughHostnames.some((hn) =>
440+
new URLPattern(`https://${hn}`).test(`https://${hostname}`)
441+
);
442+
}

test/http-combo-server.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ describe("shouldPassThrough", () => {
3535
);
3636
expect(should).to.be.false;
3737
});
38+
39+
it("should return true when hostname match a wildcard", () => {
40+
const should = shouldPassThrough("example.org", ["*.org"], undefined);
41+
expect(should).to.be.true;
42+
});
3843
});
3944
describe("interceptOnlyHostnames", () => {
4045
it("should return false when hostname is in interceptOnlyHostnames", () => {
@@ -46,5 +51,10 @@ describe("shouldPassThrough", () => {
4651
const should = shouldPassThrough("example.org", [], ["example.com"]);
4752
expect(should).to.be.true;
4853
});
54+
55+
it("should return false when hostname match a wildcard", () => {
56+
const should = shouldPassThrough("example.org", [], ["*.org"]);
57+
expect(should).to.be.false;
58+
});
4959
});
5060
});

0 commit comments

Comments
 (0)