Skip to content

Commit 9b9bd61

Browse files
committed
Inject MockRTC WebExtension into Chromiums (but behind a flag for now)
1 parent c0eb71a commit 9b9bd61

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/interceptors/chromium-based-interceptors.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { listRunningProcesses, windowsClose, waitForExit } from '../util/process
1616
import { HideWarningServer } from '../hide-warning-server';
1717
import { Interceptor } from '.';
1818
import { reportError } from '../error-tracking';
19+
import { WEBEXTENSION_PATH } from '../webextension';
1920

2021
const getBrowserDetails = async (config: HtkConfig, variant: string): Promise<Browser | undefined> => {
2122
const browsers = await getAvailableBrowsers(config.configPath);
@@ -28,7 +29,8 @@ const getChromiumLaunchOptions = async (
2829
browser: string,
2930
config: HtkConfig,
3031
proxyPort: number,
31-
hideWarningServer: HideWarningServer
32+
hideWarningServer: HideWarningServer,
33+
webExtensionEnabled: boolean
3234
): Promise<LaunchOptions & { options: Required<LaunchOptions>['options'] }> => {
3335
const certificatePem = await readFile(config.https.certPath, 'utf8');
3436
const spkiFingerprint = generateSPKIFingerprint(certificatePem);
@@ -42,7 +44,11 @@ const getChromiumLaunchOptions = async (
4244
'<-loopback>',
4345
// Don't intercept our warning hiding requests. Note that this must be
4446
// the 2nd rule here, or <-loopback> would override it.
45-
hideWarningServer.host
47+
hideWarningServer.host,
48+
// We use httptoolkit-internal.localhost (always resolves to localhost,
49+
// enforced by Chrome) to skip the proxy for internal requests from the
50+
// our webextension.
51+
'internal.httptoolkit.localhost'
4652
],
4753
options: [
4854
// Trust our CA certificate's fingerprint:
@@ -52,7 +58,16 @@ const getChromiumLaunchOptions = async (
5258
// Avoid annoying extra network noise:
5359
'--disable-background-networking',
5460
'--disable-component-update',
55-
'--check-for-update-interval=31536000' // Don't update for a year
61+
'--check-for-update-interval=31536000', // Don't update for a year
62+
...(webExtensionEnabled
63+
// Install HTTP Toolkit's extension, for advanced hook setup. Feature
64+
// flagged for now as it's still new & largely untested.
65+
? [
66+
67+
`--load-extension=${WEBEXTENSION_PATH}`
68+
]
69+
: []
70+
)
5671
]
5772
};
5873
}
@@ -80,7 +95,7 @@ abstract class FreshChromiumBasedInterceptor implements Interceptor {
8095
return !!browserDetails;
8196
}
8297

83-
async activate(proxyPort: number) {
98+
async activate(proxyPort: number, options: { webExtensionEnabled?: boolean } = {}) {
8499
if (this.isActive(proxyPort)) return;
85100

86101
const hideWarningServer = new HideWarningServer(this.config);
@@ -93,7 +108,8 @@ abstract class FreshChromiumBasedInterceptor implements Interceptor {
93108
browserDetails ? browserDetails.name : this.variantName,
94109
this.config,
95110
proxyPort,
96-
hideWarningServer
111+
hideWarningServer,
112+
!!options.webExtensionEnabled
97113
)
98114
, this.config.configPath);
99115

@@ -220,7 +236,10 @@ abstract class ExistingChromiumBasedInterceptor implements Interceptor {
220236
return defaultRootProcess && defaultRootProcess.pid;
221237
}
222238

223-
async activate(proxyPort: number, options: { closeConfirmed: boolean } = { closeConfirmed: false }) {
239+
async activate(proxyPort: number, options: {
240+
closeConfirmed: boolean,
241+
webExtensionEnabled?: boolean
242+
} = { closeConfirmed: false }) {
224243
if (!this.isActivable()) return;
225244

226245
const hideWarningServer = new HideWarningServer(this.config);
@@ -257,7 +276,8 @@ abstract class ExistingChromiumBasedInterceptor implements Interceptor {
257276
browserDetails ? browserDetails.name : this.variantName,
258277
this.config,
259278
proxyPort,
260-
hideWarningServer
279+
hideWarningServer,
280+
!!options.webExtensionEnabled
261281
);
262282

263283
// Remove almost all default arguments. Each of these changes behaviour in maybe unexpected

0 commit comments

Comments
 (0)