Don't purge ips on changing exceptions, decrease custom porxies' priority, fix exceptions UI, fix GoogleDNS

This commit is contained in:
Ilya Ig. Petrov 2017-03-02 08:00:19 +00:00
parent c997689c30
commit f22a3468b2
5 changed files with 130 additions and 118 deletions

View File

@ -2,7 +2,7 @@
{ {
const IF_DEBUG = true; const IF_DEBUG = false;
if (!IF_DEBUG) { if (!IF_DEBUG) {
// I believe logging objects precludes them from being GCed. // I believe logging objects precludes them from being GCed.

View File

@ -76,8 +76,7 @@
const getCurrentConfigs = function getCurrentConfigs() { const getCurrentConfigs = function getCurrentConfigs() {
const mods = kitchenState(modsKey); return kitchenState(modsKey) || getDefaults();
return new PacModifiers(mods || {});
}; };
@ -98,68 +97,66 @@
}; };
class PacModifiers { const createPacModifiers = function createPacModifiers(mods = {}) {
constructor(mods = {}) { const defaults = getDefaults();
const ifAllDefaults = Object.keys(defaults)
.every(
(prop) => !(prop in mods)
|| Boolean(defaults[prop]) === Boolean(mods[prop])
);
const defaults = getDefaults(); console.log('Input mods:', mods);
const ifAllDefaults = Object.keys(defaults) const self = {};
.every( Object.assign(self, defaults, mods);
(prop) => !(prop in mods) self.ifNoMods = ifAllDefaults ? true : false;
|| Boolean(defaults[prop]) === Boolean(mods[prop])
);
console.log('MODS', mods); let customProxyArray = [];
Object.assign(this, defaults, mods); if (self.customProxyStringRaw) {
this.ifNoMods = ifAllDefaults ? true : false; customProxyArray = self.customProxyStringRaw
.replace(/#.*$/mg, '') // Strip comments.
let customProxyArray = []; .split( /(?:[^\S\r\n]*(?:;|\r?\n)+[^\S\r\n]*)+/g )
if (this.customProxyStringRaw) { .map( (p) => p.trim() )
customProxyArray = this.customProxyStringRaw .filter( (p) => p && /\s+/g.test(p) );
.replace(/#.*$/mg, '') // Strip comments. if (self.ifUseSecureProxiesOnly) {
.split( /(?:[^\S\r\n]*(?:;|\r?\n)+[^\S\r\n]*)+/g ) customProxyArray = customProxyArray.filter( (p) => !p.startsWith('HTTP ') );
.map( (p) => p.trim() )
.filter( (p) => p && /\s+/g.test(p) );
if (this.ifUseSecureProxiesOnly) {
customProxyArray = customProxyArray.filter( (p) => !p.startsWith('HTTP ') );
}
} }
if (this.ifUseLocalTor) { }
customProxyArray.push('SOCKS5 localhost:9050', 'SOCKS5 localhost:9150'); if (self.ifUseLocalTor) {
} customProxyArray.push('SOCKS5 localhost:9050', 'SOCKS5 localhost:9150');
if (customProxyArray.length) {
this.customProxyArray = customProxyArray;
this.filteredCustomsString = customProxyArray.join('; ');
} else {
if (!this.ifUsePacScriptProxies) {
throw new TypeError('Нет ни одного прокси, удовлетворяющего вашим требованиям!');
}
this.customProxyArray = false;
this.filteredCustomsString = '';
}
this.included = this.excluded = undefined;
if (this.ifMindExceptions && this.exceptions) {
this.included = [];
this.excluded = [];
for(const host of Object.keys(this.exceptions)) {
if (this.exceptions[host]) {
this.included.push(host);
} else {
this.excluded.push(host);
}
}
if (this.included.length && !this.filteredCustomsString) {
throw new TypeError(
'Проксировать свои сайты можно только через свои прокси. Нет ни одного своего прокси, удовлетворяющего вашим требованиям!'
);
}
}
} }
} if (customProxyArray.length) {
self.customProxyArray = customProxyArray;
self.filteredCustomsString = customProxyArray.join('; ');
} else {
if (!self.ifUsePacScriptProxies) {
return [new TypeError('Нет ни одного прокси, удовлетворяющего вашим требованиям!')];
}
self.customProxyArray = false;
self.filteredCustomsString = '';
}
self.included = self.excluded = undefined;
if (self.ifMindExceptions && self.exceptions) {
self.included = [];
self.excluded = [];
for(const host of Object.keys(self.exceptions)) {
if (self.exceptions[host]) {
self.included.push(host);
} else {
self.excluded.push(host);
}
}
if (self.included.length && !self.filteredCustomsString) {
return [null, self, new TypeError(
'Имеются сайты, добавленные вручную. Они проксироваться не будут, т.к. нет СВОИХ проски, удовлетворяющих вашим запросам!'
)];
}
}
return [null, self];
};
window.apis.pacKitchen = { window.apis.pacKitchen = {
@ -225,7 +222,7 @@
function() { function() {
if (!pacMods.ifUsePacScriptProxies) { if (!pacMods.ifUsePacScriptProxies) {
return '"' + pacMods.filteredCustomsString + '"'; return `"${pacMods.filteredCustomsString}"`;
} }
let filteredPacExp = 'pacProxyString'; let filteredPacExp = 'pacProxyString';
if (pacMods.ifUseSecureProxiesOnly) { if (pacMods.ifUseSecureProxiesOnly) {
@ -235,7 +232,7 @@
if ( !pacMods.filteredCustomsString ) { if ( !pacMods.filteredCustomsString ) {
return filteredPacExp; return filteredPacExp;
} }
return filteredPacExp + '"; ' + pacMods.filteredCustomsString + '"'; return `${filteredPacExp} + "; ${pacMods.filteredCustomsString}"`;
}() + ' + "; DIRECT";'; // Without DIRECT you will get 'PROXY CONN FAILED' pac-error. }() + ' + "; DIRECT";'; // Without DIRECT you will get 'PROXY CONN FAILED' pac-error.
@ -247,7 +244,7 @@
}, },
_tryNowAsync(details, cb = throwIfError) { setNowAsync(details, cb = throwIfError) {
if (typeof(details) === 'function') { if (typeof(details) === 'function') {
cb = details; cb = details;
@ -288,40 +285,45 @@
checkIncontinence(details) { checkIncontinence(details) {
if ( kitchenState(ifIncontinence) ) { if ( kitchenState(ifIncontinence) ) {
this._tryNowAsync(details, () => {/* Swallow. */}); this.setNowAsync(details, () => {/* Swallow. */});
} }
}, },
keepCookedNowAsync(pacMods = mandatory(), cb = throwIfError) { keepCookedNowAsync(pacMods = mandatory(), cb = throwIfError) {
let ifProxiesChanged = false;
let modsWarns = [];
if (typeof(pacMods) === 'function') { if (typeof(pacMods) === 'function') {
cb = pacMods; cb = pacMods;
pacMods = getCurrentConfigs(); pacMods = getCurrentConfigs();
} else { } else {
try { let modsErr;
pacMods = new PacModifiers(pacMods); [modsErr, pacMods, ...modsWarns] = createPacModifiers(pacMods);
} catch(e) { if (modsErr) {
return cb(e); return cb(modsErr, null, modsWarns);
} }
const oldProxies = getCurrentConfigs().filteredCustomsString || '';
const newProxies = pacMods.filteredCustomsString || '';
ifProxiesChanged = oldProxies !== newProxies;
console.log('Proxies changed from:', oldProxies, 'to', newProxies);
kitchenState(modsKey, pacMods); kitchenState(modsKey, pacMods);
} }
console.log('Keep cooked now...', pacMods); console.log('Keep cooked now...', pacMods);
this._tryNowAsync( this.setNowAsync(
(err, res, ...warns) => { (err, res, ...setWarns) => {
const accWarns = modsWarns.concat(setWarns); // Acc = accumulated.
console.log('Try now err:', err); console.log('Try now err:', err);
if (err) { if (err) {
return cb(err, res, ...warns); return cb(err, res, ...accWarns);
} }
const par = pacMods.customProxyArray; if (!ifProxiesChanged) {
if (!(par && par.length)) { return cb(null, res, ...accWarns);
return cb(null, res, ...warns);
} }
const newHosts = (pacMods.customProxyArray || []).map( (ps) => ps.split(/\s+/)[1] );
const hosts = par.map( (ps) => ps.split(/\s+/)[1] ); window.utils.fireRequest('ip-to-host-replace-all', newHosts, (err, res, ...moreWarns) => cb( err, res, ...accWarns.concat(moreWarns) ));
window.utils.fireRequest('ip-to-host-replace-all', hosts, (err, res, ...moreWarns) => cb( err, res, ...warns.concat(moreWarns) ));
} }
); );

View File

@ -136,7 +136,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
}; };
const conduct = (beforeStatus, operation, afterStatus, onSuccess) => { const conduct = (beforeStatus, operation, afterStatus, onSuccess = () => {}, onError = () => {}) => {
setStatusTo(beforeStatus); setStatusTo(beforeStatus);
switchInputs('off'); switchInputs('off');
@ -150,7 +150,9 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
} }
switchInputs('on'); switchInputs('on');
if (!err) { if (!err) {
onSuccess && onSuccess(res); onSuccess(res);
} else {
onError(err);
} }
}); });
@ -275,9 +277,9 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
}; };
const ifProxyLabel = '✔'; const labelIfProxied = '✔';
const ifNotProxyLabel = '✘'; const labelIfNotProxied = '✘';
const ifAutoLabel = '🔄'; const labelIfAuto = '🔄';
const addOption = function addOption(host, yesNoUndefined) { const addOption = function addOption(host, yesNoUndefined) {
@ -287,13 +289,13 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
opt.dataset.host = host; opt.dataset.host = host;
switch(yesNoUndefined) { switch(yesNoUndefined) {
case true: case true:
opt.label = ifProxyLabel; opt.label = labelIfProxied;
break; break;
case false: case false:
opt.label = ifNotProxyLabel; opt.label = labelIfNotProxied;
break; break;
default: default:
opt.label = ifAutoLabel; opt.label = labelIfAuto;
} }
const editorHost = excEditor.value.trim(); const editorHost = excEditor.value.trim();
if (host === editorHost) { if (host === editorHost) {
@ -322,7 +324,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
} }
const hideOpt = (opt) => opt.value = '\n'; const hideOpt = (opt) => opt.value = '\n';
const unhideOpt = (opt) => opt.value = opt.dataset.host + ' '; const unhideOptAndAddSpace = (opt) => opt.value = opt.dataset.host + ' ';
const excList = document.getElementById('exc-list'); const excList = document.getElementById('exc-list');
@ -337,7 +339,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
}; };
const renderExceptions = function renderExceptions(event) { const renderExceptionsPanelFromExcList = function renderExceptionsPanelFromExcList(event) {
// If triangle button on right of datalist input clicked. // If triangle button on right of datalist input clicked.
@ -376,9 +378,9 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
excList.childNodes.forEach( excList.childNodes.forEach(
(opt) => { (opt) => {
unhideOpt(opt); unhideOptAndAddSpace(opt);
if(opt.label === ifAutoLabel) { if(opt.label === labelIfAuto) {
editedOpt = opt; editedOpt = opt;
return; return;
} }
@ -392,11 +394,13 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
thisAuto.checked = true; thisAuto.checked = true;
excEditor.parentNode.classList.remove(noClass, yesClass); excEditor.parentNode.classList.remove(noClass, yesClass);
if (ifTriangleClicked || !originalHost) { const ifInputEmpty = !originalHost;
if (ifTriangleClicked || ifInputEmpty) {
// Show all opts. // Show all opts.
if (editedOpt) { if (editedOpt) {
const ifBackspaced = !originalHost && editedOpt.value.length < 3; // Example of editedOpt.value: 'abcde ' <- Mind the space (see unhideOptAndAddSpace)!
if (ifBackspaced) { const ifBackspacedOneChar = ifInputEmpty && editedOpt.value.length < 3;
if (ifBackspacedOneChar) {
editedOpt.remove(); editedOpt.remove();
} }
} }
@ -404,18 +408,16 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
} }
if (editedOpt) { if (editedOpt) {
hideOpt(editedOpt); const ifEditedOptAlreadyExists = editedOpt.dataset.host === originalHost;
if(ifEditedOptAlreadyExists) {
hideOpt(editedOpt);
return true;
}
// Not exact! Update!
editedOpt.remove();
} }
if (!exactOpt) { if (!exactOpt) {
if(editedOpt) {
const ifExact = editedOpt.dataset.host === originalHost;
if(ifExact) {
return true;
}
// Not exact! Update!
editedOpt.remove();
}
editedOpt = addOption(originalHost, undefined); editedOpt = addOption(originalHost, undefined);
if (!ifClick) { if (!ifClick) {
// New value was typed -- don't show tooltip. // New value was typed -- don't show tooltip.
@ -426,7 +428,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
// Exact found! // Exact found!
hideOpt(exactOpt); hideOpt(exactOpt);
if(exactOpt.label === ifProxyLabel) { if(exactOpt.label === labelIfProxied) {
thisYes.checked = true; thisYes.checked = true;
excEditor.parentNode.classList.add(yesClass); excEditor.parentNode.classList.add(yesClass);
} else { } else {
@ -437,7 +439,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
}; };
excEditor.onclick = excEditor.oninput = renderExceptions; excEditor.onclick = excEditor.oninput = renderExceptionsPanelFromExcList;
if (currentTab && !currentTab.url.startsWith('chrome')) { if (currentTab && !currentTab.url.startsWith('chrome')) {
excEditor.value = new URL(currentTab.url).hostname; excEditor.value = new URL(currentTab.url).hostname;
@ -452,7 +454,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
for(const host of Object.keys(pacMods.exceptions || {}).sort()) { for(const host of Object.keys(pacMods.exceptions || {}).sort()) {
addOption(host, pacMods.exceptions[host]); addOption(host, pacMods.exceptions[host]);
} }
renderExceptions(); // Colorize input. renderExceptionsPanelFromExcList(); // Colorize input.
} }
@ -469,24 +471,36 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
const pacMods = pacKitchen.getPacMods(); const pacMods = pacKitchen.getPacMods();
pacMods.exceptions = pacMods.exceptions || {}; pacMods.exceptions = pacMods.exceptions || {};
let fixUi = () => {}; let fixOptions;
const curOptOrNull = excList.querySelector(`[data-host="${host}"]`);
if (thisAuto.checked) { if (thisAuto.checked) {
delete pacMods.exceptions[host]; delete pacMods.exceptions[host];
fixUi = () => excEditor.value = ''; fixOptions = () => {
curOptOrNull && curOptOrNull.remove();
}
} else { } else {
// YES or NO. // YES or NO checked.
const ifYesClicked = thisYes.checked;
if (!validateHost(host)) { if (!validateHost(host)) {
return false; return false;
} }
if (thisYes.checked && !pacMods.filteredCustomsString) { if (ifYesClicked && !pacMods.filteredCustomsString) {
showErrors( new TypeError( showErrors( new TypeError(
'Проксировать СВОИ сайты можно только при наличии СВОИХ прокси (см. «Модификаторы» ).' 'Проксировать СВОИ сайты можно только при наличии СВОИХ прокси (см. «Модификаторы» ).'
)); ));
return false; return false;
} }
pacMods.exceptions[host] = thisYes.checked; //const ifNew = !(host in pacMods.exceptions);
fixUi = () => addOption(host, thisYes.checked); pacMods.exceptions[host] = ifYesClicked;
// Change label.
fixOptions = () => {
if (curOptOrNull) {
curOptOrNull.label = ifYesClicked ? labelIfProxied : labelIfNotProxied;
} else {
addOption(host, ifYesClicked);
}
};
} }
conduct( conduct(
@ -495,17 +509,13 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
'Исключения применены. Не забывайте о кэше!', 'Исключения применены. Не забывайте о кэше!',
() => { () => {
excList.childNodes.forEach( fixOptions();
(opt) => opt.dataset.host === host && opt.remove()
);
fixUi();
// Window may be closed before this line executes. // Window may be closed before this line executes.
console.log(excEditor, excEditor.oninput); renderExceptionsPanelFromExcList();
renderExceptions();
} }
); );
return true; return false; // Don't check before operation is finished.
}; };

View File

@ -113,7 +113,7 @@
const promises = types.map( const promises = types.map(
(type) => new Promise((resolve) => (type) => new Promise((resolve) =>
httpLib.get( httpLib.get(
'https://dns.google.com/resolve?type=' + type + '&name=' + host, `https://dns.google.com/resolve?type=${type}&name=${host}&edns_client_subnet=0.0.0.0/0`,
(err, res) => { (err, res) => {
if (res) { if (res) {

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const commonContext = { const commonContext = {
version: '0.21', version: '0.24',
}; };
exports.contexts = {}; exports.contexts = {};