-
Notifications
You must be signed in to change notification settings - Fork 0
/
acme-sync.ts
56 lines (49 loc) · 1.65 KB
/
acme-sync.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
import cloudflare from 'cloudflare';
import desc from './dsec';
import { getProxyResolver } from './proxy-resolve';
import './utils';
const domain = process.env.SYNC_DOMAIN || '';
if (domain == '') {
console.error('missing domain please set SYNC_DOMAIN env variable');
process.exit(2);
}
const dSecToken = process.env.SYNC_DSEC_TOKEN || '';
if (domain == '') {
console.error('missing desec.io api token please set SYNC_DSEC_TOKEN env variable');
process.exit(2);
}
const cfToken = process.env.SYNC_CF_TOKEN || '';
if (domain == '') {
console.error('missing cloudflare api token please set SYNC_CF_TOKEN env variable');
process.exit(2);
}
const descClient = new desc(dSecToken, domain);
var cf = new cloudflare({
token: cfToken,
});
async function main() {
await descClient.getRecords();
// console.log(grouped);
const zoneResults: any = await cf.zones.browse();
const zone = zoneResults.result.find((zone: any) => zone.name === domain);
if (zone) {
const pr = getProxyResolver(zone.name_servers);
const drs = await pr.query('_acme-challenge.' + domain, 'TXT');
const cRecords: cloudflare.DnsRecord[] = drs.answers.map(x => {
return {
type: 'TXT',
name: '_acme-challenge.' + domain,
content: `"${x.data}"`,
zone_name: domain,
ttl: 60,
};
});
await descClient.clear('_acme-challenge', 'TXT');
await descClient.getRecords();
if (cRecords.length > 0) {
await descClient.createRecord(cRecords);
console.log('records updated');
}
}
}
main();