-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
119 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
# The default folder to put you mitmproxy addons to import | ||
mitmproxy-addons/har_dump.py | ||
.venv/ | ||
|
||
tmp/ | ||
*.tmp | ||
tmp.* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-nocheck | ||
|
||
import { Buffer } from 'buffer'; | ||
import fetch from 'cross-fetch'; | ||
import { execa } from 'execa'; | ||
import { existsSync } from 'fs'; | ||
import { copyFile, mkdir, writeFile } from 'fs/promises'; | ||
import globalCacheDir from 'global-cache-dir'; | ||
import { homedir } from 'os'; | ||
import { join } from 'path'; | ||
|
||
// Set up our Python dependencies (a venv with the modules from `requirements.txt` and the mitmproxy addons). | ||
// This is meant to be run in the `postinstall` script. It will always install the full set of dependencies, regardless | ||
// of whether they are already installed. | ||
export const setupPythonDependencies = async () => { | ||
const cacheDir = await globalCacheDir('cyanoacrylate'); | ||
|
||
const venvDir = join(cacheDir, 'venv'); | ||
const mitmproxyAddonsDir = join(cacheDir, 'mitmproxy-addons'); | ||
await mkdir(mitmproxyAddonsDir, { recursive: true }); | ||
|
||
// Create a venv and install all python requirements | ||
await execa('python', ['-m', 'venv', venvDir], { stdio: 'inherit' }); | ||
await execa(`${venvDir}/bin/pip`, ['install', '-r', 'requirements.txt'], { stdio: 'inherit' }); | ||
|
||
// Download the har_dump.py addon corresponding to the current mitmproxy version | ||
const mitmproxyVersion = await execa(`${venvDir}/bin/mitmdump`, ['--version']).then( | ||
({ stdout }) => stdout.match(/Mitmproxy: ([0-9.]+)/)?.[1] | ||
); | ||
const mitmproxyCommitSha = await fetch( | ||
`https://api.github.com/repos/mitmproxy/mitmproxy/git/ref/tags/${mitmproxyVersion}` | ||
) | ||
.then((res) => res.json()) | ||
.then((ref) => ref.object.sha); | ||
await fetch( | ||
`https://raw.githubusercontent.com/mitmproxy/mitmproxy/${mitmproxyCommitSha}/examples/contrib/har_dump.py` | ||
) | ||
.then((res) => res.arrayBuffer()) | ||
.then((hardumpScript) => writeFile(join(mitmproxyAddonsDir, 'har_dump.py'), Buffer.from(hardumpScript))); | ||
|
||
// Copy the ipcEventsAddon.py addon to the mitmproxy addons directory | ||
await copyFile( | ||
// eslint-disable-next-line no-undef | ||
new URL('../../src/ipcEventsAddon.py', import.meta.url), | ||
join(mitmproxyAddonsDir, 'ipcEventsAddon.py') | ||
); | ||
|
||
// Start mitmproxy once to create certificate files if they don't exist, yet. | ||
if (!existsSync(join(homedir(), '.mitmproxy'))) { | ||
const mitmproxyProcess = execa(`${venvDir}/bin/mitmdump`, [ | ||
'-q', | ||
'-s', | ||
join(mitmproxyAddonsDir, 'ipcEventsAddon.py'), | ||
'--set', | ||
'ipcPipeFd=1', | ||
]); | ||
mitmproxyProcess.stdout?.addListener('data', (data) => { | ||
const msg = JSON.parse(data); | ||
if (msg.status === 'running') mitmproxyProcess.kill(); | ||
}); | ||
} | ||
}; | ||
|
||
// This is a lighter version of `setupPythonDependencies`. It only installs if certain key files are missing. | ||
// This isn't an exhaustive check but many orders of magnitude faster in the regular case (i.e. the dependencies are | ||
// already installed). It is meant to always be run before an analysis is started. | ||
export const ensurePythonDependencies = async () => { | ||
const pathsThatNeedToExist = [ | ||
// The one file that exists both in Windows and *nix venvs. | ||
'venv/pyvenv.cfg', | ||
'mitmproxy-addons/har_dump.py', | ||
'mitmproxy-addons/ipcEventsAddon.py', | ||
]; | ||
|
||
const cacheDir = await globalCacheDir('cyanoacrylate'); | ||
|
||
if (pathsThatNeedToExist.some((path) => !existsSync(join(cacheDir, path)))) await setupPythonDependencies(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,5 @@ | ||
import { Buffer } from 'buffer'; | ||
import fetch from 'cross-fetch'; | ||
import { execa } from 'execa'; | ||
import { existsSync } from 'fs'; | ||
import { writeFile } from 'fs/promises'; | ||
import { homedir } from 'os'; | ||
import { join } from 'path'; | ||
import { setupPythonDependencies } from './common/setup.js'; | ||
|
||
(async () => { | ||
// Lifecycle scripts are always run from the project root, so this is always the same path (https://docs.npmjs.com/cli/v9/using-npm/scripts#best-practices). | ||
const venvRoot = '.venv'; | ||
|
||
// Create a venv and install all python requirements | ||
await execa('python', ['-m', 'venv', venvRoot], { stdio: 'inherit' }); | ||
await execa(`${venvRoot}/bin/pip`, ['install', '-r', 'requirements.txt'], { | ||
stdio: 'inherit', | ||
}); | ||
|
||
// Download the har_dump.py addon corresponding to the current mitmproxy version | ||
const mitmproxyVersion = await execa(`${venvRoot}/bin/mitmdump`, ['--version']).then( | ||
({ stdout }) => stdout.match(/Mitmproxy: ([0-9.]+)/)[1] | ||
); | ||
const mitmproxyCommitSha = await fetch( | ||
`https://api.github.com/repos/mitmproxy/mitmproxy/git/ref/tags/${mitmproxyVersion}` | ||
) | ||
.then((res) => res.json()) | ||
.then((ref) => ref.object.sha); | ||
await fetch( | ||
`https://raw.githubusercontent.com/mitmproxy/mitmproxy/${mitmproxyCommitSha}/examples/contrib/har_dump.py` | ||
) | ||
.then((res) => res.arrayBuffer()) | ||
.then((hardumpScript) => writeFile('mitmproxy-addons/har_dump.py', Buffer.from(hardumpScript))); | ||
|
||
// Start mitmproxy once to create certificate files if they don't exist, yet. | ||
if (!existsSync(join(homedir(), '.mitmproxy'))) { | ||
const mitmproxyProcess = execa( | ||
`${venvRoot}/bin/mitmdump`, | ||
['-s', 'mitmproxy-addons/ipcEventsAddon.py', '--set', 'ipcPipeFd=3'], | ||
{ | ||
stdio: ['inherit', 'inherit', 'inherit', 'ipc'], | ||
} | ||
); | ||
mitmproxyProcess.on('message', (msg) => { | ||
if (msg.status === 'running') mitmproxyProcess.kill(); | ||
}); | ||
} | ||
await setupPythonDependencies(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters