Skip to content

Commit

Permalink
v8.8 sync
Browse files Browse the repository at this point in the history
  • Loading branch information
erosman authored Dec 22, 2023
1 parent 4e2ae55 commit f490c3a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 56 deletions.
1 change: 1 addition & 0 deletions src/content/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h1 id="changelog">Changelog</h1>
<dt>8.8</dt>
<dd>Added Show hidden feature</dd>
<dd>Updated code to process duplicate hostname:port (#33, #76)</dd>
<dd>Updated sync process (#99)</dd>
<dd>Updated user interface to hide patterns on FoxyProxy Basic</dd>

<dt>8.7</dt>
Expand Down
1 change: 1 addition & 0 deletions src/content/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class App {

// ---------- User Preferences ---------------------------
static defaultPref = JSON.stringify(pref);
static syncProperties = Object.keys(pref).filter(i => !['mode', 'sync', 'data'].includes(i));

static getDefaultPref() {
return JSON.parse(this.defaultPref);
Expand Down
18 changes: 16 additions & 2 deletions src/content/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,14 @@ <h4>Storage quotas for sync data</h4>
<li>Maximum number of items 512</li>
</ul>

<p class="note">There is a discussion to increase the storage sync quota to 1MB.<br>
See also: <a href="https://github.com/w3c/webextensions/issues/351" target="_blank">Discuss limits applied to storage.local and storage.sync API</a></p>
<div class="note">
<p>There is a discussion to increase the storage sync quota to 1MB.</p>
<p>See also:</p>
<ul>
<li><a href="https://github.com/w3c/webextensions/issues/351" target="_blank">Discuss limits applied to storage.local and storage.sync API</a></li>
<li><a href="https://github.com/w3c/webextensions/issues/510">Proposal: Increase maximum item size in Storage sync quotas</a></li>
</ul>
</div>

<h2 id="auto-backup">Auto Backup</h2>
<p>Automatically backup settings to the browser configured Downloads folder on save</p>
Expand Down Expand Up @@ -763,6 +769,14 @@ <h2>Individual Proxy</h2>

<dt>Proxy DNS <span>(Firefox only)</span></dt>
<dd>Option to pass DNS to the SOCKS proxy when using SOCKS</dd>
<dd class="note">See also:
<ul>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1741375" target="_blank">Proxy DNS by default when using SOCKS v5</a> has landed in Firefox 123</li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1799411" target="_blank">DNS leaks with proxy extension</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1791243" target="_blank">Firefox queries DNS of Proxy destination</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1867562" target="_blank">Despite the fact that DNS over HTTPS is switched off on Privacy and Security bar in Settings it still leaks my DNS</a></li>
</ul>
</dd>

<dt>PAC URL <span>(not available on Android)</span></dt>
<dd>Required for PAC type</dd>
Expand Down
10 changes: 9 additions & 1 deletion src/content/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ import {CryptoJS} from '../lib/aes.3.1.2.js';
export class Migrate {

static async init(pref) {
// --- 8.8
// tidy up left-over obj from 8.0 Sync typo mistake
if (Object.hasOwn(pref, 'obj')) {
delete pref.obj;
await browser.storage.local.remove('obj');
await browser.storage.sync.remove('obj');
}

// --- 8.7
// change global proxyDNS to per-proxy
if (Object.hasOwn(pref, 'proxyDNS') && pref.data) {
Expand All @@ -53,7 +61,7 @@ export class Migrate {
if (Object.hasOwn(pref, 'globalExcludeWildcard')) {
delete pref.globalExcludeWildcard; // from 8.0, removed in 8.1
delete pref.globalExcludeRegex; // from 8.0, removed in 8.1
delete pref.obj; // 8.0 Sync typo error
delete pref.obj; // 8.0 Sync typo mistake
await browser.storage.local.remove(['globalExcludeWildcard', 'globalExcludeRegex', 'obj']);
}

Expand Down
3 changes: 0 additions & 3 deletions src/content/on-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class OnRequest {
this.passthrough = []; // RegExp string
this.net = []; // [start, end] strings
this.showPatternProxy = false;
// this.proxyDNS = true;
this.tabProxy = {}; // tab proxy, may be lost in MV3 if bg is unloaded
this.container = {}; // incognito/container proxy

Expand All @@ -41,9 +40,7 @@ export class OnRequest {
const [passthrough, , net] = Pattern.getPassthrough(pref.passthrough);
this.passthrough = passthrough;
this.net = net;

this.showPatternProxy = pref.showPatternProxy;
// this.proxyDNS = pref.proxyDNS; // used in mode pattern or single proxy

const data = pref.data.filter(i => i.active && i.type !== 'pac' && i.hostname); // filter data

Expand Down
2 changes: 1 addition & 1 deletion src/content/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@
<option value="wildcard" data-i18n="wildcard"></option>
<option value="regex">Reg Exp</option>
</select>
<input type="text" placeholder="*://*.example.com/">
<input type="text" placeholder="://*.example.com/">

<label data-i18n="result"></label>
<span class="test-result"></span>
Expand Down
59 changes: 32 additions & 27 deletions src/content/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ class Toggle {
class Options {

static {
this.sync = document.getElementById('sync');
// this.proxyDNS = document.getElementById('proxyDNS');

// --- container
this.container = document.querySelectorAll('.options .container select');

Expand All @@ -80,8 +77,7 @@ class Options {
// --- buttons
document.querySelector('.options button[data-i18n="restoreDefaults"]').addEventListener('click', () => this.restoreDefaults());

// this.init(['sync', 'proxyDNS', 'passthrough']);
this.init(['sync', 'showPatternProxy', 'autoBackup', 'passthrough']);
this.init(['sync', 'autoBackup', 'showPatternProxy', 'passthrough']);
}

static init(keys = Object.keys(pref)) {
Expand Down Expand Up @@ -128,7 +124,6 @@ class Options {
const id = pxy.type === 'pac' ? pxy.pac : `${pxy.hostname}:${pxy.port}`;
cache[id] = pxy;
}
const dataChanged = !App.equal(pref.data, data); // check if proxies & patterns have changed

// no errors, update pref.data
pref.data = data;
Expand All @@ -145,7 +140,6 @@ class Options {
checkSelect(i);
container[i.name] = i.value;
});
// const containerChanged = Object.keys(container).some(i => container[i] !== pref.container[i]);
pref.container = container; // set to pref

// --- keyboard shortcut proxy
Expand All @@ -154,28 +148,8 @@ class Options {
checkSelect(i);
commands[i.name] = i.value;
});
// const commandsChanged = Object.keys(commands).some(i => commands[i] !== pref.commands[i]);
pref.commands = commands; // set to pref

// --- check sync
if (this.sync.checked) {
// convert array to object {...data} to avoid sync maximum item size limit
const obj = dataChanged ? {...data} : {};

pref.passthrough !== this.passthrough.value && (obj.passthrough = this.passthrough.value);
// pref.proxyDNS !== this.proxyDNS.checked && (obj.proxyDNS = this.proxyDNS.checked);

// containerChanged && (obj.container = pref.container);
// commandsChanged && (obj.commands = pref.commands);

// save changes to sync
Object.keys(obj)[0] && browser.storage.sync.set(obj)
.catch(error => {
App.notify(browser.i18n.getMessage('syncError') + '\n\n' + error.message);
this.sync.checked = false; // disabling sync option to avoid repeated errors
});
}

// --- check mode
// get from storage in case it was changed while options page has been open
let {mode} = await browser.storage.local.get({mode: 'disable'});
Expand All @@ -196,6 +170,37 @@ class Options {

// --- Auto Backup
pref.autoBackup && ImportExport.export(pref, false);

// --- Sync
this.sync(pref);
}

static sync(pref) {
if (!pref.sync) { return; }

// convert array to object {...data} to avoid sync maximum item size limit
const obj = {...pref.data};

// add other sync properties
App.syncProperties.forEach(i => obj[i] = pref[i]);

// save changes to sync
browser.storage.sync.set(obj)
.then(() => {
// delete left-over proxies
browser.storage.sync.get()
.then(syncObj => {
// get & delete numerical keys that are equal or larger than data length, the rest are overwritten
const del = Object.keys(syncObj).filter(i => /^\d+$/.test(i) && i*1 >= pref.data.length);
del[0] && browser.storage.sync.remove(del);
});
})
.catch(error => {
App.notify(browser.i18n.getMessage('syncError') + '\n\n' + error.message);
// disabling sync option to avoid repeated errors
document.getElementById('sync').checked = false;
browser.storage.local.set({sync: false});
});
}

static async getProxyDetails(elem) {
Expand Down
30 changes: 8 additions & 22 deletions src/content/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,20 @@ import {App} from './app.js';
// ---------- Storage Sync ---------------------------------
export class Sync {

static props = ['proxyDNS', 'passthrough'];

static init(pref) {
if (pref.managed) { return; } // not for storage.managed

browser.storage.sync.onChanged.addListener(e => this.onChanged(e));
// Chrome 73, Firefox 101
// browser.storage.sync.onChanged.addListener(e => this.onChanged(e));
// Firefox 45
browser.storage.onChanged.addListener((...e) => this.onChanged(...e));
}

static async onChanged(changes) {
// no newValue on storage.local.clear()
if (!Object.hasOwn(Object.values(changes)[0] || {}, 'newValue')) { return; }
static async onChanged(changes, area) {
if (area !== 'sync') { return; }

const pref = await browser.storage.local.get();
if (!pref.sync) { return; }

// convert object to array + filter null newValue (deleted) + map to newValue
const data = Object.values(changes)
.filter(i => Object.hasOwn(i.newValue || {}, 'hostname'))
.map(i => i.newValue);

const obj = {};
data[0] && !App.equal(pref.data, data) && (obj.data = data);

this.props.forEach(item => {
Object.hasOwn(changes, item) && (obj[item] = changes[item].newValue);
});

Object.keys(obj)[0] && browser.storage.local.set(obj); // update local storage
this.getSync(pref);
}

static async get(pref) {
Expand Down Expand Up @@ -87,7 +73,7 @@ export class Sync {
pref.data = data;
}

this.props.forEach(item => {
App.syncProperties.forEach(item => {
if (Object.hasOwn(syncPref, item)) {
obj[item] = syncPref[item];
pref[item] = syncPref[item];
Expand Down

0 comments on commit f490c3a

Please sign in to comment.