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,26 +478,15 @@
); );
} }
function getOneDnsRecord(args, cb) { function getIpsFor(host, cb) {
// args: { host:..., type: 'AAAA', filter: ['AAAA'] }
if (!(args.host && args.type && cb)) {
throw new Error('Wrong args:' + args.host + ',' + args.type);
}
const type2str = {
// https://en.wikipedia.org/wiki/List_of_DNS_record_types
// A, AAAA may be localized (github, e.g.), but you may use ANY
1: 'A', // IPv4
28: 'AAAA', // IPv6
2: 'NS',
5: 'CNAME', // Synonyms, returned by server together with A/AAAA.
255: 'ANY', // Deprecated on some servers, not recommended
};
const types = [1, 28];
const promises = types.map(
(type) => new Promise((resolve) =>
httpGet( httpGet(
'https://dns.google.com/resolve?type=' + args.type + '&name=' + args.host, 'https://dns.google.com/resolve?type=' + type + '&name=' + host,
(err, res) => { (err, res) => {
if (res) { if (res) {
try { try {
res = JSON.parse(res); res = JSON.parse(res);
@ -509,81 +500,45 @@
err.data = err.data || res; err.data = err.data || res;
} else { } else {
res = res.Answer || []; res = res.Answer || [];
for (const record of res) {
record.type = type2str[record.type];
}
if ( args.filter ) {
res = res.filter( res = res.filter(
(record) => args.filter.indexOf( record.type ) > -1 (record) => types.includes(record.type)
); );
} }
}
} catch(e) { } catch(e) {
err = e || err || {clarification: {message: ''}}; err = e || err || {clarification: {message: ''}};
err.clarification = err.clarification || {message: ''}; err.clarification = err.clarification || {message: ''};
err.clarification.message += ' Сервер (текст): '+ res; err.clarification.message = (
err.clarification.message.trim(); error.clarification.message
+ ' Сервер (текст): '+ res
).trim();
err.data = err.data || res; err.data = err.data || res;
} }
} }
cb( err, res ); resolve([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(