Skip to content

Commit

Permalink
fix: only purge cache on Cloudflare for verification attempts (with 3…
Browse files Browse the repository at this point in the history
…s timeout and 1 retry)
  • Loading branch information
titanism committed Sep 29, 2023
1 parent 059d240 commit 351a1fd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
3 changes: 2 additions & 1 deletion app/controllers/web/my-account/verify-records.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ async function verifyRecords(ctx) {

const { ns, txt, mx, errors } = await Domains.getVerificationResults(
domain,
ctx.resolver
ctx.resolver,
true
);

//
Expand Down
59 changes: 31 additions & 28 deletions app/models/domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -1028,41 +1028,44 @@ async function verifySMTP(domain, resolver) {

Domains.statics.verifySMTP = verifySMTP;

async function getVerificationResults(domain, resolver) {
async function getVerificationResults(domain, resolver, purgeCache = false) {
const verificationRecord = `${config.recordPrefix}-site-verification=${domain.verification_record}`;
const verificationMarkdown = `<span class="markdown-body ml-0 mr-0"><code>${verificationRecord}</code></span>`;
const isPaidPlan = _.isString(domain.plan) && domain.plan !== 'free';

//
// attempt to purge Cloudflare cache programmatically
//
try {
await pMap(
CACHE_TYPES,
(type) => {
const url = new URL(CLOUDFLARE_PURGE_CACHE_URL);
url.searchParams.append('domain', domain.name);
url.searchParams.append('type', type);
return retryRequest(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'User-Agent': USER_AGENT
},
timeout: ms('5s'),
retries: 2
});
},
{ concurrency }
);
logger.debug('cleared DNS cache for cloudflare', {
domain,
types: CACHE_TYPES
});
// wait one second for DNS changes to propagate
await delay(ms('1s'));
} catch (err) {
logger.error(err);
if (purgeCache) {
try {
await pMap(
CACHE_TYPES,
(type) => {
const url = new URL(CLOUDFLARE_PURGE_CACHE_URL);
url.searchParams.append('domain', domain.name);
url.searchParams.append('type', type);
return retryRequest(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'User-Agent': USER_AGENT
},
timeout: ms('3s'),
retries: 1
});
},
{ concurrency }
);
logger.debug('cleared DNS cache for cloudflare', {
domain,
types: CACHE_TYPES
});
// wait one second for DNS changes to propagate
await delay(ms('1s'));
} catch (err) {
err.domain = domain;
logger.error(err);
}
}

const errors = [];
Expand Down
5 changes: 4 additions & 1 deletion helpers/retry-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ class RetryClient extends undici.Client {
if (response.statusCode !== 200) {
// still need to consume body even if an error occurs
const body = await response.body.text();
throw new undici.errors.ResponseStatusCodeError(
const err = new undici.errors.ResponseStatusCodeError(
`Response status code ${response.statusCode}`,
response.statusCode,
response.headers,
body
);
err.options = options;
err.count = count;
throw err;
}

return response;
Expand Down
6 changes: 5 additions & 1 deletion helpers/retry-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ async function retryRequest(url, opts = {}, count = 1) {
if (response.statusCode !== 200) {
// still need to consume body even if an error occurs
const body = await response.body.text();
throw new undici.errors.ResponseStatusCodeError(
const err = new undici.errors.ResponseStatusCodeError(
`Response status code ${response.statusCode}`,
response.statusCode,
response.headers,
body
);
err.url = url;
err.options = opts;
err.count = count;
throw err;
}

return response;
Expand Down

0 comments on commit 351a1fd

Please sign in to comment.