From 699bef6fea1d129dbe2bc6fa81c1932c9278b8ae Mon Sep 17 00:00:00 2001 From: ilyaigpetrov Date: Sat, 22 May 2021 17:38:32 +0000 Subject: [PATCH 1/3] Add retries (not tested) --- ...c-pac-script-with-pac-provider-api.tmpl.js | 69 +++++++++++++++---- .../src/extension-full/20-ip-to-host-api.js | 2 +- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js index 1ffc43b..92b42d1 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js @@ -60,7 +60,38 @@ }; + const tryPromiseSeveralTimesAsync = (createPromise, timeoutsInSec) => + createPromise().then( + (res) => Promise.resolve(res), + (err) => { + console.log('Promise failed, are there any retries?'); + const outSec = timeoutsInSec.shift(); + if (outSec === undefined) { + console.log('No retries left.'); + return Promise.reject(err); + } + console.log('Retrying in', outSec, 'sec'); + /* + const alarmName = 'try-promise=several-times-async'; + const res = new Promise((resolve) => { + chrome.alarms.onAlarm.addListener((alarmInfo) => { + if (alarmInfo.name === alarmName) { + console.log('Time to retry.'); + resolve(tryPromiseSeveralTimesAsync(createPromise, timeoutsInSec)); + } + }); + }); + chrome.alarms.create(alarmName, { delayInMinutes: outSec/60 }); + return res; + */ + return new Promise((resolve) => + window.setTimeout(() => resolve(tryPromiseSeveralTimesAsync(createPromise, timeoutsInSec)), outSec*1000), + ); + }, + ); + const doWithoutProxyAsync = (createPromise) => new Promise((resolve, reject) => { + console.log('Doing without proxy...'); chrome.proxy.settings.get({}, chromified((getErr, settings) => { if (getErr) { reject(getErr); @@ -147,7 +178,7 @@ }; const setPacScriptFromProviderAsync = function setPacScriptFromProviderAsync( - provider, lastModifiedStr = mandatory(), cb = throwIfError, + provider, lastModifiedStr, ifUnattended = mandatory(), cb = throwIfError, ) { const pacUrl = provider.pacUrls[0]; @@ -182,11 +213,10 @@ } - console.log('Doing without proxy...'); - const pacDataPromise = doWithoutProxyAsync( + doWithoutProxyAsync( // Employ all urls, the latter are fallbacks for the former. - () => - provider.pacUrls.reduce( + () => { + const tryAllUrlsAsync = () => provider.pacUrls.reduce( (promise, url) => promise.catch( () => new Promise( (resolve, reject) => httpLib.get( @@ -197,13 +227,18 @@ ), ), Promise.reject(), + ); + return (ifUnattended + ? tryPromiseSeveralTimesAsync(tryAllUrlsAsync, [10, 20, 30]) + : tryAllUrlsAsync() ).catch( (err) => Promise.reject(clarify( err, chrome.i18n.getMessage('FailedToDownloadPacScriptFromAddresses') + ': [ ' + provider.pacUrls.join(' , ') + ' ].', )), - ), + ); + }, ).then( (pacData) => { setPacAsync( @@ -240,7 +275,7 @@
Comparison of PAC-scripts (ru). \`, order: 0, - pacUrls: ['https://antizapret.prostovpn.org/proxy.pac'], + pacUrls: ['https://AAAAAAAAAantizapret.prostovpn.org/proxy.pac'], }, Антицензорити: { distinctKey: 'Anticensority', @@ -390,12 +425,13 @@ }, - syncWithPacProviderAsync( - key = this.currentPacProvierKey, cb = throwIfError) { - if( typeof(key) === 'function' ) { - cb = key; - key = this.getCurrentPacProviderKey(); + syncWithPacProviderAsync(opts = {}, cb = throwIfError) { + const optsDefaults = Object.freeze({ key: this.getCurrentPacProviderKey(), ifUnattended: false }); + if( typeof(opts) === 'function' ) { + cb = opts; + opts = {}; } + let { key, ifUnattended } = { ...optsDefaults, ...opts }; cb = asyncLogGroup('Syncing with PAC provider ' + key + '...', cb); if (key === null) { @@ -405,10 +441,13 @@ const pacProvider = this.getPacProvider(key); + ifUnattended = true; // TODO: + const pacSetPromise = new Promise( (resolve, reject) => setPacScriptFromProviderAsync( pacProvider, this.getLastModifiedForKey(key), + ifUnattended, (err, res, ...warns) => { if (!err) { @@ -425,12 +464,16 @@ ) ); - const ipsErrorPromise = new Promise( + const updateIpsAsync = () => new Promise( (resolve, reject) => updatePacProxyIps( resolve, ), ); + const ipsErrorPromise = !ifUnattended + ? updateIpsAsync() + : tryPromiseSeveralTimesAsync(updateIpsAsync, [10, 20, 30]); + Promise.all([pacSetPromise, ipsErrorPromise]).then( ([[pacErr, pacRes, ...pacWarns], ipsErr]) => { diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js b/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js index 6da95c8..861dd38 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js @@ -112,7 +112,7 @@ const promises = types.map( (type) => new Promise((resolve) => httpLib.get( - `https://dns.google.com/resolve?type=${type}&name=${host}&random_padding=${generateRandomHexString(30,500)}`, + `https://AAAAAAAAAdns.google.com/resolve?type=${type}&name=${host}&random_padding=${generateRandomHexString(30,500)}`, (err, res) => { if (res) { From b5154f6453ecc53f8e0cd99f5d547ea5a17db530 Mon Sep 17 00:00:00 2001 From: ilyaigpetrov Date: Mon, 24 May 2021 17:30:43 +0000 Subject: [PATCH 2/3] Add retries (finished, test0) --- ...nc-pac-script-with-pac-provider-api.tmpl.js | 18 +++++++++++------- .../src/extension-full/20-ip-to-host-api.js | 6 ++++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js index 92b42d1..e7a2ff1 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js @@ -229,7 +229,7 @@ Promise.reject(), ); return (ifUnattended - ? tryPromiseSeveralTimesAsync(tryAllUrlsAsync, [10, 20, 30]) + ? tryPromiseSeveralTimesAsync(tryAllUrlsAsync, [20, 40, 60]) : tryAllUrlsAsync() ).catch( (err) => Promise.reject(clarify( @@ -275,7 +275,7 @@
Comparison of PAC-scripts (ru). \`, order: 0, - pacUrls: ['https://AAAAAAAAAantizapret.prostovpn.org/proxy.pac'], + pacUrls: ['https://antizapret.prostovpn.org/proxy.pac'], }, Антицензорити: { distinctKey: 'Anticensority', @@ -441,8 +441,6 @@ const pacProvider = this.getPacProvider(key); - ifUnattended = true; // TODO: - const pacSetPromise = new Promise( (resolve, reject) => setPacScriptFromProviderAsync( pacProvider, @@ -466,13 +464,19 @@ const updateIpsAsync = () => new Promise( (resolve, reject) => updatePacProxyIps( - resolve, + (err, res, ...warns) => { + if (err) { + reject([err, ...warns]); + return; + } + resolve(err, res, ...warns); + }, ), ); const ipsErrorPromise = !ifUnattended ? updateIpsAsync() - : tryPromiseSeveralTimesAsync(updateIpsAsync, [10, 20, 30]); + : tryPromiseSeveralTimesAsync(updateIpsAsync, [20, 40, 60]); Promise.all([pacSetPromise, ipsErrorPromise]).then( ([[pacErr, pacRes, ...pacWarns], ipsErr]) => { @@ -622,7 +626,7 @@ 'Periodic PAC update triggered:', new Date().toLocaleString('ru-RU'), ); - antiCensorRu.syncWithPacProviderAsync(() => { /* Swallow. */ }); + antiCensorRu.syncWithPacProviderAsync({ ifUnattended: true }, () => { /* Swallow. */ }); } }) diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js b/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js index 861dd38..0fdc4df 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js @@ -112,7 +112,7 @@ const promises = types.map( (type) => new Promise((resolve) => httpLib.get( - `https://AAAAAAAAAdns.google.com/resolve?type=${type}&name=${host}&random_padding=${generateRandomHexString(30,500)}`, + `https://dns.google.com/resolve?type=${type}&name=${host}&random_padding=${generateRandomHexString(30,500)}`, (err, res) => { if (res) { @@ -255,7 +255,9 @@ _updateAllAsync(cb = mandatory()) { - const hostArr = Object.keys(privates._strToHostObj); + const hostArr = Object.keys(privates._strToHostObj) + .filter((hostStr) => hostStr !== 'localhost'); + console.log('Update all:', hostArr); const promises = hostArr.map( From f43eafaf8aed6c3771080c02fc04d2fbb0fcfe61 Mon Sep 17 00:00:00 2001 From: ilyaigpetrov Date: Wed, 26 May 2021 16:43:28 +0000 Subject: [PATCH 3/3] Fix retries bug (test4) Change args scheme in calls of the modified function --- .../37-sync-pac-script-with-pac-provider-api.tmpl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js index e7a2ff1..513abb4 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.tmpl.js @@ -539,7 +539,7 @@ throw new Error('Key must be defined.'); } if (this.currentProviderKey !== key) { - return this.syncWithPacProviderAsync(key, cb); + return this.syncWithPacProviderAsync({ key }, cb); } console.log(key + ' already installed.'); cb();