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,9 +97,7 @@
}; };
class PacModifiers { const createPacModifiers = function createPacModifiers(mods = {}) {
constructor(mods = {}) {
const defaults = getDefaults(); const defaults = getDefaults();
const ifAllDefaults = Object.keys(defaults) const ifAllDefaults = Object.keys(defaults)
@ -109,57 +106,57 @@
|| Boolean(defaults[prop]) === Boolean(mods[prop]) || Boolean(defaults[prop]) === Boolean(mods[prop])
); );
console.log('MODS', mods); console.log('Input mods:', mods);
Object.assign(this, defaults, mods); const self = {};
this.ifNoMods = ifAllDefaults ? true : false; Object.assign(self, defaults, mods);
self.ifNoMods = ifAllDefaults ? true : false;
let customProxyArray = []; let customProxyArray = [];
if (this.customProxyStringRaw) { if (self.customProxyStringRaw) {
customProxyArray = this.customProxyStringRaw customProxyArray = self.customProxyStringRaw
.replace(/#.*$/mg, '') // Strip comments. .replace(/#.*$/mg, '') // Strip comments.
.split( /(?:[^\S\r\n]*(?:;|\r?\n)+[^\S\r\n]*)+/g ) .split( /(?:[^\S\r\n]*(?:;|\r?\n)+[^\S\r\n]*)+/g )
.map( (p) => p.trim() ) .map( (p) => p.trim() )
.filter( (p) => p && /\s+/g.test(p) ); .filter( (p) => p && /\s+/g.test(p) );
if (this.ifUseSecureProxiesOnly) { if (self.ifUseSecureProxiesOnly) {
customProxyArray = customProxyArray.filter( (p) => !p.startsWith('HTTP ') ); customProxyArray = customProxyArray.filter( (p) => !p.startsWith('HTTP ') );
} }
} }
if (this.ifUseLocalTor) { if (self.ifUseLocalTor) {
customProxyArray.push('SOCKS5 localhost:9050', 'SOCKS5 localhost:9150'); customProxyArray.push('SOCKS5 localhost:9050', 'SOCKS5 localhost:9150');
} }
if (customProxyArray.length) { if (customProxyArray.length) {
this.customProxyArray = customProxyArray; self.customProxyArray = customProxyArray;
this.filteredCustomsString = customProxyArray.join('; '); self.filteredCustomsString = customProxyArray.join('; ');
} else { } else {
if (!this.ifUsePacScriptProxies) { if (!self.ifUsePacScriptProxies) {
throw new TypeError('Нет ни одного прокси, удовлетворяющего вашим требованиям!'); return [new TypeError('Нет ни одного прокси, удовлетворяющего вашим требованиям!')];
} }
this.customProxyArray = false; self.customProxyArray = false;
this.filteredCustomsString = ''; self.filteredCustomsString = '';
} }
this.included = this.excluded = undefined; self.included = self.excluded = undefined;
if (this.ifMindExceptions && this.exceptions) { if (self.ifMindExceptions && self.exceptions) {
this.included = []; self.included = [];
this.excluded = []; self.excluded = [];
for(const host of Object.keys(this.exceptions)) { for(const host of Object.keys(self.exceptions)) {
if (this.exceptions[host]) { if (self.exceptions[host]) {
this.included.push(host); self.included.push(host);
} else { } else {
this.excluded.push(host); self.excluded.push(host);
} }
} }
if (this.included.length && !this.filteredCustomsString) { if (self.included.length && !self.filteredCustomsString) {
throw new TypeError( 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) {
const ifEditedOptAlreadyExists = editedOpt.dataset.host === originalHost;
if(ifEditedOptAlreadyExists) {
hideOpt(editedOpt); hideOpt(editedOpt);
}
if (!exactOpt) {
if(editedOpt) {
const ifExact = editedOpt.dataset.host === originalHost;
if(ifExact) {
return true; return true;
} }
// Not exact! Update! // Not exact! Update!
editedOpt.remove(); editedOpt.remove();
} }
if (!exactOpt) {
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 = {};