From 736ed796ae883c664be2d74aec0e86bbd6f0779b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Luba=C5=84ski?= Date: Tue, 17 Dec 2024 15:15:22 +0100 Subject: [PATCH] Use real end-point from CDN --- src/background/config.js | 119 ++++++++++------------ src/pages/settings/components/devtools.js | 41 +++++--- src/utils/api.js | 5 + src/utils/engines.js | 10 +- 4 files changed, 84 insertions(+), 91 deletions(-) diff --git a/src/background/config.js b/src/background/config.js index 6c7ecfbac..ea54c3bfe 100644 --- a/src/background/config.js +++ b/src/background/config.js @@ -12,6 +12,9 @@ import { store } from 'hybrids'; import Config from '/store/config.js'; +import { CDN_URL } from '/utils/api.js'; + +const CONFIG_URL = CDN_URL + 'configs/v1.json'; function filter(item) { if (item.filter) { @@ -34,81 +37,61 @@ function filter(item) { (async function syncRemoteConfig() { // TODO: implement fetching remote config from the server // This is a mock of the fetched config - const fetchedConfig = { - 'domains': { - 'ghostery.com': { - 'actions': ['assist'], - 'filter': { - 'platform': ['chromium'], - }, - }, - 'consent.google.pl': { - 'actions': ['disable-autoconsent'], - }, - }, - 'flags': { - 'assist': [ - { - percentage: 100, - }, - ], - 'firefox-content-script-scriptlets': [ - { - 'percentage': 20, - 'filter': { - 'platform': ['firefox'], - }, - }, - ], - }, - }; - - const config = await store.resolve(Config); - - // -- domains -- - - const domains = { ...config.domains }; - - // Clear out domains removed from the config - for (const name of Object.keys(domains)) { - if (fetchedConfig.domains[name] === undefined) { - domains[name] = null; + try { + const fetchedConfig = await fetch(CONFIG_URL).then((res) => { + if (!res.ok) throw new Error('Failed to fetch the remote config'); + return res.json(); + }); + + const config = await store.resolve(Config); + + // -- domains -- + + const domains = { ...config.domains }; + + // Clear out domains removed from the config + for (const name of Object.keys(domains)) { + if (fetchedConfig.domains[name] === undefined) { + domains[name] = null; + } } - } - // Update the config with new values - for (const [name, item] of Object.entries(fetchedConfig.domains)) { - domains[name] = filter(item) ? item : null; - } + // Update the config with new values + for (const [name, item] of Object.entries(fetchedConfig.domains)) { + domains[name] = filter(item) ? item : null; + } - // -- flags -- + // -- flags -- - const flags = { ...config.flags }; + const flags = { ...config.flags }; - // Clear out flags removed from the config - for (const name of Object.keys(flags)) { - if (fetchedConfig.flags[name] === undefined) { - flags[name] = null; + // Clear out flags removed from the config + for (const name of Object.keys(flags)) { + if (fetchedConfig.flags[name] === undefined) { + flags[name] = null; + } } - } - // Update the config with the new values - for (const [name, items] of Object.entries(fetchedConfig.flags)) { - const item = items.find((item) => filter(item)); - if (!item) { - flags[name] = null; - continue; + // Update the config with the new values + for (const [name, items] of Object.entries(fetchedConfig.flags)) { + const item = items.find((item) => filter(item)); + if (!item) { + flags[name] = null; + continue; + } + // Generate local percentage only once for each flag + const percentage = + flags[name]?.percentage || Math.floor(Math.random() * 100) + 1; + + flags[name] = { + percentage, + enabled: percentage <= item.percentage, + }; } - // Generate local percentage only once for each flag - const percentage = - flags[name]?.percentage || Math.floor(Math.random() * 100) + 1; - - flags[name] = { - percentage, - enabled: percentage <= item.percentage, - }; - } - // Update the config - store.set(Config, { domains, flags }); + // Update the config + store.set(Config, { domains, flags }); + } catch (e) { + console.error('[config] Failed to sync remote config:', e); + } })(); diff --git a/src/pages/settings/components/devtools.js b/src/pages/settings/components/devtools.js index 410c2ebfe..77a7d85c8 100644 --- a/src/pages/settings/components/devtools.js +++ b/src/pages/settings/components/devtools.js @@ -84,21 +84,31 @@ export default {
Developer tools + ${store.ready(config) && + html` +
+ Remote Config + + + Remote configuration global switch + + + + +
+ Config +
${JSON.stringify(config, null, 2)}
+
+
+
+ + `} +
- Storage - ${store.ready(config) && - html` -
- - - Remote configuration - - -
- `} + Local Storage
@@ -109,7 +119,7 @@ export default { ${(__PLATFORM__ === 'chromium' || __PLATFORM__ === 'safari') && html`
- Enabled DNR rulesets + Enabled DNR rulesets The below list is not reactive to changes made in the extension - use refresh button @@ -133,7 +143,6 @@ export default {
- `}
` diff --git a/src/utils/api.js b/src/utils/api.js index 7875ef7b9..4b71dd3dd 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -12,10 +12,15 @@ import { jwtDecode } from 'jwt-decode'; import { GHOSTERY_DOMAIN } from '/utils/urls.js'; +import { stagingMode } from './debug.js'; const AUTH_URL = `https://consumerapi.${GHOSTERY_DOMAIN}/api/v2`; const ACCOUNT_URL = `https://accountapi.${GHOSTERY_DOMAIN}/api/v2.1.0`; +export const CDN_URL = stagingMode + ? 'https://staging-cdn.ghostery.com/' + : 'https://cdn.ghostery.com/'; + export const COOKIE_DOMAIN = `.${GHOSTERY_DOMAIN}`; const COOKIE_URL = `https://${GHOSTERY_DOMAIN}`; const COOKIE_DURATION = 60 * 60 * 24 * 90; // 90 days in seconds diff --git a/src/utils/engines.js b/src/utils/engines.js index 0d2f97baf..708a8eb56 100644 --- a/src/utils/engines.js +++ b/src/utils/engines.js @@ -19,8 +19,9 @@ import { } from '@ghostery/adblocker'; import { registerDatabase } from './indexeddb.js'; -import debug, { stagingMode } from './debug.js'; +import debug from './debug.js'; import { captureException } from './errors.js'; +import { CDN_URL } from './api.js'; export const MAIN_ENGINE = 'main'; export const CUSTOM_ENGINE = 'custom-filters'; @@ -204,10 +205,6 @@ function check(response) { return response; } -const CDN_HOSTNAME = stagingMode - ? 'staging-cdn.ghostery.com' - : 'cdn.ghostery.com'; - export async function update(name) { // If the IndexedDB is corrupted, and there is no way to load the engine // from the storage, we should skip the update. @@ -222,8 +219,7 @@ export async function update(name) { try { const urlName = name === 'trackerdb' ? 'trackerdbMv3' : `dnr-${name}`; - - const listURL = `https://${CDN_HOSTNAME}/adblocker/configs/${urlName}/allowed-lists.json`; + const listURL = CDN_URL + `adblocker/configs/${urlName}/allowed-lists.json`; console.info(`[engines] Updating engine "${name}"...`);