Refactor PAC generation, fix minor directIfAllowed bug

This commit is contained in:
Ilya Ig. Petrov 2017-05-01 08:04:19 -07:00
parent d6e4cde69f
commit 26e8e4fb45
2 changed files with 20 additions and 19 deletions

View File

@ -235,8 +235,11 @@
`; `;
} }
res += ` res += `
const directIfAllowed = ${pacMods.ifProxyOrDie ? '""/* Not allowed. */' : '"; DIRECT"'}; const directIfAllowed = ${pacMods.ifProxyOrDie ? '""/* Not allowed. */' : '"; DIRECT"'};`;
`; if (pacMods.filteredCustomsString) {
res += `
const filteredCustomProxies = "; ${pacMods.filteredCustomsString}";`;
}
const ifIncluded = pacMods.included && pacMods.included.length; const ifIncluded = pacMods.included && pacMods.included.length;
const ifExcluded = pacMods.excluded && pacMods.excluded.length; const ifExcluded = pacMods.excluded && pacMods.excluded.length;
@ -244,6 +247,7 @@
if (ifExceptions) { if (ifExceptions) {
res += ` res += `
/* EXCEPTIONS START */ /* EXCEPTIONS START */
const dotHost = '.' + host; const dotHost = '.' + host;
const isHostInDomain = (domain) => dotHost.endsWith('.' + domain); const isHostInDomain = (domain) => dotHost.endsWith('.' + domain);
@ -262,22 +266,21 @@
const excWeight = ${JSON.stringify(Object.entries(pacMods.exceptions))}.reduce( domainReducer, 0 ); const excWeight = ${JSON.stringify(Object.entries(pacMods.exceptions))}.reduce( domainReducer, 0 );
if (excWeight !== 0) { if (excWeight !== 0) {
if (excWeight > 0) { if (excWeight < 0) {
// Always proxy it!
${ pacMods.filteredCustomsString
? `return "${pacMods.filteredCustomsString}" + directIfAllowed;`
: '/* No proxies -- continue. */'
}
} else {
// Never proxy it! // Never proxy it!
return "DIRECT"; return "DIRECT";
} }
// Always proxy it!
${ pacMods.filteredCustomsString
? `return filteredCustomProxies + directIfAllowed;`
: '/* No custom proxies -- continue. */'
}
} }
/* EXCEPTIONS END */ /* EXCEPTIONS END */
`; `;
} }
res += ` res += `
const pacProxyString = originalFindProxyForURL(url, host)${ const pacScriptProxies = originalFindProxyForURL(url, host)${
pacMods.ifProxyOrDie ? '.replace(/DIRECT/g, "")' : ' + directIfAllowed' pacMods.ifProxyOrDie ? '.replace(/DIRECT/g, "")' : ' + directIfAllowed'
};`; };`;
if( if(
@ -286,11 +289,11 @@
pacMods.ifUsePacScriptProxies pacMods.ifUsePacScriptProxies
) { ) {
return res + ` return res + `
return pacProxyString;`; return pacScriptProxies + directIfAllowed;`;
} }
return res + ` return res + `
let pacProxyArray = pacProxyString.split(/(?:\\s*;\\s*)+/g).filter( (p) => p ); let pacProxyArray = pacScriptProxies.split(/(?:\\s*;\\s*)+/g).filter( (p) => p );
const ifNoProxies = pacProxyArray${pacMods.ifProxyOrDie ? '.length === 0' : '.every( (p) => /^DIRECT$/i.test(p) )'}; const ifNoProxies = pacProxyArray${pacMods.ifProxyOrDie ? '.length === 0' : '.every( (p) => /^DIRECT$/i.test(p) )'};
if (ifNoProxies) { if (ifNoProxies) {
// Directs only or null, no proxies. // Directs only or null, no proxies.
@ -300,19 +303,16 @@
function() { function() {
if (!pacMods.ifUsePacScriptProxies) { if (!pacMods.ifUsePacScriptProxies) {
return `"${pacMods.filteredCustomsString}"`; return '';
} }
let filteredPacExp = 'pacProxyString'; let filteredPacExp = 'pacScriptProxies';
if (pacMods.ifUseSecureProxiesOnly) { if (pacMods.ifUseSecureProxiesOnly) {
filteredPacExp = filteredPacExp =
'pacProxyArray.filter( (pStr) => /^HTTPS\\s/.test(pStr) ).join("; ")'; 'pacProxyArray.filter( (pStr) => /^HTTPS\\s/.test(pStr) ).join("; ")';
} }
if ( !pacMods.filteredCustomsString ) { return filteredPacExp + ' + ';
return filteredPacExp;
}
return `${filteredPacExp} + "; ${pacMods.filteredCustomsString}"`;
}() + ' + directIfAllowed;'; // Without DIRECT you will get 'PROXY CONN FAILED' pac-error. }() + `${pacMods.filteredCustomsString ? 'filteredCustomProxies + ' : ''}directIfAllowed;`; // Without DIRECT you will get 'PROXY CONN FAILED' pac-error.
}()} }()}

View File

@ -519,6 +519,7 @@
<div id="exc-address"> <div id="exc-address">
<span>*.</span><input placeholder="navalny.com" list="exc-list" name="browser" id="exc-editor" style=""/> <span>*.</span><input placeholder="navalny.com" list="exc-list" name="browser" id="exc-editor" style=""/>
</div> </div>
<!--a href class="emoji">⇄</a-->
<a href="../exceptions/index.html" title="импорт/экспорт"><svg <a href="../exceptions/index.html" title="импорт/экспорт"><svg
class="icon" class="icon"
><use xlink:href="#icon-import-export"></use></svg> ><use xlink:href="#icon-import-export"></use></svg>