-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzonefile.js
347 lines (311 loc) · 10.4 KB
/
zonefile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
(function () {
var fs = require('fs');
var pathJoin = require('path').join;
var generate = function (options, template) {
template = template || fs.readFileSync(pathJoin(__dirname, 'zonefile_template'), 'utf8');
template = process$ORIGIN(options['$origin'], template);
template = process$TTL(options['$ttl'], template);
template = processSOA(options['soa'], template);
template = processNS(options['ns'], template);
template = processA(options['a'], template);
template = processAAAA(options['aaaa'], template);
template = processCNAME(options['cname'], template);
template = processMX(options['mx'], template);
template = processPTR(options['ptr'], template);
template = processTXT(options['txt'], template);
template = processSRV(options['srv'], template);
template = processValues(options, template);
return template.replace(/\n{2,}/gim, '\n\n');
};
var process$ORIGIN = function (data, template) {
var ret = '';
if (typeof data !== 'undefined') {
ret += '$ORIGIN ' + data;
}
return template.replace('{$origin}', ret);
};
var process$TTL = function (data, template) {
var ret = '';
if (typeof data !== 'undefined') {
ret += '$TTL ' + data;
}
return template.replace('{$ttl}', ret);
};
var processSOA = function (data, template) {
var ret = template;
data.name = data.name || '@';
data.ttl = data.ttl || '';
for (var key in data) {
var value = data[key];
ret = ret.replace('{' + key + '}', value + '\t');
}
return ret;
};
var processNS = function (data, template) {
var ret = '';
for (var i in data) {
ret += (data[i].name || '@') + '\t';
if(data[i].ttl) ret += data[i].ttl + '\t';
ret += 'IN\tNS\t' + data[i].host + '\n';
}
return template.replace('{ns}', ret);
};
var processA = function (data, template) {
var ret = '';
for (var i in data) {
ret += (data[i].name || '@') + '\t';
if(data[i].ttl) ret += data[i].ttl + '\t';
ret += 'IN\tA\t' + data[i].ip + '\n';
}
return template.replace('{a}', ret);
};
var processAAAA = function (data, template) {
var ret = '';
for (var i in data) {
ret += (data[i].name || '@') + '\t';
if(data[i].ttl) ret += data[i].ttl + '\t';
ret += 'IN\tAAAA\t' + data[i].ip + '\n';
}
return template.replace('{aaaa}', ret);
};
var processCNAME = function (data, template) {
var ret = '';
for (var i in data) {
ret += (data[i].name || '@') + '\t';
if(data[i].ttl) ret += data[i].ttl + '\t';
ret += 'IN\tCNAME\t' + data[i].alias + '\n';
}
return template.replace('{cname}', ret);
};
var processMX = function (data, template) {
var ret = '';
for (var i in data) {
ret += (data[i].name || '@') + '\t';
if(data[i].ttl) ret += data[i].ttl + '\t';
ret += 'IN\tMX\t' + data[i].preference + '\t' + data[i].host + '\n';
}
return template.replace('{mx}', ret);
};
var processPTR = function (data, template) {
var ret = '';
for (var i in data) {
ret += (data[i].name || '@') + '\t';
if(data[i].ttl) ret += data[i].ttl + '\t';
ret += 'IN\tPTR\t' + data[i].host + '\n';
}
return template.replace('{ptr}', ret);
};
var processTXT = function (data, template) {
var ret = '';
for (var i in data) {
ret += (data[i].name || '@') + '\t';
if(data[i].ttl) ret += data[i].ttl + '\t';
ret += 'IN\tTXT\t"' + data[i].txt + '"\n';
}
return template.replace('{txt}', ret);
};
var processSRV = function (data, template) {
var ret = '';
for (var i in data) {
ret += (data[i].name || '@') + '\t';
if(data[i].ttl) ret += data[i].ttl + '\t';
ret += 'IN\tSRV\t' + data[i].priority + '\t';
ret += data[i].weight + '\t';
ret += data[i].port + '\t';
ret += data[i].target + '\n';
}
return template.replace('{srv}', ret);
};
var processValues = function (options, template) {
template = template.replace('{zone}', options['$origin'] || options['soa']['name'] || '');
template = template.replace('{datetime}', (new Date()).toISOString());
return template.replace('{time}', Math.round(Date.now() / 1000));
};
//////////////////////////////////////////////////////////////////////////////
var root_name = '@';
var parse = function (text) {
//text = removeComments(text);
text = flatten(text);
return parseRRs(text);
};
var removeComments = function (text) {
return text.replace(/;[\s\S]*?$/gm, '');
};
var flatten = function (text) {
var captured = [];
var re = /\([\s\S]*?\)/gim;
var match = re.exec(text);
while (match !== null) {
match.replacement = match[0].replace(/\s+/gm, ' ');
captured.push(match);
// captured Text, index, input
match = re.exec(text);
}
var arrText = text.split('');
for (var i in captured) {
match = captured[i];
arrText.splice(match.index, match[0].length, match.replacement);
}
return arrText.join('').replace(/\(|\)/gim, ' ');
};
var parseRRs = function (text) {
var ret = {};
var rrs = text.split('\n');
for (var i in rrs) {
var rr = rrs[i];
if (!rr || !rr.trim()) {
continue;
}
var uRR = rr.toUpperCase();
if (uRR.indexOf(('Zone:').toUpperCase()) === 3) {
ret.zone = rr.split(/\s+/g)[2];
} else if (/\s+SOA\s+/.test(uRR)) {
ret.soa = parseSOA(rr);
} else if (/\s+NS\s+/.test(uRR)) {
ret.ns = ret.ns || [];
ret.ns.push(parseNS(rr));
} else if (/\s+A\s+/.test(uRR)) {
ret.a = ret.a || [];
ret.a.push(parseA(rr));
} else if (/\s+AAAA\s+/.test(uRR)) {
ret.aaaa = ret.aaaa || [];
ret.aaaa.push(parseAAAA(rr));
} else if (/\s+CNAME\s+/.test(uRR)) {
ret.cname = ret.cname || [];
ret.cname.push(parseCNAME(rr));
} else if (/\s+TXT\s+/.test(uRR)) {
ret.txt = ret.txt || [];
ret.txt.push(parseTXT(rr));
} else if (/\s+MX\s+/.test(uRR)) {
ret.mx = ret.mx || [];
ret.mx.push(parseMX(rr));
} else if (/\s+PTR\s+/.test(uRR)) {
ret.ptr = ret.ptr || [];
ret.ptr.push(parsePTR(rr));
} else if (/\s+SRV\s+/.test(uRR)) {
ret.srv = ret.srv || [];
ret.srv.push(parseSRV(rr));
}
}
return ret;
};
var parseSOA = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var soa = {};
var rrTokens = rr.trim().split(/\s+/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
soa.name = rrTokens[0];
soa.minimum = parseInt(rrTokens[l - 1], 10);
soa.expire = parseInt(rrTokens[l - 2], 10);
soa.retry = parseInt(rrTokens[l - 3], 10);
soa.refresh = parseInt(rrTokens[l - 4], 10);
soa.serial = parseInt(rrTokens[l - 5], 10);
soa.rname = rrTokens[l - 6];
soa.mname = rrTokens[l - 7];
if(!isNaN(rrTokens[1])) soa.ttl = parseInt(rrTokens[1], 10);
return soa;
};
var parseNS = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var rrTokens = rr.trim().split(/\s+/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
var result = {
name: rrTokens[0],
host: rrTokens[l - 1]
};
if(!isNaN(rrTokens[1])) result.ttl = parseInt(rrTokens[1], 10);
return result;
};
var parseA = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var rrTokens = rr.trim().split(/\s+/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
var result = {
name: rrTokens[0],
ip: rrTokens[l - 1]
};
if(!isNaN(rrTokens[1])) result.ttl = parseInt(rrTokens[1], 10);
return result;
};
var parseAAAA = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var rrTokens = rr.trim().split(/\s+/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
var result = {
name: rrTokens[0],
ip: rrTokens[l - 1]
};
if(!isNaN(rrTokens[1])) result.ttl = parseInt(rrTokens[1], 10);
return result;
};
var parseCNAME = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var rrTokens = rr.trim().split(/\s+/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
var result = {
name: rrTokens[0],
alias: rrTokens[l - 1]
};
if(!isNaN(rrTokens[1])) result.ttl = parseInt(rrTokens[1], 10);
return result;
};
var parseMX = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var rrTokens = rr.trim().split(/\s+/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
var result = {
name: rrTokens[0],
preference: parseInt(rrTokens[l - 2], 10),
host: rrTokens[l - 1]
};
if(!isNaN(rrTokens[1])) result.ttl = parseInt(rrTokens[1], 10);
return result;
};
var parseTXT = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var rrTokens = rr.trim().match(/[^\s\"']+|\"[^\"]*\"|'[^']*'/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
var result = {
name: rrTokens[0],
txt: rrTokens[l - 1].split('\"')[1]
};
if(!isNaN(rrTokens[1])) result.ttl = parseInt(rrTokens[1], 10);
return result;
};
var parsePTR = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var rrTokens = rr.trim().split(/\s+/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
var result = {
name: rrTokens[0],
host: rrTokens[l - 1]
};
if(!isNaN(rrTokens[1])) result.ttl = parseInt(rrTokens[1], 10);
return result;
};
var parseSRV = function (rr) {
if (rr.indexOf('\t\t') === 0) rr = rr.replace('\t\t', root_name);
var rrTokens = rr.trim().split(/\s+/g);
if (rr.indexOf('\t\t') !== 0) root_name = rrTokens[0];
var l = rrTokens.length;
var result = {
name: rrTokens[0],
target: rrTokens[l - 1],
priority: parseInt(rrTokens[l - 4], 10),
weight: parseInt(rrTokens[l - 3], 10),
port: parseInt(rrTokens[l - 2], 10)
};
if(!isNaN(rrTokens[1])) result.ttl = parseInt(rrTokens[1], 10);
return result;
};
exports.generate = generate;
exports.parse = parse;
})();