Skip to content

Instantly share code, notes, and snippets.

@samgiles
Created August 6, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samgiles/0a605670c287089bb332 to your computer and use it in GitHub Desktop.
Save samgiles/0a605670c287089bb332 to your computer and use it in GitHub Desktop.
Parse Zone File to generate a list of all domains defined within it.
// Polyfill
// String.prototype.includes
String.prototype.includes = function (string, index) {
if (typeof string === 'object' && string instanceof RegExp) throw new TypeError("First argument to String.prototype.includes must not be a regular expression");
return this.indexOf(string, index) !== -1;
};
// Array.prototype.includes
Array.prototype.includes = function includes(searchElement /*, fromIndex*/ ) {'use strict';
var O = Object(this);
var len = parseInt(O.length) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1]) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) {
return true;
}
k++;
}
return false;
};
var split = require('split');
var originRegex = /ORIGIN/;
var records = {};
var currentRecord = false;
process.stdin.pipe(split()).on('data', function(data) {
var splitData = data.split(/\s+/);
if (data.includes('$ORIGIN')) {
// slice to remove trailing '.' character
currentRecord = splitData[1].slice(0, -1);
records[currentRecord] = [];
} else {
if (splitData.includes("A") || splitData.includes("CNAME") || splitData.includes("AAAA")) {
if (splitData[0] === "@") { return; }
records[currentRecord].push(splitData[0]);
}
}
});
process.stdin.on('end', function() {
Object.keys(records).forEach(function(key) {
var domain = records[key];
domain.forEach(function(sub) {
console.log(sub + "." + key);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment