refactor(remnawave,exit-node): split bootstrap/sync and generalize ger-nod-02 roles
ansible-core-net / deploy (push) Successful in 1m50s
ansible-core-mon / deploy (push) Successful in 28s
ansible-core-secrets / deploy (push) Successful in 18s
ansible-ger-nod-02 / deploy (push) Failing after 3s
ansible-opnsense / deploy (push) Successful in 9s
ansible-rus-central / deploy (push) Successful in 1m7s
ansible-core-net / deploy (push) Successful in 1m50s
ansible-core-mon / deploy (push) Successful in 28s
ansible-core-secrets / deploy (push) Successful in 18s
ansible-ger-nod-02 / deploy (push) Failing after 3s
ansible-opnsense / deploy (push) Successful in 9s
ansible-rus-central / deploy (push) Successful in 1m7s
This commit is contained in:
1 parent
83baec4bad
commit
2fc6e39323
19 files changed
+1092
-863
No files matched your search
@@ -0,0 +1,21 @@
|
||||
# Декларативная конфигурация профилей, нод и хостов Remnawave.
|
||||
# Добавление новой выходной VPS: добавить запись в nodes и прогнать ansible-core-net.
|
||||
xray_template:
|
||||
name: Except-RU
|
||||
file: xray-except-ru.json
|
||||
|
||||
squad_name: Default-Squad
|
||||
subscription_page_config: Homelab
|
||||
|
||||
nodes:
|
||||
- name: Ger-nod-02
|
||||
address: "150.241.64.209"
|
||||
netbird_hostname: "ger-nod-02"
|
||||
port: 2222
|
||||
country_code: "DE"
|
||||
profile_name: "DE-Yali"
|
||||
profile_template: "profile-selfsteal-xhttp.json"
|
||||
inbound_tag: "Steal-Yali"
|
||||
selfsteal_domain: "yali.gunis.top"
|
||||
xhttp_path: "/xhttp-yali"
|
||||
host_remark: "🇩🇪 ger-nod-02"
|
||||
+4
-4
@@ -13,7 +13,7 @@
|
||||
},
|
||||
"inbounds": [
|
||||
{
|
||||
"tag": "Steal-Yali",
|
||||
"tag": "__INBOUND_TAG__",
|
||||
"port": 443,
|
||||
"protocol": "vless",
|
||||
"settings": {
|
||||
@@ -28,9 +28,9 @@
|
||||
"network": "xhttp",
|
||||
"security": "reality",
|
||||
"xhttpSettings": {
|
||||
"host": "yali.gunis.top",
|
||||
"host": "__SELFSTEAL_DOMAIN__",
|
||||
"mode": "stream-one",
|
||||
"path": "/xhttp-yali"
|
||||
"path": "__XHTTP_PATH__"
|
||||
},
|
||||
"realitySettings": {
|
||||
"dest": "/dev/shm/nginx.sock",
|
||||
@@ -39,7 +39,7 @@
|
||||
"spiderX": "",
|
||||
"shortIds": ["__REALITY_SHORT_ID__"],
|
||||
"privateKey": "__REALITY_PRIVATE_KEY__",
|
||||
"serverNames": ["yali.gunis.top"]
|
||||
"serverNames": ["__SELFSTEAL_DOMAIN__"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,592 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Идемпотентная синхронизация шаблонов, профилей, нод, хостов, сквадов и пользователей Remnawave."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import secrets
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _redact_text(text: str) -> str:
|
||||
"""Убирает потенциально чувствительные поля при выводе ошибок."""
|
||||
return re.sub(
|
||||
r'("(?:privateKey|privKey|secretKey|accessToken|apiToken|password|token|vlessUuid|trojanPassword|ssPassword|shortUuid)"\s*:\s*)"[^"]*"',
|
||||
r'\1"[REDACTED]"',
|
||||
text,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
class RemnawaveAPI:
|
||||
"""Минимальный клиент Remnawave API через curl с --resolve на 127.0.0.1."""
|
||||
|
||||
def __init__(self, domain: str, token: str) -> None:
|
||||
self.domain = domain
|
||||
self.base_url = f"https://{domain}/api"
|
||||
self.token = token
|
||||
|
||||
def request(
|
||||
self,
|
||||
method: str,
|
||||
path: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
allow_error: bool = False,
|
||||
) -> tuple[int, Any]:
|
||||
cmd = [
|
||||
"curl",
|
||||
"-sSk",
|
||||
"--resolve",
|
||||
f"{self.domain}:443:127.0.0.1",
|
||||
"-w",
|
||||
"\n%{http_code}",
|
||||
"-X",
|
||||
method,
|
||||
f"{self.base_url}{path}",
|
||||
"-H",
|
||||
"X-Remnawave-Client-Type: browser",
|
||||
"-H",
|
||||
f"Authorization: Bearer {self.token}",
|
||||
]
|
||||
stdin_bytes = None
|
||||
if payload is not None:
|
||||
cmd.extend(["-H", "Content-Type: application/json", "--data-binary", "@-"])
|
||||
stdin_bytes = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
||||
|
||||
proc = subprocess.run(cmd, input=stdin_bytes, capture_output=True, check=False)
|
||||
if proc.returncode != 0:
|
||||
raise RuntimeError(
|
||||
f"curl error for {method} {path}: {proc.stderr.decode(errors='replace')}"
|
||||
)
|
||||
|
||||
raw = proc.stdout.decode("utf-8", errors="replace")
|
||||
body, _, code_str = raw.rpartition("\n")
|
||||
status = int(code_str.strip() or "0")
|
||||
data: Any = None
|
||||
if body.strip():
|
||||
try:
|
||||
data = json.loads(body)
|
||||
except json.JSONDecodeError:
|
||||
data = body
|
||||
|
||||
if not (200 <= status < 300):
|
||||
if allow_error:
|
||||
return status, data
|
||||
raise RuntimeError(
|
||||
f"Remnawave API {method} {path} failed (HTTP {status}): {_redact_text(body)}"
|
||||
)
|
||||
return status, data
|
||||
|
||||
|
||||
def resolve_netbird_peers(peers_url: str, nb_token: str) -> dict[str, str]:
|
||||
"""Возвращает карту hostname/name/dns_label -> IPv4 из NetBird API."""
|
||||
if not peers_url or not nb_token:
|
||||
return {}
|
||||
cmd = [
|
||||
"curl",
|
||||
"-fsS",
|
||||
"--max-time",
|
||||
"20",
|
||||
"-H",
|
||||
f"Authorization: Token {nb_token}",
|
||||
peers_url,
|
||||
]
|
||||
proc = subprocess.run(cmd, capture_output=True, check=False)
|
||||
if proc.returncode != 0:
|
||||
return {}
|
||||
try:
|
||||
raw = json.loads(proc.stdout.decode("utf-8", errors="replace"))
|
||||
except json.JSONDecodeError:
|
||||
return {}
|
||||
peers = raw if isinstance(raw, list) else (raw.get("items") or raw.get("peers") or [])
|
||||
mapping: dict[str, str] = {}
|
||||
ip_re = re.compile(r"^\d+\.\d+\.\d+\.\d+$")
|
||||
for peer in peers:
|
||||
if not isinstance(peer, dict):
|
||||
continue
|
||||
ip = str(peer.get("ip") or "").strip()
|
||||
if not ip_re.match(ip):
|
||||
continue
|
||||
for key in ("hostname", "name", "dns_label"):
|
||||
val = str(peer.get(key) or "").strip()
|
||||
if val and val not in mapping:
|
||||
mapping[val] = ip
|
||||
return mapping
|
||||
|
||||
|
||||
def _norm_iso(val: str | None) -> str:
|
||||
if not val:
|
||||
return ""
|
||||
return val.replace(".000Z", "Z")
|
||||
|
||||
|
||||
def _extract_list(resp: Any, *keys: str) -> list[dict[str, Any]]:
|
||||
if isinstance(resp, dict):
|
||||
inner = resp.get("response", resp)
|
||||
if isinstance(inner, list):
|
||||
return inner
|
||||
if isinstance(inner, dict):
|
||||
for k in keys:
|
||||
if isinstance(inner.get(k), list):
|
||||
return inner[k]
|
||||
if isinstance(resp, list):
|
||||
return resp
|
||||
return []
|
||||
|
||||
|
||||
def sync_xray_template(
|
||||
api: RemnawaveAPI,
|
||||
tpl_name: str,
|
||||
tpl_json: dict[str, Any],
|
||||
changes: list[str],
|
||||
) -> str:
|
||||
_, tpl_list = api.request("GET", "/subscription-templates/")
|
||||
templates = _extract_list(tpl_list, "templates")
|
||||
tpl_uuid = next(
|
||||
(
|
||||
t["uuid"]
|
||||
for t in templates
|
||||
if t.get("name") == tpl_name and t.get("templateType") == "XRAY_JSON"
|
||||
),
|
||||
"",
|
||||
)
|
||||
if not tpl_uuid:
|
||||
_, created = api.request(
|
||||
"POST",
|
||||
"/subscription-templates/",
|
||||
{"name": tpl_name, "templateType": "XRAY_JSON", "templateJson": tpl_json},
|
||||
)
|
||||
tpl_uuid = (created.get("response") or created).get("uuid", "")
|
||||
if not tpl_uuid:
|
||||
raise RuntimeError(f"Не удалось создать шаблон {tpl_name}")
|
||||
changes.append(f"created xray template {tpl_name}")
|
||||
else:
|
||||
_, tpl_full = api.request("GET", f"/subscription-templates/{tpl_uuid}")
|
||||
current = (tpl_full.get("response") or {}).get("templateJson") or {}
|
||||
if current != tpl_json:
|
||||
api.request(
|
||||
"PATCH",
|
||||
"/subscription-templates/",
|
||||
{"uuid": tpl_uuid, "templateJson": tpl_json},
|
||||
)
|
||||
changes.append(f"updated xray template {tpl_name}")
|
||||
return tpl_uuid
|
||||
|
||||
|
||||
def ensure_profile_and_inbound(
|
||||
api: RemnawaveAPI,
|
||||
node_cfg: dict[str, Any],
|
||||
profile_templates: dict[str, str],
|
||||
changes: list[str],
|
||||
) -> tuple[str, str]:
|
||||
profile_name = node_cfg["profile_name"]
|
||||
inbound_tag = node_cfg["inbound_tag"]
|
||||
|
||||
_, profiles_resp = api.request("GET", "/config-profiles/")
|
||||
profiles = _extract_list(profiles_resp, "configProfiles")
|
||||
profile_uuid = next((p["uuid"] for p in profiles if p.get("name") == profile_name), "")
|
||||
|
||||
if not profile_uuid:
|
||||
tpl_key = node_cfg["profile_template"]
|
||||
raw_tpl = profile_templates.get(tpl_key)
|
||||
if not raw_tpl:
|
||||
raise RuntimeError(f"Не найден шаблон профиля {tpl_key}")
|
||||
|
||||
_, key_resp = api.request("GET", "/system/tools/x25519/generate")
|
||||
resp_obj = key_resp.get("response") or key_resp
|
||||
keypairs = resp_obj.get("keypairs") or []
|
||||
priv = (
|
||||
(keypairs[0].get("privateKey") if keypairs else None)
|
||||
or resp_obj.get("privateKey")
|
||||
or resp_obj.get("privKey")
|
||||
or ""
|
||||
)
|
||||
if not priv:
|
||||
raise RuntimeError("Не удалось сгенерировать X25519 ключ Reality")
|
||||
|
||||
short_id = secrets.token_hex(8)
|
||||
rendered = (
|
||||
raw_tpl.replace("__REALITY_PRIVATE_KEY__", priv)
|
||||
.replace("__REALITY_SHORT_ID__", short_id)
|
||||
.replace("__INBOUND_TAG__", inbound_tag)
|
||||
.replace("__SELFSTEAL_DOMAIN__", node_cfg["selfsteal_domain"])
|
||||
.replace("__XHTTP_PATH__", node_cfg["xhttp_path"])
|
||||
)
|
||||
_, created = api.request(
|
||||
"POST",
|
||||
"/config-profiles/",
|
||||
{"name": profile_name, "config": json.loads(rendered)},
|
||||
)
|
||||
profile_uuid = (created.get("response") or created).get("uuid", "")
|
||||
if not profile_uuid:
|
||||
raise RuntimeError(f"Не удалось создать config profile {profile_name}")
|
||||
changes.append(f"created profile {profile_name}")
|
||||
|
||||
_, profile_full = api.request("GET", f"/config-profiles/{profile_uuid}")
|
||||
inbounds = _extract_list(profile_full, "inbounds")
|
||||
if not inbounds:
|
||||
_, inb_resp = api.request("GET", f"/config-profiles/{profile_uuid}/inbounds")
|
||||
inbounds = _extract_list(inb_resp, "inbounds")
|
||||
|
||||
inbound_uuid = next((i["uuid"] for i in inbounds if i.get("tag") == inbound_tag), "")
|
||||
if not inbound_uuid:
|
||||
raise RuntimeError(f"Не найден inbound {inbound_tag} в профиле {profile_uuid}")
|
||||
return profile_uuid, inbound_uuid
|
||||
|
||||
|
||||
def sync_node_and_host(
|
||||
api: RemnawaveAPI,
|
||||
node_cfg: dict[str, Any],
|
||||
profile_uuid: str,
|
||||
inbound_uuid: str,
|
||||
tpl_uuid: str,
|
||||
nb_peers: dict[str, str],
|
||||
netbird_connected: bool,
|
||||
changes: list[str],
|
||||
warnings: list[str],
|
||||
) -> None:
|
||||
node_name = node_cfg["name"]
|
||||
target_addr = node_cfg["address"]
|
||||
nb_host = node_cfg.get("netbird_hostname", "")
|
||||
if netbird_connected and nb_host and nb_host in nb_peers:
|
||||
target_addr = nb_peers[nb_host]
|
||||
elif nb_host:
|
||||
warnings.append(
|
||||
f"node {node_name}: используется fallback-адрес {target_addr} (NetBird peer {nb_host} недоступен)"
|
||||
)
|
||||
|
||||
_, nodes_resp = api.request("GET", "/nodes/")
|
||||
nodes = _extract_list(nodes_resp)
|
||||
existing_node = next((n for n in nodes if n.get("name") == node_name), None)
|
||||
|
||||
desired_cp = {
|
||||
"activeConfigProfileUuid": profile_uuid,
|
||||
"activeInbounds": [inbound_uuid],
|
||||
}
|
||||
|
||||
if not existing_node:
|
||||
_, created = api.request(
|
||||
"POST",
|
||||
"/nodes/",
|
||||
{
|
||||
"name": node_name,
|
||||
"address": target_addr,
|
||||
"port": int(node_cfg["port"]),
|
||||
"countryCode": node_cfg["country_code"],
|
||||
"configProfile": desired_cp,
|
||||
},
|
||||
)
|
||||
node_uuid = (created.get("response") or created).get("uuid", "")
|
||||
if not node_uuid:
|
||||
raise RuntimeError(f"Не удалось создать ноду {node_name}")
|
||||
changes.append(f"created node {node_name} ({target_addr})")
|
||||
else:
|
||||
node_uuid = existing_node["uuid"]
|
||||
cur_cp = existing_node.get("configProfile") or {}
|
||||
cur_inbounds = [
|
||||
i["uuid"] if isinstance(i, dict) else i
|
||||
for i in (cur_cp.get("activeInbounds") or [])
|
||||
]
|
||||
needs_update = (
|
||||
existing_node.get("address") != target_addr
|
||||
or int(existing_node.get("port") or 0) != int(node_cfg["port"])
|
||||
or cur_cp.get("activeConfigProfileUuid") != profile_uuid
|
||||
or cur_inbounds != [inbound_uuid]
|
||||
)
|
||||
if needs_update:
|
||||
payload = {
|
||||
"uuid": node_uuid,
|
||||
"address": target_addr,
|
||||
"port": int(node_cfg["port"]),
|
||||
"configProfile": desired_cp,
|
||||
}
|
||||
code, _ = api.request("PATCH", "/nodes/", payload, allow_error=True)
|
||||
if not (200 <= code < 300):
|
||||
api.request("PUT", "/nodes/", payload)
|
||||
changes.append(f"updated node {node_name} ({target_addr})")
|
||||
|
||||
selfsteal = node_cfg["selfsteal_domain"]
|
||||
_, hosts_resp = api.request("GET", "/hosts/")
|
||||
hosts = _extract_list(hosts_resp)
|
||||
existing_host = next((h for h in hosts if h.get("address") == selfsteal), None)
|
||||
|
||||
if not existing_host:
|
||||
host_payload = {
|
||||
"remark": node_cfg["host_remark"],
|
||||
"address": selfsteal,
|
||||
"port": 443,
|
||||
"path": node_cfg["xhttp_path"],
|
||||
"sni": selfsteal,
|
||||
"host": selfsteal,
|
||||
"fingerprint": "chrome",
|
||||
"isDisabled": False,
|
||||
"securityLayer": "DEFAULT",
|
||||
"alpn": "h2",
|
||||
"xHttpExtraParams": {
|
||||
"xmux": {
|
||||
"maxConcurrency": "16-32",
|
||||
"hMaxRequestTimes": "600-900",
|
||||
"hMaxReusableSecs": "1800-3000",
|
||||
},
|
||||
"noSSEHeader": True,
|
||||
"xPaddingBytes": "100-1000",
|
||||
},
|
||||
"inbound": {
|
||||
"configProfileUuid": profile_uuid,
|
||||
"configProfileInboundUuid": inbound_uuid,
|
||||
},
|
||||
"nodes": [node_uuid],
|
||||
}
|
||||
if tpl_uuid:
|
||||
host_payload["xrayJsonTemplateUuid"] = tpl_uuid
|
||||
_, created = api.request("POST", "/hosts/", host_payload)
|
||||
host_uuid = (created.get("response") or created).get("uuid", "")
|
||||
if not host_uuid:
|
||||
raise RuntimeError(f"Не удалось создать host {selfsteal}")
|
||||
changes.append(f"created host {selfsteal}")
|
||||
else:
|
||||
host_uuid = existing_host["uuid"]
|
||||
patch_fields: dict[str, Any] = {"uuid": host_uuid}
|
||||
if existing_host.get("remark") != node_cfg["host_remark"]:
|
||||
patch_fields["remark"] = node_cfg["host_remark"]
|
||||
if tpl_uuid and existing_host.get("xrayJsonTemplateUuid") != tpl_uuid:
|
||||
patch_fields["xrayJsonTemplateUuid"] = tpl_uuid
|
||||
if len(patch_fields) > 1:
|
||||
api.request("PATCH", "/hosts/", patch_fields)
|
||||
changes.append(f"updated host {selfsteal}")
|
||||
|
||||
|
||||
def sync_squad(
|
||||
api: RemnawaveAPI,
|
||||
squad_name: str,
|
||||
inbound_uuids: list[str],
|
||||
changes: list[str],
|
||||
) -> dict[str, str]:
|
||||
_, squads_resp = api.request("GET", "/internal-squads/")
|
||||
squads = _extract_list(squads_resp, "internalSquads")
|
||||
squad_map = {s["name"]: s["uuid"] for s in squads if s.get("name") and s.get("uuid")}
|
||||
|
||||
target = next((s for s in squads if s.get("name") == squad_name), None)
|
||||
if target and inbound_uuids:
|
||||
cur_inbounds = [i["uuid"] for i in (target.get("inbounds") or []) if i.get("uuid")]
|
||||
missing = [u for u in inbound_uuids if u not in cur_inbounds]
|
||||
if missing:
|
||||
merged = list(dict.fromkeys(cur_inbounds + inbound_uuids))
|
||||
api.request(
|
||||
"PATCH",
|
||||
"/internal-squads/",
|
||||
{"uuid": target["uuid"], "inbounds": merged},
|
||||
)
|
||||
changes.append(f"updated squad {squad_name} inbounds")
|
||||
return squad_map
|
||||
|
||||
|
||||
def sync_subscription_settings(
|
||||
api: RemnawaveAPI,
|
||||
sub_page_cfg_name: str,
|
||||
changes: list[str],
|
||||
) -> None:
|
||||
_, cfgs_resp = api.request("GET", "/subscription-page-configs/")
|
||||
configs = _extract_list(cfgs_resp, "configs")
|
||||
if not any(c.get("name") == sub_page_cfg_name for c in configs):
|
||||
api.request("POST", "/subscription-page-configs/", {"name": sub_page_cfg_name})
|
||||
changes.append(f"created subscription-page-config {sub_page_cfg_name}")
|
||||
|
||||
code, settings_resp = api.request("GET", "/subscription-settings/", allow_error=True)
|
||||
if not (200 <= code < 300) or not isinstance(settings_resp, dict):
|
||||
return
|
||||
|
||||
resp_obj = settings_resp.get("response") or settings_resp
|
||||
browser_rule = {
|
||||
"name": "Browser",
|
||||
"enabled": True,
|
||||
"operator": "OR",
|
||||
"description": "IaC: HTML-страница подписки",
|
||||
"responseType": "BROWSER",
|
||||
"conditions": [
|
||||
{"headerName": "accept", "operator": "CONTAINS", "value": "text/html", "caseSensitive": False},
|
||||
{"headerName": "user-agent", "operator": "CONTAINS", "value": "Mozilla", "caseSensitive": False},
|
||||
],
|
||||
}
|
||||
cur_rules = (resp_obj.get("responseRules") or {}).get("rules") or []
|
||||
other_rules = [
|
||||
r
|
||||
for r in cur_rules
|
||||
if r.get("name") not in ("Browser", "Browser Subscription")
|
||||
and r.get("responseType") != "BROWSER"
|
||||
]
|
||||
desired_rules = [browser_rule] + other_rules
|
||||
|
||||
if (
|
||||
resp_obj.get("serveJsonAtBaseSubscription") is True
|
||||
and len(cur_rules) == len(desired_rules)
|
||||
and cur_rules
|
||||
and cur_rules[0].get("name") == "Browser"
|
||||
and cur_rules[0].get("responseType") == "BROWSER"
|
||||
and cur_rules[0].get("enabled") is True
|
||||
):
|
||||
return
|
||||
|
||||
patched = dict(resp_obj)
|
||||
patched["serveJsonAtBaseSubscription"] = True
|
||||
patched["responseRules"] = dict(resp_obj.get("responseRules") or {})
|
||||
patched["responseRules"]["rules"] = desired_rules
|
||||
|
||||
pcode, _ = api.request("PATCH", "/subscription-settings/", patched, allow_error=True)
|
||||
if not (200 <= pcode < 300):
|
||||
api.request("PUT", "/subscription-settings/", patched)
|
||||
changes.append("updated subscription-settings Browser rule")
|
||||
|
||||
|
||||
def sync_users(
|
||||
api: RemnawaveAPI,
|
||||
desired_users: list[dict[str, Any]],
|
||||
squad_map: dict[str, str],
|
||||
xray_checker_user: str,
|
||||
sub_domain: str,
|
||||
changes: list[str],
|
||||
) -> str:
|
||||
code, users_resp = api.request("GET", "/users/?size=500", allow_error=True)
|
||||
if not (200 <= code < 300):
|
||||
_, users_resp = api.request("GET", "/users/")
|
||||
existing_users = _extract_list(users_resp, "users")
|
||||
by_name = {u["username"]: u for u in existing_users if u.get("username")}
|
||||
|
||||
desired_names: set[str] = set()
|
||||
mutated = False
|
||||
|
||||
for u in desired_users:
|
||||
uname = u["username"]
|
||||
desired_names.add(uname)
|
||||
squad_name = u.get("squad", "Default-Squad")
|
||||
sid = squad_map.get(squad_name, "")
|
||||
want_squads = [sid] if sid else []
|
||||
want_body = {
|
||||
"username": uname,
|
||||
"status": u.get("status", "ACTIVE"),
|
||||
"expireAt": u["expire_at"],
|
||||
"description": u.get("description", ""),
|
||||
"trafficLimitBytes": int(u.get("traffic_limit_bytes", 0)),
|
||||
"trafficLimitStrategy": u.get("traffic_limit_strategy", "NO_RESET"),
|
||||
"activeInternalSquads": want_squads,
|
||||
}
|
||||
|
||||
cur = by_name.get(uname)
|
||||
if not cur:
|
||||
api.request("POST", "/users/", want_body)
|
||||
changes.append(f"created user {uname}")
|
||||
mutated = True
|
||||
else:
|
||||
cur_squads = [
|
||||
s["uuid"] if isinstance(s, dict) else s
|
||||
for s in (cur.get("activeInternalSquads") or [])
|
||||
]
|
||||
differs = (
|
||||
cur.get("status") != want_body["status"]
|
||||
or _norm_iso(cur.get("expireAt")) != _norm_iso(want_body["expireAt"])
|
||||
or (cur.get("description") or "") != want_body["description"]
|
||||
or int(cur.get("trafficLimitBytes") or 0) != want_body["trafficLimitBytes"]
|
||||
or cur.get("trafficLimitStrategy") != want_body["trafficLimitStrategy"]
|
||||
or cur_squads != want_squads
|
||||
)
|
||||
if differs:
|
||||
api.request("PATCH", "/users/", {**want_body, "uuid": cur["uuid"]})
|
||||
changes.append(f"updated user {uname}")
|
||||
mutated = True
|
||||
|
||||
for uname, cur in by_name.items():
|
||||
if uname not in desired_names:
|
||||
api.request("DELETE", f"/users/{cur['uuid']}")
|
||||
changes.append(f"deleted user {uname}")
|
||||
mutated = True
|
||||
|
||||
if mutated:
|
||||
_, users_resp = api.request("GET", "/users/?size=500")
|
||||
existing_users = _extract_list(users_resp, "users")
|
||||
by_name = {u["username"]: u for u in existing_users if u.get("username")}
|
||||
|
||||
xray_url = ""
|
||||
if xray_checker_user and xray_checker_user in by_name:
|
||||
uobj = by_name[xray_checker_user]
|
||||
xray_url = uobj.get("subscriptionUrl") or ""
|
||||
if not xray_url and uobj.get("shortUuid") and sub_domain:
|
||||
xray_url = f"https://{sub_domain}/{uobj['shortUuid']}"
|
||||
return xray_url
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: sync_remnawave.py <sync_config.json>", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
with open(sys.argv[1], "r", encoding="utf-8") as fh:
|
||||
cfg = json.load(fh)
|
||||
|
||||
token = os.environ.get("RW_API_TOKEN", "").strip()
|
||||
if not token:
|
||||
print("RW_API_TOKEN is required", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
api = RemnawaveAPI(cfg["panel_domain"], token)
|
||||
changes: list[str] = []
|
||||
warnings: list[str] = []
|
||||
|
||||
nb_peers = {}
|
||||
if cfg.get("netbird_connected"):
|
||||
nb_peers = resolve_netbird_peers(
|
||||
cfg.get("netbird_peers_url", ""),
|
||||
os.environ.get("NB_API_TOKEN", "").strip(),
|
||||
)
|
||||
|
||||
tpl_uuid = sync_xray_template(
|
||||
api,
|
||||
cfg["xray_template"]["name"],
|
||||
cfg["xray_template_json"],
|
||||
changes,
|
||||
)
|
||||
|
||||
inbound_uuids: list[str] = []
|
||||
for node_cfg in cfg.get("nodes", []):
|
||||
profile_uuid, inbound_uuid = ensure_profile_and_inbound(
|
||||
api, node_cfg, cfg.get("profile_templates", {}), changes
|
||||
)
|
||||
inbound_uuids.append(inbound_uuid)
|
||||
sync_node_and_host(
|
||||
api,
|
||||
node_cfg,
|
||||
profile_uuid,
|
||||
inbound_uuid,
|
||||
tpl_uuid,
|
||||
nb_peers,
|
||||
bool(cfg.get("netbird_connected")),
|
||||
changes,
|
||||
warnings,
|
||||
)
|
||||
|
||||
squad_map = sync_squad(api, cfg.get("squad_name", "Default-Squad"), inbound_uuids, changes)
|
||||
sync_subscription_settings(api, cfg.get("subscription_page_config", "Homelab"), changes)
|
||||
xray_url = sync_users(
|
||||
api,
|
||||
cfg.get("users", []),
|
||||
squad_map,
|
||||
cfg.get("xray_checker_user", "homelab"),
|
||||
cfg.get("sub_domain", ""),
|
||||
changes,
|
||||
)
|
||||
|
||||
result = {
|
||||
"changed": len(changes) > 0,
|
||||
"changes": changes,
|
||||
"warnings": warnings,
|
||||
"xray_checker_subscription_url": xray_url,
|
||||
}
|
||||
print(json.dumps(result, ensure_ascii=False))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user