来自Cloudflare工作者的对Cloudflare DNS的请求未返回DNS结果

时间:2019-06-13 11:31:55

标签: dns cloudflare cloudflare-workers

我有一个Cloudflare(CF)工作者,我希望使用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"
        }
    ]
}

但是,在结果中,我得到的似乎是作为 fetch()的一部分建立的websocket的结果(假设我绕过循环一次)< / p>

[
    {
        "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响应?

1 个答案:

答案 0 :(得分:1)

fetch()返回对Response object的承诺,其中包含响应状态,标头和正文流。该对象就是您在“结果”中看到的。为了读取响应 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)