Simplify getIp function, comment unused storage puller

This commit is contained in:
Ilya Ig. Petrov 2016-11-30 05:07:00 -08:00
parent df1bb35e87
commit 00221bb96e
2 changed files with 62 additions and 106 deletions

View File

@ -123,6 +123,7 @@
}, },
/*
pullFromStorage(cb) { pullFromStorage(cb) {
chrome.storage.local.get(null, (storage) => { chrome.storage.local.get(null, (storage) => {
@ -140,6 +141,7 @@
}); });
}, },
*/
syncWithPacProvider(key, cb) { syncWithPacProvider(key, cb) {
@ -476,114 +478,67 @@
); );
} }
function getOneDnsRecord(args, cb) { function getIpsFor(host, cb) {
// args: { host:..., type: 'AAAA', filter: ['AAAA'] } const types = [1, 28];
if (!(args.host && args.type && cb)) { const promises = types.map(
throw new Error('Wrong args:' + args.host + ',' + args.type); (type) => new Promise((resolve) =>
} httpGet(
'https://dns.google.com/resolve?type=' + type + '&name=' + host,
(err, res) => {
const type2str = { if (res) {
// https://en.wikipedia.org/wiki/List_of_DNS_record_types try {
// A, AAAA may be localized (github, e.g.), but you may use ANY res = JSON.parse(res);
1: 'A', // IPv4 console.log('Json parsed.');
28: 'AAAA', // IPv6 if (err || res.Status) {
2: 'NS', const msg = ['Answer', 'Comment', 'Status']
5: 'CNAME', // Synonyms, returned by server together with A/AAAA. .filter( (prop) => res[prop] )
255: 'ANY', // Deprecated on some servers, not recommended .map( (prop) => prop + ': ' + JSON.stringify( res[prop] ) )
}; .join(', \n');
err.clarification.message += ' Сервер (json): ' + msg;
httpGet( err.data = err.data || res;
'https://dns.google.com/resolve?type=' + args.type + '&name=' + args.host, } else {
(err, res) => { res = res.Answer || [];
if (res) { res = res.filter(
try { (record) => types.includes(record.type)
res = JSON.parse(res); );
console.log('Json parsed.'); }
if (err || res.Status) { } catch(e) {
const msg = ['Answer', 'Comment', 'Status'] err = e || err || {clarification: {message: ''}};
.filter( (prop) => res[prop] ) err.clarification = err.clarification || {message: ''};
.map( (prop) => prop + ': ' + JSON.stringify( res[prop] ) ) err.clarification.message = (
.join(', \n'); error.clarification.message
err.clarification.message += ' Сервер (json): ' + msg; + ' Сервер (текст): '+ res
err.data = err.data || res; ).trim();
} else { err.data = err.data || res;
res = res.Answer || [];
for (const record of res) {
record.type = type2str[record.type];
}
if ( args.filter ) {
res = res.filter(
(record) => args.filter.indexOf( record.type ) > -1
);
} }
} }
} catch(e) { resolve([err, res]);
err = e || err || {clarification: {message: ''}};
err.clarification = err.clarification || {message: ''};
err.clarification.message += ' Сервер (текст): '+ res;
err.clarification.message.trim();
err.data = err.data || res;
} }
} )
cb( err, res );
}
);
}
function getDnsRecords(args, cb) {
/*
Example of input:
{
// Required
host: 'proxy.navalny.cia.gov',
// Optional
types: {
string: ['A', 'AAAA'],
// ^ Default. Makes one request per each type.
filter: ['A', 'AAAA'],
// ^ Default. E.g., you want to get rid of CNAME type from
// response.
}
}
Exmple of answer from google:
"Answer":
[
{
"name": "apple.com.", // Always matches name in the Question
// section.
"type": 1, // A - Standard DNS RR type.
"TTL": 3599, // Record's time-to-live in seconds.
"data": "17.178.96.59" // Data for A - IP address as text.
},
...
Exmple of output:
The same as google, but types _may be_ canonical strings ('AAAA', 'A')
**/
if ( !args.host.length ) {
throw new Error('args.host is required: ' + args.host);
}
args.types = Object.assign({
string: ['A', 'AAAA'],
filter: ['A', 'AAAA'],
}, args.types);
const promises = args.types.string.map(
(type) => new Promise( (resolve, reject) =>
getOneDnsRecord(
{host: args.host, type: type, filter: args.types.filter},
(err, res) => err ? reject(err) : resolve(res) )
) )
); );
Promise.all(promises).then( Promise.all(promises).then(
(answers) => cb( null, [].concat(...answers) ), ([[v4err, v4res], [v6err, v6res]]) => {
cb
);
}
const getIpDnsRecords = (host, cb) => getDnsRecords({host: host}, cb); if(v4err) {
return cb(v4err, v4res);
}
const ips = v4res;
if (!v6err) {
ips.push(...v6res);
} else {
v6err.clarification.ifNotCritical = true;
console.warn(v6err);
}
cb(v6err, ips);
}
);
}
function updatePacProxyIps(provider, cb) { function updatePacProxyIps(provider, cb) {
@ -600,15 +555,15 @@
errors: {}, errors: {},
}; };
let i = 0; let i = 0;
provider.proxyHosts.map( provider.proxyHosts.forEach(
(proxyHost) => getIpDnsRecords( (proxyHost) => getIpsFor(
proxyHost, proxyHost,
(err, records) => { (err, ips) => {
if (!err) { if (!err || err.clarification.ifNotCritical) {
provider.proxyIps = provider.proxyIps || {}; provider.proxyIps = provider.proxyIps || {};
records.forEach( ips.forEach(
(ans) => provider.proxyIps[ans.data] = proxyHost (ip) => provider.proxyIps[ip] = proxyHost
); );
} else { } else {
failure.errors[proxyHost] = err; failure.errors[proxyHost] = err;

View File

@ -139,7 +139,8 @@ chrome://extensions</a>
const li = document.createElement('li'); const li = document.createElement('li');
li.innerHTML = `<input type="radio" name="pacProvider" id="${providerKey}"> li.innerHTML = `<input type="radio" name="pacProvider" id="${providerKey}">
<label for="${providerKey}">${providerKey}</label> <label for="${providerKey}">${providerKey}</label>
<a href class="link-button checked-radio-panel" id="update-${providerKey}">[обновить]</a>`; <a href class="link-button checked-radio-panel"
id="update-${providerKey}">[обновить]</a>`;
li.querySelector('.link-button').onclick = li.querySelector('.link-button').onclick =
() => { () => {
conduct( conduct(