-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsec.ts
189 lines (169 loc) · 5.7 KB
/
dsec.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import axios from 'axios';
import cloudflare from 'cloudflare';
export default class dSec {
private url = 'https://desec.io/api/v1/domains/';
private records: DRecord[] = [];
constructor(private apiKey: string, private domain: string) {
}
async getRecords(): Promise<DRecord[]> {
const recs = await this.get('rrsets');
this.records = [...recs];
return recs;
}
checkExits(record: cloudflare.DnsRecord) {
const r = this.findRecord(record);
const c = this.transformRecordContent(record);
if (r) {
for (let rc of r.records) {
if (rc == c) return true;
}
}
return false;
}
async createRecord(records: cloudflare.DnsRecord[]) {
const rx: DRecord[] = [];
for (let record of records) {
if (record.type === 'SRV') continue; //TODO SRV
const ss = this.transformRecordContent(record);
let zoneName = record.zone_name;
const subname = zoneName == record.name ? '' : record.name.replace('.' + zoneName, '');
let ri = rx.find(x =>
x.type === record.type && x.subname === subname);
if (ri == null) {
ri = this.findRecord(record);
if (ri != null) rx.push(ri!);
}
if (ri == null) {
ri = { type: record.type, subname: subname, ttl: record.ttl * 60, records: [] } as any;
rx.push(ri!);
}
ri?.records.push(ss!);
}
return this.patch(`rrsets/`, JSON.stringify(rx));
}
async clearExtra(records: cloudflare.DnsRecord[]) {
const rx: DRecord[] = [];
for (let record of this.records) {
record.removeRecords = [];
if (record.type === 'NS') continue; // don't touch NS keep as it initial manual setup
if (record.type === 'SRV') continue; // TODO SRV
if (record.type === 'TXT' && record.subname == '_acme-challenge') continue; // acme records hidden on cloudflare rest api
const crs = records.filter(
(x: any) => x.type == record.type &&
x.name + '.' === record.name,
);
const crsX = crs.map(x => {
return {
...x,
trc: this.transformRecordContent(x),
};
});
for (let r of record.records) {
const cr = crsX.find(x => x.trc === r);
if (!cr) {
console.log('extra record', record.type, record.name, r);
record.removeRecords.push(r);
}
}
}
const mods = this.records.filter(x => x.removeRecords && x.removeRecords.length > 0);
for (let mod of mods) {
const nr = mod.records.filter(x => {
return !mod.removeRecords?.includes(x);
});
rx.push({
...mod,
records: nr,
removeRecords: undefined,
});
}
if (rx.length > 0)
return this.patch(`rrsets/`, JSON.stringify(rx));
return [];
}
async clear(sub: string, type: string) {
const rx: Partial<DRecord>[] = [];
rx.push({
subname: sub,
type: type,
records: [],
});
return this.patch(`rrsets/`, JSON.stringify(rx));
}
private async get(path: string) {
let config = {
method: 'get',
url: `${this.url}/${this.domain}/${path}`,
headers: {
'Authorization': `Token ${this.apiKey}`,
},
};
let resp = await axios.request(config);
return resp.data;
}
private async patch(path: string, data: string) {
let config = {
method: 'patch',
url: `${this.url}/${this.domain}/${path}`,
headers: {
'Authorization': `Token ${this.apiKey}`,
'Content-Type': 'application/json',
},
data: data,
};
// console.log('PATCH', data);
let resp = await axios.request(config);
return resp.data;
}
private findRecord(record: cloudflare.DnsRecord) {
const type = record.type;
switch (type) {
case 'SRV':
//TODO
break;
case 'MX':
case 'URI':
default:
// console.log(record.type, record.name, record.content);
const name = record.name + '.';
const dgrt = this.records.find(x => x.type === record.type && x.name === name);
if (dgrt) {
return dgrt;
}
}
return undefined;
}
private transformRecordContent(record: cloudflare.DnsRecord) {
const type = record.type;
switch (type) {
case 'SRV':
//TODO
break;
case 'MX':
return `${record.priority} ${record.content}.`;
case 'CNAME':
if (!record.content.endsWith('.'))
return `${record.content}.`;
return record.content;
case 'URI':
break;
case 'TXT':
let content = record.content;
if (content.match(/^".*"$/)) return content;
return `"${content}"`;
default:
return record.content;
}
}
}
interface DRecord {
domain: string;
subname: string;
name: string;
type: string;
records: string[];
ttl: number;
created: string;
touched: string;
removeRecords?: string[];
}