-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_subdomain.js
executable file
·97 lines (88 loc) · 3.5 KB
/
index_subdomain.js
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
#!/usr/bin/env node
import 'dotenv/config';
import process from 'process';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import dayjs from 'dayjs';
import fs from 'fs';
import Alidns20150109 from '@alicloud/alidns20150109';
import OpenApi from '@alicloud/openapi-client';
import Util from '@alicloud/tea-util';
const {
ALIYUN_ACCESSKEY_ID,
ALIYUN_ACCESSKEY_SECRET,
CERTBOT_DOMAIN,
CERTBOT_VALIDATION
} = process.env;
const argv = yargs(hideBin(process.argv)).argv;
const config = new OpenApi.Config({
accessKeyId: ALIYUN_ACCESSKEY_ID,
accessKeySecret: ALIYUN_ACCESSKEY_SECRET,
});
config.endpoint = 'alidns.cn-beijing.aliyuncs.com';
const client = new Alidns20150109.default(config);
const logger = (...args) => {
console.log(`[${dayjs().format('YYYY-MM-DD HH:mm:ss')}]`, ...args);
fs.appendFileSync('./latest.log', `[${dayjs().format('YYYY-MM-DD HH:mm:ss')}] ${args.join(' ')}\n`);
};
const addDomainRecord = (domainName, value) => {
const domain = domainName.split('.');
const subdomain = domain.splice(0, domain.length - 2);
const addDomainRecordRequest = new Alidns20150109.AddDomainRecordRequest({
domainName: domain.join('.'),
RR: ['_acme-challenge', ...subdomain].join('.'),
type: 'TXT',
value,
});
const runtime = new Util.RuntimeOptions({});
return client.addDomainRecordWithOptions(addDomainRecordRequest, runtime);
};
const deleteDomainRecord = (recordId) => {
const deleteDomainRecordRequest = new Alidns20150109.DeleteDomainRecordRequest({
recordId,
});
const runtime = new Util.RuntimeOptions({});
return client.deleteDomainRecordWithOptions(deleteDomainRecordRequest, runtime);
};
const describeDomainRecord = (domainName, value) => {
const domain = domainName.split('.');
const subdomain = domain.splice(0, domain.length - 2);
const deleteDomainRecordRequest = new Alidns20150109.DescribeDomainRecordsRequest({
domainName: domain.join('.'),
RR: ['_acme-challenge', ...subdomain].join('.'),
typeKeyWord: 'TXT',
valueKeyWord: value,
});
const runtime = new Util.RuntimeOptions({});
return client.describeDomainRecordsWithOptions(deleteDomainRecordRequest, runtime);
};
if (!argv.clean) {
if (CERTBOT_DOMAIN && CERTBOT_VALIDATION) {
logger('Running manual auth hook, please be patient...');
logger('Adding record:', CERTBOT_DOMAIN, '_acme-challenge', 'TXT', CERTBOT_VALIDATION);
await addDomainRecord(CERTBOT_DOMAIN, CERTBOT_VALIDATION);
logger('Record Added:', CERTBOT_DOMAIN, '_acme-challenge', 'TXT', CERTBOT_VALIDATION);
logger('Please wait for the DNS to be updated...');
setTimeout(() => {}, 10000);
} else {
logger('CERTBOT_DOMAIN & CERTBOT_VALIDATION required.');
}
} else {
if (CERTBOT_DOMAIN && CERTBOT_VALIDATION) {
logger('Running manual cleanup hook, please be patient...');
logger('Looking up record:', CERTBOT_DOMAIN, '_acme-challenge', 'TXT', CERTBOT_VALIDATION);
const res = await describeDomainRecord(CERTBOT_DOMAIN, CERTBOT_VALIDATION);
const records = res.body.domainRecords.record;
if (records.length > 0) {
for (let record of records) {
logger('Record Found:', CERTBOT_DOMAIN, '_acme-challenge', 'TXT', record.value);
await deleteDomainRecord(record.recordId);
logger('Record Deleted:', CERTBOT_DOMAIN, '_acme-challenge', 'TXT', record.value);
}
} else {
logger('Record Not Found:', CERTBOT_DOMAIN, '_acme-challenge', 'TXT', CERTBOT_VALIDATION);
}
} else {
logger('CERTBOT_DOMAIN & CERTBOT_VALIDATION required.');
}
}