File size: 2,906 Bytes
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
import asyncio
import hashlib
import hmac
import time

import httpx

from Backend.helper.settings_manager import SettingsManager
from Backend.logger import LOGGER

#----- Signed Cloudflare links stay valid this long. The Worker can't read the token DB, so a
#----- revoked or expired token keeps streaming on Cloudflare until its links run out.
LINK_TTL = 48 * 3600

#----- Max clock skew accepted on requests signed by the Worker
REQUEST_MAX_AGE = 300


def cf_enabled() -> bool:
    s = SettingsManager.current()
    return s.cf_stream_mode != "off" and bool(s.cf_stream_url and s.cf_stream_secret)


def _hmac(secret: str, message: str) -> str:
    return hmac.new(secret.encode(), message.encode(), hashlib.sha256).hexdigest()


#----- Cloudflare twin of a /dl/{token}/{id}/{name} link. Only /dl/{token}/{id} and the expiry
#----- are signed, so players re-encoding the file name don't break the signature.
def cf_stream_url(token: str, file_id: str, name: str, url: str = None, secret: str = None) -> str:
    s = SettingsManager.current()
    exp = int(time.time()) + LINK_TTL
    sig = _hmac(secret or s.cf_stream_secret, f"/dl/{token}/{file_id}:{exp}")[:32]
    return f"{url or s.cf_stream_url}/dl/{token}/{file_id}/{name}?e={exp}&s={sig}"


#----- Check a Worker -> app request signed as HMAC(secret, "{ts}\n{METHOD} {path}\n{body}")
def verify_worker_request(method: str, path: str, body: bytes, ts: str, sig: str, secret: str = None) -> bool:
    secret = secret or SettingsManager.current().cf_stream_secret
    if not secret or not ts or not sig:
        return False
    try:
        if abs(time.time() - int(ts)) > REQUEST_MAX_AGE:
            return False
    except ValueError:
        return False
    expected = _hmac(secret, f"{ts}\n{method} {path}\n{body.decode('utf-8', 'replace')}")
    return hmac.compare_digest(expected, sig)


#----- Ask the Worker to reload bot tokens / the userbot session now instead of within 10 minutes
async def notify_worker(url: str = None, secret: str = None) -> None:
    if not url and not cf_enabled():
        return
    s = SettingsManager.current()
    url, secret = url or s.cf_stream_url, secret or s.cf_stream_secret
    ts, body = str(int(time.time())), "{}"
    headers = {"x-cf-time": ts, "x-cf-sig": _hmac(secret, f"{ts}\nPOST /api/sync\n{body}"), "content-type": "application/json"}
    try:
        async with httpx.AsyncClient(timeout=20) as client:
            r = await client.post(f"{url}/api/sync", content=body, headers=headers)
        if r.status_code != 200:
            LOGGER.warning(f"[CF] Worker sync failed: HTTP {r.status_code} {r.text[:200]}")
    except Exception as e:
        LOGGER.warning(f"[CF] Worker sync failed: {e}")


#----- Fire-and-forget: never delays the save or login that triggered it
def sync_worker_soon(url: str = None, secret: str = None) -> None:
    asyncio.create_task(notify_worker(url, secret))