|
|
@ -17,7 +17,7 @@ export class CloudflareAccount {
|
|
|
|
* @param optionsArg
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
constructor(authTokenArg: string) {
|
|
|
|
constructor(authTokenArg: string) {
|
|
|
|
this.authToken = authTokenArg
|
|
|
|
this.authToken = authTokenArg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -34,25 +34,25 @@ export class CloudflareAccount {
|
|
|
|
|
|
|
|
|
|
|
|
public convenience = {
|
|
|
|
public convenience = {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* gets a zone id of a domain from cloudflare
|
|
|
|
* gets a zone id of a domain from cloudflare
|
|
|
|
* @param domainName
|
|
|
|
* @param domainName
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
getZoneId: async(domainName: string) => {
|
|
|
|
getZoneId: async (domainName: string) => {
|
|
|
|
const domain = new plugins.smartstring.Domain(domainName);
|
|
|
|
const domain = new plugins.smartstring.Domain(domainName);
|
|
|
|
const zoneArray = await this.convenience.listZones(domain.zoneName);
|
|
|
|
const zoneArray = await this.convenience.listZones(domain.zoneName);
|
|
|
|
const filteredResponse = zoneArray.filter(zoneArg => {
|
|
|
|
const filteredResponse = zoneArray.filter(zoneArg => {
|
|
|
|
return zoneArg.name === domainName;
|
|
|
|
return zoneArg.name === domainName;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (filteredResponse.length >= 1) {
|
|
|
|
if (filteredResponse.length >= 1) {
|
|
|
|
return filteredResponse[0].id;
|
|
|
|
return filteredResponse[0].id;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
plugins.smartlog.defaultLogger.log(
|
|
|
|
plugins.smartlog.defaultLogger.log(
|
|
|
|
'error',
|
|
|
|
'error',
|
|
|
|
`the domain ${domainName} does not appear to be in this account!`
|
|
|
|
`the domain ${domainName} does not appear to be in this account!`
|
|
|
|
);
|
|
|
|
);
|
|
|
|
throw new Error(`the domain ${domainName} does not appear to be in this account!`);
|
|
|
|
throw new Error(`the domain ${domainName} does not appear to be in this account!`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* gets a record
|
|
|
|
* gets a record
|
|
|
|
* @param domainNameArg
|
|
|
|
* @param domainNameArg
|
|
|
@ -121,7 +121,11 @@ export class CloudflareAccount {
|
|
|
|
* @param typeArg
|
|
|
|
* @param typeArg
|
|
|
|
* @param valueArg
|
|
|
|
* @param valueArg
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
updateRecord: async (domainNameArg: string, typeArg: plugins.tsclass.network.TDnsRecordType, valueArg) => {
|
|
|
|
updateRecord: async (
|
|
|
|
|
|
|
|
domainNameArg: string,
|
|
|
|
|
|
|
|
typeArg: plugins.tsclass.network.TDnsRecordType,
|
|
|
|
|
|
|
|
valueArg
|
|
|
|
|
|
|
|
) => {
|
|
|
|
// TODO: implement
|
|
|
|
// TODO: implement
|
|
|
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
|
|
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -188,29 +192,60 @@ export class CloudflareAccount {
|
|
|
|
method: methodArg,
|
|
|
|
method: methodArg,
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${this.authToken}`,
|
|
|
|
Authorization: `Bearer ${this.authToken}`,
|
|
|
|
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
|
|
|
|
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
|
|
|
|
...requestHeadersArg
|
|
|
|
...requestHeadersArg
|
|
|
|
},
|
|
|
|
},
|
|
|
|
requestBody: dataArg
|
|
|
|
requestBody: dataArg
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// route analysis
|
|
|
|
|
|
|
|
const routeWithoutQuery = routeArg.split('?')[0];
|
|
|
|
|
|
|
|
let queryParams: string[] = [];
|
|
|
|
|
|
|
|
if (routeArg.split('?').length > 1) {
|
|
|
|
|
|
|
|
queryParams = routeArg.split('?')[1].split('&');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// console.log(options);
|
|
|
|
// console.log(options);
|
|
|
|
|
|
|
|
|
|
|
|
let retryCount = 0; // count the amount of retries
|
|
|
|
let retryCount = 0; // count the amount of retries
|
|
|
|
let pageCount = 1;
|
|
|
|
let pageCount = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getQueryParams = () => {
|
|
|
|
|
|
|
|
let result = '';
|
|
|
|
|
|
|
|
if (queryParams.length > 0) {
|
|
|
|
|
|
|
|
result += '?';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let isFirst = true;
|
|
|
|
|
|
|
|
for (const queryParam of queryParams) {
|
|
|
|
|
|
|
|
if (!isFirst) {
|
|
|
|
|
|
|
|
result += '&';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
isFirst = false;
|
|
|
|
|
|
|
|
const queryParamSerialized = queryParam.split('=');
|
|
|
|
|
|
|
|
if (queryParam === 'page') {
|
|
|
|
|
|
|
|
result += `page=${pageCount}`;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
result += queryParam;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const makeRequest = async (): Promise<plugins.smartrequest.IExtendedIncomingMessage> => {
|
|
|
|
const makeRequest = async (): Promise<plugins.smartrequest.IExtendedIncomingMessage> => {
|
|
|
|
const requestUrl = `https://api.cloudflare.com/client/v4${routeArg}`;
|
|
|
|
const requestUrl = `https://api.cloudflare.com/client/v4${routeWithoutQuery}${getQueryParams()}`;
|
|
|
|
const response = await plugins.smartrequest.request(
|
|
|
|
const response = await plugins.smartrequest.request(requestUrl, options);
|
|
|
|
requestUrl,
|
|
|
|
|
|
|
|
options
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
if(response.body.result_info) {
|
|
|
|
if (response.body.result_info) {
|
|
|
|
const rI = response.body.result_info;
|
|
|
|
const rI = response.body.result_info;
|
|
|
|
if ((rI.total_count / rI.per_page) > pageCount) {
|
|
|
|
if (rI.total_count / rI.per_page > pageCount) {
|
|
|
|
pageCount++;
|
|
|
|
pageCount++;
|
|
|
|
return await makeRequest();
|
|
|
|
const subresponse = await makeRequest();
|
|
|
|
|
|
|
|
response.body.result = response.body.result.concat(subresponse.body.result);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return response;
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -221,12 +256,12 @@ export class CloudflareAccount {
|
|
|
|
console.log('rate limited! Waiting for retry!');
|
|
|
|
console.log('rate limited! Waiting for retry!');
|
|
|
|
return await retryRequest();
|
|
|
|
return await retryRequest();
|
|
|
|
} else if (response.statusCode === 400) {
|
|
|
|
} else if (response.statusCode === 400) {
|
|
|
|
console.log(`bad request for route ${routeArg}!`);
|
|
|
|
console.log(`bad request for route ${requestUrl}!`);
|
|
|
|
console.log(response.body);
|
|
|
|
console.log(response.body);
|
|
|
|
throw new Error(`request failed for ${routeArg}`);
|
|
|
|
throw new Error(`request failed for ${requestUrl}`);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.log(response.statusCode);
|
|
|
|
console.log(response.body);
|
|
|
|
throw Error('request failed');
|
|
|
|
throw new Error(`request failed for ${requestUrl} with status${response.statusCode}}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|