我的 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
})
}
我的工人(通过我的 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))
})