Cloudflare worker 对 Cloudflare DNS 的请求未返回 DNS 结果

Request to Cloudflare DNS from Cloudflare worker not returning the DNS result

我有一个 Cloudflare (CF) worker,我想使用 CF DNS (https://developers.cloudflare.com/1.1.1.1/dns-over-https/json-format/) 发出一些 DNS 请求。

所以一个非常基本的工人:

/**
 * readRequestBody reads in the incoming request body
 * Use await readRequestBody(..) in an async function to get the string
 * @param {Request} request the incoming request to read from
 */
async function readRequestBody(request) {
  const { headers } = request
  const contentType = headers.get('content-type')

  if (contentType.includes('application/json')) {
    const body = await request.json()
    return JSON.stringify(body)
  }

  return ''
}

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
   let reqBody = await readRequestBody(request)

   var jsonTlds = JSON.parse(reqBody);

   const fetchInit = {
      method: 'GET',
    }
   let promises = []
   for (const tld of jsonTlds.tlds) {

       //Dummy request until I can work out why I am not getting the response of the DNS query
       var requestStr = 'https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=example.com&type=A'

       let promise = fetch(requestStr, fetchInit)

       promises.push(promise)
   }

   try {
      let results = await Promise.all(promises)

      return new Response(JSON.stringify(results), {status: 200})
   } catch(err) {
       return new Response(JSON.stringify(err), {status: 500})
   }
}

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

我现在刚刚将 DNS 查询硬编码为:

https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=example.com&type=A

我希望得到的 JSON 结果是:

{
    "Status": 0,
    "TC": false,
    "RD": true,
    "RA": true,
    "AD": true,
    "CD": false,
    "Question": [
        {
            "name": "example.com.",
            "type": 1
        }
    ],
    "Answer": [
        {
            "name": "example.com.",
            "type": 1,
            "TTL": 9540,
            "data": "93.184.216.34"
        }
    ]
}

然而,在 results 中,我得到的似乎是作为 fetch() 的一部分建立的 websocket 的结果(假设我绕了一圈)

[
    {
        "webSocket": null,
        "url": "https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=example.com&type=A",
        "redirected": false,
        "ok": true,
        "headers": {},
        "statusText": "OK",
        "status": 200,
        "bodyUsed": false,
        "body": {
            "locked": false
        }
    }
]

所以我的问题是,我在这里做错了什么,以至于我没有从 1.1.1.1 API 获得 DNS JSON 响应?

fetch() returns Response object 的承诺,其中包含响应状态 headers 和 body 流。 object 就是您在 "results" 中看到的内容。为了读取响应 body,您必须进行进一步的调用。

尝试像这样定义一个函数:

async function fetchJsonBody(req, init) {
  let response = await fetch(req, init);
  if (!response.ok()) {
    // Did not return status 200; throw an error.
    throw new Error(response.status + " " + response.statusText);
  }

  // OK, now we can read the body and parse it as JSON.
  return await response.json();
}

现在您可以更改:

let promise = fetch(requestStr, fetchInit)

至:

let promise = fetchJsonBody(requestStr, fetchInit)