2022-09-27 17:23:20 +00:00
|
|
|
import * as plugins from './cloudflare.plugins.js';
|
|
|
|
import { logger } from './cloudflare.logger.js';
|
|
|
|
import * as interfaces from './interfaces/index.js';
|
2018-08-13 23:53:52 +00:00
|
|
|
|
|
|
|
// interfaces
|
2022-09-27 17:23:20 +00:00
|
|
|
import { WorkerManager } from './cloudflare.classes.workermanager.js';
|
|
|
|
import { ZoneManager } from './cloudflare.classes.zonemanager.js';
|
2018-08-13 23:53:52 +00:00
|
|
|
|
|
|
|
export class CloudflareAccount {
|
2020-02-19 16:58:46 +00:00
|
|
|
private authToken: string;
|
2019-07-18 12:25:10 +00:00
|
|
|
private accountIdentifier: string;
|
2019-07-18 09:51:56 +00:00
|
|
|
|
|
|
|
public workerManager = new WorkerManager(this);
|
|
|
|
public zoneManager = new ZoneManager(this);
|
|
|
|
|
2024-06-15 17:47:09 +00:00
|
|
|
public apiAccount: plugins.cloudflare.Cloudflare;
|
|
|
|
|
2019-07-18 12:25:10 +00:00
|
|
|
/**
|
|
|
|
* constructor sets auth information on the CloudflareAccountInstance
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
2020-02-19 16:58:46 +00:00
|
|
|
constructor(authTokenArg: string) {
|
2020-02-28 14:47:03 +00:00
|
|
|
this.authToken = authTokenArg;
|
2024-06-15 17:47:09 +00:00
|
|
|
this.apiAccount = new plugins.cloudflare.Cloudflare({
|
|
|
|
apiToken: this.authToken,
|
|
|
|
});
|
2019-07-18 12:25:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 14:24:36 +00:00
|
|
|
public convenience = {
|
|
|
|
/**
|
2020-02-28 14:47:03 +00:00
|
|
|
* gets a zone id of a domain from cloudflare
|
|
|
|
* @param domainName
|
|
|
|
*/
|
|
|
|
getZoneId: async (domainName: string) => {
|
|
|
|
const domain = new plugins.smartstring.Domain(domainName);
|
|
|
|
const zoneArray = await this.convenience.listZones(domain.zoneName);
|
2021-01-22 20:46:26 +00:00
|
|
|
const filteredResponse = zoneArray.filter((zoneArg) => {
|
2020-02-28 14:47:03 +00:00
|
|
|
return zoneArg.name === domainName;
|
|
|
|
});
|
|
|
|
if (filteredResponse.length >= 1) {
|
|
|
|
return filteredResponse[0].id;
|
|
|
|
} else {
|
2020-06-10 05:34:47 +00:00
|
|
|
logger.log('error', `the domain ${domainName} does not appear to be in this account!`);
|
2020-02-28 14:47:03 +00:00
|
|
|
throw new Error(`the domain ${domainName} does not appear to be in this account!`);
|
|
|
|
}
|
|
|
|
},
|
2020-02-09 18:22:34 +00:00
|
|
|
/**
|
|
|
|
* gets a record
|
|
|
|
* @param domainNameArg
|
|
|
|
* @param typeArg
|
|
|
|
*/
|
|
|
|
getRecord: async (
|
|
|
|
domainNameArg: string,
|
2020-02-19 14:16:23 +00:00
|
|
|
typeArg: plugins.tsclass.network.TDnsRecordType
|
2024-06-15 17:47:09 +00:00
|
|
|
): Promise<plugins.ICloudflareTypes['Record']> => {
|
2020-02-09 18:22:34 +00:00
|
|
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
|
|
|
const recordArrayArg = await this.convenience.listRecords(domain.zoneName);
|
2021-01-22 20:46:26 +00:00
|
|
|
const filteredResponse = recordArrayArg.filter((recordArg) => {
|
2020-02-09 18:22:34 +00:00
|
|
|
return recordArg.type === typeArg && recordArg.name === domainNameArg;
|
|
|
|
});
|
|
|
|
return filteredResponse[0];
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* creates a record
|
|
|
|
*/
|
|
|
|
createRecord: async (
|
|
|
|
domainNameArg: string,
|
2020-02-19 14:16:23 +00:00
|
|
|
typeArg: plugins.tsclass.network.TDnsRecordType,
|
2021-01-22 20:45:35 +00:00
|
|
|
contentArg: string,
|
|
|
|
ttlArg = 1
|
2020-02-09 18:22:34 +00:00
|
|
|
): Promise<any> => {
|
|
|
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
2024-06-15 17:47:09 +00:00
|
|
|
const zoneId = await this.convenience.getZoneId(domain.zoneName);
|
|
|
|
const response = await this.apiAccount.dns.records.create({
|
|
|
|
zone_id: zoneId,
|
|
|
|
type: typeArg as any,
|
2020-02-09 18:22:34 +00:00
|
|
|
name: domain.fullName,
|
2021-01-22 20:45:35 +00:00
|
|
|
content: contentArg,
|
2021-01-22 20:46:26 +00:00
|
|
|
ttl: ttlArg,
|
2024-06-15 17:47:09 +00:00
|
|
|
})
|
2020-02-09 18:22:34 +00:00
|
|
|
return response;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* removes a record from Cloudflare
|
|
|
|
* @param domainNameArg
|
|
|
|
* @param typeArg
|
|
|
|
*/
|
|
|
|
removeRecord: async (
|
|
|
|
domainNameArg: string,
|
2020-02-19 14:16:23 +00:00
|
|
|
typeArg: plugins.tsclass.network.TDnsRecordType
|
2020-02-09 18:22:34 +00:00
|
|
|
): Promise<any> => {
|
|
|
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
2024-06-15 17:47:09 +00:00
|
|
|
const zoneId = await this.convenience.getZoneId(domain.zoneName);
|
|
|
|
const records = await this.convenience.listRecords(domain.zoneName);
|
|
|
|
const recordToDelete = records.find((recordArg) => {
|
|
|
|
return recordArg.name === domainNameArg && recordArg.type === typeArg;
|
|
|
|
});
|
|
|
|
if (recordToDelete) {
|
|
|
|
await this.apiAccount.dns.records.delete(recordToDelete.id, {
|
|
|
|
zone_id: zoneId,
|
|
|
|
});
|
2020-02-09 18:22:34 +00:00
|
|
|
} else {
|
2024-06-15 17:47:09 +00:00
|
|
|
logger.log('warn', `record ${domainNameArg} of type ${typeArg} not found`);
|
2020-02-09 18:22:34 +00:00
|
|
|
}
|
|
|
|
},
|
2024-06-15 17:47:09 +00:00
|
|
|
|
2020-02-10 11:26:13 +00:00
|
|
|
/**
|
|
|
|
* cleanrecord allows the cleaning of any previous records to avoid unwanted sideeffects
|
|
|
|
*/
|
2020-02-19 14:16:23 +00:00
|
|
|
cleanRecord: async (domainNameArg: string, typeArg: plugins.tsclass.network.TDnsRecordType) => {
|
|
|
|
console.log(`cleaning record for ${domainNameArg}`);
|
2024-06-15 17:47:09 +00:00
|
|
|
const records = await this.convenience.listRecords(domainNameArg);
|
|
|
|
const recordsToDelete = records.filter((recordArg) => {
|
|
|
|
return recordArg.type === typeArg;
|
|
|
|
});
|
|
|
|
for (const recordToDelete of recordsToDelete) {
|
|
|
|
await this.apiAccount.dns.records.delete(recordToDelete.id, {
|
|
|
|
zone_id: recordToDelete.zone_id,
|
|
|
|
});
|
|
|
|
}
|
2020-02-10 14:40:55 +00:00
|
|
|
},
|
2024-06-15 17:47:09 +00:00
|
|
|
|
2020-02-09 18:22:34 +00:00
|
|
|
/**
|
|
|
|
* updates a record
|
|
|
|
* @param domainNameArg
|
|
|
|
* @param typeArg
|
|
|
|
* @param valueArg
|
|
|
|
*/
|
2020-02-28 14:47:03 +00:00
|
|
|
updateRecord: async (
|
|
|
|
domainNameArg: string,
|
|
|
|
typeArg: plugins.tsclass.network.TDnsRecordType,
|
|
|
|
valueArg
|
|
|
|
) => {
|
2020-02-09 18:22:34 +00:00
|
|
|
// TODO: implement
|
|
|
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* list all records of a specified domain name
|
|
|
|
* @param domainNameArg - the domain name that you want to get the records from
|
|
|
|
*/
|
2024-06-15 17:47:09 +00:00
|
|
|
listRecords: async (domainNameArg: string) => {
|
2020-02-09 18:22:34 +00:00
|
|
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
2024-06-15 17:47:09 +00:00
|
|
|
const zoneId = await this.convenience.getZoneId(domain.zoneName);
|
|
|
|
const records: plugins.ICloudflareTypes['Record'][] = [];
|
|
|
|
for await (const record of this.apiAccount.dns.records.list({
|
|
|
|
zone_id: zoneId,
|
|
|
|
})) {
|
|
|
|
records.push(record);
|
|
|
|
}
|
|
|
|
return records;
|
2020-02-09 18:22:34 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* list all zones in the associated authenticated account
|
|
|
|
* @param domainName
|
|
|
|
*/
|
2024-06-15 17:47:09 +00:00
|
|
|
listZones: async (domainName?: string) => {
|
|
|
|
const zones: plugins.ICloudflareTypes['Zone'][] = [];
|
|
|
|
for await (const zone of this.apiAccount.zones.list()) {
|
|
|
|
zones.push(zone);
|
2020-02-09 18:22:34 +00:00
|
|
|
}
|
2024-06-15 17:47:09 +00:00
|
|
|
return zones;
|
2020-02-09 18:22:34 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* purges a zone
|
|
|
|
*/
|
|
|
|
purgeZone: async (domainName: string): Promise<void> => {
|
|
|
|
const domain = new plugins.smartstring.Domain(domainName);
|
2024-06-15 17:47:09 +00:00
|
|
|
const zoneId = await this.convenience.getZoneId(domain.zoneName);
|
|
|
|
await this.apiAccount.cache.purge({
|
|
|
|
zone_id: zoneId,
|
2021-01-22 20:46:26 +00:00
|
|
|
purge_everything: true,
|
2024-06-15 17:47:09 +00:00
|
|
|
});
|
2020-02-10 11:26:13 +00:00
|
|
|
},
|
2024-06-15 17:47:09 +00:00
|
|
|
|
2020-02-10 11:26:13 +00:00
|
|
|
// acme convenience functions
|
|
|
|
acmeSetDnsChallenge: async (dnsChallenge: plugins.tsclass.network.IDnsChallenge) => {
|
2020-02-10 14:40:55 +00:00
|
|
|
await this.convenience.cleanRecord(dnsChallenge.hostName, 'TXT');
|
2022-09-27 17:24:52 +00:00
|
|
|
await this.convenience.createRecord(
|
|
|
|
dnsChallenge.hostName,
|
|
|
|
'TXT',
|
|
|
|
dnsChallenge.challenge,
|
|
|
|
120
|
|
|
|
);
|
2020-02-10 11:26:13 +00:00
|
|
|
},
|
|
|
|
acmeRemoveDnsChallenge: async (dnsChallenge: plugins.tsclass.network.IDnsChallenge) => {
|
|
|
|
await this.convenience.removeRecord(dnsChallenge.hostName, 'TXT');
|
2021-01-22 20:46:26 +00:00
|
|
|
},
|
2020-02-09 18:22:34 +00:00
|
|
|
};
|
2018-08-13 23:53:52 +00:00
|
|
|
}
|