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

30
app/services/cache.py Normal file
View File

@@ -0,0 +1,30 @@
from __future__ import annotations
import time
from dataclasses import dataclass
from typing import Generic, TypeVar
T = TypeVar("T")
@dataclass
class CacheItem(Generic[T]):
value: T
expires_at: float
class TTLCache(Generic[T]):
def __init__(self) -> None:
self._store: dict[str, CacheItem[T]] = {}
def get(self, key: str) -> T | None:
item = self._store.get(key)
if item is None:
return None
if item.expires_at < time.time():
self._store.pop(key, None)
return None
return item.value
def set(self, key: str, value: T, ttl_seconds: int) -> None:
self._store[key] = CacheItem(value=value, expires_at=time.time() + ttl_seconds)

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

27
app/services/loader.py Normal file
View File

@@ -0,0 +1,27 @@
from __future__ import annotations
import os
import re
from pathlib import Path
import yaml
from app.models import AppConfig
_ENV_PATTERN = re.compile(r"\$\{([A-Z0-9_]+)\}")
def _expand_env(value):
if isinstance(value, str):
return _ENV_PATTERN.sub(lambda m: os.getenv(m.group(1), ""), value)
if isinstance(value, list):
return [_expand_env(v) for v in value]
if isinstance(value, dict):
return {k: _expand_env(v) for k, v in value.items()}
return value
def load_app_config(path: Path) -> AppConfig:
raw = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
expanded = _expand_env(raw)
return AppConfig.model_validate(expanded)

275
app/services/profiles.py Normal file
View File

@@ -0,0 +1,275 @@
from __future__ import annotations
import re
from typing import Any
import yaml
from app.models import AppConfig, ClientConfig, SourceSnapshot
from app.services.rules import build_inline_rules, build_rule_provider_entries, build_rule_set_references
def dump_yaml(data: dict[str, Any]) -> str:
return yaml.safe_dump(data, allow_unicode=True, sort_keys=False, default_flow_style=False)
def build_thin_profile(
*,
client_type: str,
app_config: AppConfig,
client: ClientConfig,
selected_source_names: list[str],
base_url: str,
public_path: str,
) -> dict[str, Any]:
profile: dict[str, Any] = {
"mode": client.mode,
"ipv6": client.ipv6,
}
if client.log_level:
profile["log-level"] = client.log_level
if client_type == "mihomo":
if client.mixed_port is not None:
profile["mixed-port"] = client.mixed_port
if client.socks_port is not None:
profile["socks-port"] = client.socks_port
profile["allow-lan"] = client.allow_lan
proxy_providers: dict[str, dict[str, Any]] = {}
for name in selected_source_names:
if client_type == "mihomo":
proxy_providers[name] = {
"type": "http",
"url": f"{base_url}/{public_path}/providers/{name}.yaml",
"path": f"./providers/{name}.yaml",
"interval": client.provider_interval,
"health-check": {
"enable": True,
"url": str(client.test_url),
"interval": client.test_interval,
},
}
else:
proxy_providers[name] = {
"url": f"{base_url}/{public_path}/providers/{name}.yaml",
"interval": client.provider_interval,
}
profile["proxy-providers"] = proxy_providers
profile["proxy-groups"] = _build_thin_groups(client_type, app_config, client, selected_source_names)
profile["rule-providers"] = build_rule_provider_entries(app_config, client, base_url, public_path)
profile["rules"] = build_rule_set_references(app_config, client)
return profile
def build_bundle_profile(
*,
client_type: str,
app_config: AppConfig,
client: ClientConfig,
snapshots: list[SourceSnapshot],
) -> dict[str, Any]:
profile: dict[str, Any] = {
"mode": client.mode,
"ipv6": client.ipv6,
}
if client.log_level:
profile["log-level"] = client.log_level
if client_type == "mihomo":
if client.mixed_port is not None:
profile["mixed-port"] = client.mixed_port
if client.socks_port is not None:
profile["socks-port"] = client.socks_port
profile["allow-lan"] = client.allow_lan
proxies: list[dict[str, Any]] = []
source_proxy_names: dict[str, list[str]] = {}
seen: set[str] = set()
for snapshot in snapshots:
names: list[str] = []
for proxy in snapshot.document.proxies:
candidate = dict(proxy)
name = str(candidate.get("name", "")).strip()
if not name:
continue
original = name
index = 2
while name in seen:
name = f"{original} #{index}"
index += 1
seen.add(name)
candidate["name"] = name
proxies.append(candidate)
names.append(name)
source_proxy_names[snapshot.name] = names
profile["proxies"] = proxies
profile["proxy-groups"] = _build_bundle_groups(app_config, client, snapshots, source_proxy_names)
profile["rules"] = build_inline_rules(app_config, client)
return profile
def _build_thin_groups(client_type: str, app_config: AppConfig, client: ClientConfig, selected_source_names: list[str]) -> list[dict[str, Any]]:
groups: list[dict[str, Any]] = []
source_auto_names: list[str] = []
for source_name in selected_source_names:
display_name = app_config.sources[source_name].display_name or source_name
group_name = f"{display_name} 自动"
source_auto_names.append(group_name)
groups.append(
{
"name": group_name,
"type": "url-test",
"url": str(client.test_url),
"interval": client.test_interval,
"use": [source_name],
}
)
if client_type == "mihomo":
mixed_auto = {
"name": client.mixed_auto_policy,
"type": "url-test",
"url": str(client.test_url),
"interval": client.test_interval,
"include-all-providers": True,
}
manual = {
"name": client.manual_policy,
"type": "select",
"proxies": [client.direct_policy],
"include-all-providers": True,
}
else:
mixed_auto = {
"name": client.mixed_auto_policy,
"type": "url-test",
"url": str(client.test_url),
"interval": client.test_interval,
"include-all": True,
}
manual = {
"name": client.manual_policy,
"type": "select",
"proxies": [client.direct_policy],
"include-all": True,
}
groups.append(mixed_auto)
region_names: list[str] = []
for region in app_config.regions.values():
group = {
"name": region.name,
"type": "url-test",
"url": str(client.test_url),
"interval": client.test_interval,
"filter": region.filter,
"tolerance": region.tolerance,
}
if client_type == "mihomo":
group["include-all-providers"] = True
else:
group["include-all"] = True
groups.append(group)
region_names.append(region.name)
groups.append(
{
"name": client.source_policy,
"type": "select",
"proxies": [client.mixed_auto_policy, *source_auto_names, client.direct_policy],
}
)
groups.append(manual)
groups.append(
{
"name": client.main_policy,
"type": "select",
"proxies": [
client.source_policy,
client.mixed_auto_policy,
*region_names,
client.manual_policy,
client.direct_policy,
],
}
)
return groups
def _build_bundle_groups(
app_config: AppConfig,
client: ClientConfig,
snapshots: list[SourceSnapshot],
source_proxy_names: dict[str, list[str]],
) -> list[dict[str, Any]]:
groups: list[dict[str, Any]] = []
source_auto_names: list[str] = []
all_proxy_names = [name for names in source_proxy_names.values() for name in names]
for snapshot in snapshots:
group_name = f"{snapshot.display_name} 自动"
source_auto_names.append(group_name)
groups.append(
{
"name": group_name,
"type": "url-test",
"url": str(client.test_url),
"interval": client.test_interval,
"proxies": source_proxy_names.get(snapshot.name) or [client.direct_policy],
}
)
groups.append(
{
"name": client.mixed_auto_policy,
"type": "url-test",
"url": str(client.test_url),
"interval": client.test_interval,
"proxies": all_proxy_names or [client.direct_policy],
}
)
region_names: list[str] = []
for region in app_config.regions.values():
matched = [name for name in all_proxy_names if re.search(region.filter, name)]
groups.append(
{
"name": region.name,
"type": "url-test",
"url": str(client.test_url),
"interval": client.test_interval,
"tolerance": region.tolerance,
"proxies": matched or [client.direct_policy],
}
)
region_names.append(region.name)
groups.append(
{
"name": client.source_policy,
"type": "select",
"proxies": [client.mixed_auto_policy, *source_auto_names, client.direct_policy],
}
)
groups.append(
{
"name": client.manual_policy,
"type": "select",
"proxies": [*all_proxy_names, client.direct_policy] if all_proxy_names else [client.direct_policy],
}
)
groups.append(
{
"name": client.main_policy,
"type": "select",
"proxies": [
client.source_policy,
client.mixed_auto_policy,
*region_names,
client.manual_policy,
client.direct_policy,
],
}
)
return groups

80
app/services/rules.py Normal file
View File

@@ -0,0 +1,80 @@
from __future__ import annotations
from pathlib import Path
import yaml
from app.config import get_settings
from app.models import AppConfig, ClientConfig, RuleConfig
def resolve_policy(policy: str, client: ClientConfig) -> str:
return (
policy.replace("{{ main_policy }}", client.main_policy)
.replace("{{main_policy}}", client.main_policy)
.replace("{{ direct_policy }}", client.direct_policy)
.replace("{{direct_policy}}", client.direct_policy)
)
def load_rule_text(path: Path) -> str:
return path.read_text(encoding="utf-8")
def load_rule_payload(path: Path) -> list[str]:
if path.suffix.lower() in {".yaml", ".yml"}:
data = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
payload = data.get("payload", [])
if not isinstance(payload, list):
raise ValueError(f"Rule file {path.name} must contain a list field named 'payload'")
return [str(item).strip() for item in payload if str(item).strip()]
lines: list[str] = []
for line in path.read_text(encoding="utf-8").splitlines():
stripped = line.strip()
if not stripped or stripped.startswith("#"):
continue
lines.append(stripped)
return lines
def build_rule_provider_entries(app_config: AppConfig, client: ClientConfig, base_url: str, public_path: str):
providers: dict[str, dict] = {}
for name, rule in app_config.rules.items():
entry = {
"behavior": rule.behavior,
"format": rule.format,
"url": f"{base_url}/{public_path}/rules/{name}.yaml",
"interval": rule.interval,
}
providers[name] = entry
return providers
def build_rule_set_references(app_config: AppConfig, client: ClientConfig) -> list[str]:
refs: list[str] = []
for name, rule in app_config.rules.items():
target = resolve_policy(rule.policy, client)
line = f"RULE-SET,{name},{target}"
if rule.no_resolve:
line += ",no-resolve"
refs.append(line)
refs.append(f"MATCH,{client.main_policy}")
return refs
def build_inline_rules(app_config: AppConfig, client: ClientConfig) -> list[str]:
settings = get_settings()
lines: list[str] = []
for rule in app_config.rules.values():
path = (settings.rules_dir / rule.file).resolve()
if not path.is_file() or settings.rules_dir.resolve() not in path.parents:
raise FileNotFoundError(f"Rule file missing: {rule.file}")
target = resolve_policy(rule.policy, client)
for payload_line in load_rule_payload(path):
line = f"{payload_line},{target}"
if rule.no_resolve:
line += ",no-resolve"
lines.append(line)
lines.append(f"MATCH,{client.main_policy}")
return lines

View File

@@ -0,0 +1,177 @@
from __future__ import annotations
import re
from typing import Any, Iterable
import httpx
import yaml
from app.config import get_settings
from app.models import FetchResult, ProviderDocument, SourceConfig, SourceSnapshot
from app.services.cache import TTLCache
from app.services.headers import parse_subscription_userinfo
_fetch_cache: TTLCache[FetchResult] = TTLCache()
_provider_cache: TTLCache[ProviderDocument] = TTLCache()
_snapshot_cache: TTLCache[SourceSnapshot] = TTLCache()
async def fetch_source(name: str, source: SourceConfig) -> FetchResult:
settings = get_settings()
ttl = source.cache_ttl_seconds or settings.cache_ttl_seconds
cached = _fetch_cache.get(name)
if cached is not None:
return cached
headers = {"User-Agent": settings.default_user_agent}
headers.update(source.headers)
async with httpx.AsyncClient(timeout=settings.request_timeout_seconds, follow_redirects=True) as client:
response = await client.get(source.url, headers=headers)
response.raise_for_status()
result = FetchResult(text=response.text, headers=dict(response.headers))
_fetch_cache.set(name, result, ttl)
return result
async def build_provider_document(name: str, source: SourceConfig) -> ProviderDocument:
settings = get_settings()
ttl = source.cache_ttl_seconds or settings.cache_ttl_seconds
cache_key = f"provider:{name}"
cached = _provider_cache.get(cache_key)
if cached is not None:
return cached
fetched = await fetch_source(name, source)
if source.kind != "clash_yaml":
raise ValueError(f"Unsupported source kind: {source.kind}")
proxies = parse_clash_yaml_proxies(fetched.text)
proxies = transform_proxies(proxies, source, settings.max_proxy_name_length)
document = ProviderDocument(proxies=proxies)
_provider_cache.set(cache_key, document, ttl)
return document
async def build_source_snapshot(name: str, source: SourceConfig) -> SourceSnapshot:
settings = get_settings()
ttl = source.cache_ttl_seconds or settings.cache_ttl_seconds
cache_key = f"snapshot:{name}"
cached = _snapshot_cache.get(cache_key)
if cached is not None:
return cached
fetched = await fetch_source(name, source)
document = await build_provider_document(name, source)
snapshot = SourceSnapshot(
name=name,
display_name=source.display_name or name,
document=document,
headers=fetched.headers,
quota=parse_subscription_userinfo(fetched.headers),
)
_snapshot_cache.set(cache_key, snapshot, ttl)
return snapshot
async def build_source_snapshots(source_items: Iterable[tuple[str, SourceConfig]]) -> list[SourceSnapshot]:
snapshots: list[SourceSnapshot] = []
for name, source in source_items:
snapshots.append(await build_source_snapshot(name, source))
return snapshots
async def build_merged_provider_document(source_items: Iterable[tuple[str, SourceConfig]]) -> ProviderDocument:
snapshots = await build_source_snapshots(source_items)
proxies: list[dict[str, Any]] = []
seen: set[str] = set()
for snapshot in snapshots:
for proxy in snapshot.document.proxies:
candidate = dict(proxy)
name = str(candidate.get("name", "")).strip()
if not name:
continue
original = name
index = 2
while name in seen:
name = f"{original} #{index}"
index += 1
candidate["name"] = name
seen.add(name)
proxies.append(candidate)
return ProviderDocument(proxies=proxies)
async def get_first_quota(source_items: Iterable[tuple[str, SourceConfig]]):
source_list = list(source_items)
if not source_list:
return None
snapshot = await build_source_snapshot(source_list[0][0], source_list[0][1])
return snapshot.quota
def parse_clash_yaml_proxies(text: str) -> list[dict[str, Any]]:
data = yaml.safe_load(text)
if not isinstance(data, dict):
raise ValueError("Upstream YAML must be a mapping with a top-level 'proxies' field")
proxies = data.get("proxies")
if not isinstance(proxies, list):
raise ValueError("Upstream YAML must contain a list field named 'proxies'")
normalized: list[dict[str, Any]] = []
for item in proxies:
if not isinstance(item, dict):
continue
if not item.get("name") or not item.get("type"):
continue
normalized.append(item)
return normalized
def transform_proxies(
proxies: list[dict[str, Any]], source: SourceConfig, max_proxy_name_length: int
) -> list[dict[str, Any]]:
include = re.compile(source.include_regex) if source.include_regex else None
exclude = re.compile(source.exclude_regex) if source.exclude_regex else None
transformed: list[dict[str, Any]] = []
seen: dict[str, int] = {}
for proxy in proxies:
name = str(proxy.get("name", "")).strip()
if not name:
continue
if include and not include.search(name):
continue
if exclude and exclude.search(name):
continue
new_proxy = dict(proxy)
new_name = f"{source.prefix}{name}{source.suffix}".strip()
if len(new_name) > max_proxy_name_length:
new_name = new_name[:max_proxy_name_length].rstrip()
count = seen.get(new_name, 0) + 1
seen[new_name] = count
if count > 1:
new_name = f"{new_name} #{count}"
new_proxy["name"] = new_name
transformed.append(new_proxy)
return transformed
def dump_provider_yaml(document: ProviderDocument) -> str:
return yaml.safe_dump(
{"proxies": document.proxies},
allow_unicode=True,
sort_keys=False,
default_flow_style=False,
)