Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/frontend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import App from "./views/App.vue";

import { ScanLauncher } from "@/components/launcher";
import { useLauncher } from "@/stores/launcher";
import { getSelectedRequestFromDOM } from "@/utils/utils";

export const init = (sdk: FrontendSDK) => {
const app = createApp(App);
Expand Down Expand Up @@ -80,6 +81,16 @@ export const init = (sdk: FrontendSDK) => {
sdk.window.showToast("No requests selected", { variant: "warning" });
return;
}
} else if (context.type === "BaseContext") {
const request = getSelectedRequestFromDOM();
if (request !== undefined) {
requests.push(request);
} else {
sdk.window.showToast("No request editor active or selected", {
variant: "warning",
});
return;
}
} else {
sdk.window.showToast("No requests selected", { variant: "warning" });
return;
Expand All @@ -104,10 +115,11 @@ export const init = (sdk: FrontendSDK) => {

const launcherStore = useLauncher();
launcherStore.restart();
launcherStore.form.targets = requests.map((request) => ({
const targets = requests.map((request) => ({
...request,
method: "GET",
}));
launcherStore.form.targets = targets;

const dialog = sdk.window.showDialog(
{
Expand Down Expand Up @@ -135,10 +147,15 @@ export const init = (sdk: FrontendSDK) => {
if (context.type === "RequestContext") {
return context.request.type === "RequestFull";
}
if (context.type === "BaseContext") {
return true;
}
return false;
},
});

sdk.shortcuts.register("run-active-scanner", ["Control", "Shift", "S"]);

sdk.menu.registerItem({
type: "RequestRow",
commandId: "run-active-scanner",
Expand Down
123 changes: 123 additions & 0 deletions packages/frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { type EditorView } from "@codemirror/view";

export function getSelectedRequestFromDOM() {
const requestEditor = document.querySelector("[data-language='http-request']");

if (requestEditor === null) {
return undefined;
}

if (!(requestEditor instanceof HTMLElement)) {
return undefined;
}

let view = (requestEditor as unknown as { cmView?: EditorView }).cmView;
if (view === undefined) {
const cmEditor = requestEditor.querySelector(".cm-editor");
if (cmEditor instanceof HTMLElement) {
view = (cmEditor as unknown as { cmView?: EditorView }).cmView;
}
}

if (view === undefined) {
return undefined;
}

const rawRequest = view.state.doc.toString();
const lines = rawRequest.split("\n");
const firstLine = lines[0]?.trim();

if (firstLine === undefined || firstLine === "") {
return undefined;
}

const parts = firstLine.split(" ");
const pathAndQuery: string = parts[1] ?? "/";
const [path] =
pathAndQuery.includes("?") === true
? pathAndQuery.split("?")
: [pathAndQuery, ""];

const hostLine = lines.find((line: string) =>
line.toLowerCase().startsWith("host:"),
);
const hostFromRequest =
hostLine !== undefined
? hostLine.split(":").slice(1).join(":").trim()
: undefined;

let requestId: string | undefined;

if (hostFromRequest !== undefined && path !== undefined && path !== "") {
const allRows = Array.from(document.querySelectorAll(".c-item-row"));

for (const row of allRows) {
const hostCell = row
.querySelector("[data-column-id='REQ_HOST']")
?.textContent?.trim();
const pathCell = row
.querySelector("[data-column-id='REQ_PATH']")
?.textContent?.trim();
const rowId = row.getAttribute("data-row-id");

if (
hostCell !== undefined &&
hostCell !== "" &&
hostCell.includes(hostFromRequest) &&
pathCell === path &&
rowId !== null &&
rowId !== ""
) {
requestId = rowId;
break;
}
}
}

let url: string | undefined;

if (hostLine !== undefined) {
const host = hostLine.split(":").slice(1).join(":").trim();
url = `https://${host}`;
} else {
return undefined;
}

try {
const urlObj = new URL(url);
const [finalPath, query = ""] =
pathAndQuery.includes("?") === true
? pathAndQuery.split("?")
: [pathAndQuery, ""];

const hostMatch = rawRequest.match(/Host:\s*(.+?)(?::(\d+))?\r?\n/i);
const hostFromMatch = hostMatch?.[1]?.trim();
const host =
hostFromMatch !== undefined && hostFromMatch !== ""
? hostFromMatch
: urlObj.hostname;
const portFromMatch = hostMatch?.[2];
const port =
portFromMatch !== undefined
? parseInt(portFromMatch)
: urlObj.port !== ""
? parseInt(urlObj.port)
: urlObj.protocol === "https:"
? 443
: 80;

const finalIdValue = requestId ?? Date.now().toString();
const finalPathValue =
finalPath !== undefined && finalPath !== "" ? finalPath : "/";

return {
id: finalIdValue,
host,
port,
path: finalPathValue,
query,
};
} catch {
return undefined;
}
}