feat: spoof the rewards app headers

This commit is contained in:
Mally Dang
2026-09-02 12:54:14 +07:00
parent 0a3ab5ba6f
commit afccf81f8f
+23 -6
View File
@@ -8,7 +8,11 @@ import rewards_tasks
from selenium import webdriver from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException from selenium.common.exceptions import SessionNotCreatedException
HEADLESS = os.environ.get("REWARDS_HEADLESS", "").strip().lower() in ("1", "true", "yes") HEADLESS = os.environ.get("REWARDS_HEADLESS", "").strip().lower() in (
"1",
"true",
"yes",
)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -17,7 +21,7 @@ def build_options(account: accounts.Account) -> webdriver.EdgeOptions:
options = webdriver.EdgeOptions() options = webdriver.EdgeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False) options.add_experimental_option("useAutomationExtension", False)
options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument(f"--user-data-dir={account.user_data_dir}") options.add_argument(f"--user-data-dir={account.user_data_dir}")
options.add_argument(f"--profile-directory={account.profile_name}") options.add_argument(f"--profile-directory={account.profile_name}")
@@ -38,6 +42,14 @@ def run_account(account: accounts.Account) -> bool:
"""Work one account. Returns whether the browser started.""" """Work one account. Returns whether the browser started."""
try: try:
driver = webdriver.Edge(options=build_options(account)) driver = webdriver.Edge(options=build_options(account))
# Set headers to spoof the rewards app for the rewards only quests
driver.execute_cdp_cmd("Network.enable", {})
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36 Edg/151.0.0.0 MSRewards/Desktop/1.1.0",
"X-Rewards-Source": "msrewards-desktop",
}
driver.execute_cdp_cmd("Network.setExtraHTTPHeaders", {"headers": headers})
except SessionNotCreatedException as exc: except SessionNotCreatedException as exc:
# Chromium allows one process per user data directory. When the profile # Chromium allows one process per user data directory. When the profile
# is already open the driver's copy exits during startup, and selenium # is already open the driver's copy exits during startup, and selenium
@@ -45,7 +57,9 @@ def run_account(account: accounts.Account) -> bool:
# the profile nor the other window. # the profile nor the other window.
logger.error("[FAIL] %s: could not start Edge with this profile.", account.name) logger.error("[FAIL] %s: could not start Edge with this profile.", account.name)
logger.error(" profile directory: %s", account.user_data_dir) logger.error(" profile directory: %s", account.user_data_dir)
logger.error(" The usual cause is that this profile is already open in another") logger.error(
" The usual cause is that this profile is already open in another"
)
logger.error(" Edge window, including one left over from a previous run.") logger.error(" Edge window, including one left over from a previous run.")
logger.error(" driver said: %s", log_utils.exception_summary(exc)) logger.error(" driver said: %s", log_utils.exception_summary(exc))
@@ -63,7 +77,8 @@ def run_account(account: accounts.Account) -> bool:
# own error, and the process it is meant to end is dead anyway. # own error, and the process it is meant to end is dead anyway.
logger.warning( logger.warning(
"%s: the driver did not shut down cleanly: %s", "%s: the driver did not shut down cleanly: %s",
account.name, log_utils.exception_summary(exc) account.name,
log_utils.exception_summary(exc),
) )
return True return True
@@ -97,8 +112,10 @@ def main() -> int:
except Exception as exc: except Exception as exc:
logger.error( logger.error(
"[FAIL] %s: %s: %s", "[FAIL] %s: %s: %s",
account.name, type(exc).__name__, log_utils.exception_summary(exc), account.name,
exc_info=logger.isEnabledFor(logging.DEBUG) type(exc).__name__,
log_utils.exception_summary(exc),
exc_info=logger.isEnabledFor(logging.DEBUG),
) )
if len(configured) > 1: if len(configured) > 1: