Spaces:
Paused
Paused
Give the push send a deadline and a valid sub claim
Browse files
app.py
CHANGED
|
@@ -800,6 +800,14 @@ def _push_oeffentlich():
|
|
| 800 |
return base64.urlsafe_b64encode(roh).decode().rstrip("=")
|
| 801 |
|
| 802 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
def _push_bereit():
|
| 804 |
"""Ist die Versandbibliothek überhaupt da? -> (ja, Grund)
|
| 805 |
|
|
@@ -823,23 +831,36 @@ def _push_senden(titel, text):
|
|
| 823 |
uebrig, geaendert, zugestellt, fehler = [], False, 0, []
|
| 824 |
for abo in z.get("abos", []):
|
| 825 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 826 |
webpush(subscription_info=abo,
|
| 827 |
data=json.dumps({"titel": titel, "text": text}),
|
| 828 |
vapid_private_key=z["vapid_pem"],
|
| 829 |
-
vapid_claims={"sub":
|
|
|
|
| 830 |
uebrig.append(abo)
|
| 831 |
zugestellt += 1
|
| 832 |
except WebPushException as e:
|
| 833 |
# 404/410 heißt: Gerät hat abgemeldet. Eintrag darf weg.
|
| 834 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 835 |
if code in (404, 410):
|
| 836 |
geaendert = True
|
| 837 |
else:
|
| 838 |
uebrig.append(abo)
|
| 839 |
-
fehler.append(f"{code or ''} {e}"
|
| 840 |
except Exception as e:
|
| 841 |
uebrig.append(abo)
|
| 842 |
-
fehler.append(f"{type(e).__name__}: {e}"[:200])
|
| 843 |
if geaendert:
|
| 844 |
z["abos"] = uebrig
|
| 845 |
try:
|
|
@@ -858,6 +879,24 @@ def push_schluessel_api(key):
|
|
| 858 |
return {"ok": False, "fehler": str(e)}
|
| 859 |
|
| 860 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
def push_pruefen_api(key):
|
| 862 |
"""Schickt eine Probe-Benachrichtigung und sagt, was dabei geschah."""
|
| 863 |
if not _pruefe(key):
|
|
@@ -1268,6 +1307,8 @@ with gr.Blocks(title="Sprecher-Analyse API") as demo:
|
|
| 1268 |
api_name="auftrag")
|
| 1269 |
gr.Button("Push-Schlüssel").click(
|
| 1270 |
push_schluessel_api, [key], gr.JSON(), api_name="push_schluessel")
|
|
|
|
|
|
|
| 1271 |
gr.Button("Push prüfen").click(
|
| 1272 |
push_pruefen_api, [key], gr.JSON(), api_name="push_pruefen")
|
| 1273 |
gr.Button("Push anmelden").click(
|
|
|
|
| 800 |
return base64.urlsafe_b64encode(roh).decode().rstrip("=")
|
| 801 |
|
| 802 |
|
| 803 |
+
def _dienst(abo):
|
| 804 |
+
"""Welcher Zustelldienst — hilft, Apple von anderen zu unterscheiden."""
|
| 805 |
+
try:
|
| 806 |
+
return (abo.get("endpoint", "").split("/")[2]) or "?"
|
| 807 |
+
except Exception:
|
| 808 |
+
return "?"
|
| 809 |
+
|
| 810 |
+
|
| 811 |
def _push_bereit():
|
| 812 |
"""Ist die Versandbibliothek überhaupt da? -> (ja, Grund)
|
| 813 |
|
|
|
|
| 831 |
uebrig, geaendert, zugestellt, fehler = [], False, 0, []
|
| 832 |
for abo in z.get("abos", []):
|
| 833 |
try:
|
| 834 |
+
# Zwei Dinge, die beim ersten echten Versand aufgefallen sind:
|
| 835 |
+
# ohne Zeitgrenze kann der Aufruf hängen, bis die HTTP-Anfrage
|
| 836 |
+
# des Browsers aufgibt — dann sieht der Nutzer nur „An error
|
| 837 |
+
# occurred“ und weiß nichts. Und der sub-Anspruch muss eine
|
| 838 |
+
# erreichbare Adresse sein; erfundene .invalid-Domains weisen
|
| 839 |
+
# Zustelldienste zurück.
|
| 840 |
webpush(subscription_info=abo,
|
| 841 |
data=json.dumps({"titel": titel, "text": text}),
|
| 842 |
vapid_private_key=z["vapid_pem"],
|
| 843 |
+
vapid_claims={"sub": FRONTEND},
|
| 844 |
+
timeout=10)
|
| 845 |
uebrig.append(abo)
|
| 846 |
zugestellt += 1
|
| 847 |
except WebPushException as e:
|
| 848 |
# 404/410 heißt: Gerät hat abgemeldet. Eintrag darf weg.
|
| 849 |
+
antwort = getattr(e, "response", None)
|
| 850 |
+
code = getattr(antwort, "status_code", None)
|
| 851 |
+
rumpf = ""
|
| 852 |
+
try:
|
| 853 |
+
rumpf = (antwort.text or "")[:120]
|
| 854 |
+
except Exception:
|
| 855 |
+
pass
|
| 856 |
if code in (404, 410):
|
| 857 |
geaendert = True
|
| 858 |
else:
|
| 859 |
uebrig.append(abo)
|
| 860 |
+
fehler.append(f"{_dienst(abo)} {code or '?'}: {rumpf or e}"[:200])
|
| 861 |
except Exception as e:
|
| 862 |
uebrig.append(abo)
|
| 863 |
+
fehler.append(f"{_dienst(abo)} {type(e).__name__}: {e}"[:200])
|
| 864 |
if geaendert:
|
| 865 |
z["abos"] = uebrig
|
| 866 |
try:
|
|
|
|
| 879 |
return {"ok": False, "fehler": str(e)}
|
| 880 |
|
| 881 |
|
| 882 |
+
def push_stand_api(key):
|
| 883 |
+
"""Sagt ohne Netzverkehr, wie es um Push steht.
|
| 884 |
+
|
| 885 |
+
Bewusst getrennt vom Versand: hängt der Versand, liefert diese
|
| 886 |
+
Auskunft trotzdem sofort und man sieht, woran es nicht liegt.
|
| 887 |
+
"""
|
| 888 |
+
if not _pruefe(key):
|
| 889 |
+
return {"ok": False, "fehler": "Ungültiger Zugangsschlüssel."}
|
| 890 |
+
bereit, grund = _push_bereit()
|
| 891 |
+
try:
|
| 892 |
+
z = _push_zustand()
|
| 893 |
+
return {"ok": True, "bibliothek": grund,
|
| 894 |
+
"geraete": [_dienst(a) for a in z.get("abos", [])],
|
| 895 |
+
"schluessel": bool(z.get("vapid_pem"))}
|
| 896 |
+
except Exception as e:
|
| 897 |
+
return {"ok": False, "bibliothek": grund, "fehler": str(e)}
|
| 898 |
+
|
| 899 |
+
|
| 900 |
def push_pruefen_api(key):
|
| 901 |
"""Schickt eine Probe-Benachrichtigung und sagt, was dabei geschah."""
|
| 902 |
if not _pruefe(key):
|
|
|
|
| 1307 |
api_name="auftrag")
|
| 1308 |
gr.Button("Push-Schlüssel").click(
|
| 1309 |
push_schluessel_api, [key], gr.JSON(), api_name="push_schluessel")
|
| 1310 |
+
gr.Button("Push-Zustand").click(
|
| 1311 |
+
push_stand_api, [key], gr.JSON(), api_name="push_stand")
|
| 1312 |
gr.Button("Push prüfen").click(
|
| 1313 |
push_pruefen_api, [key], gr.JSON(), api_name="push_pruefen")
|
| 1314 |
gr.Button("Push anmelden").click(
|