Merge branch 'v1.30-refactor' into development

This commit is contained in:
Ilya Ig. Petrov 2019-04-04 17:28:59 +00:00
commit 817fa5abe0

View File

@ -58,7 +58,7 @@
}; };
const setPacAsync = function setPacAsync( const setPacAsync = function setPacAsync(
pacData = mandatory(), cb = throwIfError pacData = mandatory(), cb = throwIfError,
) { ) {
const config = { const config = {
@ -80,7 +80,7 @@
console.warn('Failed, other extension is in control.'); console.warn('Failed, other extension is in control.');
return cb( return cb(
new Error( window.utils.messages.whichExtensionHtml() ) new Error( window.utils.messages.whichExtensionHtml() ),
); );
} }
@ -99,20 +99,20 @@
cb = asyncLogGroup( cb = asyncLogGroup(
'Getting IPs for PAC hosts...', 'Getting IPs for PAC hosts...',
cb cb,
); );
window.utils.fireRequest('ip-to-host-update-all', cb); window.utils.fireRequest('ip-to-host-update-all', cb);
}; };
const setPacScriptFromProviderAsync = function setPacScriptFromProviderAsync( const setPacScriptFromProviderAsync = function setPacScriptFromProviderAsync(
provider, lastModifiedStr = mandatory(), cb = throwIfError provider, lastModifiedStr = mandatory(), cb = throwIfError,
) { ) {
const pacUrl = provider.pacUrls[0]; const pacUrl = provider.pacUrls[0];
cb = asyncLogGroup( cb = asyncLogGroup(
'Getting PAC script from provider...', pacUrl, 'Getting PAC script from provider...', pacUrl,
cb cb,
); );
const warnings = []; const warnings = [];
@ -127,9 +127,8 @@
addWarning( addWarning(
\` \`
Не найдено СВОИХ прокси. Этот PAC-скрипт Не найдено СВОИХ прокси. Этот PAC-скрипт
<a href="https://github.com/anticensority/runet-censorship-bypass/issues/10#issuecomment-387436191">теперь</a>
работает только со <a href="https://git.io/ac-own-proxy">СВОИМИ прокси</a> работает только со <a href="https://git.io/ac-own-proxy">СВОИМИ прокси</a>
(по умолчанию используется локальный <a href="https://git.io/ac-tor">TOR</a> и прокси "Антизапрет", для их отключения: Свои прокси -> откл. "Использовать прокси PAC-скрипта"). (по умолчанию будет использоваться локальный <a href="https://git.io/ac-tor">Tor</a>).
\`, \`,
); );
} }
@ -139,15 +138,14 @@
httpLib.ifModifiedSince(pacUrl, lastModifiedStr, (err, newLastModifiedStr) => { httpLib.ifModifiedSince(pacUrl, lastModifiedStr, (err, newLastModifiedStr) => {
if (!newLastModifiedStr) { if (!newLastModifiedStr) {
const res = {lastModified: lastModifiedStr};
const ifWasEverModified = lastModifiedStr !== new Date(0).toUTCString(); const ifWasEverModified = lastModifiedStr !== new Date(0).toUTCString();
if (ifWasEverModified) { if (ifWasEverModified) {
addWarning( addWarning(
'Ваш PAC-скрипт не нуждается в обновлении. Его дата: ' + 'Ваш PAC-скрипт не нуждается в обновлении. Его дата: ' +
lastModifiedStr lastModifiedStr,
); );
const res = {lastModified: lastModifiedStr};
return cb(null, res); return cb(null, res);
} }
} }
@ -158,11 +156,11 @@
() => new Promise( () => new Promise(
(resolve, reject) => httpLib.get( (resolve, reject) => httpLib.get(
url, url,
(newErr, pacData) => newErr ? reject(newErr) : resolve(pacData) (newErr, pacData) => newErr ? reject(newErr) : resolve(pacData),
) ),
) ),
), ),
Promise.reject() Promise.reject(),
); );
pacDataPromise.then( pacDataPromise.then(
@ -174,7 +172,7 @@
(err, res) => cb( (err, res) => cb(
err, err,
Object.assign(res || {}, {lastModified: newLastModifiedStr}), Object.assign(res || {}, {lastModified: newLastModifiedStr}),
) ),
); );
}, },
@ -231,7 +229,7 @@
order: 99, order: 99,
pacUrls: [ pacUrls: [
'data:application/x-ns-proxy-autoconfig,' + escape('function FindProxyForURL(){ return "DIRECT"; }'), 'data:application/x-ns-proxy-autoconfig,' + escape('function FindProxyForURL(){ return "DIRECT"; }'),
] ],
} }
}, },
@ -247,6 +245,12 @@
Do something, e.g. initiate PAC sync. Do something, e.g. initiate PAC sync.
*/ */
ifFirstInstall: false, ifFirstInstall: false,
/* We have .lastPacUpdateStamp and ._currentPacProviderLastModified.
LastModified is received from a server, we kind of don't trust it,
just use it for cache and maybe show to the user.
UpdateStamp is got from client and we base our timers on it,
malicious server can't interfere with it.
*/
lastPacUpdateStamp: 0, lastPacUpdateStamp: 0,
setTitle() { setTitle() {
@ -259,7 +263,7 @@
}, },
_currentPacProviderLastModified: 0, // Not initialized. _currentPacProviderLastModified: 0,
getLastModifiedForKey(key = mandatory()) { getLastModifiedForKey(key = mandatory()) {
@ -292,7 +296,7 @@
setCurrentPacProviderKey( setCurrentPacProviderKey(
newKey = mandatory(), newKey = mandatory(),
lastModified = new Date().toUTCString() lastModified = new Date().toUTCString(),
) { ) {
this.mustBeKey(newKey); this.mustBeKey(newKey);
@ -332,7 +336,7 @@
chrome.storage.local.clear( chrome.storage.local.clear(
() => chrome.storage.local.set( () => chrome.storage.local.set(
onlySettable, onlySettable,
chromified(cb) chromified(cb),
) )
); );
@ -376,8 +380,8 @@
const ipsErrorPromise = new Promise( const ipsErrorPromise = new Promise(
(resolve, reject) => updatePacProxyIps( (resolve, reject) => updatePacProxyIps(
resolve resolve,
) ),
); );
Promise.all([pacSetPromise, ipsErrorPromise]).then( Promise.all([pacSetPromise, ipsErrorPromise]).then(
@ -391,7 +395,7 @@
warns.push(ipsErr); warns.push(ipsErr);
} }
this.pushToStorageAsync( this.pushToStorageAsync(
(pushErr) => cb(pacErr || pushErr, null, ...warns) (pushErr) => cb(pacErr || pushErr, null, ...warns),
); );
}, },
@ -418,7 +422,7 @@
console.log( console.log(
'Next PAC update is scheduled on', 'Next PAC update is scheduled on',
new Date(nextUpdateMoment).toLocaleString('ru-RU') new Date(nextUpdateMoment).toLocaleString('ru-RU'),
); );
chrome.alarms.create( chrome.alarms.create(
@ -461,11 +465,11 @@
} }
this.setCurrentPacProviderKey(null); this.setCurrentPacProviderKey(null);
this.pushToStorageAsync( this.pushToStorageAsync(
() => handlers.updateControlState(cb) () => handlers.updateControlState(cb),
); );
}) }),
) ),
); );
}, },
@ -493,7 +497,7 @@
if (alarm.name === antiCensorRu._periodicUpdateAlarmReason) { if (alarm.name === antiCensorRu._periodicUpdateAlarmReason) {
console.log( console.log(
'Periodic PAC update triggered:', 'Periodic PAC update triggered:',
new Date().toLocaleString('ru-RU') new Date().toLocaleString('ru-RU'),
); );
antiCensorRu.syncWithPacProviderAsync(() => {/* swallow */}); antiCensorRu.syncWithPacProviderAsync(() => {/* swallow */});
} }
@ -533,7 +537,7 @@
|| antiCensorRu._currentPacProviderLastModified; || antiCensorRu._currentPacProviderLastModified;
console.log( console.log(
'Last PAC update was on', 'Last PAC update was on',
new Date(antiCensorRu.lastPacUpdateStamp).toLocaleString('ru-RU') new Date(antiCensorRu.lastPacUpdateStamp).toLocaleString('ru-RU'),
); );