Skip to content

Commit c0eb71a

Browse files
committed
Include WebExtension, automatically updating config to match active sessions
1 parent 7fcb579 commit c0eb71a

File tree

9 files changed

+23867
-3
lines changed

9 files changed

+23867
-3
lines changed

overrides/webextension/build/background.js

Lines changed: 18782 additions & 0 deletions
Large diffs are not rendered by default.

overrides/webextension/build/content-script.js

Lines changed: 2865 additions & 0 deletions
Large diffs are not rendered by default.

overrides/webextension/build/injected-script.js

Lines changed: 2127 additions & 0 deletions
Large diffs are not rendered by default.

overrides/webextension/icon-128.png

6.99 KB
Loading

overrides/webextension/icon-16.png

578 Bytes
Loading

overrides/webextension/icon-48.png

5.39 KB
Loading

overrides/webextension/manifest.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "HTTP Toolkit",
3+
"description": " A browser extension used in HTTP Toolkit",
4+
"icons": {
5+
"16": "icon-16.png",
6+
"48": "icon-48.png",
7+
"128": "icon-128.png"
8+
},
9+
"key": "jggocihfdfgogbofcmdlfbakdkgdkdoe",
10+
"version": "1.1.0",
11+
"manifest_version": 3,
12+
"background": {
13+
"service_worker": "build/background.js"
14+
},
15+
"permissions": [
16+
"scripting",
17+
"proxy"
18+
],
19+
"host_permissions": [
20+
"<all_urls>"
21+
],
22+
"web_accessible_resources": [
23+
{
24+
"resources": [
25+
"build/injected-script.js"
26+
],
27+
"matches": [
28+
"<all_urls>"
29+
]
30+
}
31+
]
32+
}

src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
startDockerInterceptionServices,
3333
stopDockerInterceptionServices
3434
} from './interceptors/docker/docker-interception-services';
35+
import { clearWebExtensionConfig, updateWebExtensionConfig } from './webextension';
3536

3637
const APP_NAME = "HTTP Toolkit";
3738

@@ -82,18 +83,29 @@ function manageBackgroundServices(
8283
}>,
8384
httpsConfig: { certPath: string, certContent: string }
8485
) {
85-
standalone.on('mock-session-started', async ({ http }) => {
86-
startDockerInterceptionServices(http.getMockServer().port, httpsConfig, ruleParameters)
86+
standalone.on('mock-session-started', async ({ http, webrtc }, sessionId) => {
87+
const httpProxyPort = http.getMockServer().port;
88+
89+
startDockerInterceptionServices(httpProxyPort, httpsConfig, ruleParameters)
8790
.catch((error) => {
8891
console.log("Could not start Docker components:", error);
8992
});
93+
94+
updateWebExtensionConfig(sessionId, httpProxyPort, !!webrtc)
95+
.catch((error) => {
96+
console.log("Could not update WebRTC config:", error);
97+
});
9098
});
9199

92100
standalone.on('mock-session-stopping', ({ http }) => {
93-
stopDockerInterceptionServices(http.getMockServer().port, ruleParameters)
101+
const httpProxyPort = http.getMockServer().port;
102+
103+
stopDockerInterceptionServices(httpProxyPort, ruleParameters)
94104
.catch((error) => {
95105
console.log("Could not stop Docker components:", error);
96106
});
107+
108+
clearWebExtensionConfig(httpProxyPort);
97109
});
98110
}
99111

src/webextension.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as path from 'path';
2+
import { OVERRIDES_DIR } from './interceptors/terminal/terminal-env-overrides';
3+
4+
import { deleteFile, writeFile } from "./util/fs";
5+
6+
export const WEBEXTENSION_PATH = path.join(OVERRIDES_DIR, 'webextension');
7+
8+
interface WebExtensionConfig { // Should match config in the WebExtension itself
9+
mockRtc: {
10+
peerId: string;
11+
adminBaseUrl: string;
12+
} | false;
13+
}
14+
15+
const getConfigKey = (proxyPort: number) =>
16+
`127_0_0_1.${proxyPort}`; // Filename-safe proxy address
17+
18+
const getConfigPath = (proxyPort: number) =>
19+
path.join(WEBEXTENSION_PATH, 'config', getConfigKey(proxyPort));
20+
21+
export function clearWebExtensionConfig(httpProxyPort: number) {
22+
return deleteFile(getConfigPath(httpProxyPort))
23+
.catch(() => {}); // We ignore errors - nothing we can do, not very important.
24+
}
25+
26+
export async function updateWebExtensionConfig(
27+
sessionId: string,
28+
httpProxyPort: number,
29+
webRTCEnabled: boolean
30+
) {
31+
if (webRTCEnabled) {
32+
const adminBaseUrl = `http://internal.httptoolkit.localhost:45456/session/${sessionId}`;
33+
await writeConfig(httpProxyPort, {
34+
mockRtc: {
35+
peerId: 'matching-peer',
36+
adminBaseUrl
37+
}
38+
});
39+
} else {
40+
await writeConfig(httpProxyPort, { mockRtc: false });
41+
}
42+
}
43+
44+
async function writeConfig(proxyPort: number, config: WebExtensionConfig) {
45+
return writeFile(getConfigPath(proxyPort), JSON.stringify(config));
46+
}

0 commit comments

Comments
 (0)