This commit is contained in:
riglen
2026-03-31 15:51:18 +08:00
parent e3cc26d4f6
commit 0d49398e2d
21 changed files with 1483 additions and 0 deletions

29
app/services/headers.py Normal file
View File

@@ -0,0 +1,29 @@
from __future__ import annotations
import re
from typing import Mapping
from app.models import SubscriptionUserInfo
_SUBSCRIPTION_FIELDS = re.compile(r"(upload|download|total|expire)=(\d+)")
def get_header_case_insensitive(headers: Mapping[str, str], name: str) -> str | None:
target = name.lower()
for key, value in headers.items():
if key.lower() == target:
return value
return None
def parse_subscription_userinfo(headers: Mapping[str, str]) -> SubscriptionUserInfo | None:
raw = get_header_case_insensitive(headers, "Subscription-Userinfo")
if not raw:
return None
values: dict[str, int] = {}
for key, value in _SUBSCRIPTION_FIELDS.findall(raw):
values[key] = int(value)
info = SubscriptionUserInfo(**values)
return None if info.is_empty() else info