-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathchromium-based-interceptors.ts
514 lines (401 loc) · 17 KB
/
chromium-based-interceptors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import _ from 'lodash';
import * as path from 'path';
import { delay } from '@httptoolkit/util';
import { generateSPKIFingerprint } from 'mockttp';
import { HtkConfig } from '../config';
import { readFile, deleteFolder } from '../util/fs';
import { listRunningProcesses, windowsClose, waitForExit } from '../util/process-management';
import { getSnapConfigPath, isSnap } from '../util/snap';
import {
getBrowserDetails,
launchBrowser,
BrowserInstance,
LaunchOptions
} from '../browsers';
import { HideWarningServer } from '../hide-warning-server';
import { Interceptor } from '.';
import { logError } from '../error-tracking';
import { WEBEXTENSION_INSTALL } from '../webextension';
const getChromiumLaunchOptions = async (
browser: string,
config: HtkConfig,
profilePath: string | null | undefined,
proxyPort: number,
hideWarningServer: HideWarningServer,
webExtensionEnabled: boolean
): Promise<LaunchOptions & { options: Required<LaunchOptions>['options'] }> => {
const certificatePem = await readFile(config.https.certPath, 'utf8');
const spkiFingerprint = generateSPKIFingerprint(certificatePem);
return {
profile: profilePath,
browser,
proxy: `https://127.0.0.1:${proxyPort}`,
noProxy: [
// Force even localhost requests to go through the proxy
// See https://bugs.chromium.org/p/chromium/issues/detail?id=899126#c17
'<-loopback>',
// Don't intercept our warning hiding requests. Note that this must be
// the 2nd rule here, or <-loopback> would override it.
hideWarningServer.host,
// We use httptoolkit-internal.localhost (always resolves to localhost,
// enforced by Chrome) to skip the proxy for internal requests from the
// our webextension.
'internal.httptoolkit.localhost'
],
options: [
// Trust our CA certificate's fingerprint:
`--ignore-certificate-errors-spki-list=${spkiFingerprint}`,
// Disable annoying "What's New" page & side panel (and its annoying bookmarks popup)
'--disable-features=ChromeWhatsNewUI,SidePanelPinning',
// Avoid annoying extra network noise:
'--disable-background-networking',
// Disable component update (without disabling components themselves, e.g. widevine)
// See https://bugs.chromium.org/p/chromium/issues/detail?id=331932
'--component-updater=url-source=http://disabled-chromium-update.localhost:0',
'--check-for-update-interval=31536000', // Don't update for a year
...(webExtensionEnabled && WEBEXTENSION_INSTALL
// Install HTTP Toolkit's extension, for advanced hook setup. Feature
// flagged for now as it's still new & largely untested.
? [
`--load-extension=${WEBEXTENSION_INSTALL.path}`
]
: []
)
]
};
}
abstract class FreshChromiumBasedInterceptor implements Interceptor {
readonly abstract id: string;
readonly abstract version: string;
private readonly activeBrowsers: _.Dictionary<BrowserInstance> = {};
constructor(
private config: HtkConfig,
private variantName: string
) { }
isActive(proxyPort: number | string) {
const browser = this.activeBrowsers[proxyPort];
return !!browser && !!browser.pid;
}
async isActivable() {
const browserDetails = await getBrowserDetails(this.config.configPath, this.variantName)
return !!browserDetails;
}
async activate(proxyPort: number, options: { webExtensionEnabled?: boolean } = {}) {
const alreadyActive = this.isActive(proxyPort);
const hideWarningServer = new HideWarningServer(this.config, {
delay: this.variantName === 'opera'
? 1
: undefined
});
await hideWarningServer.start('https://amiusing.httptoolkit.tech');
const browserDetails = await getBrowserDetails(this.config.configPath, this.variantName);
const snapProfilePath = browserDetails && await isSnap(browserDetails.command)
? path.join(await getSnapConfigPath(this.variantName), 'profile')
: undefined;
const browser = await launchBrowser(hideWarningServer.hideWarningUrl,
await getChromiumLaunchOptions(
browserDetails ? browserDetails.name : this.variantName,
this.config,
snapProfilePath,
proxyPort,
hideWarningServer,
!!options.webExtensionEnabled
)
, this.config.configPath);
if (browser.process.stdout) browser.process.stdout.pipe(process.stdout);
if (browser.process.stderr) browser.process.stderr.pipe(process.stderr);
await hideWarningServer.completedPromise;
// We want to stop the warning server, but some browsers (Opera) sometimes reload during app startup,
// and this causes problems if it goes away quickly, so we wait just a moment first:
delay(10_000).then(() => hideWarningServer.stop());
if (alreadyActive) return; // If we're just opening a new tab, we're done, don't track the process.
this.activeBrowsers[proxyPort] = browser;
browser.process.once('close', async () => {
delete this.activeBrowsers[proxyPort];
// Opera has a launch proc that exits immediately in Windows, so we can't clear the profile there.
if (process.platform === 'win32' && this.variantName === 'opera') return;
await delay(1000); // No hurry, make sure the browser & related processes have all cleaned up
const activeBrowserCount = Object.keys(this.activeBrowsers).length;
const profilePath = snapProfilePath ?? browserDetails?.profile;
if (activeBrowserCount === 0 && typeof profilePath === 'string') {
// If we were the last browser and we have a profile path, and it's definitely part of our
// local/snap config (just in case something's gone wrong) -> delete it & reset everything.
if (
// Part of our local config path:
!profilePath.startsWith(this.config.configPath) &&
// Or part of the .httptoolkit dir within a Snap's data dir:
!profilePath.includes(path.join('snap', this.variantName, 'current', '.httptoolkit'))
) {
logError(
`Unexpected ${this.variantName} profile location, not deleting: ${profilePath}`
);
} else {
deleteFolder(profilePath)
.catch(async() => {
// After 1 error, wait a little and retry
await delay(1000);
await deleteFolder(profilePath);
}).catch(console.warn); // If it still fails, just give up, not a big deal
}
}
});
// Delay the approx amount of time it normally takes the browser to really open, just to be sure
await delay(500);
}
async deactivate(proxyPort: number | string) {
if (this.isActive(proxyPort)) {
const browser = this.activeBrowsers[proxyPort];
const exitPromise = new Promise((resolve) => browser!.process.once('close', resolve));
browser!.stop();
await exitPromise;
}
}
async deactivateAll(): Promise<void> {
await Promise.all(
Object.keys(this.activeBrowsers).map((proxyPort) => this.deactivate(proxyPort))
);
}
};
abstract class ExistingChromiumBasedInterceptor implements Interceptor {
readonly abstract id: string;
readonly abstract version: string;
private activeBrowser: { // We can only intercept one instance
proxyPort: number,
browser: BrowserInstance
} | undefined;
constructor(
private config: HtkConfig,
private variantName: string
) { }
async browserDetails() {
return getBrowserDetails(this.config.configPath, this.variantName);
}
isActive(proxyPort: number | string) {
const activeBrowser = this.activeBrowser;
return !!activeBrowser &&
activeBrowser.proxyPort === proxyPort &&
!!activeBrowser.browser.pid;
}
async isActivable() {
if (this.activeBrowser) return false;
return !!await this.browserDetails();
}
async findExistingPid(): Promise<number | undefined> {
const processes = await listRunningProcesses();
const browserDetails = await this.browserDetails();
if (!browserDetails) {
throw new Error("Can't intercept existing browser without browser details");
}
const browserProcesses = processes.filter((proc) => {
if (process.platform === 'darwin') {
if (!proc.command.startsWith(browserDetails.command)) return false;
const appBundlePath = proc.command.substring(browserDetails.command.length);
// Only *.app/Contents/MacOS/* is the main app process:
return appBundlePath.match(/^\/Contents\/MacOS\//);
} else {
return proc.bin && (
// Find a binary that exactly matches the specific command:
proc.bin === browserDetails.command ||
// Or who matches the path for this specific variant:
proc.bin.includes(`${browserDetails.name}/${browserDetails.type}`) ||
// Or the snap for this variant:
proc.bin.includes(`/snap/${browserDetails.name}/`)
);
}
});
const defaultRootProcess = browserProcesses.find(({ args }) =>
args !== undefined &&
// Find the main process, skipping any renderer/util processes
!args.includes('--type=') &&
// Also skip any non-default profile processes (e.g. our own fresh Chromes)
!args.includes('--user-data-dir')
);
return defaultRootProcess && defaultRootProcess.pid;
}
async activate(proxyPort: number, options: {
closeConfirmed: boolean,
webExtensionEnabled?: boolean
} = { closeConfirmed: false }) {
if (!this.isActivable()) return;
const existingPid = await this.findExistingPid();
if (existingPid) {
if (!options.closeConfirmed) {
// Fail, with metadata requesting the UI to confirm that Chrome should be killed
throw Object.assign(
new Error(`Not killing ${this.variantName}: not confirmed`),
{ metadata: { closeConfirmRequired: true }, reportable: false }
);
}
if (process.platform === 'win32') {
windowsClose(existingPid);
try {
await waitForExit(existingPid);
} catch (e) {
// Try again, but less gently this time:
process.kill(existingPid);
await waitForExit(existingPid);
}
} else {
process.kill(existingPid);
await waitForExit(existingPid);
}
}
const hideWarningServer = new HideWarningServer(this.config);
await hideWarningServer.start('https://amiusing.httptoolkit.tech');
const browserDetails = await getBrowserDetails(this.config.configPath, this.variantName);
const launchOptions = await getChromiumLaunchOptions(
browserDetails ? browserDetails.name : this.variantName,
this.config,
null, // Null profile path ensures we use the system default profile
proxyPort,
hideWarningServer,
!!options.webExtensionEnabled
);
// Remove almost all default arguments. Each of these changes behaviour in maybe unexpected
// ways, notably including --disable-restore which actively causes problems.
launchOptions.skipDefaults = true;
launchOptions.options.push(
'--no-default-browser-check',
'--no-first-run',
'--disable-popup-blocking', // Required for hide-warning -> amiusing hop
// If we killed something, use --restore-last-session to ensure it comes back:
...(existingPid ? ['--restore-last-session'] : []),
// Passing the URL here instead of passing it to launchBrowser ensures that it isn't
// opened in a separate window when launching on Mac
hideWarningServer.hideWarningUrl
);
const browser = await launchBrowser("", {
...launchOptions,
skipDefaults: true
}, this.config.configPath);
if (browser.process.stdout) browser.process.stdout.pipe(process.stdout);
if (browser.process.stderr) browser.process.stderr.pipe(process.stderr);
await hideWarningServer.completedPromise;
await hideWarningServer.stop();
this.activeBrowser = { browser, proxyPort };
browser.process.once('close', async () => {
delete this.activeBrowser;
});
// Delay the approx amount of time it normally takes the browser to really open, just to be sure
await delay(500);
}
async deactivate(proxyPort: number | string) {
if (this.isActive(proxyPort)) {
const { browser } = this.activeBrowser!;
if (process.platform === 'win32') {
// Try to cleanly close if we can, rather than killing Chrome directly:
try {
await windowsClose(browser!.pid)
.then(() => waitForExit(browser!.pid));
return;
} catch (e) {} // If this fails/times out, kill like we do elsewhere:
}
const exitPromise = new Promise((resolve) => browser!.process.once('close', resolve));
browser!.stop();
await exitPromise;
}
}
async deactivateAll(): Promise<void> {
if (this.activeBrowser) {
await this.deactivate(this.activeBrowser.proxyPort);
}
}
};
export class FreshChrome extends FreshChromiumBasedInterceptor {
id = 'fresh-chrome';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'chrome');
}
};
export class ExistingChrome extends ExistingChromiumBasedInterceptor {
id = 'existing-chrome';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'chrome');
}
};
export class FreshChromeBeta extends FreshChromiumBasedInterceptor {
id = 'fresh-chrome-beta';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'chrome-beta');
}
};
export class FreshChromeDev extends FreshChromiumBasedInterceptor {
id = 'fresh-chrome-dev';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'chrome-dev');
}
};
export class FreshChromeCanary extends FreshChromiumBasedInterceptor {
id = 'fresh-chrome-canary';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'chrome-canary');
}
};
export class FreshChromium extends FreshChromiumBasedInterceptor {
id = 'fresh-chromium';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'chromium');
}
};
export class ExistingChromium extends ExistingChromiumBasedInterceptor {
id = 'existing-chromium';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'chromium');
}
};
export class FreshChromiumDev extends FreshChromiumBasedInterceptor {
id = 'fresh-chromium-dev';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'chromium-dev');
}
};
export class FreshEdge extends FreshChromiumBasedInterceptor {
id = 'fresh-edge';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'msedge');
}
};
export class FreshEdgeBeta extends FreshChromiumBasedInterceptor {
id = 'fresh-edge-beta';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'msedge-beta');
}
};
export class FreshEdgeDev extends FreshChromiumBasedInterceptor {
id = 'fresh-edge-dev';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'msedge-dev');
}
};
export class FreshEdgeCanary extends FreshChromiumBasedInterceptor {
id = 'fresh-edge-canary';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'msedge-canary');
}
};
export class FreshBrave extends FreshChromiumBasedInterceptor {
id = 'fresh-brave';
version = '1.0.0';
constructor(config: HtkConfig) {
super(config, 'brave');
}
};
export class FreshOpera extends FreshChromiumBasedInterceptor {
id = 'fresh-opera';
version = '1.0.3';
constructor(config: HtkConfig) {
super(config, 'opera');
}
};