Skip to content

Commit

Permalink
Merge branch 'master' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
nafees87n committed May 13, 2024
2 parents be6abc2 + d2caa42 commit db1904d
Show file tree
Hide file tree
Showing 20 changed files with 554 additions and 195 deletions.
72 changes: 42 additions & 30 deletions app/src/modules/extension/ruleParser/parseHeadersRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,53 @@ import { ExtensionRule, ExtensionRuleAction, HeadersRuleOperation, ModifyHeaderI
import { parseConditionFromSource } from "./utils";

const parseHeaders = (headers: HeadersRuleModificationData[]): ModifyHeaderInfo[] => {
return headers.map((header) => {
if (header.type === HeaderRuleActionType.REMOVE) {
return {
header: header.header,
operation: "remove" as HeadersRuleOperation,
};
} else {
return {
header: header.header,
value: header.value,
operation: "set" as HeadersRuleOperation,
};
}
});
return headers
.map((header) => {
if (header.value === "rq_request_initiator_origin()") {
return null;
}

if (header.type === HeaderRuleActionType.REMOVE) {
return {
header: header.header,
operation: "remove" as HeadersRuleOperation,
};
} else {
return {
header: header.header,
value: header.value,
operation: "set" as HeadersRuleOperation,
};
}
})
.filter(Boolean);
};

const parseHeadersRule = (rule: HeadersRule): ExtensionRule[] => {
return rule.pairs.map(
(rulePair): ExtensionRule => {
const condition = parseConditionFromSource(rulePair.source);
const action: ExtensionRuleAction = {
type: RuleActionType.MODIFY_HEADERS,
};

if (rulePair.modifications?.Request?.length) {
action.requestHeaders = parseHeaders(rulePair.modifications?.Request);
}
return rule.pairs
.map(
(rulePair): ExtensionRule => {
const condition = parseConditionFromSource(rulePair.source);
const action: ExtensionRuleAction = {
type: RuleActionType.MODIFY_HEADERS,
};

if (rulePair.modifications?.Response?.length) {
action.responseHeaders = parseHeaders(rulePair.modifications?.Response);
}
if (rulePair.modifications?.Request?.length) {
action.requestHeaders = parseHeaders(rulePair.modifications?.Request);
}

if (rulePair.modifications?.Response?.length) {
action.responseHeaders = parseHeaders(rulePair.modifications?.Response);
}

return { action, condition };
}
);
if (!(action.requestHeaders?.length || action.responseHeaders?.length)) {
return null;
}

return { action, condition };
}
)
.filter(Boolean);
};

export default parseHeadersRule;
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ import { PUBLIC_NAMESPACE } from "common/constants";
{
source: "requestly:client",
action: "response_rule_applied",
ruleId: message.ruleDetails.id,
rule: message.ruleDetails,
requestDetails: message["requestDetails"],
},
window.location.href
Expand All @@ -265,7 +265,7 @@ import { PUBLIC_NAMESPACE } from "common/constants";
{
source: "requestly:client",
action: "request_rule_applied",
ruleId: message.ruleDetails.id,
rule: message.ruleDetails,
requestDetails: message["requestDetails"],
},
window.location.href
Expand Down
5 changes: 2 additions & 3 deletions browser-extension/mv3/src/content-scripts/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { EXTENSION_MESSAGES } from "common/constants";
import { initRuleExecutionHandler } from "./ruleExecution";
import { initSessionRecording } from "./sessionRecorder";
import { initResponseRuleHandler } from "./responseRuleHandler";
import { initRequestRuleHandler } from "./requestRuleHandler";
import { Variable, getVariable } from "../../service-worker/variable";
import { initPageScriptMessageListener } from "./pageScriptMessageHandler";
import { initPageScriptMessageListener } from "./pageScriptMessageListener";

if (document.doctype?.name === "html" || document.contentType?.includes("html")) {
getVariable<boolean>(Variable.IS_EXTENSION_ENABLED, true).then((isExtensionStatusEnabled) => {
if (isExtensionStatusEnabled) {
chrome.runtime.sendMessage({ action: EXTENSION_MESSAGES.HANDSHAKE_CLIENT });
initSessionRecording();
initPageScriptMessageListener();
initRuleExecutionHandler();
// initRuleExecutionHandler();
initResponseRuleHandler();
initRequestRuleHandler();
}
Expand Down
29 changes: 0 additions & 29 deletions browser-extension/mv3/src/content-scripts/client/ruleExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,8 @@ let implictTestRuleWidgetConfig: Record<string, any> = null;
export const initRuleExecutionHandler = () => {
fetchAndStoreImplicitTestRuleWidgetConfig();

window.addEventListener("message", function (event) {
if (event.source !== window || event.data.source !== "requestly:client") {
return;
}

switch (event.data.action) {
case "response_rule_applied":
case "request_rule_applied":
appliedRuleIds.add(event.data.ruleId);
handleAppliedRuleNotification(event.data.ruleId);
break;
}
});

chrome.runtime.onMessage.addListener((message, _, sendResponse) => {
switch (message.action) {
case CLIENT_MESSAGES.UPDATE_APPLIED_SCRIPT_RULES:
message.ruleIds.forEach((ruleId: string) => {
appliedRuleIds.add(ruleId);
handleAppliedRuleNotification(ruleId);
});
break;
case CLIENT_MESSAGES.GET_APPLIED_RULES:
sendResponse(Array.from(appliedRuleIds));
break;
case CLIENT_MESSAGES.SYNC_APPLIED_RULES:
message.appliedRuleIds.forEach((ruleId: string) => {
appliedRuleIds.add(ruleId);
handleAppliedRuleNotification(ruleId);
});
break;
case CLIENT_MESSAGES.START_EXPLICIT_RULE_TESTING:
if (message.record) {
chrome.runtime.sendMessage({
Expand Down
4 changes: 2 additions & 2 deletions browser-extension/mv3/src/service-worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { registerCommands } from "./services/commands";
import { initContextMenu } from "./services/contextMenu";
import { initExtensionIcon } from "./services/extensionIcon";
import { handleInstallUninstall } from "./services/installUninstall";
import { initMessageHandler } from "./services/messageHandler";
import { initRulesManager } from "./services/rulesManager";
import { initWebRequestInterceptor } from "./services/webRequestInterceptor";

// initialize
(async () => {
initExtensionIcon();
registerCommands();
handleInstallUninstall();
initRulesManager();
initMessageHandler();
initContextMenu();
initWebRequestInterceptor();
})();
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { onVariableChange, setVariable, Variable } from "../variable";
import { disableExtensionIcon, enableExtensionIcon } from "./extensionIcon";
import extensionIconManager from "./extensionIconManager";
import { isExtensionEnabled } from "./utils";

// TODO: fix circular dependency
Expand All @@ -20,9 +20,9 @@ export const updateActivationStatus = (isExtensionEnabled: boolean) => {
});

if (isExtensionEnabled) {
enableExtensionIcon();
extensionIconManager.markExtensionEnabled();
} else {
disableExtensionIcon();
extensionIconManager.markExtensionDisabled();
}

// sendMessageToApp({ isExtensionEnabled });
Expand Down
30 changes: 0 additions & 30 deletions browser-extension/mv3/src/service-worker/services/extensionIcon.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { tabService } from "./tabService";

interface ExtensionIconConfig {
ruleExecuted?: boolean;
isRecording?: boolean;
}

class ExtensionIconManager {
#isExtensionDisabled = false;

#icons = {
DEFAULT: "/resources/images/48x48.png",
DISABLED: "/resources/images/48x48_greyscale.png",
RULE_EXECUTED: "/resources/images/48x48_green.png",
DEFAULT_WITH_REC: "/resources/images/48x48_rec.png",
RULE_EXECUTED_WITH_REC: "/resources/images/48x48_green_rec.png",
};

#CONSTANTS = {
PAGE_DATA_ICON_CONFIG: "extensionIconConfig",
};

constructor() {
chrome.tabs.onUpdated.addListener((tabId) => {
// FIXME: Can be made better by only listening to url changes on tabs
this.#updateIconState(tabId);
});
}

#getDefaultConfig(): ExtensionIconConfig {
return {
ruleExecuted: false,
isRecording: false,
};
}

#getIcon(config: ExtensionIconConfig) {
if (this.#isExtensionDisabled) {
return this.#icons.DISABLED;
}

if (config.ruleExecuted) {
if (config.isRecording) {
return this.#icons.RULE_EXECUTED_WITH_REC;
}

return this.#icons.RULE_EXECUTED;
}

if (config.isRecording) {
return this.#icons.DEFAULT_WITH_REC;
}

return this.#icons.DEFAULT;
}

#updateIconState(tabId?: number, newConfigKey?: keyof ExtensionIconConfig, newConfigValue?: boolean) {
let config = tabService.getPageData(tabId, this.#CONSTANTS.PAGE_DATA_ICON_CONFIG) || this.#getDefaultConfig();

if (newConfigKey && config[newConfigKey] !== newConfigValue) {
config = { ...config, [newConfigKey]: newConfigValue };
tabService.setPageData(tabId, this.#CONSTANTS.PAGE_DATA_ICON_CONFIG, config);
}

this.#setExtensionIcon(this.#getIcon(config), tabId);
// tabService.setExtensionIcon(this.#getIcon(config), tabId);
}

#updateIconStateForAllTabs() {
const tabsMap = tabService.getTabs();
const tabsWithIconConfig = Object.values(tabsMap).filter((tab) => {
if (tab && tab.pageData) {
return !!tab.pageData[this.#CONSTANTS.PAGE_DATA_ICON_CONFIG];
}
return false;
});
tabsWithIconConfig.forEach((tab) => this.#updateIconState(tab.id));
}

#setExtensionIcon(path: string, tabId?: number) {
if (tabId === undefined) {
chrome.action.setIcon({ path });
} else {
chrome.action.setIcon({ path, tabId });
}
}

markExtensionEnabled = () => {
this.#isExtensionDisabled = false;
this.#setExtensionIcon(this.#icons.DEFAULT);
// tabService.setExtensionIcon(this.#icons.DEFAULT);
this.#updateIconStateForAllTabs();
};

markExtensionDisabled = () => {
this.#isExtensionDisabled = true;
this.#setExtensionIcon(this.#icons.DISABLED);
// tabService.setExtensionIcon(this.#icons.DISABLED);
this.#updateIconStateForAllTabs();
};

markRuleExecuted(tabId: number) {
this.#updateIconState(tabId, "ruleExecuted", true);
}

markRecording(tabId: number) {
this.#updateIconState(tabId, "isRecording", true);
}

markNotRecording(tabId: number) {
this.#updateIconState(tabId, "isRecording", false);
}
}

const extensionIconManager = new ExtensionIconManager();

export default extensionIconManager;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CLIENT_MESSAGES, EXTENSION_MESSAGES } from "common/constants";
import { checkIfNoRulesPresent, getRulesAndGroups } from "common/rulesStore";
import { initClientHandler } from "./clientHandler";
import { getAppTabs, isExtensionEnabled, toggleExtensionStatus } from "./utils";
import { getExecutedRules, handleRuleExecutionsOnClientPageLoad } from "./rulesManager";
// import { handleRuleExecutionsOnClientPageLoad } from "./rulesManager";
import { applyScriptRules } from "./scriptRuleHandler";
import {
cacheRecordedSessionOnClientPageUnload,
Expand All @@ -24,6 +24,7 @@ import {
launchUrlAndStartRuleTesting,
saveTestRuleResult,
} from "./testThisRuleHandler";
import ruleExecutionHandler from "./ruleExecutionHandler";

// TODO: relay this message from content script to app, so UI could be updated immediately
export const sendMessageToApp = (messageObject: unknown, callback?: () => void) => {
Expand Down Expand Up @@ -52,7 +53,7 @@ export const initMessageHandler = () => {
break;

case EXTENSION_MESSAGES.CLIENT_PAGE_LOADED:
handleRuleExecutionsOnClientPageLoad(sender.tab.id);
ruleExecutionHandler.processTabCachedRulesExecutions(sender.tab.id);
handleTestRuleOnClientPageLoad(sender.tab);
handleSessionRecordingOnClientPageLoad(sender.tab, sender.frameId);
break;
Expand Down Expand Up @@ -94,7 +95,7 @@ export const initMessageHandler = () => {
return true;

case EXTENSION_MESSAGES.GET_EXECUTED_RULES:
getExecutedRules(message.tabId).then(sendResponse);
ruleExecutionHandler.getExecutedRules(message.tabId).then(sendResponse);
return true;

case EXTENSION_MESSAGES.CHECK_IF_NO_RULES_PRESENT:
Expand Down Expand Up @@ -128,6 +129,11 @@ export const initMessageHandler = () => {
case EXTENSION_MESSAGES.SAVE_TEST_RULE_RESULT:
saveTestRuleResult(message, sender.tab);
break;

case EXTENSION_MESSAGES.RULE_EXECUTED:
const requestDetails = { ...message.requestDetails, tabId: message.requestDetails?.tabId || sender.tab?.id };
ruleExecutionHandler.onRuleExecuted(message.rule, requestDetails);
break;
}

return false;
Expand Down

0 comments on commit db1904d

Please sign in to comment.