welsonjs/lib/hosts.js

30 lines
847 B
JavaScript
Raw Normal View History

////////////////////////////////////////////////////////////////////////
// Hosts API
////////////////////////////////////////////////////////////////////////
var SYS = require("lib/system");
var FILE = require("lib/file");
exports.getHosts = function() {
var hosts = [];
var filePath = SYS.getEnvString("windir") + "\\System32\\\drivers\\etc\\hosts";
var fileContent = FILE.readFile(filePath, FILE.CdoCharset.CdoUTF_8);
2020-07-29 06:48:49 +00:00
var rows = fileContent.split(/[\r\n]+/g).filter(function(s) {
return !(s.indexOf('#') == 0);
}).map(function(s) {
2020-07-29 06:48:49 +00:00
var pos = s.indexOf(" #");
return (pos > -1 ? s.substring(0, pos) : s).split(/\s+/);
});
2020-07-29 06:48:49 +00:00
for (var i = 0; i < rows.length; i++) {
hosts.push({
2020-07-29 06:48:49 +00:00
host: rows[i][0],
domain: rows[i][1]
});
}
return hosts;
};