-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzone_record.js
More file actions
344 lines (311 loc) · 8.62 KB
/
zone_record.js
File metadata and controls
344 lines (311 loc) · 8.62 KB
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
import * as RR from '@nictool/dns-resource-record'
import Mysql from './mysql.js'
import { mapToDbColumn } from './util.js'
const zrDbMap = { id: 'nt_zone_record_id', zid: 'nt_zone_id', owner: 'name' }
const boolFields = ['deleted']
const keepZeroWeightFor = new Set(['SRV', 'URI'])
const keepZeroPriorityFor = new Set(['HTTPS', 'SVCB', 'URI'])
class ZoneRecord {
constructor() {
this.mysql = Mysql
}
async create(args) {
if (args.id) {
const g = await this.get({ id: args.id })
if (g.length === 1) return g[0].id
}
new RR[args.type](args)
args = objectToDb(args)
return await Mysql.execute(...Mysql.insert(`nt_zone_record`, mapToDbColumn(args, zrDbMap)))
}
async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false
if (args.type !== undefined) {
args.type_id = RR.typeMap[args.type]
delete args.type
}
const rows = await Mysql.execute(
...Mysql.select(
`SELECT nt_zone_record_id AS id
, nt_zone_id AS zid
, name
, ttl
, description
, type_id
, address
, weight
, priority
, other
, location
, timestamp
, deleted
FROM nt_zone_record`,
mapToDbColumn(args, zrDbMap),
),
)
for (const row of rows) {
for (const b of boolFields) {
row[b] = row[b] === 1
}
if (args.deleted === false) delete row.deleted
}
return dbToObject(rows)
}
async put(args) {
if (!args.id) return false
const id = args.id
delete args.id
const r = await Mysql.execute(
...Mysql.update(`nt_zone_record`, `nt_zone_record_id=${id}`, mapToDbColumn(args, zrDbMap)),
)
return r.changedRows === 1
}
async delete(args) {
const r = await Mysql.execute(
...Mysql.update(`nt_zone_record`, `nt_zone_record_id=${args.id}`, {
deleted: args.deleted ?? 1,
}),
)
return r.changedRows === 1
}
async destroy(args) {
const r = await Mysql.execute(...Mysql.delete(`nt_zone_record`, { nt_zone_record_id: args.id }))
return r.affectedRows === 1
}
}
export default new ZoneRecord()
function dbToObject(rows) {
rows = JSON.parse(JSON.stringify(rows))
for (const row of rows) {
row.owner = row.name
delete row.name
row.type = RR.typeMap[row.type_id]
delete row.type_id
const map = getMap(row.type)
if (map) unApplyMap(row, map)
if ([null, ''].includes(row.description)) delete row.description
if ([null, '', '0'].includes(row.other)) delete row.other
if ([null, ''].includes(row.location)) delete row.location
if (row.timestamp === null) delete row.timestamp
if (row.weight === null) {
delete row.weight
} else if (row.weight === 0 && !keepZeroWeightFor.has(row.type)) {
delete row.weight
}
if (row.priority === null) {
delete row.priority
} else if (row.priority === 0 && !keepZeroPriorityFor.has(row.type)) {
delete row.priority
}
}
return rows
}
function objectToDb(obj) {
obj = JSON.parse(JSON.stringify(obj))
const map = getMap(obj.type)
if (map) applyMap(obj, map)
obj.type_id = RR.typeMap[obj.type]
delete obj.type
return obj
}
function applyMap(obj, map) {
// map dns-r-r (RFC/IETF) field names to NicTool 2.0 DB fields
for (const [key, value] of Object.entries(map)) {
if (Array.isArray(value)) {
obj[key] = `'${value.map((a) => obj[a]).join("','")}'`
for (const f of value) {
delete obj[f]
}
} else {
obj[key] = obj[value]
}
delete obj[value]
}
}
function unApplyMap(obj, map) {
// map NicTool 2.0 DB fields to dns-r-r (RFC/IETF) field names
if (obj.type === 'NAPTR') {
const [flags, service, regexp] = obj.address.slice(1, -1).split("','")
obj.flags = flags ?? ''
obj.service = service ?? ''
obj.regexp = regexp ?? ''
delete obj.address
delete map.address
}
if (obj.type === 'NSEC3') {
const [algo, flags, iters, salt, bitmaps, next] = obj.address.slice(1, -1).split("','")
obj['hash algorithm'] = /^\d+$/.test(algo) ? parseInt(algo) : algo ?? ''
obj.flags = /^\d+$/.test(flags) ? parseInt(flags) : flags ?? ''
obj.iterations = /^\d+$/.test(iters) ? parseInt(iters) : iters ?? ''
obj.salt = salt
obj['type bit maps'] = bitmaps
obj['next hashed owner name'] = next
delete obj.address
delete map.address
}
if (obj.type === 'NSEC3PARAM') {
const [algo, flags, iters, salt] = obj.address.slice(1, -1).split("','")
obj['hash algorithm'] = /^\d+$/.test(algo) ? parseInt(algo) : algo ?? ''
obj.flags = /^\d+$/.test(flags) ? parseInt(flags) : flags ?? ''
obj.iterations = /^\d+$/.test(iters) ? parseInt(iters) : iters ?? ''
obj.salt = salt
delete obj.address
delete map.address
}
if (obj.type === 'SOA') {
const [one, two, three, four, five, six, seven] = obj.address.slice(1, -1).split("','")
obj.mname = one
obj.rname = two
obj.serial = parseInt(three)
obj.refresh = parseInt(four)
obj.retry = parseInt(five)
obj.expire = parseInt(six)
obj.minimum = parseInt(seven)
delete obj.address
delete map.address
}
for (const [key, value] of Object.entries(map)) {
switch (value) {
case 'key tag': // DS record
case 'port': // SRV
case 'certificate usage': // SMIMEA
case 'algorithm': // IPSECKEY
case 'flags': // KEY
case 'matching type': // TLSA
obj[value] = parseInt(obj[key])
break
default:
obj[value] = obj[key]
}
delete obj[key]
}
}
// map of NicTool 2.0 fields to RR field names
function getMap(rrType) {
switch (rrType) {
case 'CAA':
return {
weight: 'flags',
other: 'tag',
address: 'value',
}
case 'CERT':
return {
other: 'cert type',
priority: 'key tag',
weight: 'algorithm',
address: 'certificate',
}
case 'CNAME':
return { address: 'cname' }
case 'DNAME':
return { address: 'target' }
case 'DNSKEY':
return {
address: 'publickey',
weight: 'flags',
priority: 'protocol',
other: 'algorithm',
}
case 'DS':
return {
address: 'digest',
weight: 'digest type',
priority: 'algorithm',
other: 'key tag',
}
case 'HINFO':
return { address: 'os', other: 'cpu' }
case 'HTTPS':
return {
address: 'target name',
other: 'params',
}
case 'IPSECKEY':
return {
address: 'gateway',
description: 'publickey',
weight: 'precedence',
priority: 'gateway type',
other: 'algorithm',
}
case 'KEY':
return {
address: 'publickey',
weight: 'protocol',
priority: 'algorithm',
other: 'flags',
}
case 'MX':
return { weight: 'preference', address: 'exchange' }
case 'NAPTR':
return {
weight: 'order',
priority: 'preference',
address: ['flags', 'service', 'regexp'],
description: 'replacement',
}
case 'NS':
return { address: 'dname' }
case 'NSEC':
return {
address: 'next domain',
description: 'type bit maps',
}
case 'NSEC3':
return {
address: ['hash algorithm', 'flags', 'iterations', 'salt', 'type bit maps', 'next hashed owner name'],
}
case 'NSEC3PARAM':
return {
address: ['hash algorithm', 'flags', 'iterations', 'salt'],
}
case 'NXT':
return {
address: 'next domain',
description: 'type bit map',
}
case 'OPENPGPKEY':
return { address: 'public key' }
case 'PTR':
return { address: 'dname' }
case 'SMIMEA':
return {
address: 'certificate association data',
weight: 'matching type',
priority: 'selector',
other: 'certificate usage',
}
case 'SOA':
return {
address: ['mname', 'rname', 'serial', 'refresh', 'retry', 'expire', 'minimum'],
}
case 'SPF':
return { address: 'data' }
case 'SSHFP':
return {
address: 'fingerprint',
weight: 'algorithm',
priority: 'fptype',
}
case 'SRV':
return { address: 'target', other: 'port' }
case 'SVCB':
return {
address: 'target name',
other: 'params',
}
case 'TLSA':
return {
weight: 'certificate usage',
priority: 'selector',
address: 'certificate association data',
other: 'matching type',
}
case 'TXT':
return { address: 'data' }
case 'URI':
return { address: 'target' }
}
}