Last active
April 12, 2026 23:12
-
-
Save bored-engineer/7e2008dc9e031bcab5089e37beb61f9a to your computer and use it in GitHub Desktop.
A minimal Cloudflare worker that behaves like a Splunk HEC collector
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
| export default { | |
| async fetch(request, env, ctx) { | |
| // Parse the request body as text as it's multiple concatenated JSON documented, ex: | |
| // {"event":{...},"time":"123"}{"event":{...},"time":"456"} | |
| let body = await request.text(); | |
| // Extract some request properties | |
| const requestIP = request.headers.get('CF-Connecting-IP'); | |
| const userAgent = request.headers.get('User-Agent'); | |
| // Convert to valid JSON by wrapping in an array and injecting commas | |
| let bodyJSON = JSON.parse(`[` + body.replace(/}{"/g, `},{"`) + `]`); | |
| // Iterate over the events | |
| for (const [idx, {time, event}] of bodyJSON.entries()) { | |
| const timeISO = new Date(parseFloat(time) * 1000).toISOString(); | |
| console.log({ | |
| message: `HEC Event ${idx} at ${timeISO} from '${requestIP}' via '${userAgent}'`, | |
| event: event, | |
| }); | |
| } | |
| // Pretend we successfully ingested the event | |
| return Response.json({ | |
| "text": "Success", | |
| "code": 0, | |
| }); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment