Start adding auth support

This commit is contained in:
Ilya Ig. Petrov 2017-10-18 00:14:11 +05:00
parent 5f3da48cc9
commit 98cbd5fd02
3 changed files with 90 additions and 6 deletions

View File

@ -71,6 +71,19 @@
},
getOrDie(cb = self.mandatory()) {
return self.chromified((err, ...args) => {
if (err) {
throw err;
}
cb(...args);
});
},
getProp(obj, path = self.mandatory()) {
const props = path.split('.');

View File

@ -12,6 +12,73 @@
const ifIncontinence = 'if-incontinence';
const modsKey = 'mods';
const proxyHostToCredsList = {
// One host may be used several times with different creds.
'blablabla:6110': [
{ username: 'foo', password: 'bar' },
{ username: 'foo1', password: 'bar' },
{ username: 'foo2', password: 'bar' },
{ username: 'foo3', password: 'bar' },
{ username: 'foo4', password: 'bar' },
{ username: 'foo5', password: 'bar' },
{ username: 'foo6', password: 'bar' },
],
};
const ifAuthSupported = chrome.webRequest && chrome.webRequest.onAuthRequired && !window.apis.version.ifMini;
if (ifAuthSupported) {
console.log('WebRequest is supported!');
const requestIdToTries = {};
chrome.webRequest.onAuthRequired.addListener(
window.utils.getOrDie((details) => {
console.log('AUTH REQUIRED.');
if (!details.isProxy) {
console.log('Not from proxy.');
return {};
}
const proxyHost = `${details.challenger.host}:${details.challenger.port}`;
const credsList = proxyHostToCredsList[proxyHost];
if (!credsList) {
console.log('Creds list is empty.');
return {}; // No creds found for this proxy.
}
const requestId = details.requestId;
const tries = requestIdToTries[requestId] || 0;
if (tries > credsList.length) {
return {}; // All creds for this proxy were tried already.
}
requestIdToTries[requestId] = tries + 1;
console.log('TRIES=', tries, 'for', requestId);
return {
authCredentials: credsList[tries],
};
}),
{urls: ['<all_urls>']},
['blocking'],
);
const forgetRequestId = (details) => {
delete requestIdToTries[details.requestId];
};
chrome.webRequest.onCompleted.addListener(
forgetRequestId,
{urls: ['<all_urls>']},
);
chrome.webRequest.onErrorOccurred.addListener(
forgetRequestId,
{urls: ['<all_urls>']},
);
}
const getDefaultConfigs = () => ({// Configs user may mutate them and we don't care!
ifProxyHttpsUrlsOnly: {
@ -143,8 +210,10 @@
.every((dProp) => {
const ifDflt = (
!(dProp in mods) ||
Boolean(configs[dProp].dflt) === Boolean(mods[dProp])
!(
dProp in mods &&
Boolean(configs[dProp].dflt) !== Boolean(mods[dProp])
)
);
const ifMods = configs[dProp].ifDfltMods; // If default value implies PAC-script modification.
return ifDflt ? !ifMods : ifMods;
@ -382,9 +451,9 @@ ${ pacMods.filteredCustomsString
details
? resolve(details)
: chrome.proxy.settings.get({}, timeouted(resolve) )
: chrome.proxy.settings.get({}, timeouted(resolve) ),
).then( (details) => {
).then((details) => {
if (
details.levelOfControl === 'controlled_by_this_extension'

View File

@ -20,11 +20,13 @@ const commonContext = {
exports.contexts = {};
const extra_permissions = ', "webRequest", "webRequestBlocking", "webNavigation"';
exports.contexts.full = Object.assign({}, commonContext, {
versionSuffix: '',
nameSuffixEn: '',
nameSuffixRu: '',
extra_permissions: ', "webRequest", "webNavigation"',
extra_permissions,
persistent: '',
scripts_2x: ', "20-ip-to-host-api.js"',
scripts_8x: ', "80-error-menu.js", "83-last-errors.js", "85-block-informer.js"',
@ -49,7 +51,7 @@ exports.contexts.beta = Object.assign({}, commonContext, {
versionSuffix: '',
nameSuffixEn: ' FOR TESTING',
nameSuffixRu: ' ДЛЯ ТЕСТОВ',
extra_permissions: ', "webRequest", "webNavigation"',
extra_permissions,
persistent: '',
scripts_2x: ', "20-ip-to-host-api.js"',
scripts_8x: ', "80-error-menu.js", "83-last-errors.js", "85-block-informer.js"',