App Worker和Custom Worker发生冲突

时间:2019-03-26 03:54:05

标签: cloudflare cloudflare-workers cloudflare-apps

我的Cloudflare App工作程序似乎与用户的自定义工作程序冲突。基本上,当他们安装了工作程序并安装我的应用程序时,他们的网站完全不会响应。

我不确定是什么原因造成的。我的工作人员根本没有修改请求。他们的是,但是我看不到任何明显的东西。

我在他的允许下将他的工人张贴在这里。

他们的工人:

let securityHeaders = {
    "Content-Security-Policy" : "upgrade-insecure-requests",
    "Strict-Transport-Security" : "max-age=1000",
    "X-Xss-Protection" : "1; mode=block",
    "X-Frame-Options" : "DENY",
    "X-Content-Type-Options" : "nosniff",
    "Referrer-Policy" : "strict-origin-when-cross-origin",
}

let sanitiseHeaders = {
    "Plan" : "Dental",
    "Batman" : "Scientist",
    "Lisa-Needs" : "Braces"
}

let removeHeaders = [
    "x-goog-generation",
    "x-goog-hash",
    "x-goog-metageneration",
  "x-goog-meta-goog-reserved-file-mtime",
  "x-goog-stored-content-encoding",
  "x-goog-storage-class",
  "x-goog-stored-content-length",
  "X-GUploader-UploadID"
]

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

async function addHeaders(req) {
    let response = await fetch(req)
    let newHdrs = new Headers(response.headers)

    if (newHdrs.has("Content-Type") && !newHdrs.get("Content-Type").includes("text/html")) {
        return new Response(response.body , {
            status: response.status,
            statusText: response.statusText,
            headers: newHdrs
        })
    }

    Object.keys(securityHeaders).map(function(name, index) {
        newHdrs.set(name, securityHeaders[name]);
    })

    Object.keys(sanitiseHeaders).map(function(name, index) {
        newHdrs.set(name, sanitiseHeaders[name]);
    })

    removeHeaders.forEach(function(name){
        newHdrs.delete(name)
    })

    return new Response(response.body , {
        status: response.status,
        statusText: response.statusText,
        headers: newHdrs
    })
}

我的工作者(通过my Cloudflare App安装):

function buildLogEntry(request, response) {
  const options = INSTALL_OPTIONS

  const logDefs = {
    rMeth: request.method,
    rUrl: request.url,
    uAgent: request.headers.get("user-agent"),
    cfRay: request.headers.get("cf-ray"),
    cIP: request.headers.get("cf-connecting-ip"),
    statusCode: response.status,
    contentLength: response.headers.get("content-legth"),
    cfCacheStatus: response.headers.get("cf-cache-status"),
    contentType: response.headers.get("content-type"),
    responseConnection: response.headers.get("connection"),
    requestConnection: request.headers.get("connection"),
    cacheControl: response.headers.get("cache-control"),
    acceptRanges: response.headers.get("accept-ranges"),
    expectCt: response.headers.get("expect-ct"),
    expires: response.headers.get("expires"),
    lastModified: response.headers.get("last-modified"),
    vary: response.headers.get("vary"),
    server: response.headers.get("server"),
    etag: response.headers.get("etag"),
    date: response.headers.get("date"),
    transferEncoding: response.headers.get("transfer-encoding"),
  }

  const logArray = []

  options.metadata.forEach(entry => {
    logArray.push(logDefs[entry.field])
  })

  const logEntry = logArray.join(" | ")

  return logEntry
}

async function handleRequest(event) {
  const { request } = event
  const response = await fetch(request)
  const rHost = request.headers.get("host")

  const options = INSTALL_OPTIONS
  const sourceKey = options.source
  const apiKey = options.logflare.api_key

  const logEntry = buildLogEntry(request, response)

  const init = {
    method: "POST",
    headers: {
      "X-API-KEY": apiKey,
      "Content-Type": "application/json",
      "User-Agent": `Cloudflare Worker via ${rHost}`,
    },
    body: JSON.stringify({ source: sourceKey, log_entry: logEntry }),
  }

  event.waitUntil(fetch("https://logflare.app/api/logs", init))

  // console.log(cIP)

  return response
}

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

1 个答案:

答案 0 :(得分:0)

这听起来可能是系统中更深的错误,而不是您的错。您能给我发电子邮件(kenfla at cloudflare),我们将尝试追踪这里发生的事情吗?