Skip to content

Instantly share code, notes, and snippets.

@auravoid
Last active September 9, 2022 02:59
Show Gist options
  • Save auravoid/3dc2fbd218f4fbaacaef4d411e28388b to your computer and use it in GitHub Desktop.
Save auravoid/3dc2fbd218f4fbaacaef4d411e28388b to your computer and use it in GitHub Desktop.
Cloudflare Worker Fail2Ban Discord Webhook

index.js is the Worker file

wrangler.toml is the Wrangler file that holds your variables

fail2banaction.conf is the action you can add to Fail2Ban

[Definition]
actionstart = curl -d '{"content": "Fail2ban has started."}' -H "Content-Type: application/json" -X POST "https://yourdiscordwebhook.url/"
actionstop = curl -d '{"content": "Fail2ban has stopped."}' -H "Content-Type: application/json" -X POST "https://yourdiscordwebhook.url/"
actioncheck =
actionban = curl -d '{"ip": "`<ip>`", "failures": "`<failures>`", "type": "ban"}' -H "Content-Type: application/json" -H "cf-webhook-auth: your-secret-key" -X POST "https://your.worker.workers.dev/"
actionunban = curl -d '{"ip": "`<ip>`", "failures": "null", "type": "unban"}' -H "Content-Type: application/json" -H "cf-webhook-auth: your-secret-key" -X POST "https://your.worker.workers.dev/"
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const headers = request.headers;
if (headers.get("cf-webhook-auth") !== WEBHOOK_SECRET) {
return new Response(":(", {
headers: { "content-type": "text/plain" },
status: 401,
});
}
let incReq = await request.json();
console.log(incReq);
let dataJSON = {};
let init = {};
let response;
switch (incReq.type) {
case "ban":
dataJSON = {
username: "Fail2Ban Bans",
avatar_url:
"https://domoticproject.com/wp-content/uploads/2018/06/fail2ban-logo.jpg",
content: "",
embeds: [
{
title: `\`${incReq.ip}\``,
color: 3840,
description: "Banned",
timestamp: "",
author: {
name: "",
},
image: {},
thumbnail: {},
footer: {},
fields: [
{
name: "Failures",
value: incReq.failures,
inline: true,
},
{
name: "Time",
value: "36 hours",
inline: true,
},
],
},
],
components: [],
};
init = {
body: JSON.stringify(dataJSON),
method: "POST",
headers: {
"content-type": "application/json;charset=UTF-8",
},
};
response = await fetch(WEBHOOK_URL, init);
return new Response(":)", {
headers: { "content-type": "text/plain" },
});
case "unban":
dataJSON = {
username: "Fail2Ban Bans",
avatar_url:
"https://domoticproject.com/wp-content/uploads/2018/06/fail2ban-logo.jpg",
content: "",
embeds: [
{
title: `\`${incReq.ip}\``,
color: 3840,
description: "Unbanned",
timestamp: "",
author: {
name: "",
},
image: {},
thumbnail: {},
footer: {},
fields: [],
},
],
components: [],
};
init = {
body: JSON.stringify(dataJSON),
method: "POST",
headers: {
"content-type": "application/json;charset=UTF-8",
},
};
response = await fetch(WEBHOOK_URL, init);
return new Response(":)", {
headers: { "content-type": "text/plain" },
});
default:
return new Response(":(", {
headers: { "content-type": "text/plain" },
});
}
return new Response(":)", {
headers: { "content-type": "text/plain" },
});
}
[vars]
WEBHOOK_SECRET = "your-secret-key"
WEBHOOK_URL = "https://yourdiscordwebhook.url/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment