diff --git a/lib/net/bip150.js b/lib/net/bip150.js index 1251ddb4..810857bc 100644 --- a/lib/net/bip150.js +++ b/lib/net/bip150.js @@ -685,46 +685,55 @@ AuthDB.prototype.readKnown = async function readKnown() { */ AuthDB.prototype.parseKnown = function parseKnown(text) { - const lines = text.split(/\n+/); + assert(typeof text === 'string'); - for (let line of lines) { - line = line.trim(); + if (text.charCodeAt(0) === 0xfeff) + text = text.substring(1); + + text = text.replace(/\r\n/g, '\n'); + text = text.replace(/\r/g, '\n'); + + let num = 0; + + for (const chunk of text.split('\n')) { + const line = chunk.trim(); + + num += 1; if (line.length === 0) continue; - if (/^\s*#/.test(line)) + if (line[0] === '#') continue; const parts = line.split(/\s+/); if (parts.length < 2) - continue; + throw new Error(`No key present on line ${num}: "${line}".`); - const hostname = parts[0].trim().split(','); + const hosts = parts[0].split(','); - let host, ip; - if (hostname.length >= 2) { - host = hostname[0]; - ip = hostname[1]; + let host, addr; + if (hosts.length >= 2) { + host = hosts[0]; + addr = hosts[1]; } else { host = null; - ip = hostname[0]; + addr = hosts[0]; } - const hex = parts[1].trim(); - const key = Buffer.from(hex, 'hex'); + const key = Buffer.from(parts[1], 'hex'); if (key.length !== 33) - throw new Error(`Invalid key: ${parts[1]}.`); + throw new Error(`Invalid key on line ${num}: "${parts[1]}".`); if (host && host.length > 0) this.addKnown(host, key); - if (ip.length === 0) + if (addr.length === 0) continue; - this.addKnown(ip, key); + this.addKnown(addr, key); } }; @@ -762,21 +771,31 @@ AuthDB.prototype.readAuth = async function readAuth() { */ AuthDB.prototype.parseAuth = function parseAuth(text) { - const lines = text.split(/\n+/); + assert(typeof text === 'string'); - for (let line of lines) { - line = line.trim(); + if (text.charCodeAt(0) === 0xfeff) + text = text.substring(1); + + text = text.replace(/\r\n/g, '\n'); + text = text.replace(/\r/g, '\n'); + + let num = 0; + + for (const chunk of text.split('\n')) { + const line = chunk.trim(); + + num += 1; if (line.length === 0) continue; - if (/^\s*#/.test(line)) + if (line[0] === '#') continue; const key = Buffer.from(line, 'hex'); if (key.length !== 33) - throw new Error(`Invalid key: ${line}.`); + throw new Error(`Invalid key on line ${num}: "${line}".`); this.addAuthorized(key); }