Last active
January 25, 2024 23:34
-
-
Save erfanium/3befdf7bdced75e2c47606893a668237 to your computer and use it in GitHub Desktop.
sentry tunnel via cloudflare workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const corsHeaders = { | |
"Access-Control-Allow-Origin": "*", | |
"Access-Control-Allow-Methods": "*", | |
"Access-Control-Allow-Headers": "Content-Type", | |
"Access-Control-Max-Age": "86400", | |
}; | |
// Set known project IDs for the Sentry projects you want to accept through this proxy | |
const knownProjectIds = []; // TODO: fill this | |
async function handleRequest(request) { | |
const host = "sentry.io"; | |
const envelope = await request.text(); | |
const pieces = envelope.split("\n", 2); | |
const header = JSON.parse(pieces[0]); | |
if (header["dsn"]) { | |
const dsn = new URL(header["dsn"]); | |
console.log(`[dsn.pathname] `, dsn.pathname); | |
const projectId = parseInt(dsn.pathname.split("/").filter(Boolean)[0]); | |
if (knownProjectIds.includes(projectId)) { | |
console.log(`made it here`); | |
const response = await fetch( | |
`https://${host}/api/${projectId}/envelope/`, | |
{ | |
method: "POST", | |
headers: { | |
"Content-Type": "application/x-sentry-envelope", | |
}, | |
body: envelope, | |
} | |
); | |
return new Response(response.body, { | |
status: response.status, | |
statusText: response.statusText, | |
headers: { | |
...response.headers, | |
...corsHeaders, | |
}, | |
}); | |
} | |
} | |
// Return a 404 Not Found response for all other requests | |
return new Response(null, { status: 404 }); | |
} | |
function handleOptions(request) { | |
// Make sure the necessary headers are present | |
// for this to be a valid pre-flight request | |
let headers = request.headers; | |
if ( | |
headers.get("Origin") !== null && | |
headers.get("Access-Control-Request-Method") !== null && | |
headers.get("Access-Control-Request-Headers") !== null | |
) { | |
// Handle CORS pre-flight request. | |
// If you want to check or reject the requested method + headers | |
// you can do that here. | |
let respHeaders = { | |
...corsHeaders, | |
// Allow all future content Request headers to go back to browser | |
// such as Authorization (Bearer) or X-Client-Name-Version | |
"Access-Control-Allow-Headers": request.headers.get( | |
"Access-Control-Request-Headers" | |
), | |
}; | |
return new Response(null, { | |
headers: respHeaders, | |
}); | |
} else { | |
// Handle standard OPTIONS request. | |
// If you want to allow other HTTP Methods, you can do that here. | |
return new Response(null, { | |
headers: { | |
Allow: "*", | |
}, | |
}); | |
} | |
} | |
export default { | |
async fetch(request) { | |
if (request.method === "OPTIONS") { | |
return await handleOptions(request); | |
} | |
return await handleRequest(request); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment