From c954f6aaafe8af3454b0aba8679551cc830d38ed Mon Sep 17 00:00:00 2001 From: Jens Beltofte Date: Thu, 24 Oct 2024 11:47:03 +0200 Subject: [PATCH] [1.1.1.1] Add NSLookup caching to dns-in-google-sheets.mdx (#17298) * Update dns-in-google-sheets.mdx to include cache support in the NSLookup function. Adding internal caching of the results on the NSLookup function to limit the number of the DNS resolver requests and speed up the results - especially in larger Google Sheets. * Process suggested changes for Answer TTL and usecache disabled by default * Change how minCacheTTL is set - Change minCacheTTL from Math.min to Math.max - Change default value of cachettl from 1800 to 30. * Minor code style - change tab to spaces. * Apply patch + rename usecache to useCache Applying changes from https://github.com/cloudflare/cloudflare-docs/pull/17298#issuecomment-2433142066 + rename usecache to useCache to make all variables/constants follow lowerCamelCase. * Remove outdated cache value from descriptive paragraph * Replace one remaining var by const --------- Co-authored-by: Rebecca Tamachiro --- .../dns-in-google-sheets.mdx | 54 ++++++++++++++----- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx index 71e6c516ad1401a..912e75eb5bb4da3 100644 --- a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx +++ b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx @@ -12,7 +12,7 @@ import { Details } from "~/components" 1.1.1.1 works directly inside Google Sheets. To get started, create a [Google Function](https://developers.google.com/apps-script/guides/sheets/functions) with the following code: ```js -function NSLookup(type, domain) { +function NSLookup(type, domain, useCache = false, minCacheTTL = 30) { if (typeof type == 'undefined') { throw new Error('Missing parameter 1 dns type'); @@ -22,26 +22,48 @@ function NSLookup(type, domain) { throw new Error('Missing parameter 2 domain name'); } - type = type.toUpperCase(); + if (typeof useCache != "boolean") { + throw new Error('Only boolean values allowed in 3 use cache'); + } - var url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type); + if (typeof minCacheTTL != "number") { + throw new Error('Only numeric values allowed in 4 min cache ttl'); + } - var options = { + type = type.toUpperCase(); + domain = domain.toLowerCase(); + + let cache = null; + if (useCache) { + // Cache key and hash + cacheKey = domain + "@" + type; + cacheHash = Utilities.base64Encode(cacheKey); + cacheBinKey = "nslookup-result-" + cacheHash; + + cache = CacheService.getScriptCache(); + const cachedResult = cache.get(cacheBinKey); + if (cachedResult != null) { + return cachedResult; + } + } + + const url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type); + const options = { muteHttpExceptions: true, headers: { accept: "application/dns-json" } }; - var result = UrlFetchApp.fetch(url, options); - var rc = result.getResponseCode(); - var resultText = result.getContentText(); + const result = UrlFetchApp.fetch(url, options); + const rc = result.getResponseCode(); + const resultText = result.getContentText(); if (rc !== 200) { throw new Error(rc); } - var errors = [ + const errors = [ { name: "NoError", description: "No Error"}, // 0 { name: "FormErr", description: "Format Error"}, // 1 { name: "ServFail", description: "Server Failure"}, // 2 @@ -54,19 +76,26 @@ function NSLookup(type, domain) { { name: "NotAuth", description: "Not Authorized"} // 9 ]; - var response = JSON.parse(resultText); + const response = JSON.parse(resultText); if (response.Status !== 0) { return errors[response.Status].name; } - var outputData = []; + const outputData = []; + let cacheTTL = 0; - for (var i in response.Answer) { + for (const i in response.Answer) { outputData.push(response.Answer[i].data); + const ttl = response.Answer[i].TTL; + cacheTTL = Math.min(cacheTTL || ttl, ttl); } - var outputString = outputData.join(','); + const outputString = outputData.join(','); + + if (useCache) { + cache.put(cacheBinKey, outputString, Math.max(cacheTTL, minCacheTTL)); + } return outputString; } @@ -76,6 +105,7 @@ function NSLookup(type, domain) { When you feed the function `NSLookup` a record type and a domain, you will get a DNS record value in the cell you called `NSLookup`. +To limit the number of DNS lookups and speed up the results (especially in larger Google Sheets), you can cache the returned DNS record value. Both the cache usage and the cache TTL can be controlled in arguments 3 and 4, respectively.