version0.4 Общие исправления
This commit is contained in:
@@ -662,6 +662,63 @@ def api_warp_rules_set():
|
||||
|
||||
return jsonify({"ok": True})
|
||||
|
||||
def _get_geosite_data():
|
||||
"""
|
||||
Скачивает (с кэшированием на 7 дней) единый YAML файл со всеми
|
||||
geosite-категориями и возвращает распарсенный dict.
|
||||
Старый способ (отдельный .txt на каждую категорию на ветке release)
|
||||
больше не поддерживается проектом v2fly — теперь только единый
|
||||
dlc.dat_plain.yml в latest release.
|
||||
"""
|
||||
import urllib.request, os, time, yaml
|
||||
|
||||
cache_path = "/var/cache/mita-geosite.yml"
|
||||
url = "https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat_plain.yml"
|
||||
|
||||
need_download = True
|
||||
if os.path.exists(cache_path):
|
||||
age_days = (time.time() - os.path.getmtime(cache_path)) / 86400
|
||||
if age_days < 7:
|
||||
need_download = False
|
||||
|
||||
if need_download:
|
||||
try:
|
||||
os.makedirs(os.path.dirname(cache_path), exist_ok=True)
|
||||
req = urllib.request.Request(url, headers={"User-Agent": "mita-panel"})
|
||||
with urllib.request.urlopen(req, timeout=60) as resp:
|
||||
data = resp.read()
|
||||
with open(cache_path, "wb") as f:
|
||||
f.write(data)
|
||||
except Exception:
|
||||
pass # используем старый кэш, если есть
|
||||
|
||||
if not os.path.exists(cache_path):
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(cache_path) as f:
|
||||
return yaml.safe_load(f)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def _geosite_category_domains(category):
|
||||
"""Возвращает список доменов для одной geosite-категории."""
|
||||
data = _get_geosite_data()
|
||||
if not data:
|
||||
return []
|
||||
domains = []
|
||||
cat_lower = category.lower()
|
||||
for entry in data.get("lists", []):
|
||||
if entry.get("name", "").lower() == cat_lower:
|
||||
for rule in entry.get("rules", []):
|
||||
for prefix in ("domain:", "full:"):
|
||||
if rule.startswith(prefix):
|
||||
domains.append(rule[len(prefix):])
|
||||
break
|
||||
# regexp: и include: пропускаем — не прямые доменные правила
|
||||
break
|
||||
return domains
|
||||
|
||||
def _rebuild_egress(pc):
|
||||
"""
|
||||
Собирает egress.rules из warp_rules всех пользователей и записывает в mita config.
|
||||
@@ -675,8 +732,7 @@ def _rebuild_egress(pc):
|
||||
all_domains = set()
|
||||
all_ips = set()
|
||||
|
||||
GEOSITE_BASE = "https://raw.githubusercontent.com/v2fly/domain-list-community/release"
|
||||
GEOIP_BASE = "https://raw.githubusercontent.com/herrbischoff/country-ip-blocks/master/ipv4"
|
||||
GEOIP_BASE = "https://raw.githubusercontent.com/herrbischoff/country-ip-blocks/master/ipv4"
|
||||
|
||||
for uname, rules in pc.get("warp_rules", {}).items():
|
||||
if uname not in warp_users:
|
||||
@@ -690,12 +746,7 @@ def _rebuild_egress(pc):
|
||||
try:
|
||||
if src.startswith("geosite:"):
|
||||
cat = src[len("geosite:"):]
|
||||
url = f"{GEOSITE_BASE}/{cat}.txt"
|
||||
lines = urllib.request.urlopen(url, timeout=10).read().decode().splitlines()
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "include:" not in line and "regexp:" not in line:
|
||||
all_domains.add(line.lstrip("full:"))
|
||||
all_domains.update(_geosite_category_domains(cat))
|
||||
elif src.startswith("geoip:"):
|
||||
country = src[len("geoip:"):]
|
||||
url = f"{GEOIP_BASE}/{country}.cidr"
|
||||
|
||||
Reference in New Issue
Block a user