-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rr.js
340 lines (275 loc) · 8.56 KB
/
rr.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
import util from 'node:util'
export default class RR extends Map {
constructor(opts) {
super()
if (opts === null) return
if (opts.default) this.default = opts.default
if (opts.bindline) return this.fromBind(opts)
if (opts.tinyline) return this.fromTinydns(opts)
// tinydns specific
this.setLocation(opts?.location)
this.setTimestamp(opts?.timestamp)
this.setOwner(opts?.owner)
this.setType(opts?.type)
this.setTtl(opts?.ttl)
this.setClass(opts?.class)
for (const f of this.getFields('rdata')) {
const fnName = `set${this.ucfirst(f)}`
if (this[fnName] === undefined)
this.throwHelp(`Missing ${fnName} in class ${this.get('type')}`)
this[fnName](opts[f])
}
if (opts.comment) this.set('comment', opts.comment)
}
ucfirst(str) {
return str
.split(/\s/)
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
.join('')
}
setClass(c) {
switch (c) {
case 'IN': // 1
case undefined:
case null:
case '':
this.set('class', 'IN')
break
case 'CS': // 2
case 'CH': // 3
case 'HS': // 4
case 'NONE': // 254
case 'ANY': // 255
this.set('class', c)
break
default:
this.throwHelp(`invalid class ${c}`)
}
}
setLocation(l) {
switch (l) {
case undefined:
return
default:
this.set('location', l)
}
}
setTimestamp(l) {
switch (l) {
case undefined:
return
default:
this.set('timestamp', l)
}
}
setOwner(n) {
if (n === undefined) this.throwHelp(`owner is required`)
if (n.length < 1 || n.length > 255)
this.throwHelp(
'Domain names must have 1-255 octets (characters): RFC 2181',
)
this.isFullyQualified(this.constructor.name, 'owner', n)
this.hasValidLabels(n)
// wildcard records: RFC 1034, 4592
if (/\*/.test(n)) {
if (!/^\*\./.test(n) && !/\.\*\./.test(n))
this.throwHelp('only *.something or * (by itself) is a valid wildcard')
}
this.set('owner', n.toLowerCase())
}
setTtl(t) {
if (t === undefined) t = this?.default?.ttl
if (t === undefined) {
if (['SOA', 'SSHPF'].includes(this.get('type'))) return
this.throwHelp('TTL is required, no default available')
}
if (typeof t !== 'number')
this.throwHelp(`TTL must be numeric (${typeof t})`)
// RFC 1035, 2181
this.is32bitInt(this.owner, 'TTL', t)
this.set('ttl', t)
}
setType(t) {
switch (t) {
case '':
case undefined:
this.throwHelp(`type is required`)
}
if (t.toUpperCase() !== this.constructor.name)
this.throwHelp(`type ${t} doesn't match ${this.constructor.name}`)
this.set('type', t.toUpperCase())
}
throwHelp(e) {
if (this.constructor.name === 'RR') throw new Error(e)
const example = this.getCanonical
? `Example ${this.constructor.name}:\n${util.inspect(this.getCanonical(), { depth: null })}\n\n`
: `${this.constructor.name} records have the fields: ${this.getFields().join(', ')}\n\n`
throw new Error(`${e}\n\n${example}${this.citeRFC()}\n`)
}
citeRFC() {
return `see RFC${this.getRFCs().length > 1 ? 's' : ''} ${this.getRFCs()}`
}
fullyQualify(hostname, origin) {
if (!hostname) return hostname
if (hostname === '@' && origin) hostname = origin
if (hostname.endsWith('.')) return hostname.toLowerCase()
if (origin) return `${hostname}.${origin}`.toLowerCase()
return `${hostname}.`
}
getPrefix(zone_opts = {}) {
const classVal = zone_opts.hide?.class ? '' : this.get('class')
let rrTTL = this.get('ttl')
if (zone_opts.hide?.ttl && rrTTL === zone_opts.ttl) rrTTL = ''
let owner = this.get('owner')
if (zone_opts.hide?.sameOwner && zone_opts.previousOwner === owner) {
owner = ''
} else {
owner = this.getFQDN('owner', zone_opts)
}
return `${owner}\t${rrTTL}\t${classVal}\t${this.get('type')}`
}
getEmpty(prop) {
return this.get(prop) === undefined ? '' : this.get(prop)
}
getComment(prop) {
const c = this.get('comment')
if (!c || !c[prop]) return ''
return c[prop]
}
getQuoted(prop) {
// if prop is not in quoted list, return bare
if (!this.getQuotedFields().includes(prop)) return this.get(prop)
// if it's already quoted, return as-is
if (/['"]/.test(this.get(prop)[0])) return this.get(prop)
return `"${this.get(prop)}"` // add double quotes
}
getQuotedFields() {
return []
}
getRdataFields() {
return []
}
getFields(arg) {
const commonFields = ['owner', 'ttl', 'class', 'type']
Object.freeze(commonFields)
switch (arg) {
case 'common':
return commonFields
case 'rdata':
return this.getRdataFields()
default:
return commonFields.concat(this.getRdataFields())
}
}
getFQDN(field, zone_opts = {}) {
let fqdn = this.get(field)
if (!fqdn) this.throwHelp(`empty value for field ${field}`)
if (!fqdn.endsWith('.')) fqdn += '.'
if (zone_opts.hide?.origin && zone_opts.origin) {
if (fqdn === zone_opts.origin) return '@'
if (fqdn.endsWith(zone_opts.origin))
return fqdn.slice(0, fqdn.length - zone_opts.origin.length - 1)
}
return fqdn
}
getTinyFQDN(field) {
const val = this.get(field)
if (val === '') return val // empty
if (val === '.') return val // null MX
// strip off trailing ., tinydns doesn't require it for FQDN
if (val.endsWith('.')) return val.slice(0, -1)
return val
}
getTinydnsGeneric(rdata) {
return `:${this.getTinyFQDN('owner')}:${this.getTypeId()}:${rdata}:${this.getTinydnsPostamble()}\n`
}
getTinydnsPostamble() {
return ['ttl', 'timestamp', 'location']
.map((f) => this.getEmpty(f))
.join(':')
}
hasValidLabels(hostname) {
// RFC 952 defined valid hostnames
// RFC 1035 limited domain label chars to letters, digits, and hyphen
// RFC 1123 allowed hostnames to start with a digit
// RFC 2181 'any binary string can be used as the label'
const fq = hostname.endsWith('.') ? hostname.slice(0, -1) : hostname
for (const label of fq.split('.')) {
if (label.length < 1 || label.length > 63)
this.throwHelp('Labels must have 1-63 octets (characters), RFC 2181')
}
}
is8bitInt(type, field, value) {
if (
typeof value === 'number' &&
parseInt(value, 10) === value && // assure integer
value >= 0 &&
value <= 255
)
return true
this.throwHelp(
`${type} ${field} must be a 8-bit integer (in the range 0-255)`,
)
}
is16bitInt(type, field, value) {
if (
typeof value === 'number' &&
parseInt(value, 10) === value && // assure integer
value >= 0 &&
value <= 65535
)
return true
this.throwHelp(
`${type} ${field} must be a 16-bit integer (in the range 0-65535)`,
)
}
is32bitInt(type, field, value) {
if (
typeof value === 'number' &&
parseInt(value, 10) === value && // assure integer
value >= 0 &&
value <= 2147483647
)
return true
this.throwHelp(
`${type} ${field} must be a 32-bit integer (in the range 0-2147483647)`,
)
}
isQuoted(val) {
return /^["']/.test(val) && /["']$/.test(val)
}
isFullyQualified(type, field, hostname) {
if (hostname.endsWith('.')) return true
this.throwHelp(`${type}: ${field} must be fully qualified`)
}
isValidHostname(type, field, hostname) {
const allowed = new RegExp(/[^a-zA-Z0-9\-._/\\]/)
if (!allowed.test(hostname)) return true
const matches = allowed.exec(hostname)
this.throwHelp(
`${type}, ${field} has invalid hostname character (${matches[0]})`,
)
}
toBind(zone_opts) {
return `${this.getPrefix(zone_opts)}\t${this.getRdataFields()
.map((f) => this.getQuoted(f))
.join('\t')}\n`
}
toMaraDNS() {
const type = this.get('type')
const supportedTypes =
'A PTR MX AAAA SRV NAPTR NS SOA TXT SPF RAW FQDN4 FQDN6 CNAME HINFO WKS LOC'.split(
/\s+/g,
)
if (!supportedTypes.includes(type)) return this.toMaraGeneric()
return `${this.get('owner')}\t+${this.get('ttl')}\t${type}\t${this.getRdataFields()
.map((f) => this.getQuoted(f))
.join('\t')} ~\n`
}
toMaraGeneric() {
// this.throwHelp(`\nMaraDNS does not support ${type} records yet and this package does not support MaraDNS generic records. Yet.\n`)
return `${this.get('owner')}\t+${this.get('ttl')}\tRAW ${this.getTypeId()}\t'${this.getRdataFields()
.map((f) => this.getQuoted(f))
.join(' ')}' ~\n`
}
}