import hmac
import hashlib
from flask import Flask, request, jsonify
app = Flask(__name__)
WEBHOOK_SECRET = "whsec_your_signing_secret"
def verify_signature(payload: bytes, signature: str) -> bool:
expected = hmac.new(
WEBHOOK_SECRET.encode(),
payload,
hashlib.sha256
).hexdigest()
return hmac.compare_digest(f"sha256={expected}", signature)
@app.route("/webhooks/zentislabs", methods=["POST"])
def handle_webhook():
signature = request.headers.get("X-ZentisLabs-Signature", "")
if not verify_signature(request.data, signature):
return jsonify({"error": "Invalid signature"}), 401
event = request.json
event_type = event["type"]
if event_type == "bandwidth.threshold":
threshold = event["data"]["threshold"]
remaining = event["data"]["remaining_gb"]
print(f"Bandwidth alert: {threshold}% used, {remaining}GB remaining")
elif event_type == "pool.health.degraded":
pool = event["data"]["pool_name"]
success_rate = event["data"]["success_rate"]
print(f"Pool {pool} degraded: {success_rate}% success rate")
return jsonify({"received": True}), 200