hostlist/authdb: dns resolution refactor.
This commit is contained in:
parent
120ceef984
commit
d840144f3e
@ -469,10 +469,10 @@ function AuthDB(options) {
|
|||||||
this.logger = null;
|
this.logger = null;
|
||||||
this.resolve = dns.resolve;
|
this.resolve = dns.resolve;
|
||||||
this.proxyServer = null;
|
this.proxyServer = null;
|
||||||
|
this.dnsKnown = [];
|
||||||
|
|
||||||
this.known = {};
|
this.known = {};
|
||||||
this.authorized = [];
|
this.authorized = [];
|
||||||
this.dns = [];
|
|
||||||
|
|
||||||
this._init(options);
|
this._init(options);
|
||||||
}
|
}
|
||||||
@ -521,15 +521,17 @@ AuthDB.prototype._init = function _init(options) {
|
|||||||
AuthDB.prototype.addKnown = function addKnown(host, key) {
|
AuthDB.prototype.addKnown = function addKnown(host, key) {
|
||||||
var addr;
|
var addr;
|
||||||
|
|
||||||
assert(typeof host === 'string');
|
assert(typeof host === 'string',
|
||||||
|
'Known host must be a string.');
|
||||||
|
|
||||||
assert(Buffer.isBuffer(key) && key.length === 33,
|
assert(Buffer.isBuffer(key) && key.length === 33,
|
||||||
'Invalid public key for known peer.');
|
'Invalid public key for known peer.');
|
||||||
|
|
||||||
addr = IP.parseHost(host);
|
addr = IP.parseHost(host);
|
||||||
|
|
||||||
// Defer this for resolution.
|
|
||||||
if (addr.version === -1) {
|
if (addr.version === -1) {
|
||||||
this.dns.push([addr, key]);
|
// Defer this for resolution.
|
||||||
|
this.dnsKnown.push([addr, key]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -556,6 +558,8 @@ AuthDB.prototype.setKnown = function setKnown(map) {
|
|||||||
var keys = Object.keys(map);
|
var keys = Object.keys(map);
|
||||||
var i, host, key;
|
var i, host, key;
|
||||||
|
|
||||||
|
this.known = {};
|
||||||
|
|
||||||
for (i = 0; i < keys.length; i++) {
|
for (i = 0; i < keys.length; i++) {
|
||||||
host = keys[i];
|
host = keys[i];
|
||||||
key = map[host];
|
key = map[host];
|
||||||
@ -571,6 +575,8 @@ AuthDB.prototype.setKnown = function setKnown(map) {
|
|||||||
AuthDB.prototype.setAuthorized = function setAuthorized(keys) {
|
AuthDB.prototype.setAuthorized = function setAuthorized(keys) {
|
||||||
var i, key;
|
var i, key;
|
||||||
|
|
||||||
|
this.authorized.length = 0;
|
||||||
|
|
||||||
for (i = 0; i < keys.length; i++) {
|
for (i = 0; i < keys.length; i++) {
|
||||||
key = keys[i];
|
key = keys[i];
|
||||||
this.addAuthorized(key);
|
this.addAuthorized(key);
|
||||||
@ -596,7 +602,7 @@ AuthDB.prototype.getKnown = function getKnown(hostname) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lookup any dns-based known peers.
|
* Lookup known peers.
|
||||||
* @private
|
* @private
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
@ -605,18 +611,18 @@ AuthDB.prototype.discover = co(function* discover() {
|
|||||||
var jobs = [];
|
var jobs = [];
|
||||||
var i, addr;
|
var i, addr;
|
||||||
|
|
||||||
for (i = 0; i < this.dns.length; i++) {
|
for (i = 0; i < this.dnsKnown.length; i++) {
|
||||||
addr = this.dns[i];
|
addr = this.dnsKnown[i];
|
||||||
jobs.push(this.populate(addr[0], addr[1]));
|
jobs.push(this.populate(addr[0], addr[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.dns.length = 0;
|
this.dnsKnown.length = 0;
|
||||||
|
|
||||||
yield Promise.all(jobs);
|
yield Promise.all(jobs);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populate known peers with a dns-based host.
|
* Populate known peers with hosts.
|
||||||
* @private
|
* @private
|
||||||
* @param {Object} addr
|
* @param {Object} addr
|
||||||
* @param {Buffer} key
|
* @param {Buffer} key
|
||||||
@ -626,7 +632,7 @@ AuthDB.prototype.discover = co(function* discover() {
|
|||||||
AuthDB.prototype.populate = co(function* populate(addr, key) {
|
AuthDB.prototype.populate = co(function* populate(addr, key) {
|
||||||
var i, hosts, host;
|
var i, hosts, host;
|
||||||
|
|
||||||
assert(addr.version === -1);
|
assert(addr.version === -1, 'Resolved host passed.');
|
||||||
|
|
||||||
if (this.logger)
|
if (this.logger)
|
||||||
this.logger.info('Resolving authorized hosts from: %s.', addr.host);
|
this.logger.info('Resolving authorized hosts from: %s.', addr.host);
|
||||||
|
|||||||
@ -35,11 +35,13 @@ function HostList(options) {
|
|||||||
this.proxyServer = null;
|
this.proxyServer = null;
|
||||||
this.resolve = dns.resolve;
|
this.resolve = dns.resolve;
|
||||||
this.banTime = common.BAN_TIME;
|
this.banTime = common.BAN_TIME;
|
||||||
this.rawSeeds = this.network.seeds;
|
|
||||||
this.rawNodes = [];
|
|
||||||
|
|
||||||
this.seeds = [];
|
this.rawSeeds = this.network.seeds;
|
||||||
|
this.dnsSeeds = [];
|
||||||
|
this.rawNodes = [];
|
||||||
|
this.dnsNodes = [];
|
||||||
this.nodes = [];
|
this.nodes = [];
|
||||||
|
|
||||||
this.banned = {};
|
this.banned = {};
|
||||||
|
|
||||||
this.map = {};
|
this.map = {};
|
||||||
@ -59,7 +61,8 @@ function HostList(options) {
|
|||||||
this.maxFailures = 10;
|
this.maxFailures = 10;
|
||||||
this.maxRefs = 8;
|
this.maxRefs = 8;
|
||||||
|
|
||||||
this._init(options);
|
this._initOptions(options);
|
||||||
|
this._init();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -108,7 +111,7 @@ HostList.prototype._initOptions = function initOptions(options) {
|
|||||||
|
|
||||||
if (options.nodes) {
|
if (options.nodes) {
|
||||||
assert(Array.isArray(options.nodes));
|
assert(Array.isArray(options.nodes));
|
||||||
this.setNodes(options.nodes);
|
this.rawNodes = options.nodes;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -117,11 +120,9 @@ HostList.prototype._initOptions = function initOptions(options) {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HostList.prototype._init = function init(options) {
|
HostList.prototype._init = function init() {
|
||||||
var i;
|
var i;
|
||||||
|
|
||||||
this._initOptions(options);
|
|
||||||
|
|
||||||
for (i = 0; i < this.maxBuckets; i++)
|
for (i = 0; i < this.maxBuckets; i++)
|
||||||
this.fresh.push(new Map());
|
this.fresh.push(new Map());
|
||||||
|
|
||||||
@ -129,6 +130,7 @@ HostList.prototype._init = function init(options) {
|
|||||||
this.used.push(new List());
|
this.used.push(new List());
|
||||||
|
|
||||||
this.setSeeds(this.rawSeeds);
|
this.setSeeds(this.rawSeeds);
|
||||||
|
this.setNodes(this.rawNodes);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -654,22 +656,6 @@ HostList.prototype.toArray = function toArray() {
|
|||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Set initial seeds.
|
|
||||||
* @param {String[]} seeds
|
|
||||||
*/
|
|
||||||
|
|
||||||
HostList.prototype.setSeeds = function setSeeds(seeds) {
|
|
||||||
var i, seed;
|
|
||||||
|
|
||||||
this.seeds.length = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < seeds.length; i++) {
|
|
||||||
seed = seeds[i];
|
|
||||||
this.addSeed(seed);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a preferred seed.
|
* Add a preferred seed.
|
||||||
* @param {String} host
|
* @param {String} host
|
||||||
@ -677,24 +663,18 @@ HostList.prototype.setSeeds = function setSeeds(seeds) {
|
|||||||
|
|
||||||
HostList.prototype.addSeed = function addSeed(host) {
|
HostList.prototype.addSeed = function addSeed(host) {
|
||||||
var addr = IP.parseHost(host, this.network.port);
|
var addr = IP.parseHost(host, this.network.port);
|
||||||
this.seeds.push(addr);
|
|
||||||
return addr;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
if (addr.version === -1) {
|
||||||
* Set priority nodes.
|
// Defer for resolution.
|
||||||
* @param {String[]} nodes
|
this.dnsSeeds.push(addr);
|
||||||
*/
|
return;
|
||||||
|
|
||||||
HostList.prototype.setNodes = function setNodes(nodes) {
|
|
||||||
var i, node;
|
|
||||||
|
|
||||||
this.rawNodes.length = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < nodes.length; i++) {
|
|
||||||
node = nodes[i];
|
|
||||||
this.addNode(node);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addr = NetAddress.fromHost(addr.host, addr.port, this.network);
|
||||||
|
|
||||||
|
this.add(addr);
|
||||||
|
|
||||||
|
return addr;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -704,12 +684,56 @@ HostList.prototype.setNodes = function setNodes(nodes) {
|
|||||||
|
|
||||||
HostList.prototype.addNode = function addNode(host) {
|
HostList.prototype.addNode = function addNode(host) {
|
||||||
var addr = IP.parseHost(host, this.network.port);
|
var addr = IP.parseHost(host, this.network.port);
|
||||||
this.rawNodes.push(addr);
|
|
||||||
|
if (addr.version === -1) {
|
||||||
|
// Defer for resolution.
|
||||||
|
this.dnsNodes.push(addr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
addr = NetAddress.fromHost(addr.host, addr.port, this.network);
|
||||||
|
|
||||||
|
this.nodes.push(addr);
|
||||||
|
this.add(addr);
|
||||||
|
|
||||||
return addr;
|
return addr;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Discover hosts from seeds.
|
* Set initial seeds.
|
||||||
|
* @param {String[]} seeds
|
||||||
|
*/
|
||||||
|
|
||||||
|
HostList.prototype.setSeeds = function setSeeds(seeds) {
|
||||||
|
var i, host;
|
||||||
|
|
||||||
|
this.dnsSeeds.length = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < seeds.length; i++) {
|
||||||
|
host = seeds[i];
|
||||||
|
this.addSeed(host);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set priority nodes.
|
||||||
|
* @param {String[]} nodes
|
||||||
|
*/
|
||||||
|
|
||||||
|
HostList.prototype.setNodes = function setNodes(nodes) {
|
||||||
|
var i, host;
|
||||||
|
|
||||||
|
this.dnsNodes.length = 0;
|
||||||
|
this.nodes.length = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < nodes.length; i++) {
|
||||||
|
host = nodes[i];
|
||||||
|
this.addNode(host);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discover hosts from seeds and nodes.
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -717,16 +741,18 @@ HostList.prototype.discover = co(function* discover() {
|
|||||||
var jobs = [];
|
var jobs = [];
|
||||||
var i, node, seed;
|
var i, node, seed;
|
||||||
|
|
||||||
for (i = 0; i < this.rawNodes.length; i++) {
|
for (i = 0; i < this.dnsSeeds.length; i++) {
|
||||||
node = this.rawNodes[i];
|
seed = this.dnsSeeds[i];
|
||||||
jobs.push(this.lookupNode(node));
|
jobs.push(this.populateSeed(seed));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < this.seeds.length; i++) {
|
for (i = 0; i < this.dnsNodes.length; i++) {
|
||||||
seed = this.seeds[i];
|
node = this.dnsNodes[i];
|
||||||
jobs.push(this.populate(seed));
|
jobs.push(this.populateNode(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.dnsNodes.length = 0;
|
||||||
|
|
||||||
yield Promise.all(jobs);
|
yield Promise.all(jobs);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -736,8 +762,8 @@ HostList.prototype.discover = co(function* discover() {
|
|||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HostList.prototype.lookupNode = co(function* lookupNode(addr) {
|
HostList.prototype.populateNode = co(function* populateNode(addr) {
|
||||||
var addrs = yield this.lookup(addr);
|
var addrs = yield this.populate(addr);
|
||||||
|
|
||||||
if (addrs.length === 0)
|
if (addrs.length === 0)
|
||||||
return;
|
return;
|
||||||
@ -752,8 +778,8 @@ HostList.prototype.lookupNode = co(function* lookupNode(addr) {
|
|||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HostList.prototype.populate = co(function* populate(seed) {
|
HostList.prototype.populateSeed = co(function* populateSeed(seed) {
|
||||||
var addrs = yield this.lookup(seed);
|
var addrs = yield this.populate(seed);
|
||||||
var i, addr;
|
var i, addr;
|
||||||
|
|
||||||
for (i = 0; i < addrs.length; i++) {
|
for (i = 0; i < addrs.length; i++) {
|
||||||
@ -768,15 +794,11 @@ HostList.prototype.populate = co(function* populate(seed) {
|
|||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HostList.prototype.lookup = co(function* lookup(target) {
|
HostList.prototype.populate = co(function* populate(target) {
|
||||||
var addrs = [];
|
var addrs = [];
|
||||||
var i, addr, hosts, host;
|
var i, addr, hosts, host;
|
||||||
|
|
||||||
if (target.version !== -1) {
|
assert(target.version === -1, 'Resolved host passed.');
|
||||||
addr = NetAddress.fromHost(target.host, target.port, this.network);
|
|
||||||
addrs.push(addr);
|
|
||||||
return addrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.logger)
|
if (this.logger)
|
||||||
this.logger.info('Resolving host: %s.', target.host);
|
this.logger.info('Resolving host: %s.', target.host);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user