Best way for DNS lookups by workers

I have some things I’d like to use a cloudflare worker for. They do DNS lookups for TXT records. And potentially will do other things like look up NS and MX records. I.e. more than the standard hostname->IP address

I’ve got this working by using DoH to 1.1.1.1 and requesting a JSON response in a fetch… but I feel there ought to be a better way to do it, in part because I see the worker limits as 6 requests simultaneously and 50 in total and while I doubt I’d hit the 50 limit I have hit the 6 simultaneous requests limit

First, am I missing a standard workers DNS library/functrion that can do this? The worker protocol page says I can use TCP sockets but not UDP so UDP DNS seems to be not supported. Is this correct?

Second if I use 1.1.1.1 and DoH is there are way to keep the request open and make multiple queries not just one at a time so that there’s only one request not 1-10

Regards

Francis

Current code is nicked from the worker example

    const dnsHost = "https://1.1.1.1/dns-query?";
...
    const url = dnsHost + "type=TXT&name="+revip+".origin.asn.cymru.com";

    async function gatherResponse(response) {
      const ans = await response.json();
      if ((response.status == 200 )&& ans["Answer"] && ans["Answer"][0]) {
        return (ans["Answer"][0]["data"]+"\n");
      } else {
        return (JSON.stringify(ans)+"\n");
      }
    }

    const dnshdr = {
      headers: {
        "accept": "application/dns-json",
      },
    };

    const ipresponse = await fetch(url, dnshdr);
    const ipresults = await gatherResponse(ipresponse);

Hey Francis,

Currently, there isn’t a dedicated DNS library or function within Cloudflare Workers for performing DNS lookups. You are correct that UDP is not supported, so traditional DNS queries over UDP can’t be done within Workers.

For your second question, the DoH (DNS over HTTPS) requests to 1.1.1.1 are stateless, meaning you can’t keep the connection open to make multiple queries over a single request. Each DNS query will be a separate HTTPS request.

However, you can optimize your Worker by managing the concurrency of your fetch requests to stay within the limits. If you’re hitting the limit of 6 simultaneous connections, consider implementing a queue system within your Worker to handle DNS lookups sequentially or with controlled concurrency.

If you’re expecting high volumes of DNS queries, you might want to contact Cloudflare Sales to discuss your use case and any potential solutions they can offer.

1 Like

Thanks. I’ve got ways to manage the 6 simultaneous connections limit. I was just hoping there were better ways to do this.

1 Like

Hi @Micronetia, your topic has a solution here.

Let us know what you think of the solution by logging in and give it a :+1: or :-1:.


Solutions help the person that asked the question and anyone else that sees the answer later. Login to tell us what you think of the solution with a :+1: or :-1:.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.