-
Notifications
You must be signed in to change notification settings - Fork 2
/
dnsrecord.ts
166 lines (146 loc) · 3.02 KB
/
dnsrecord.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
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* What all records have in common.
*/
interface IDnsRecordBase {
host: string;
data: string;
type: string;
ttl?: number;
id?: number;
}
/**
* Interface for an A-record
*/
export interface IDnsRecordA extends IDnsRecordBase {
type: "A";
}
/**
* Interface for an A-record
*/
export interface IDnsRecordTXT extends IDnsRecordBase {
type: "TXT";
}
/**
* Interface for an AAAA-record
*/
export interface IDnsRecordAAAA extends IDnsRecordBase {
type: "AAAA";
}
/**
* Interface for a CNAME-record
*/
export interface IDnsRecordCname extends IDnsRecordBase {
type: "CNAME";
}
/**
* Interface for an ANAME-record
*/
export interface IDnsRecordAname extends IDnsRecordBase {
type: "ANAME";
}
/**
* Interface for a MX-record
*/
export interface IDnsRecordMX extends IDnsRecordBase {
type: "MX";
priority: number;
}
/**
* Interface for an A-record
*/
export interface IDnsRecordNS extends IDnsRecordBase {
type: "NS";
}
/**
* Interface for a SRV-record
*/
export interface IDnsRecordSRV extends IDnsRecordBase {
type: "SRV";
priority: number;
weight: number;
port: number;
}
/**
* Interface for a TLSA-record
*/
export interface IDnsRecordTLSA extends IDnsRecordBase {
type: "TLSA";
usage: number;
selector: number;
dtype: number;
}
/**
* Interface for a DS-record
*/
export interface IDnsRecordDS extends IDnsRecordBase {
type: "DS";
tag: number;
alg: number;
digest: number;
}
/**
* Interface for a CAA-record
*/
export interface IDnsRecordCAA extends IDnsRecordBase {
type: "CAA";
flags: number;
tag: number;
}
/**
* Type which gathers all interfaces.
*/
export type DnsRecord =
| IDnsRecordA
| IDnsRecordAAAA
| IDnsRecordCname
| IDnsRecordAname
| IDnsRecordMX
| IDnsRecordNS
| IDnsRecordSRV
| IDnsRecordTLSA
| IDnsRecordTXT
| IDnsRecordDS
| IDnsRecordCAA;
/**
* Performs a simple validation a DNS-record.
* @param params Json object which describes a DNS-record.
*/
export function validate(params: DnsRecord) {
const common: string[] = ["host", "data", "type"];
const types: { [index: string]: string[] } = {
A: [],
AAAA: [],
ANAME: [],
CAA: ["flags", "tag"],
CNAME: [],
DS: ["tag", "alg", "digest"],
MX: ["priority"],
NS: [],
SRV: ["priority", "weight", "port"],
TLSA: ["usage", "selector", "dtype"],
TXT: [],
};
if (!params.hasOwnProperty("type")) {
throw new Error("Record does not have any type");
}
if (!types.hasOwnProperty(params.type.toUpperCase())) {
throw new Error("Record has an unknown type");
}
const special: string[] = types[params.type.toUpperCase()];
const fields = common.concat(special);
if (params.hasOwnProperty("id")) {
fields.push("id");
}
if (params.hasOwnProperty("ttl")) {
fields.push("ttl");
}
for (const field of fields) {
if (!params.hasOwnProperty(field)) {
throw new Error("Record missing required field");
}
}
if (fields.length < Object.keys(params).length) {
throw new Error("Too many fields in object");
}
return true;
}