Skip to content

Commit

Permalink
Use real end-point from CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed Dec 17, 2024
1 parent bd075c8 commit 736ed79
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 91 deletions.
119 changes: 51 additions & 68 deletions src/background/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
})();
41 changes: 25 additions & 16 deletions src/pages/settings/components/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,31 @@ export default {
<section layout="column gap:3" translate="no">
<ui-text type="headline-m">Developer tools</ui-text>
${store.ready(config) &&
html`
<div layout="column gap items:start" translate="no">
<ui-text type="headline-xs">Remote Config</ui-text>
<ui-toggle
value="${config.enabled}"
onchange="${html.set(config, 'enabled')}"
>
<ui-text layout="row items:center">
Remote configuration global switch
</ui-text>
</ui-toggle>
<ui-text>
<details>
<summary>Config</summary>
<pre>${JSON.stringify(config, null, 2)}</pre>
</details>
</ui-text>
</div>
<ui-line></ui-line>
`}
<div layout="column gap">
<ui-text type="headline-s">Storage</ui-text>
${store.ready(config) &&
html`
<div layout="column gap items:start" translate="no">
<ui-toggle
value="${config.enabled}"
onchange="${html.set(config, 'enabled')}"
>
<ui-text layout="row items:center">
Remote configuration
</ui-text>
</ui-toggle>
</div>
`}
<ui-text type="headline-xs">Local Storage</ui-text>
<div layout="row gap items:start">
<ui-button onclick="${clearStorage}" layout="shrink:0">
<button>Clear local storage</button>
Expand All @@ -109,7 +119,7 @@ export default {
${(__PLATFORM__ === 'chromium' || __PLATFORM__ === 'safari') &&
html`
<div layout="column gap items:start" translate="no">
<ui-text type="headline-s">Enabled DNR rulesets</ui-text>
<ui-text type="headline-xs">Enabled DNR rulesets</ui-text>
<ui-text type="body-xs" color="gray-400">
The below list is not reactive to changes made in the
extension - use refresh button
Expand All @@ -133,7 +143,6 @@ export default {
<button>Refresh</button>
</ui-button>
</div>
<ui-line></ui-line>
`}
</section>
`
Expand Down
5 changes: 5 additions & 0 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions src/utils/engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand All @@ -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}"...`);

Expand Down

0 comments on commit 736ed79

Please sign in to comment.