diff --git a/src/dns.js b/src/dns.js index 1d06eae98..39bee25d7 100644 --- a/src/dns.js +++ b/src/dns.js @@ -222,6 +222,11 @@ function makeWildcard(fqdn) { } async function registerLocation(location, options, recordType, recordValue) { + assert.strictEqual(typeof location, 'object'); + assert.strictEqual(typeof options, 'object'); + assert.strictEqual(typeof recordType, 'string'); + assert.strictEqual(typeof recordValue, 'string'); + const overwriteDns = options.overwriteDns || false; // get the current record before updating it @@ -267,13 +272,28 @@ async function registerLocations(locations, options, progressCallback) { await removeDnsRecords(location.subdomain, location.domain, 'CNAME', values); } - if (ipv4) await registerLocation(location, options, 'A', ipv4); - if (ipv6) await registerLocation(location, options, 'AAAA', ipv6); + if (ipv4) { + await registerLocation(location, options, 'A', ipv4); + } else { + const [error, values] = await safe(getDnsRecords(location.subdomain, location.domain, 'A')); + if (!error && values.length) await safe(removeDnsRecords(location.subdomain, location.domain, 'A', values), { debug }); + } + + if (ipv6) { + await registerLocation(location, options, 'AAAA', ipv6); + } else { + const [error, values] = await safe(getDnsRecords(location.subdomain, location.domain, 'AAAA')); + if (!error && values.length) await safe(removeDnsRecords(location.subdomain, location.domain, 'AAAA', values), { debug }); + } }); } } async function unregisterLocation(location, recordType, recordValue) { + assert.strictEqual(typeof location, 'object'); + assert.strictEqual(typeof recordType, 'string'); + assert.strictEqual(typeof recordValue, 'string'); + const [error] = await safe(removeDnsRecords(location.subdomain, location.domain, recordType, [ recordValue ])); if (!error || error.reason === BoxError.NOT_FOUND) return; @@ -313,9 +333,8 @@ async function syncDnsRecords(options, progressCallback) { const { domain:mailDomain, fqdn:mailFqdn, subdomain:mailSubdomain } = await mailServer.getLocation(); const dashboardLocation = await dashboard.getLocation(); - const allApps = await apps.list(); - - let progress = 1, errors = []; + const allApps = await apps.list(), errors = []; + let progress = 1; // we sync by domain only to get some nice progress for (const domain of allDomains) { diff --git a/src/dns/cloudflare.js b/src/dns/cloudflare.js index 005800b03..fd457f1b3 100644 --- a/src/dns/cloudflare.js +++ b/src/dns/cloudflare.js @@ -45,7 +45,7 @@ function translateRequestError(result) { if (typeof result.body.error === 'string') { message = `message: ${result.body.error} statusCode: ${result.statusCode}`; } else if (Array.isArray(result.body.errors) && result.body.errors.length > 0) { - let error = result.body.errors[0]; + const error = result.body.errors[0]; message = `message: ${error.message} statusCode: ${result.statusCode} code:${error.code}`; } return new BoxError(BoxError.ACCESS_DENIED, message);