-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstandardise_records.ts
153 lines (144 loc) · 5.21 KB
/
standardise_records.ts
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
/*
Copyright 2019 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Defines stuff for SSHFP.
const sshfpAlgorithm = [
"Reserved for future use",
"RSA",
"Diffie-Hellman",
"ECDSA",
"ED25519",
]
const sshfpFingerprint = [
"Reserved for future use",
"SHA-1",
"SHA-256",
]
// Defines stuff for TLSA.
const tlsaUsage = [
"CA constraint",
"Service certificate constraint",
"Trust anchor assertion",
"Domain-issued certificate",
]
const tlsaSelector = [
"Full TLS certificate",
"The SubjectPublicKeyInfo of a TLS certificate",
]
const tlsaMatchingType = [
"CA constraint",
"Service certificate constraint",
"Trust anchor assertion",
]
// Defines the main function.
export default (key: string, json: any, txtRecordFragments: any, recordsJoined: any, txtSplit: RegExp) => {
// Handle edgecases for different record types.
if (key === "MX") {
for (const record of json.Answer) {
const dataSplit = record.data.split(" ")
if (dataSplit.length === 2) {
record.data = dataSplit[1]
record.priority = Number(dataSplit[0])
}
}
} else if (key === "DMARC") {
const newRecords = []
for (const record of json.Answer) {
const recordData = record.data.startsWith("\"") ? record.data.substr(1).slice(0, -1) : record.data
const dataSplit = recordData.split(";")
for (const newSplit of dataSplit) {
if (newSplit === "") continue
newRecords.push({
name: record.name,
TTL: record.TTL,
data: newSplit,
type: undefined,
})
}
}
json.Answer = newRecords
} else if (key === "SSHFP") {
const newRecords = []
for (const record of json.Answer) {
const dataSplit = record.data.split(" ")
newRecords.push({
name: record.name,
algorithm: sshfpAlgorithm[Number(dataSplit[0])] || "Unknown",
"Fingerprint Type": sshfpFingerprint[Number(dataSplit[1])] || "Unknown",
fingerprint: dataSplit[2],
TTL: record.TTL,
type: undefined,
})
}
json.Answer = newRecords
} else if (key === "TLSA") {
const newRecords = []
for (const record of json.Answer) {
const dataSplit = record.data.split(" ")
newRecords.push({
name: record.name,
usage: tlsaUsage[Number(dataSplit[0])] || "Unknown",
selector: tlsaSelector[Number(dataSplit[1])] || "Unknown",
"Matching Type": tlsaMatchingType[Number(dataSplit[2])] || "Unknown",
fingerprint: dataSplit[3],
TTL: record.TTL,
type: undefined,
})
}
json.Answer = newRecords
} else if (key === "TXT") {
for (const record of json.Answer) {
const recordDataSplit = record.data.split(txtSplit)
if (recordDataSplit.length > 1) {
const consumableRecord = `${recordDataSplit[0].substr(1).startsWith("_") ? recordDataSplit[0].substr(2) : recordDataSplit[0].substr(1)}%${record.name}%${record.TTL}`
if (txtRecordFragments[consumableRecord]) {
(txtRecordFragments as any)[consumableRecord] += `\n${record.data}`
} else {
txtRecordFragments[consumableRecord] = record.data
}
delete json.Answer[record]
continue
}
}
} else if (key === "SRV") {
for (const record of json.Answer) {
const dataSplit = record.data.split(" ").reverse()
record.priority = dataSplit.pop()
record.weight = dataSplit.pop()
record.port = dataSplit.pop()
record.target = dataSplit.reverse().join(" ")
delete record.data
}
}
// Sorts them by either priority or TTL.
json.Answer.sort((a: any, b: any) => {
if (a.priority) {
return a.priority - b.priority
}
if (a.TTL) {
return b.TTL - a.TTL
}
})
// Capitalise the keys.
for (const record of json.Answer) {
delete record.type
const recordKeys = Object.keys(record)
for (const recordKeyOrigin of recordKeys) {
const recordKey = `${recordKeyOrigin[0].toUpperCase()}${recordKeyOrigin.substr(1)}`
if (recordsJoined[recordKey]) {
recordsJoined[recordKey].push(record[recordKeyOrigin])
} else {
recordsJoined[recordKey] = [record[recordKeyOrigin]]
}
}
}
}