From 4beddcc65d7ea4b123fd702288f1a1f70203cb9d Mon Sep 17 00:00:00 2001 From: yan Date: Wed, 17 Oct 2018 19:19:35 -0700 Subject: [PATCH] add npm command for running a network audit 'npm run network-audit' produces a JSON file, network-audit-results.json, which contains the URL requests and 307's in the current build. --- .gitignore | 2 ++ lib/start.js | 26 +++++++++++++++++++++++++- package.json | 1 + scripts/commands.js | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d53ebf3c6d8ae..74e2b600f7a86 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ npm-debug.log .vscode .cipd .idea +network_log.json +network-audit-results.json # Rendered Sphinx files should be excluded from source control build diff --git a/lib/start.js b/lib/start.js index 378b7a1d5bc7d..e83e38168c40d 100644 --- a/lib/start.js +++ b/lib/start.js @@ -1,4 +1,5 @@ const path = require('path') +const fs = require('fs-extra') const config = require('../lib/config') const util = require('../lib/util') @@ -44,8 +45,8 @@ const start = (buildConfig = config.defaultBuildConfig, options) => { if (options.rewards_reconcile_interval) { braveArgs.push(`--rewards-reconcile-interval=${options.rewards_reconcile_interval}`) } + let user_data_dir if (options.user_data_dir_name) { - let user_data_dir if (process.platform === 'darwin') { user_data_dir = path.join(process.env.HOME, 'Library', 'Application\\ Support', 'BraveSoftware', options.user_data_dir_name) } else if (process.platform === 'win32') { @@ -55,9 +56,20 @@ const start = (buildConfig = config.defaultBuildConfig, options) => { } braveArgs.push('--user-data-dir=' + user_data_dir); } + const networkLogFile = path.resolve(path.join(__dirname, '..', 'network_log.json')) + if (options.network_log) { + braveArgs.push(`--log-net-log=${networkLogFile}`) + braveArgs.push(`--net-log-capture-mode=IncludeSocketBytes`) + if (user_data_dir) { + // clear the data directory before doing a network test + console.log('clearing user data directory', user_data_dir) + fs.removeSync(user_data_dir) + } + } let cmdOptions = { stdio: 'inherit', + timeout: options.network_log ? 10000 : undefined, shell: true } @@ -68,6 +80,18 @@ const start = (buildConfig = config.defaultBuildConfig, options) => { } else { util.run(path.join(config.outputDir, 'brave'), braveArgs, cmdOptions) } + + if (options.network_log) { + // Read the network log + const jsonOutput = fs.readJsonSync(networkLogFile) + const URL_REQUEST_TYPE = jsonOutput.constants.logSourceType.URL_REQUEST + const URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED = jsonOutput.constants.logEventTypes.URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED + const urlRequests = jsonOutput.events.filter((event) => + (event.source.type === URL_REQUEST_TYPE && event.params && event.params.url && event.params.url.startsWith('http')) || event.type === URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED + ) + // TODO: parse this and fail travis if it includes unexpected events + fs.writeJsonSync('network-audit-results.json', urlRequests) + } } module.exports = start diff --git a/package.json b/package.json index 4399831a7c9b9..8d686304d3c4b 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "update_patches": "node ./scripts/commands.js update_patches", "apply_patches": "node ./scripts/sync.js --run_hooks", "start": "node ./scripts/commands.js start", + "network-audit": "node ./scripts/commands.js start --network_log --user_data_dir_name=brave-network-test", "push_l10n": "node ./scripts/commands.js push_l10n", "pull_l10n": "node ./scripts/commands.js pull_l10n", "chromium_rebase_l10n": "node ./scripts/commands.js chromium_rebase_l10n", diff --git a/scripts/commands.js b/scripts/commands.js index 93861b1402eb9..6a69afae3788d 100644 --- a/scripts/commands.js +++ b/scripts/commands.js @@ -77,6 +77,7 @@ program .option('--rewards_env [server]', 'switch between staging and production', /^(stag|prod)$/i) .option('--rewards_reconcile_interval [reconcile_interval]', 'set reconcile interval for contribution in minutes', parseInt) .option('--single_process', 'use a single process') + .option('--network_log', 'log network activity to network_log.json') .arguments('[build_config]') .action(start)