Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,166 +1,220 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import time
|
|
|
|
| 4 |
import hashlib
|
| 5 |
-
import
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
| 8 |
-
import
|
| 9 |
-
from fastapi import FastAPI, Request, HTTPException
|
| 10 |
from fastapi.responses import JSONResponse
|
|
|
|
| 11 |
|
| 12 |
-
# ----------------------------
|
| 13 |
-
#
|
| 14 |
-
# ----------------------------
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
}
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
#
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# ----------------------------
|
| 97 |
-
@app.get("/")
|
| 98 |
-
def root():
|
| 99 |
return {
|
| 100 |
"ok": True,
|
| 101 |
-
"
|
| 102 |
-
"
|
| 103 |
-
"forward_to": normalize_url(WAKEUP_URL),
|
| 104 |
-
"max_inflight": MAX_INFLIGHT,
|
| 105 |
-
"seen_ttl_seconds": SEEN_TTL_SECONDS,
|
| 106 |
}
|
| 107 |
|
| 108 |
-
@app.get("/health")
|
| 109 |
-
def health():
|
| 110 |
-
return {"ok": True}
|
| 111 |
|
|
|
|
|
|
|
|
|
|
| 112 |
@app.post("/webhook")
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
try:
|
| 116 |
-
payload = await
|
|
|
|
|
|
|
| 117 |
except Exception:
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
# 1) processing state default
|
| 124 |
-
state = norm(payload.get("sa_processing_state"))
|
| 125 |
-
if not state:
|
| 126 |
-
payload["sa_processing_state"] = "new"
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
payload["sa_fingerprint"] = fp
|
| 133 |
|
| 134 |
-
if seen_check_and_set(fp):
|
| 135 |
-
# Do not forward duplicates (safe default)
|
| 136 |
-
payload["sa_processing_state"] = "duplicate"
|
| 137 |
-
return {"ok": True, "duplicate": True, "fingerprint": fp}
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
-
try:
|
| 146 |
-
forward_url = normalize_url(WAKEUP_URL)
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
"fingerprint": fp,
|
| 154 |
-
"forward_url": forward_url,
|
| 155 |
-
"forward_status": r.status_code,
|
| 156 |
-
"forward_body": (r.text or "")[:FORWARD_BODY_LIMIT],
|
| 157 |
-
}
|
| 158 |
-
except requests.RequestException as e:
|
| 159 |
-
raise HTTPException(status_code=502, detail=f"relay_failed: {e}")
|
| 160 |
-
finally:
|
| 161 |
-
INFLIGHT.release()
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import time
|
| 4 |
+
import hmac
|
| 5 |
import hashlib
|
| 6 |
+
from collections import deque
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
| 9 |
+
from fastapi import FastAPI, Request, Header, HTTPException
|
|
|
|
| 10 |
from fastapi.responses import JSONResponse
|
| 11 |
+
import gradio as gr
|
| 12 |
|
| 13 |
+
# -----------------------------------------------------------------------------
|
| 14 |
+
# Configuration (set these as HF Space "Secrets" or "Variables")
|
| 15 |
+
# -----------------------------------------------------------------------------
|
| 16 |
+
WEBHOOK_TOKEN = os.getenv("WEBHOOK_TOKEN", "").strip()
|
| 17 |
+
WEBHOOK_HMAC_SECRET = os.getenv("WEBHOOK_HMAC_SECRET", "").strip()
|
| 18 |
+
WEBHOOK_TOKEN_HEADER = os.getenv("WEBHOOK_TOKEN_HEADER", "X-SA-Token").strip()
|
| 19 |
+
|
| 20 |
+
MAX_MESSAGES = int(os.getenv("MAX_MESSAGES", "200"))
|
| 21 |
+
|
| 22 |
+
PERSIST_TO_FILE = os.getenv("PERSIST_TO_FILE", "0").strip() == "1"
|
| 23 |
+
PERSIST_PATH = os.getenv("PERSIST_PATH", "webhook_messages.jsonl").strip()
|
| 24 |
+
|
| 25 |
+
# -----------------------------------------------------------------------------
|
| 26 |
+
# In-memory state
|
| 27 |
+
# -----------------------------------------------------------------------------
|
| 28 |
+
MESSAGES = deque(maxlen=MAX_MESSAGES)
|
| 29 |
+
TOTAL_COUNT = 0
|
| 30 |
+
LAST_RECEIVED_TS: Optional[float] = None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _now_ts() -> float:
|
| 34 |
+
return time.time()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _ts_to_str(ts: Optional[float]) -> str:
|
| 38 |
+
if not ts:
|
| 39 |
+
return "n/a"
|
| 40 |
+
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _safe_json_dumps(obj: Any) -> str:
|
| 44 |
+
try:
|
| 45 |
+
return json.dumps(obj, indent=2, sort_keys=True, ensure_ascii=False)
|
| 46 |
+
except Exception:
|
| 47 |
+
return str(obj)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _append_message(payload: Dict[str, Any], meta: Dict[str, Any]) -> None:
|
| 51 |
+
global TOTAL_COUNT, LAST_RECEIVED_TS
|
| 52 |
+
TOTAL_COUNT += 1
|
| 53 |
+
LAST_RECEIVED_TS = _now_ts()
|
| 54 |
+
|
| 55 |
+
entry = {
|
| 56 |
+
"received_at_ts": LAST_RECEIVED_TS,
|
| 57 |
+
"received_at": _ts_to_str(LAST_RECEIVED_TS),
|
| 58 |
+
"meta": meta,
|
| 59 |
+
"payload": payload,
|
| 60 |
}
|
| 61 |
+
|
| 62 |
+
MESSAGES.appendleft(entry)
|
| 63 |
+
|
| 64 |
+
if PERSIST_TO_FILE:
|
| 65 |
+
try:
|
| 66 |
+
with open(PERSIST_PATH, "a", encoding="utf-8") as f:
|
| 67 |
+
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
| 68 |
+
except Exception:
|
| 69 |
+
pass
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def _verify_token(provided_token: Optional[str]) -> None:
|
| 73 |
+
if not WEBHOOK_TOKEN:
|
| 74 |
+
return
|
| 75 |
+
if not provided_token or provided_token.strip() != WEBHOOK_TOKEN:
|
| 76 |
+
raise HTTPException(status_code=401, detail="Invalid or missing webhook token.")
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def _verify_hmac_signature(raw_body: bytes, provided_sig: Optional[str]) -> None:
|
| 80 |
+
if not WEBHOOK_HMAC_SECRET:
|
| 81 |
+
return
|
| 82 |
+
if not provided_sig:
|
| 83 |
+
raise HTTPException(status_code=401, detail="Missing signature header.")
|
| 84 |
+
mac = hmac.new(WEBHOOK_HMAC_SECRET.encode("utf-8"), raw_body, hashlib.sha256).hexdigest()
|
| 85 |
+
if not hmac.compare_digest(mac, provided_sig.strip()):
|
| 86 |
+
raise HTTPException(status_code=401, detail="Invalid signature.")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# -----------------------------------------------------------------------------
|
| 90 |
+
# FastAPI app (webhook listener)
|
| 91 |
+
# -----------------------------------------------------------------------------
|
| 92 |
+
app = FastAPI(title="Webhook Listener", version="1.0.0")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@app.get("/health")
|
| 96 |
+
def health():
|
|
|
|
|
|
|
|
|
|
| 97 |
return {
|
| 98 |
"ok": True,
|
| 99 |
+
"total_count": TOTAL_COUNT,
|
| 100 |
+
"last_received_at": _ts_to_str(LAST_RECEIVED_TS),
|
|
|
|
|
|
|
|
|
|
| 101 |
}
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# IMPORTANT: define both with and without trailing slash
|
| 105 |
+
@app.post("/wakeup")
|
| 106 |
+
@app.post("/wakeup/")
|
| 107 |
@app.post("/webhook")
|
| 108 |
+
@app.post("/webhook/")
|
| 109 |
+
async def webhook(
|
| 110 |
+
request: Request,
|
| 111 |
+
x_sa_signature: Optional[str] = Header(default=None),
|
| 112 |
+
x_sa_token: Optional[str] = Header(default=None),
|
| 113 |
+
):
|
| 114 |
+
raw_body = await request.body()
|
| 115 |
+
|
| 116 |
+
# Token auth (optional)
|
| 117 |
+
if WEBHOOK_TOKEN:
|
| 118 |
+
token_value = request.headers.get(WEBHOOK_TOKEN_HEADER) or x_sa_token
|
| 119 |
+
_verify_token(token_value)
|
| 120 |
+
|
| 121 |
+
# HMAC auth (optional)
|
| 122 |
+
if WEBHOOK_HMAC_SECRET:
|
| 123 |
+
sig_value = request.headers.get("X-SA-Signature") or x_sa_signature
|
| 124 |
+
_verify_hmac_signature(raw_body, sig_value)
|
| 125 |
+
|
| 126 |
+
# Parse JSON
|
| 127 |
try:
|
| 128 |
+
payload = await request.json()
|
| 129 |
+
if not isinstance(payload, dict):
|
| 130 |
+
payload = {"_non_object_payload": payload}
|
| 131 |
except Exception:
|
| 132 |
+
payload = {"_raw_body": raw_body.decode("utf-8", errors="replace")}
|
| 133 |
+
|
| 134 |
+
meta = {
|
| 135 |
+
"client": request.client.host if request.client else None,
|
| 136 |
+
"path": str(request.url.path),
|
| 137 |
+
"headers": {
|
| 138 |
+
"user-agent": request.headers.get("user-agent"),
|
| 139 |
+
"content-type": request.headers.get("content-type"),
|
| 140 |
+
WEBHOOK_TOKEN_HEADER: "***" if request.headers.get(WEBHOOK_TOKEN_HEADER) else None,
|
| 141 |
+
"x-sa-signature": "***" if request.headers.get("x-sa-signature") else None,
|
| 142 |
+
},
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
_append_message(payload=payload, meta=meta)
|
| 146 |
|
| 147 |
+
return JSONResponse(
|
| 148 |
+
status_code=200,
|
| 149 |
+
content={
|
| 150 |
+
"ok": True,
|
| 151 |
+
"message": "Webhook received.",
|
| 152 |
+
"total_count": TOTAL_COUNT,
|
| 153 |
+
"last_received_at": _ts_to_str(LAST_RECEIVED_TS),
|
| 154 |
+
"echo": payload,
|
| 155 |
+
},
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
# -----------------------------------------------------------------------------
|
| 160 |
+
# Gradio UI (simple dashboard)
|
| 161 |
+
# -----------------------------------------------------------------------------
|
| 162 |
+
def ui_status() -> str:
|
| 163 |
+
return _safe_json_dumps(
|
| 164 |
+
{
|
| 165 |
+
"total_count": TOTAL_COUNT,
|
| 166 |
+
"last_received_at": _ts_to_str(LAST_RECEIVED_TS),
|
| 167 |
+
"buffer_size": len(MESSAGES),
|
| 168 |
+
"max_buffer": MAX_MESSAGES,
|
| 169 |
+
"auth": {
|
| 170 |
+
"token_enabled": bool(WEBHOOK_TOKEN),
|
| 171 |
+
"token_header": WEBHOOK_TOKEN_HEADER,
|
| 172 |
+
"hmac_enabled": bool(WEBHOOK_HMAC_SECRET),
|
| 173 |
+
},
|
| 174 |
+
"endpoints": {
|
| 175 |
+
"health": "/health",
|
| 176 |
+
"webhook_post": "/wakeup",
|
| 177 |
+
"webhook_post_alias": "/webhook",
|
| 178 |
+
},
|
| 179 |
+
}
|
| 180 |
+
)
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
def ui_recent(limit: int = 25) -> str:
|
| 184 |
+
limit = max(1, min(int(limit), 200))
|
| 185 |
+
items = list(MESSAGES)[:limit]
|
| 186 |
+
return _safe_json_dumps(items)
|
|
|
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
+
def ui_clear() -> str:
|
| 190 |
+
global TOTAL_COUNT, LAST_RECEIVED_TS
|
| 191 |
+
MESSAGES.clear()
|
| 192 |
+
TOTAL_COUNT = 0
|
| 193 |
+
LAST_RECEIVED_TS = None
|
| 194 |
+
return "Cleared in-memory messages."
|
| 195 |
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
with gr.Blocks(title="Webhook Listener Dashboard") as demo:
|
| 198 |
+
gr.Markdown("# Webhook Listener Dashboard")
|
| 199 |
+
gr.Markdown(
|
| 200 |
+
"Send a POST request with JSON to `/wakeup` or `/webhook` and it will appear below.\n\n"
|
| 201 |
+
"This is for payload debugging / confirming delivery."
|
| 202 |
+
)
|
| 203 |
|
| 204 |
+
with gr.Row():
|
| 205 |
+
status_box = gr.Code(label="Status", value=ui_status(), language="json")
|
| 206 |
+
clear_btn = gr.Button("Clear messages", variant="stop")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
with gr.Row():
|
| 209 |
+
limit = gr.Slider(1, 200, value=25, step=1, label="Recent message limit")
|
| 210 |
+
|
| 211 |
+
recent_box = gr.Code(label="Recent payloads (newest first)", value=ui_recent(25), language="json")
|
| 212 |
+
|
| 213 |
+
def refresh(limit_val: int):
|
| 214 |
+
return ui_status(), ui_recent(limit_val)
|
| 215 |
+
|
| 216 |
+
demo.load(refresh, inputs=[limit], outputs=[status_box, recent_box], every=2)
|
| 217 |
+
limit.change(refresh, inputs=[limit], outputs=[status_box, recent_box])
|
| 218 |
+
clear_btn.click(lambda: (ui_clear(), ui_status(), ui_recent(25)), outputs=[recent_box, status_box, recent_box])
|
| 219 |
+
|
| 220 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|