Skip to content

Commit

Permalink
chore: typescript support & custom flow removal (#101)
Browse files Browse the repository at this point in the history
* chore: support typescript calling of backend

* chore: remove custom flow 1.0

* fix: remove run build, added in previously unintentionally

* fix: abort scan feature
  • Loading branch information
joshualai9922 authored Jun 6, 2024
1 parent df15a93 commit 2c66ffc
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 564 deletions.
3 changes: 0 additions & 3 deletions public/electron/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ const getPathVariable = () => {

const scanResultsPath = path.join(resultsPath, "results");

const customFlowGeneratedScriptsPath = path.join(resultsPath, "custom_flow_scripts");

const updateBackupsFolder = path.join(
appPath,
"30789f0f-73f5-43bc-93a6-e499e4a20f7a"
Expand Down Expand Up @@ -644,7 +642,6 @@ module.exports = {
playwrightBrowsersPath,
getPathVariable,
scanResultsPath,
customFlowGeneratedScriptsPath,
updateBackupsFolder,
phZipPath,
resultsPath,
Expand Down
10 changes: 2 additions & 8 deletions public/electron/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ contextBridge.exposeInMainWorld("services", {
const results = await ipcRenderer.invoke("startScan", scanDetails);
return results;
},
abortScan: async (scanDetails) => {
abortScan: async () => {
await ipcRenderer.invoke("abortScan");
},
startReplay: async (generatedScript, scanDetails, isReplay) => {
const results = await ipcRenderer.invoke("startReplay", generatedScript, scanDetails, isReplay);
return results;
},

generateReport: (customFormLabel, scanId) => {
ipcRenderer.send("generateReport", customFormLabel, scanId);
},
Expand All @@ -45,9 +42,6 @@ contextBridge.exposeInMainWorld("services", {
openUploadFolder: () => {
ipcRenderer.send("openUploadFolder");
},
cleanUpCustomFlowScripts: (() => {
ipcRenderer.send("cleanUpCustomFlowScripts");
}),
getEngineVersion: async () => {
const phEngineVersion = await ipcRenderer.invoke('getEngineVersion');
return phEngineVersion;
Expand Down
122 changes: 2 additions & 120 deletions public/electron/scanManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,17 @@ const {
enginePath,
appPath,
getPathVariable,
customFlowGeneratedScriptsPath,
playwrightBrowsersPath,
resultsPath,
scanResultsPath,
forbiddenCharactersInDirPath,
createPlaywrightContext,
deleteClonedProfiles,
backendPath,
isWindows,
} = require("./constants");
const {
browserTypes,
getDefaultChromeDataDir,
getDefaultEdgeDataDir,
uploadFolderName,
} = require("./constants");
const { env, report } = require("process");
const { readUserDataFromFile, createExportDir } = require("./userDataManager");
const { escape } = require("querystring");
const { json } = require("react-router-dom");
const { time } = require("console");
const scanHistory = {};

let currentChildProcess;
Expand Down Expand Up @@ -143,7 +133,7 @@ const validateUrlConnectivity = async (scanDetails) => {
const response = await new Promise(async (resolve) => {
const check = spawn(
"node",
[path.join(enginePath, "cli.js"), ...getScanOptions(scanDetails)],
[`${enginePath}/dist/cli.js`, ...getScanOptions(scanDetails)],
{
cwd: resultsPath,
env: {
Expand Down Expand Up @@ -204,7 +194,7 @@ const startScan = async (scanDetails, scanEvent) => {
let intermediateFolderName;
const scan = spawn(
"node",
[path.join(enginePath, "cli.js"), ...getScanOptions(scanDetails)],
[`${enginePath}/dist/cli.js`, ...getScanOptions(scanDetails)],
{
cwd: resultsPath,
env: {
Expand Down Expand Up @@ -258,13 +248,6 @@ const startScan = async (scanDetails, scanEvent) => {
resolve({ success: false });
}

if (scanDetails.scanType === 'custom' && data.includes('generatedScript')) {
const generatedScriptName = data.trim();
console.log('generated script: ', generatedScriptName);
const generatedScript = path.join(customFlowGeneratedScriptsPath, generatedScriptName);
resolve({ success: true, generatedScript: generatedScript});
}

// The true success where the process ran and pages were scanned
if (data.includes("Results directory is at")) {
console.log(data);
Expand Down Expand Up @@ -313,87 +296,6 @@ const startScan = async (scanDetails, scanEvent) => {
return response;
};

const startReplay = async (generatedScript, scanDetails, scanEvent, isReplay) => {
let useChromium = false;
if (
scanDetails.browser === browserTypes.chromium ||
(!getDefaultChromeDataDir() && !getDefaultEdgeDataDir())
) {
useChromium = true;
}

if (isReplay && scanDetails.encryptionParams) {
decryptGeneratedScript(generatedScript, scanDetails.encryptionParams);
}

const response = await new Promise((resolve, reject) => {
const replay = spawn(
"node",
[path.join(enginePath, "runCustomFlowFromGUI.js"), generatedScript], {
cwd: resultsPath,
env: {
...process.env,
RUNNING_FROM_PH_GUI: true,
PLAYWRIGHT_BROWSERS_PATH: `${playwrightBrowsersPath}`,
PATH: getPathVariable(),
},
});

currentChildProcess = replay;

replay.stderr.setEncoding("utf8");
replay.stderr.on("data", function (data) {
console.log("stderr: " + data);
});

replay.stdout.setEncoding("utf8");
replay.stdout.on("data", async (data) => {
if (data.includes("An error has occurred when running the custom flow scan.")) {
replay.kill("SIGKILL");
currentChildProcess = null;
resolve({ success: false });
}

// Handle live crawling output
if (data.includes("crawling::")) {
const urlScannedNum = parseInt(data.split("::")[1].trim());
const status = data.split("::")[2].trim();
const url = data.split("::")[3].trim();
console.log(urlScannedNum, ":", status, ":", url);
scanEvent.emit("scanningUrl", {status, url, urlScannedNum});
}

if (data.includes("Scan completed")) {
scanEvent.emit("scanningCompleted");
}

if (data.includes("results/")) {
console.log(data);
const resultsFolderName = data.split(" ").slice(-2)[0].split("/").pop();
console.log(resultsFolderName);

const scanId = randomUUID();
scanHistory[scanId] = resultsFolderName;

const encryptionParams = encryptGeneratedScript(generatedScript);
moveCustomFlowResultsToExportDir(scanId, resultsFolderName, isReplay);
replay.kill("SIGKILL");
currentChildProcess = null;
await cleanUpIntermediateFolders(resultsFolderName);
resolve({ success: true, scanId, encryptionParams });
}
});

replay.on("close", (code) => {
if (code !== 0) {
resolve({ success: false, statusCode: code });
}
});
});

return response;
};

const encryptGeneratedScript = (generatedScript) => {
// Generate random password and IV
const password = randomBytes(32);
Expand Down Expand Up @@ -574,18 +476,6 @@ const cleanUpIntermediateFolders = async (folderName, setDefaultFolders = false)
});
};

const cleanUpCustomFlowGeneratedScripts = () => {
if (fs.existsSync(customFlowGeneratedScriptsPath)) {
fs.rm(customFlowGeneratedScriptsPath, { recursive: true }, (err) => {
if (err) {
console.error(
`Error while deleting ${customFlowGeneratedScriptsPath}.`
);
}
});
}
}

const moveCustomFlowResultsToExportDir = (scanId, resultsFolderName, isReplay) => {
const currentResultsPath = path.join(scanResultsPath, resultsFolderName);
let newResultsPath;
Expand Down Expand Up @@ -616,10 +506,6 @@ const init = (scanEvent) => {
setKillChildProcessSignal();
});

ipcMain.handle("startReplay", async (_event, generatedScript, scanDetails, isReplay) => {
return await startReplay(generatedScript, scanDetails, scanEvent, isReplay);
})

ipcMain.on("generateReport", (_event, customFlowLabel, scanId) => {
return generateReport(customFlowLabel, scanId);
});
Expand Down Expand Up @@ -685,10 +571,6 @@ const init = (scanEvent) => {
return allErrors;
})

ipcMain.on("cleanUpCustomFlowScripts", async () => {
cleanUpCustomFlowGeneratedScripts();
})

ipcMain.handle("mailReport", (_event, formDetails, scanId) => {
return mailResults(formDetails, scanId);
});
Expand Down

This file was deleted.

4 changes: 1 addition & 3 deletions src/MainWindow/CustomFlow/components/CustomFlowDisplay.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
const CustomFlowDisplay = ({
icon,
step,
title,
url,
description,
scanType //optional
}) => {
return (
<>
<div className="custom-flow-header">
<div className="custom-flow-header-content">
<img className="custom-flow-header-img" src={icon} alt=""></img>
<div className="custom-flow-header-title-container">
<p className="custom-flow-header-step">{(scanType == 'Custom flow 2.0')? 'FINAL STEP' : `STEP ${step} of 4`}</p>
<p className="custom-flow-header-step">FINAL STEP</p>
<h3 className="custom-flow-header-title">{title}</h3>
</div>
</div>
Expand Down
52 changes: 0 additions & 52 deletions src/MainWindow/CustomFlow/components/ProgressStepComponent.jsx

This file was deleted.

Loading

0 comments on commit 2c66ffc

Please sign in to comment.