diff --git a/.gitea/workflows/ansible-deploy.yml b/.gitea/workflows/ansible-deploy.yml index e7de13e..7f61afe 100644 --- a/.gitea/workflows/ansible-deploy.yml +++ b/.gitea/workflows/ansible-deploy.yml @@ -11,18 +11,10 @@ on: workflow_dispatch: inputs: machine: - description: 'Целевая машина (или all для полного прогона)' + description: 'Машина: all, имя плейбука или хост группы remnanodes (имя с -nod-)' required: true default: 'all' - type: choice - options: - - all - - core-net - - core-secrets - - core-mon - - rus-central - - ger-nod-02 - - opnsense + type: string tags: description: 'Необязательный фильтр тегов (например: remnawave, netbird, monitoring)' required: false diff --git a/ansible/ci/ansible-plan.py b/ansible/ci/ansible-plan.py index 68b2c60..468f00a 100644 --- a/ansible/ci/ansible-plan.py +++ b/ansible/ci/ansible-plan.py @@ -9,7 +9,7 @@ import subprocess import sys from pathlib import Path -SKIP_PLAYBOOKS = {"site", "host", "core-git"} +SKIP_PLAYBOOKS = {"site", "host", "core-git", "remnanodes"} def list_machines(playbooks_dir: Path) -> list[str]: @@ -21,6 +21,25 @@ def list_machines(playbooks_dir: Path) -> list[str]: return machines +def remnanode_hosts(inventory: Path) -> list[str]: + hosts: list[str] = [] + in_group = False + for line in inventory.read_text(encoding="utf-8").splitlines(): + if not in_group: + if line.startswith(" remnanodes:"): + in_group = True + continue + if not line.strip() or line.lstrip().startswith("#"): + continue + indent = len(line) - len(line.lstrip(" ")) + if indent <= 4: + break + stripped = line.strip() + if indent >= 8 and stripped.endswith(":") and stripped != "hosts:": + hosts.append(stripped[:-1]) + return hosts + + def main() -> int: event = os.environ.get("EVENT_NAME", "push").strip() before = os.environ.get("EVENT_BEFORE", "").strip() @@ -30,6 +49,9 @@ def main() -> int: root = Path(__file__).resolve().parents[2] machines = list_machines(root / "ansible" / "playbooks") + for host in remnanode_hosts(root / "ansible" / "inventories" / "prod" / "hosts.yml"): + if host not in machines: + machines.append(host) include: list[dict[str, str]] = [] if event == "workflow_dispatch": diff --git a/ansible/ci/ansible-target.sh b/ansible/ci/ansible-target.sh index 2ce06fc..850582b 100755 --- a/ansible/ci/ansible-target.sh +++ b/ansible/ci/ansible-target.sh @@ -12,14 +12,39 @@ before=${3:-} after=${4:-} override_tags=${5:-} +is_remnanode() { + awk -v m="$1" ' + $0 ~ "^ remnanodes:" {s=1; next} + s && $0 ~ "^ [^ ]" {s=0} + s && $0 ~ "^ " m ":$" {f=1} + END {exit !f} + ' ansible/inventories/prod/hosts.yml +} + playbook="ansible/playbooks/${machine}.yml" -if [ "$machine" = "site" ] || [ "$machine" = "host" ] || [ ! -f "$playbook" ]; then +playbook_out="playbooks/${machine}.yml" +limit="" +if [ "$machine" = "site" ] || [ "$machine" = "host" ]; then echo "Неизвестная машина: $machine" >&2 exit 2 fi +if [ ! -f "$playbook" ]; then + if is_remnanode "$machine"; then + playbook="ansible/playbooks/remnanodes.yml" + playbook_out="playbooks/remnanodes.yml" + limit="$machine" + else + echo "Неизвестная машина: $machine" >&2 + exit 2 + fi +fi if [ -n "$override_tags" ]; then - printf '%s\n' "playbooks/${machine}.yml" "--tags" "$override_tags" + if [ -n "$limit" ]; then + printf '%s\n' "$playbook_out" "--limit" "$limit" "--tags" "$override_tags" + else + printf '%s\n' "$playbook_out" "--tags" "$override_tags" + fi exit 0 fi @@ -63,9 +88,19 @@ if [ "$full" -eq 0 ] && [ -n "$changed" ]; then for file in $changed; do [ -n "$file" ] || continue case "$file" in - ansible/ansible.cfg | ansible/roles/host/* | ansible/playbooks/host.yml | ansible/playbooks/site.yml | ansible/playbooks/"$machine".yml | ansible/inventories/prod/hosts.yml | ansible/inventories/prod/group_vars/* | ansible/inventories/prod/host_vars/"$machine".yml) + ansible/ansible.cfg | ansible/roles/host/* | ansible/playbooks/host.yml | ansible/playbooks/site.yml | ansible/playbooks/"$machine".yml | ansible/inventories/prod/group_vars/* | ansible/inventories/prod/host_vars/"$machine".yml) full_from_files=1 ;; + ansible/inventories/prod/hosts.yml) + if git diff -U0 "$before" "$after" -- "$file" | grep -E "^[+-].*${machine}:" >/dev/null; then + full_from_files=1 + fi + ;; + ansible/playbooks/remnanodes.yml) + if is_remnanode "$machine"; then + full_from_files=1 + fi + ;; ansible/roles/netbird_client/* | ansible/roles/netbird_exit_node/*) if grep -qE "role: (netbird_client|netbird_exit_node)" "$playbook"; then add_tag netbird @@ -86,12 +121,19 @@ if [ "$full" -eq 0 ] && [ -n "$changed" ]; then add_tag vector fi ;; + ansible/roles/core-net/remnawave/files/*|\ + ansible/roles/core-net/remnawave/tasks/sync.yml|\ + ansible/roles/core-net/remnawave/tasks/publish.yml) + if [ "$machine" = "core-net" ]; then + add_tag remnawave_sync + fi + ;; ansible/roles/"$machine"/.ci-trigger) ;; ansible/roles/"$machine"/*/*) product=${file#ansible/roles/"$machine"/} product=${product%%/*} - if grep -q "role: ${machine}/${product}" "$playbook" && grep -q "tags: \[${product}\]" "$playbook"; then + if grep -qE "role: ${machine}/${product}|name: ${machine}/${product}" "$playbook" && grep -q "tags: \\[${product}" "$playbook"; then add_tag "$product" elif [ ! -e "$file" ] && [ ! -d "ansible/roles/${machine}/${product}" ]; then : @@ -115,5 +157,9 @@ fi if [ "$full" -eq 1 ] || [ "$full_from_files" -eq 1 ]; then printf '%s\n' "playbooks/site.yml" "--limit" "$machine" elif [ -n "$tags" ]; then - printf '%s\n' "playbooks/${machine}.yml" "--tags" "$tags" + if [ -n "$limit" ]; then + printf '%s\n' "$playbook_out" "--limit" "$limit" "--tags" "$tags" + else + printf '%s\n' "$playbook_out" "--tags" "$tags" + fi fi diff --git a/ansible/ci/deploy-machine.sh b/ansible/ci/deploy-machine.sh index 762f5f9..3d5f2f0 100755 --- a/ansible/ci/deploy-machine.sh +++ b/ansible/ci/deploy-machine.sh @@ -18,7 +18,20 @@ case "$machine" in ;; esac -if [ "$machine" = "site" ] || [ "$machine" = "host" ] || [ ! -f "$root/ansible/playbooks/${machine}.yml" ]; then +is_remnanode() { + awk -v m="$1" ' + $0 ~ "^ remnanodes:" {s=1; next} + s && $0 ~ "^ [^ ]" {s=0} + s && $0 ~ "^ " m ":$" {f=1} + END {exit !f} + ' "$root/ansible/inventories/prod/hosts.yml" +} + +if [ "$machine" = "site" ] || [ "$machine" = "host" ]; then + echo "Неизвестная машина: $machine" >&2 + exit 2 +fi +if [ ! -f "$root/ansible/playbooks/${machine}.yml" ] && ! is_remnanode "$machine"; then echo "Неизвестная машина: $machine" >&2 exit 2 fi diff --git a/ansible/inventories/prod/host_vars/ger-nod-02.yml b/ansible/inventories/prod/host_vars/ger-nod-02.yml index 8a4c2ce..989966e 100644 --- a/ansible/inventories/prod/host_vars/ger-nod-02.yml +++ b/ansible/inventories/prod/host_vars/ger-nod-02.yml @@ -1,7 +1,7 @@ --- ansible_host: 150.241.64.209 ansible_user: root -remnanode_selfsteal_domain: "yali.gunis.top" +# Домен SelfSteal берётся из desired.yml (профиль ноды). # WAN egress панели (core-net): оба провайдера → NODE_PORT remnanode_panel_egress_ips: - "213.128.28.107" # провайдер 1 (МТС) diff --git a/ansible/inventories/prod/hosts.yml b/ansible/inventories/prod/hosts.yml index 77b03c3..bb35219 100644 --- a/ansible/inventories/prod/hosts.yml +++ b/ansible/inventories/prod/hosts.yml @@ -3,6 +3,7 @@ # кроме monitoring_scrape_exclude_hosts, хостов с monitoring_scrape_enabled: false # и хостов из vmagent_central_scrape_skip (у них свой vmagent). # ger-nod-02 отдаёт метрики своим vmagent. +# Выходные ноды Remnawave: имя с «-nod-», группы remnanodes и exit_nodes. all: hosts: core-net: @@ -27,6 +28,9 @@ all: ansible_host: 192.168.1.1 ansible_connection: local children: + remnanodes: + hosts: + ger-nod-02: exit_nodes: hosts: ger-nod-02: diff --git a/ansible/playbooks/core-net.yml b/ansible/playbooks/core-net.yml index c2ab079..b090cdb 100644 --- a/ansible/playbooks/core-net.yml +++ b/ansible/playbooks/core-net.yml @@ -5,8 +5,6 @@ roles: - role: core-net/traefik tags: [traefik] - - role: core-net/remnawave - tags: [remnawave] - role: core-net/authentik tags: [authentik] - role: core-net/vmagent @@ -15,3 +13,19 @@ tags: [netbird] - role: vector tags: [vector] + tasks: + - name: Развернуть панель Remnawave + ansible.builtin.import_role: + name: core-net/remnawave + tasks_from: bootstrap.yml + tags: [remnawave, remnawave_bootstrap] + - name: Синхронизировать состояние Remnawave + ansible.builtin.import_role: + name: core-net/remnawave + tasks_from: sync.yml + tags: [remnawave, remnawave_sync] + - name: Опубликовать секреты Remnawave + ansible.builtin.import_role: + name: core-net/remnawave + tasks_from: publish.yml + tags: [remnawave, remnawave_sync, remnawave_bootstrap] diff --git a/ansible/playbooks/remnanodes.yml b/ansible/playbooks/remnanodes.yml new file mode 100644 index 0000000..9443d1c --- /dev/null +++ b/ansible/playbooks/remnanodes.yml @@ -0,0 +1,11 @@ +--- +- name: Выходные ноды Remnawave + hosts: remnanodes + gather_facts: false + roles: + - role: netbird_exit_node + tags: [netbird] + - role: remnanode + tags: [remnanode] + - role: vmagent + tags: [vmagent] diff --git a/ansible/playbooks/site.yml b/ansible/playbooks/site.yml index 6595f65..5b42c6a 100644 --- a/ansible/playbooks/site.yml +++ b/ansible/playbooks/site.yml @@ -5,5 +5,5 @@ - ansible.builtin.import_playbook: core-mon.yml - ansible.builtin.import_playbook: core-git.yml - ansible.builtin.import_playbook: rus-central.yml -- ansible.builtin.import_playbook: ger-nod-02.yml +- ansible.builtin.import_playbook: remnanodes.yml - ansible.builtin.import_playbook: opnsense.yml diff --git a/ansible/roles/core-net/remnawave/defaults/main.yml b/ansible/roles/core-net/remnawave/defaults/main.yml index 60f454b..d1bf13a 100644 --- a/ansible/roles/core-net/remnawave/defaults/main.yml +++ b/ansible/roles/core-net/remnawave/defaults/main.yml @@ -30,6 +30,9 @@ remnawave_netbird_peers_url: "https://central.gunihome.ru/api/peers" remnawave_xray_checker_user: "homelab" remnawave_api_token_name: "infra-automation" +# Pull образов только если изменился compose или явно -e remnawave_pull_images=true. +remnawave_pull_images: false + # Документация WAN egress панели (см. remnanode_panel_egress_ips на выходных нодах). remnawave_panel_egress_ips: - "213.128.28.107" # провайдер 1 (МТС) diff --git a/ansible/roles/core-net/remnawave/files/commands.yml b/ansible/roles/core-net/remnawave/files/commands.yml new file mode 100644 index 0000000..dc02b18 --- /dev/null +++ b/ansible/roles/core-net/remnawave/files/commands.yml @@ -0,0 +1,177 @@ +# Шаблоны команд Remnawave API. Новый однотипный ресурс — шаг здесь. +# prepare/compare_with/after/run — короткие хуки в sync_remnawave.py +# (ключ Reality, адрес NetBird, uuid профилей, правило Browser, пользователи). +# {поле} подставляется из элемента. Целая строка "{поле}" сохраняет тип (объект, список). +steps: + - id: xray_templates + from: xray_templates + prepare: load_json_file + list: {method: GET, path: /subscription-templates/, key: templates} + match: [name, templateType] + compare: [templateJson] + prune: + where: {templateType: XRAY_JSON} + save: {as: xray_template, key: name} + create: + method: POST + path: /subscription-templates/ + body: + name: "{name}" + templateType: "{templateType}" + templateJson: "{templateJson}" + update: + method: PATCH + path: /subscription-templates/ + body: + uuid: "{uuid}" + templateJson: "{templateJson}" + delete: + method: DELETE + path: "/subscription-templates/{uuid}" + + - id: profiles + from: profiles + prepare: render_profile + list: {method: GET, path: /config-profiles/, key: configProfiles} + match: [name] + compare_with: profile_config + prune: true + save: {as: profile, key: name} + refresh: {method: GET, path: "/config-profiles/{uuid}"} + create: + method: POST + path: /config-profiles/ + body: + name: "{name}" + config: "{config}" + update: + method: PATCH + path: /config-profiles/ + body: + uuid: "{uuid}" + config: "{config}" + delete: + method: DELETE + path: "/config-profiles/{uuid}" + + - id: nodes + from: nodes + prepare: prepare_node + list: {method: GET, path: /nodes/, key: nodes} + match: [name] + compare_with: node_binding + prune: true + after: enable_if_disabled + save: {as: node, key: name} + create: + method: POST + path: /nodes/ + body: + name: "{name}" + address: "{address}" + port: "{port}" + countryCode: "{countryCode}" + configProfile: "{configProfile}" + update: + method: PATCH + path: /nodes/ + body: + uuid: "{uuid}" + address: "{address}" + port: "{port}" + countryCode: "{countryCode}" + configProfile: "{configProfile}" + update_fallback: + method: PUT + path: /nodes/ + body: + uuid: "{uuid}" + address: "{address}" + port: "{port}" + countryCode: "{countryCode}" + configProfile: "{configProfile}" + delete: + method: DELETE + path: "/nodes/{uuid}" + + - id: hosts + from: nodes + prepare: prepare_host + list: {method: GET, path: /hosts/, key: hosts} + match_prepared: [address] + compare_with: host_binding + prune: true + save: {as: host, key: address} + create: + method: POST + path: /hosts/ + body: + remark: "{remark}" + address: "{address}" + port: "{port}" + path: "{path}" + sni: "{sni}" + host: "{host}" + 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: "{nodes}" + xrayJsonTemplateUuid: "{xrayJsonTemplateUuid}" + update: + method: PATCH + path: /hosts/ + body: + uuid: "{uuid}" + remark: "{remark}" + path: "{path}" + sni: "{sni}" + host: "{host}" + port: "{port}" + alpn: "{alpn}" + fingerprint: "{fingerprint}" + xrayJsonTemplateUuid: "{xrayJsonTemplateUuid}" + nodes: "{nodes}" + delete: + method: DELETE + path: "/hosts/{uuid}" + + - id: squads + from: squads + prepare: prepare_squad + list: {method: GET, path: /internal-squads/, key: internalSquads} + match: [name] + compare_with: squad_inbounds + prune: true + save: {as: squad, key: name} + create: + method: POST + path: /internal-squads/ + body: + name: "{name}" + inbounds: "{inbounds}" + update: + method: PATCH + path: /internal-squads/ + body: + uuid: "{uuid}" + inbounds: "{inbounds}" + delete: + method: DELETE + path: "/internal-squads/{uuid}" + + - id: subscription + run: subscription_settings + + - id: users + run: users diff --git a/ansible/roles/core-net/remnawave/files/desired.yml b/ansible/roles/core-net/remnawave/files/desired.yml new file mode 100644 index 0000000..1882159 --- /dev/null +++ b/ansible/roles/core-net/remnawave/files/desired.yml @@ -0,0 +1,32 @@ +# Желаемое состояние панели Remnawave. +# Новая выходная нода: запись в nodes, профиль (если новый), сквад, хост в inventory +# (группы remnanodes и exit_nodes, имя с «-nod-»). Повторный прогон панели — тег remnawave_sync, +# без pull образов. Сквад, нода, host и профиль, которых здесь нет, с панели удаляются. +# Тег inbound уникален среди профилей. + +xray_templates: + - name: Except-RU + templateType: XRAY_JSON + file: xray-except-ru.json + +profiles: + - name: DE-Yali + template: profile-selfsteal-xhttp.json + inbound_tag: Steal-Yali + selfsteal_domain: yali.gunis.top + xhttp_path: /xhttp-yali + +squads: + - name: Default-Squad + inbounds: [Steal-Yali] + +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: DE-Yali + host_remark: "🇩🇪 ger-nod-02" diff --git a/ansible/roles/core-net/remnawave/files/nodes.yml b/ansible/roles/core-net/remnawave/files/nodes.yml deleted file mode 100644 index 72bd41a..0000000 --- a/ansible/roles/core-net/remnawave/files/nodes.yml +++ /dev/null @@ -1,22 +0,0 @@ -# Декларативная конфигурация профилей, нод и хостов 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" - diff --git a/ansible/roles/core-net/remnawave/files/sync_remnawave.py b/ansible/roles/core-net/remnawave/files/sync_remnawave.py index a593786..12ec5a4 100644 --- a/ansible/roles/core-net/remnawave/files/sync_remnawave.py +++ b/ansible/roles/core-net/remnawave/files/sync_remnawave.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 -"""Идемпотентная синхронизация шаблонов, профилей, нод, хостов, сквадов и пользователей Remnawave.""" +"""Синхронизация Remnawave по desired.yml и шаблонам команд commands.yml.""" from __future__ import annotations +import copy import json import os import re @@ -13,7 +14,6 @@ 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]"', @@ -23,8 +23,6 @@ def _redact_text(text: str) -> str: 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" @@ -34,7 +32,7 @@ class RemnawaveAPI: self, method: str, path: str, - payload: dict[str, Any] | None = None, + payload: dict[str, Any] | list[Any] | None = None, allow_error: bool = False, ) -> tuple[int, Any]: cmd = [ @@ -56,13 +54,9 @@ class RemnawaveAPI: 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')}" - ) - + 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") @@ -72,30 +66,19 @@ class RemnawaveAPI: 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)}" - ) + if not (200 <= status < 300) and not allow_error: + 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) + proc = subprocess.run( + ["curl", "-fsS", "--max-time", "20", "-H", f"Authorization: Token {nb_token}", peers_url], + capture_output=True, + check=False, + ) if proc.returncode != 0: return {} try: @@ -118,538 +101,648 @@ def resolve_netbird_peers(peers_url: str, nb_token: str) -> dict[str, str]: 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 + return [x for x in inner if isinstance(x, dict)] if isinstance(inner, dict): - for k in keys: - if isinstance(inner.get(k), list): - return inner[k] + for key in keys: + if isinstance(inner.get(key), list): + return [x for x in inner[key] if isinstance(x, dict)] if isinstance(resp, list): - return resp + return [x for x in resp if isinstance(x, dict)] 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}, +def _as_obj(data: Any) -> dict[str, Any]: + if isinstance(data, dict) and isinstance(data.get("response"), dict): + return data["response"] + if isinstance(data, dict): + return data + return {} + + +def _norm_iso(val: str | None) -> str: + return (val or "").replace(".000Z", "Z") + + +def _fill(value: Any, item: dict[str, Any]) -> Any: + if isinstance(value, dict): + return {k: _fill(v, item) for k, v in value.items()} + if isinstance(value, list): + return [_fill(v, item) for v in value] + if isinstance(value, str) and value.startswith("{") and value.endswith("}") and value.count("{") == 1: + key = value[1:-1] + if key in item: + return item[key] + if isinstance(value, str) and "{" in value: + return re.sub(r"\{([^{}]+)\}", lambda m: str(item.get(m.group(1), m.group(0))), value) + return value + + +def _key(obj: dict[str, Any], fields: list[str]) -> tuple[Any, ...]: + return tuple(obj.get(field) for field in fields) + + +def _canon_profile(cfg: Any) -> Any: + if not isinstance(cfg, dict): + return cfg + cfg = copy.deepcopy(cfg) + for inbound in cfg.get("inbounds") or []: + settings = inbound.get("settings") + if isinstance(settings, dict): + settings.pop("clients", None) + reality = (inbound.get("streamSettings") or {}).get("realitySettings") + if isinstance(reality, dict): + for field in ("privateKey", "shortIds", "publicKey"): + reality.pop(field, None) + return cfg + + +def _reality(cfg: Any, tag: str) -> tuple[str, str]: + if not isinstance(cfg, dict): + return "", "" + for inbound in cfg.get("inbounds") or []: + if inbound.get("tag") != tag: + continue + reality = (inbound.get("streamSettings") or {}).get("realitySettings") or {} + private = str(reality.get("privateKey") or "") + shorts = reality.get("shortIds") or [] + short = str(shorts[0]) if shorts else "" + if private and not private.startswith("__"): + return private, short + return "", "" + + +def _ids(values: Any) -> list[str]: + out: list[str] = [] + for value in values or []: + if isinstance(value, dict) and value.get("uuid"): + out.append(value["uuid"]) + elif isinstance(value, str): + out.append(value) + return out + + +class Sync: + def __init__(self, cfg: dict[str, Any], api: RemnawaveAPI) -> None: + self.cfg = cfg + self.api = api + self.desired = cfg["desired"] + self.files: dict[str, str] = cfg.get("files") or {} + self.commands = cfg["commands"]["steps"] + self.changes: list[str] = [] + self.warnings: list[str] = [] + self.refs: dict[str, dict[str, dict[str, Any]]] = {} + self.inbounds: dict[str, dict[str, Any]] = {} + self.xray_url = "" + self.peers: dict[str, str] = {} + + def run(self) -> dict[str, Any]: + if self.cfg.get("netbird_connected"): + self.peers = resolve_netbird_peers( + self.cfg.get("netbird_peers_url", ""), + os.environ.get("NB_API_TOKEN", "").strip(), ) - changes.append(f"updated xray template {tpl_name}") - return tpl_uuid + pending: list[tuple[dict[str, Any], list[dict[str, Any]], set[tuple[Any, ...]]]] = [] + later = [step for step in self.commands if step.get("run")] + for step in self.commands: + if step.get("run"): + continue + pending.append(self._upsert(step)) + for step, remote, keep in reversed(pending): + self._prune(step, remote, keep) + for step in later: + getattr(self, f"run_{step['run']}")() + self._write_xray_url() + return {"changed": bool(self.changes), "changes": self.changes, "warnings": self.warnings} + def _list(self, step: dict[str, Any]) -> list[dict[str, Any]]: + spec = step["list"] + _, data = self.api.request(spec["method"], spec["path"]) + return _extract_list(data, spec.get("key", "")) -def _render_profile_config( - api: RemnawaveAPI, - node_cfg: dict[str, Any], - profile_templates: dict[str, str], -) -> dict[str, Any]: - tpl_key = node_cfg["profile_template"] - raw_tpl = profile_templates.get(tpl_key) - if not raw_tpl: - raise RuntimeError(f"Не найден шаблон профиля {tpl_key}") - if isinstance(raw_tpl, dict): - raw_tpl = json.dumps(raw_tpl) + def _upsert(self, step: dict[str, Any]) -> tuple[dict[str, Any], list[dict[str, Any]], set[tuple[Any, ...]]]: + remote = self._list(step) + fields = step.get("match") or step.get("match_prepared") or ["name"] + index = {_key(obj, fields): obj for obj in remote} + keep: set[tuple[Any, ...]] = set() + for raw in self.desired.get(step["from"], []) or []: + existing = index.get(_key(raw, step["match"])) if step.get("match") else None + prepared = self._prepare(step, raw, existing) + ident = _key(prepared, fields) + if step.get("match_prepared"): + existing = index.get(ident) + keep.add(ident) + merged = dict(prepared) + if existing: + merged["uuid"] = existing.get("uuid", "") + if not existing: + _, created = self._send(step["create"], merged) + obj = _as_obj(created) + self.changes.append(f"created {step['id']} {ident[0]}") + elif self._differs(step, existing, prepared): + code, updated = self._send(step["update"], merged, allow_error=bool(step.get("update_fallback"))) + if not (200 <= code < 300) and step.get("update_fallback"): + _, updated = self._send(step["update_fallback"], merged) + obj = _as_obj(updated) or dict(existing) + obj.setdefault("uuid", existing.get("uuid")) + self.changes.append(f"updated {step['id']} {ident[0]}") + else: + obj = dict(existing) + if step.get("refresh") and obj.get("uuid"): + _, refreshed = self.api.request(step["refresh"]["method"], _fill(step["refresh"]["path"], obj)) + obj = _as_obj(refreshed) or obj + if step.get("id") == "profiles": + obj = self._with_inbounds(obj) + self._save(step, prepared, obj) + if step.get("after") == "enable_if_disabled" and obj.get("uuid"): + if not existing or existing.get("isDisabled"): + self.api.request("POST", f"/nodes/{obj['uuid']}/actions/enable", {}) + if existing and existing.get("isDisabled"): + self.changes.append(f"enabled node {prepared.get('name')}") + return step, remote, keep - _, 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") + def _prepare(self, step: dict[str, Any], raw: dict[str, Any], existing: dict[str, Any] | None) -> dict[str, Any]: + name = step.get("prepare") + if not name: + return dict(raw) + return getattr(self, name)(dict(raw), existing) - short_id = secrets.token_hex(8) - rendered = ( - raw_tpl.replace("__REALITY_PRIVATE_KEY__", priv) - .replace("__REALITY_SHORT_ID__", short_id) - .replace("__INBOUND_TAG__", node_cfg["inbound_tag"]) - .replace("__SELFSTEAL_DOMAIN__", node_cfg["selfsteal_domain"]) - .replace("__XHTTP_PATH__", node_cfg["xhttp_path"]) - ) - return json.loads(rendered) + def _send( + self, + spec: dict[str, Any], + item: dict[str, Any], + allow_error: bool = False, + ) -> tuple[int, Any]: + body = _fill(spec.get("body"), item) if "body" in spec else None + return self.api.request(_fill(spec["method"], item), _fill(spec["path"], item), body, allow_error=allow_error) + def _differs(self, step: dict[str, Any], existing: dict[str, Any], prepared: dict[str, Any]) -> bool: + kind = step.get("compare_with") + if kind: + return getattr(self, f"diff_{kind}")(existing, prepared) + for field in step.get("compare") or []: + if existing.get(field) != prepared.get(field): + return True + return False -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"] + def _save(self, step: dict[str, Any], prepared: dict[str, Any], obj: dict[str, Any]) -> None: + save = step.get("save") + if not save: + return + key = prepared.get(save.get("key", "name")) + stored = dict(obj) + stored.update({k: v for k, v in prepared.items() if k not in stored or k in ("config", "inbounds")}) + self.refs.setdefault(save["as"], {})[key] = stored - _, 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), "") + def _with_inbounds(self, obj: dict[str, Any]) -> dict[str, Any]: + inbounds = obj.get("inbounds") if isinstance(obj.get("inbounds"), list) else [] + if not inbounds and obj.get("uuid"): + _, data = self.api.request("GET", f"/config-profiles/{obj['uuid']}/inbounds") + inbounds = _extract_list(data, "inbounds") + obj["inbounds"] = inbounds + for inbound in inbounds: + tag = inbound.get("tag") + if not tag: + continue + if tag in self.inbounds and self.inbounds[tag].get("uuid") != inbound.get("uuid"): + raise RuntimeError(f"Тег inbound {tag} используется больше чем в одном профиле") + self.inbounds[tag] = inbound + return obj - if not profile_uuid: - cfg_obj = _render_profile_config(api, node_cfg, profile_templates) - _, created = api.request( - "POST", - "/config-profiles/", - {"name": profile_name, "config": cfg_obj}, + def _prune(self, step: dict[str, Any], remote: list[dict[str, Any]], keep: set[tuple[Any, ...]]) -> None: + prune = step.get("prune", False) + if not prune or not step.get("delete"): + return + where = prune.get("where", {}) if isinstance(prune, dict) else {} + fields = step.get("match") or step.get("match_prepared") or ["name"] + for obj in remote: + if any(obj.get(key) != value for key, value in where.items()): + continue + if _key(obj, fields) in keep or not obj.get("uuid"): + continue + self.api.request(step["delete"]["method"], _fill(step["delete"]["path"], obj)) + self.changes.append(f"deleted {step['id']} {obj.get(fields[0])}") + + def load_json_file(self, item: dict[str, Any], _existing: dict[str, Any] | None) -> dict[str, Any]: + item["templateJson"] = json.loads(self.files[item["file"]]) + item["templateType"] = item.get("templateType") or "XRAY_JSON" + return item + + def render_profile(self, item: dict[str, Any], existing: dict[str, Any] | None) -> dict[str, Any]: + current = (existing or {}).get("config") + if existing and not current and existing.get("uuid"): + _, data = self.api.request("GET", f"/config-profiles/{existing['uuid']}") + current = _as_obj(data).get("config") + private, short = _reality(current, item["inbound_tag"]) + if not private: + _, data = self.api.request("GET", "/system/tools/x25519/generate") + body = _as_obj(data) + pairs = body.get("keypairs") or [] + private = str((pairs[0].get("privateKey") if pairs else None) or body.get("privateKey") or body.get("privKey") or "") + if not private: + raise RuntimeError(f"Не удалось сгенерировать X25519 для профиля {item['name']}") + if not short: + short = secrets.token_hex(8) + raw = self.files[item["template"]] + rendered = ( + raw.replace("__REALITY_PRIVATE_KEY__", private) + .replace("__REALITY_SHORT_ID__", short) + .replace("__INBOUND_TAG__", item["inbound_tag"]) + .replace("__SELFSTEAL_DOMAIN__", item["selfsteal_domain"]) + .replace("__XHTTP_PATH__", item["xhttp_path"]) ) - 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}") + item["config"] = json.loads(rendered) + return item - def _get_inbounds() -> list[dict[str, Any]]: - _, profile_full = api.request("GET", f"/config-profiles/{profile_uuid}") - inbs = _extract_list(profile_full, "inbounds") - if not inbs: - _, inb_resp = api.request("GET", f"/config-profiles/{profile_uuid}/inbounds") - inbs = _extract_list(inb_resp, "inbounds") - return inbs + def diff_profile_config(self, existing: dict[str, Any], prepared: dict[str, Any]) -> bool: + current = existing.get("config") + if current is None and existing.get("uuid"): + _, data = self.api.request("GET", f"/config-profiles/{existing['uuid']}") + current = _as_obj(data).get("config") + if _reality(current, prepared["inbound_tag"])[0] == "": + return True + return _canon_profile(current) != _canon_profile(prepared.get("config")) - inbounds = _get_inbounds() - inbound_uuid = next((i["uuid"] for i in inbounds if i.get("tag") == inbound_tag), "") - if not inbound_uuid: - cfg_obj = _render_profile_config(api, node_cfg, profile_templates) - api.request("PATCH", "/config-profiles/", {"uuid": profile_uuid, "config": cfg_obj}) - changes.append(f"updated profile {profile_name} for inbound {inbound_tag}") - inbounds = _get_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 _profile(self, name: str) -> dict[str, Any]: + try: + return self.refs["profile"][name] + except KeyError as exc: + raise RuntimeError(f"Профиль {name} не синхронизирован") from exc + def prepare_node(self, item: dict[str, Any], _existing: dict[str, Any] | None) -> dict[str, Any]: + profile = self._profile(item["profile"]) + inbound = self.inbounds.get(profile["inbound_tag"]) + if not inbound or not inbound.get("uuid"): + raise RuntimeError(f"Нет inbound {profile.get('inbound_tag')} у профиля {item['profile']}") + host = item.get("netbird_hostname") or "" + address = item["address"] + if self.cfg.get("netbird_connected") and host and host in self.peers: + address = self.peers[host] + elif host: + self.warnings.append(f"node {item['name']}: fallback-адрес {address} (NetBird peer {host} недоступен)") + item["address"] = address + item["port"] = int(item["port"]) + item["countryCode"] = item["country_code"] + item["configProfile"] = { + "activeConfigProfileUuid": profile["uuid"], + "activeInbounds": [inbound["uuid"]], + } + return item -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} недоступен)" + def diff_node_binding(self, existing: dict[str, Any], prepared: dict[str, Any]) -> bool: + current = existing.get("configProfile") or {} + want = prepared["configProfile"] + return ( + existing.get("address") != prepared["address"] + or int(existing.get("port") or 0) != int(prepared["port"]) + or (bool(existing.get("countryCode")) and existing.get("countryCode") != prepared["countryCode"]) + or current.get("activeConfigProfileUuid") != want["activeConfigProfileUuid"] + or _ids(current.get("activeInbounds")) != want["activeInbounds"] ) - _, 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})") - if existing_node.get("isDisabled") or needs_update: - ecode, _ = api.request("POST", f"/nodes/{node_uuid}/actions/enable", {}, allow_error=True) - if 200 <= ecode < 300 and existing_node.get("isDisabled"): - changes.append(f"enabled node {node_name}") - - 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, + def prepare_host(self, item: dict[str, Any], _existing: dict[str, Any] | None) -> dict[str, Any]: + profile = self._profile(item["profile"]) + node = self.refs["node"][item["name"]] + templates = self.desired.get("xray_templates") or [] + template_name = item.get("xray_template") or (templates[0]["name"] if templates else "") + template = self.refs.get("xray_template", {}).get(template_name) or {} + inbound = self.inbounds[profile["inbound_tag"]] + return { + "remark": item["host_remark"], + "address": profile["selfsteal_domain"], "port": 443, - "path": node_cfg["xhttp_path"], - "sni": selfsteal, - "host": selfsteal, + "path": profile["xhttp_path"], + "sni": profile["selfsteal_domain"], + "host": profile["selfsteal_domain"], + "profile_uuid": profile["uuid"], + "inbound_uuid": inbound["uuid"], + "nodes": [node["uuid"]], "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: - cur_inbounds = [i["uuid"] for i in (target.get("inbounds") or []) if i.get("uuid")] - desired_inbounds = list(dict.fromkeys(inbound_uuids)) - if set(cur_inbounds) != set(desired_inbounds): - api.request( - "PATCH", - "/internal-squads/", - {"uuid": target["uuid"], "inbounds": desired_inbounds}, - ) - changes.append(f"updated squad {squad_name} inbounds") - return squad_map - - -def cleanup_removed_nodes_and_hosts( - api: RemnawaveAPI, - desired_nodes: list[dict[str, Any]], - changes: list[str], -) -> None: - desired_domains = {n["selfsteal_domain"] for n in desired_nodes} - desired_node_names = {n["name"] for n in desired_nodes} - desired_profile_names = {n["profile_name"] for n in desired_nodes} - - _, hosts_resp = api.request("GET", "/hosts/") - for h in _extract_list(hosts_resp): - if h.get("address") not in desired_domains and h.get("uuid"): - api.request("DELETE", f"/hosts/{h['uuid']}") - changes.append(f"deleted host {h.get('address')}") - - _, nodes_resp = api.request("GET", "/nodes/") - for n in _extract_list(nodes_resp): - if n.get("name") not in desired_node_names and n.get("uuid"): - api.request("DELETE", f"/nodes/{n['uuid']}") - changes.append(f"deleted node {n.get('name')}") - - _, profiles_resp = api.request("GET", "/config-profiles/") - for p in _extract_list(profiles_resp, "configProfiles"): - if p.get("name") not in desired_profile_names and p.get("uuid"): - api.request("DELETE", f"/config-profiles/{p['uuid']}") - changes.append(f"deleted profile {p.get('name')}") - - -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]], - managed_tag: str, - squad_map: dict[str, str], - xray_checker_user: str, - sub_domain: str, - changes: list[str], -) -> str: - if not re.match(r"^[A-Z0-9_]{1,16}$", managed_tag): - raise RuntimeError(f"Некорректный managed_tag '{managed_tag}' (допустимо ^[A-Z0-9_]{{1,16}}$)") - - 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_tag = u.get("tag") or managed_tag - 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, - "tag": want_tag, + "xrayJsonTemplateUuid": template.get("uuid", ""), } - 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 []) - ] + def diff_host_binding(self, existing: dict[str, Any], prepared: dict[str, Any]) -> bool: + for field in ("remark", "path", "sni", "host", "xrayJsonTemplateUuid"): + if (existing.get(field) or "") != (prepared.get(field) or ""): + return True + for field in ("fingerprint", "alpn"): + if existing.get(field) and existing.get(field) != prepared.get(field): + return True + if int(existing.get("port") or 0) != int(prepared["port"]): + return True + return _ids(existing.get("nodes")) != prepared["nodes"] + + def prepare_squad(self, item: dict[str, Any], _existing: dict[str, Any] | None) -> dict[str, Any]: + uuids: list[str] = [] + for tag in item.get("inbounds") or []: + inbound = self.inbounds.get(tag) + if not inbound: + raise RuntimeError(f"Сквад {item['name']} ссылается на неизвестный inbound {tag}") + uuids.append(inbound["uuid"]) + item["inbounds"] = list(dict.fromkeys(uuids)) + return item + + def diff_squad_inbounds(self, existing: dict[str, Any], prepared: dict[str, Any]) -> bool: + return set(_ids(existing.get("inbounds"))) != set(prepared.get("inbounds") or []) + + def run_subscription_settings(self) -> None: + name = self.desired.get("subscription_page_config") or "Homelab" + _, data = self.api.request("GET", "/subscription-page-configs/") + configs = _extract_list(data, "configs") + if not any(cfg.get("name") == name for cfg in configs): + self.api.request("POST", "/subscription-page-configs/", {"name": name}) + self.changes.append(f"created subscription-page-config {name}") + code, settings = self.api.request("GET", "/subscription-settings/", allow_error=True) + if not (200 <= code < 300) or not isinstance(settings, dict): + return + current = _as_obj(settings) + browser = { + "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}, + ], + } + rules = (current.get("responseRules") or {}).get("rules") or [] + others = [rule for rule in rules if rule.get("name") not in ("Browser", "Browser Subscription") and rule.get("responseType") != "BROWSER"] + desired_rules = [browser] + others + if ( + current.get("serveJsonAtBaseSubscription") is True + and rules + and rules[0].get("name") == "Browser" + and rules[0].get("responseType") == "BROWSER" + and rules[0].get("enabled") is True + and len(rules) == len(desired_rules) + ): + return + patched = dict(current) + patched["serveJsonAtBaseSubscription"] = True + patched["responseRules"] = dict(current.get("responseRules") or {}) + patched["responseRules"]["rules"] = desired_rules + status, _ = self.api.request("PATCH", "/subscription-settings/", patched, allow_error=True) + if not (200 <= status < 300): + self.api.request("PUT", "/subscription-settings/", patched) + self.changes.append("updated subscription-settings Browser rule") + + def run_users(self) -> None: + doc = self.cfg.get("users_doc") or {} + managed_tag = doc.get("managed_tag") or "IAC" + if not re.match(r"^[A-Z0-9_]{1,16}$", managed_tag): + raise RuntimeError(f"Некорректный managed_tag '{managed_tag}'") + code, data = self.api.request("GET", "/users/?size=500", allow_error=True) + if not (200 <= code < 300): + _, data = self.api.request("GET", "/users/") + existing = {user["username"]: user for user in _extract_list(data, "users") if user.get("username")} + wanted: set[str] = set() + mutated = False + for user in doc.get("users") or []: + name = user["username"] + wanted.add(name) + squad = self.refs.get("squad", {}).get(user.get("squad") or "Default-Squad") or {} + body = { + "username": name, + "status": user.get("status", "ACTIVE"), + "expireAt": user["expire_at"], + "description": user.get("description", ""), + "trafficLimitBytes": int(user.get("traffic_limit_bytes", 0)), + "trafficLimitStrategy": user.get("traffic_limit_strategy", "NO_RESET"), + "activeInternalSquads": [squad["uuid"]] if squad.get("uuid") else [], + "tag": user.get("tag") or managed_tag, + } + current = existing.get(name) + if not current: + self.api.request("POST", "/users/", body) + self.changes.append(f"created user {name}") + mutated = True + continue + current_squads = _ids(current.get("activeInternalSquads")) 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 - or (cur.get("tag") or "") != want_tag + current.get("status") != body["status"] + or _norm_iso(current.get("expireAt")) != _norm_iso(body["expireAt"]) + or (current.get("description") or "") != body["description"] + or int(current.get("trafficLimitBytes") or 0) != body["trafficLimitBytes"] + or current.get("trafficLimitStrategy") != body["trafficLimitStrategy"] + or current_squads != body["activeInternalSquads"] + or (current.get("tag") or "") != body["tag"] ) if differs: - patch_body = {**want_body, "uuid": cur["uuid"]} if cur.get("uuid") else want_body - api.request("PATCH", "/users/", patch_body) - changes.append(f"updated user {uname}") + self.api.request("PATCH", "/users/", {**body, "uuid": current["uuid"]}) + self.changes.append(f"updated user {name}") mutated = True + for name, current in existing.items(): + if name not in wanted and (current.get("tag") or "") == managed_tag and current.get("uuid"): + self.api.request("DELETE", f"/users/{current['uuid']}") + self.changes.append(f"deleted user {name}") + mutated = True + checker = self.cfg.get("xray_checker_user") or "" + if not checker: + return + if mutated or checker not in existing: + _, data = self.api.request("GET", "/users/?size=500") + existing = {user["username"]: user for user in _extract_list(data, "users") if user.get("username")} + user = existing.get(checker) or {} + self.xray_url = user.get("subscriptionUrl") or "" + if not self.xray_url and user.get("shortUuid") and self.cfg.get("sub_domain"): + self.xray_url = f"https://{self.cfg['sub_domain']}/{user['shortUuid']}" - for uname, cur in by_name.items(): - if uname not in desired_names and (cur.get("tag") or "") == managed_tag: - uid = cur.get("uuid") or cur.get("id") - api.request("DELETE", f"/users/{uid}") - 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 _write_xray_url(self) -> None: + path = os.environ.get("RW_XRAY_URL_FILE", "").strip() + if not path: + return + with open(path, "w", encoding="utf-8") as handle: + handle.write(self.xray_url) + os.chmod(path, 0o600) def main() -> int: + if len(sys.argv) == 2 and sys.argv[1] == "--selftest": + _selftest() + return 0 if len(sys.argv) != 2: print("Usage: sync_remnawave.py ", file=sys.stderr) return 2 - - with open(sys.argv[1], "r", encoding="utf-8") as fh: - cfg = json.load(fh) - + with open(sys.argv[1], encoding="utf-8") as handle: + cfg = json.load(handle) 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) - cleanup_removed_nodes_and_hosts(api, cfg.get("nodes", []), changes) - sync_subscription_settings(api, cfg.get("subscription_page_config", "Homelab"), changes) - xray_url = sync_users( - api, - cfg.get("users", []), - cfg.get("managed_tag", "IAC"), - 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, - } + result = Sync(cfg, RemnawaveAPI(cfg["panel_domain"], token)).run() print(json.dumps(result, ensure_ascii=False)) return 0 +def _selftest() -> None: + import pathlib + import tempfile + + root = pathlib.Path(__file__).resolve().parent + commands = json.loads(json.dumps(_load_yaml_commands(root / "commands.yml"))) + state: dict[str, list[dict[str, Any]]] = { + "templates": [], + "profiles": [], + "nodes": [], + "hosts": [], + "squads": [], + "users": [], + "subconfigs": [], + "settings": {"response": {"serveJsonAtBaseSubscription": False, "responseRules": {"rules": []}}}, + } + calls: list[str] = [] + + class Fake(RemnawaveAPI): + def __init__(self) -> None: + super().__init__("panel.example", "token") + + def request(self, method: str, path: str, payload: Any = None, allow_error: bool = False) -> tuple[int, Any]: + calls.append(f"{method} {path}") + if path == "/system/tools/x25519/generate": + return 200, {"response": {"keypairs": [{"privateKey": "priv-test"}]}} + if path.startswith("/subscription-templates"): + return _rest(state["templates"], method, path, payload, "templateType") + if path.startswith("/config-profiles") and path.endswith("/inbounds"): + uuid = path.split("/")[2] + found = next(obj for obj in state["profiles"] if obj["uuid"] == uuid) + return 200, {"response": {"inbounds": found.get("inbounds") or []}} + if path.startswith("/config-profiles"): + return _rest(state["profiles"], method, path, payload, "name") + if path.startswith("/nodes/") and path.endswith("/actions/enable"): + return 200, {"response": {"ok": True}} + if path.startswith("/nodes"): + return _rest(state["nodes"], method, path, payload, "name") + if path.startswith("/hosts"): + return _rest(state["hosts"], method, path, payload, "address") + if path.startswith("/internal-squads"): + return _rest(state["squads"], method, path, payload, "name") + if path.startswith("/subscription-page-configs"): + return _rest(state["subconfigs"], method, path, payload, "name", key="configs") + if path.startswith("/subscription-settings"): + if method == "GET": + return 200, state["settings"] + state["settings"] = {"response": payload} + return 200, state["settings"] + if path.startswith("/users"): + return _rest(state["users"], method, path, payload, "username", key="users") + raise AssertionError(path) + + def finish_profile(obj: dict[str, Any]) -> dict[str, Any]: + if obj.get("config"): + obj["inbounds"] = [{"uuid": "inb-1", "tag": obj["config"]["inbounds"][0]["tag"]}] + return obj + + cfg = { + "panel_domain": "panel.example", + "sub_domain": "sub.example", + "netbird_connected": True, + "netbird_peers_url": "", + "xray_checker_user": "homelab", + "desired": { + "xray_templates": [{"name": "Except-RU", "templateType": "XRAY_JSON", "file": "xray-except-ru.json"}], + "profiles": [{ + "name": "DE-Yali", + "template": "profile-selfsteal-xhttp.json", + "inbound_tag": "Steal-Yali", + "selfsteal_domain": "yali.gunis.top", + "xhttp_path": "/xhttp-yali", + }], + "squads": [{"name": "Default-Squad", "inbounds": ["Steal-Yali"]}], + "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": "DE-Yali", + "host_remark": "de", + }], + }, + "users_doc": {"managed_tag": "IAC", "users": [{ + "username": "homelab", + "status": "ACTIVE", + "expire_at": "2099-01-01T00:00:00.000Z", + "description": "homelab", + "traffic_limit_bytes": 0, + "traffic_limit_strategy": "NO_RESET", + "squad": "Default-Squad", + }]}, + "commands": commands, + "files": { + "xray-except-ru.json": (root / "xray-except-ru.json").read_text(encoding="utf-8"), + "profile-selfsteal-xhttp.json": (root / "profile-selfsteal-xhttp.json").read_text(encoding="utf-8"), + }, + } + original_rest = _rest + + def wrapped_rest(bucket, method, path, payload, field, key=""): + status, data = original_rest(bucket, method, path, payload, field, key) + if method == "POST" and bucket is state["profiles"]: + finish_profile(_as_obj(data)) + if method == "POST" and bucket is state["users"]: + _as_obj(data)["subscriptionUrl"] = "https://sub.example/short" + return status, data + + globals()["_rest"] = wrapped_rest + os.environ["NB_API_TOKEN"] = "" + with tempfile.TemporaryDirectory() as tmp: + os.environ["RW_XRAY_URL_FILE"] = str(pathlib.Path(tmp) / "url") + first = Sync(cfg, Fake()).run() + assert any(item.startswith("created ") for item in first["changes"]), first + assert "priv-test" in json.dumps(state["profiles"]) + keygens = [call for call in calls if "x25519" in call] + assert len(keygens) == 1, keygens + second = Sync(cfg, Fake()).run() + assert second["changes"] == [], second + assert len([call for call in calls if "x25519" in call]) == 1 + cfg["desired"]["squads"] = [] + third = Sync(cfg, Fake()).run() + assert any(item.startswith("deleted squads") for item in third["changes"]), third + assert pathlib.Path(os.environ["RW_XRAY_URL_FILE"]).read_text(encoding="utf-8").startswith("https://") + print("selftest ok") + + +def _load_yaml_commands(path: Any) -> dict[str, Any]: + import yaml + + return yaml.safe_load(path.read_text(encoding="utf-8")) + + +def _rest(bucket: list[dict[str, Any]], method: str, path: str, payload: Any, field: str, key: str = "") -> tuple[int, Any]: + if method == "GET" and path.rstrip("/").count("/") == 1 or (method == "GET" and "?" in path): + body = bucket + return 200, {"response": {key: body}} if key else {"response": body} + if method == "GET" and path.count("/") >= 2 and not path.endswith("/inbounds"): + uuid = path.rstrip("/").split("/")[-1] + found = next(obj for obj in bucket if obj.get("uuid") == uuid) + return 200, {"response": found} + if method == "POST": + obj = dict(payload or {}) + obj.setdefault("uuid", f"id-{len(bucket)+1}-{field}") + config = obj.get("config") if isinstance(obj.get("config"), dict) else {} + tags = [inbound.get("tag") for inbound in (config.get("inbounds") or []) if inbound.get("tag")] + if tags: + obj["inbounds"] = [{"uuid": f"in-{obj['uuid']}-{tag}", "tag": tag} for tag in tags] + bucket.append(obj) + return 201, {"response": obj} + if method in ("PATCH", "PUT"): + uuid = (payload or {}).get("uuid") + found = next(obj for obj in bucket if obj.get("uuid") == uuid) + found.update(payload or {}) + return 200, {"response": found} + if method == "DELETE": + uuid = path.rstrip("/").split("/")[-1] + bucket[:] = [obj for obj in bucket if obj.get("uuid") != uuid] + return 200, {"response": {"ok": True}} + raise AssertionError((method, path)) + + if __name__ == "__main__": raise SystemExit(main()) - diff --git a/ansible/roles/core-net/remnawave/tasks/bootstrap.yml b/ansible/roles/core-net/remnawave/tasks/bootstrap.yml index f927ad3..25ed21e 100644 --- a/ansible/roles/core-net/remnawave/tasks/bootstrap.yml +++ b/ansible/roles/core-net/remnawave/tasks/bootstrap.yml @@ -136,9 +136,11 @@ owner: root group: root mode: "0644" + register: remnawave_compose_file - name: Стянуть образы панели Remnawave ansible.builtin.command: docker compose -f {{ remnawave_dir }}/compose.yaml pull + when: remnawave_compose_file is changed or (remnawave_pull_images | bool) register: remnawave_compose_pull changed_when: "'Pulled' in remnawave_compose_pull.stderr or 'Downloaded' in remnawave_compose_pull.stderr" retries: 3 diff --git a/ansible/roles/core-net/remnawave/tasks/main.yml b/ansible/roles/core-net/remnawave/tasks/main.yml index b534c2e..b17f831 100644 --- a/ansible/roles/core-net/remnawave/tasks/main.yml +++ b/ansible/roles/core-net/remnawave/tasks/main.yml @@ -1,78 +1,12 @@ --- -- name: Развернуть и инициализировать панель Remnawave - ansible.builtin.include_tasks: bootstrap.yml +- name: Развернуть панель Remnawave + ansible.builtin.import_tasks: bootstrap.yml + tags: [remnawave, remnawave_bootstrap] -- name: Синхронизировать ноды, профили, шаблоны и пользователей Remnawave - ansible.builtin.include_tasks: sync.yml +- name: Синхронизировать состояние панели Remnawave + ansible.builtin.import_tasks: sync.yml + tags: [remnawave, remnawave_sync] -- name: Определить изменившиеся секреты для Infisical - ansible.builtin.set_fact: - remnawave_current_xray_url: "{{ lookup('env', 'XRAY_CHECKER_SUBSCRIPTION_URL', default='') }}" - remnawave_need_infisical_sync: >- - {{ - (remnawave_bootstrap.token_updated | default(false)) or - (remnawave_bootstrap.node_secret_created | default(false)) or - ((remnawave_xray_checker_url | default('') | length > 0) and (remnawave_xray_checker_url != lookup('env', 'XRAY_CHECKER_SUBSCRIPTION_URL', default=''))) - }} - no_log: true - -- name: Сохранить API-токен, SECRET_KEY ноды и ссылку Xray Checker в Infisical при изменении - ansible.builtin.shell: | - set -euo pipefail - args=( - "REMNAWAVE_API_TOKEN=${RW_API_TOKEN}" - "REMNANODE_SECRET_KEY=${RW_NODE_SECRET}" - ) - if [ -n "${RW_XRAY_URL:-}" ]; then - args+=("XRAY_CHECKER_SUBSCRIPTION_URL=${RW_XRAY_URL}") - fi - infisical secrets set \ - --domain="${INFISICAL_DOMAIN}" \ - --token="${INFISICAL_TOKEN}" \ - --env=prod \ - --projectId="${INFISICAL_PROJECT_ID}" \ - --path="/services/remnawave" \ - "${args[@]}" \ - --silent >/dev/null - args: - executable: /bin/bash - delegate_to: localhost - become: false - environment: - INFISICAL_DOMAIN: "{{ lookup('env', 'INFISICAL_DOMAIN') }}" - INFISICAL_TOKEN: "{{ lookup('env', 'INFISICAL_TOKEN') }}" - INFISICAL_PROJECT_ID: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}" - RW_API_TOKEN: "{{ remnawave_api_token }}" - RW_NODE_SECRET: "{{ remnawave_node_secret | default(lookup('env', 'REMNANODE_SECRET_KEY', default='')) }}" - RW_XRAY_URL: "{{ remnawave_xray_checker_url | default('') }}" - when: - - remnawave_api_token | default('') | length > 0 - - remnawave_need_infisical_sync | bool - changed_when: true - no_log: true - -- name: Зарегистрировать панель Remnawave в Agent Vault - ansible.builtin.include_role: - name: agent_vault_service - vars: - agent_vault_service_name: remnawave-panel - agent_vault_service_description: >- - Панель Remnawave (rw.gunihome.ru): ноды, пользователи и подписки VPN. - Креденшл — сервисный API-токен панели. - agent_vault_service_host: "{{ remnawave_panel_domain }}:443" - agent_vault_service_paths: ["/api"] - agent_vault_service_methods: [GET, POST] - agent_vault_secret_path: /services/remnawave - agent_vault_secret_key: REMNAWAVE_API_TOKEN - when: remnawave_api_token | default('') | length > 0 - -- name: Итог Remnawave panel - ansible.builtin.debug: - msg: - - "Панель: https://{{ remnawave_panel_domain }}" - - "Подписка: https://{{ remnawave_sub_domain }}" - - "Админ создан в этом прогоне: {{ remnawave_bootstrap.admin_created | default(false) }}" - - "API-токен обновлён в Infisical: {{ remnawave_bootstrap.token_updated | default(false) }}" - - "SECRET_KEY ноды получен в этом прогоне: {{ remnawave_bootstrap.node_secret_created | default(false) }}" - - "Изменения sync (nodes.yml / users.yml): {{ remnawave_sync.changes | default([]) }}" - - "Предупреждения sync: {{ remnawave_sync.warnings | default([]) }}" +- name: Опубликовать секреты и итог Remnawave + ansible.builtin.import_tasks: publish.yml + tags: [remnawave, remnawave_sync, remnawave_bootstrap] diff --git a/ansible/roles/core-net/remnawave/tasks/publish.yml b/ansible/roles/core-net/remnawave/tasks/publish.yml new file mode 100644 index 0000000..2ebb3db --- /dev/null +++ b/ansible/roles/core-net/remnawave/tasks/publish.yml @@ -0,0 +1,72 @@ +--- +- name: Определить изменившиеся секреты для Infisical + ansible.builtin.set_fact: + remnawave_current_xray_url: "{{ lookup('env', 'XRAY_CHECKER_SUBSCRIPTION_URL', default='') }}" + remnawave_need_infisical_sync: >- + {{ + (remnawave_bootstrap.token_updated | default(false)) or + (remnawave_bootstrap.node_secret_created | default(false)) or + ((remnawave_xray_checker_url | default('') | length > 0) and (remnawave_xray_checker_url != lookup('env', 'XRAY_CHECKER_SUBSCRIPTION_URL', default=''))) + }} + no_log: true + +- name: Сохранить API-токен, SECRET_KEY ноды и ссылку Xray Checker в Infisical при изменении + ansible.builtin.shell: | + set -euo pipefail + args=( + "REMNAWAVE_API_TOKEN=${RW_API_TOKEN}" + "REMNANODE_SECRET_KEY=${RW_NODE_SECRET}" + ) + if [ -n "${RW_XRAY_URL:-}" ]; then + args+=("XRAY_CHECKER_SUBSCRIPTION_URL=${RW_XRAY_URL}") + fi + infisical secrets set \ + --domain="${INFISICAL_DOMAIN}" \ + --token="${INFISICAL_TOKEN}" \ + --env=prod \ + --projectId="${INFISICAL_PROJECT_ID}" \ + --path="/services/remnawave" \ + "${args[@]}" \ + --silent >/dev/null + args: + executable: /bin/bash + delegate_to: localhost + become: false + environment: + INFISICAL_DOMAIN: "{{ lookup('env', 'INFISICAL_DOMAIN') }}" + INFISICAL_TOKEN: "{{ lookup('env', 'INFISICAL_TOKEN') }}" + INFISICAL_PROJECT_ID: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}" + RW_API_TOKEN: "{{ remnawave_api_token }}" + RW_NODE_SECRET: "{{ remnawave_node_secret | default(lookup('env', 'REMNANODE_SECRET_KEY', default='')) }}" + RW_XRAY_URL: "{{ remnawave_xray_checker_url | default('') }}" + when: + - remnawave_api_token | default('') | length > 0 + - remnawave_need_infisical_sync | bool + changed_when: true + no_log: true + +- name: Зарегистрировать панель Remnawave в Agent Vault + ansible.builtin.include_role: + name: agent_vault_service + vars: + agent_vault_service_name: remnawave-panel + agent_vault_service_description: >- + Панель Remnawave (rw.gunihome.ru): ноды, пользователи и подписки VPN. + Креденшл — сервисный API-токен панели. + agent_vault_service_host: "{{ remnawave_panel_domain }}:443" + agent_vault_service_paths: ["/api"] + agent_vault_service_methods: [GET, POST] + agent_vault_secret_path: /services/remnawave + agent_vault_secret_key: REMNAWAVE_API_TOKEN + when: remnawave_api_token | default('') | length > 0 + +- name: Итог Remnawave panel + ansible.builtin.debug: + msg: + - "Панель: https://{{ remnawave_panel_domain }}" + - "Подписка: https://{{ remnawave_sub_domain }}" + - "Админ создан в этом прогоне: {{ remnawave_bootstrap.admin_created | default(false) }}" + - "API-токен обновлён в Infisical: {{ remnawave_bootstrap.token_updated | default(false) }}" + - "SECRET_KEY ноды получен в этом прогоне: {{ remnawave_bootstrap.node_secret_created | default(false) }}" + - "Изменения sync (desired.yml / users.yml): {{ remnawave_sync.changes | default([]) }}" + - "Предупреждения sync: {{ remnawave_sync.warnings | default([]) }}" diff --git a/ansible/roles/core-net/remnawave/tasks/sync.yml b/ansible/roles/core-net/remnawave/tasks/sync.yml index 41f9b41..d82323a 100644 --- a/ansible/roles/core-net/remnawave/tasks/sync.yml +++ b/ansible/roles/core-net/remnawave/tasks/sync.yml @@ -1,8 +1,9 @@ --- -- name: Загрузить декларативную конфигурацию нод и пользователей Remnawave +- name: Загрузить желаемое состояние и шаблоны команд Remnawave ansible.builtin.set_fact: - remnawave_nodes_cfg: "{{ lookup('file', role_path ~ '/files/nodes.yml') | from_yaml }}" + remnawave_desired: "{{ lookup('file', role_path ~ '/files/desired.yml') | from_yaml }}" remnawave_users_cfg: "{{ lookup('file', role_path ~ '/files/users.yml') | from_yaml }}" + remnawave_commands: "{{ lookup('file', role_path ~ '/files/commands.yml') | from_yaml }}" - name: Проверить, что панель сама в NetBird ansible.builtin.command: netbird status @@ -10,6 +11,11 @@ changed_when: false failed_when: false +- name: Собрать тексты JSON-шаблонов + ansible.builtin.set_fact: + remnawave_spec_files: "{{ remnawave_spec_files | default({}) | combine({item: lookup('file', role_path ~ '/files/' ~ item)}) }}" + loop: "{{ ((remnawave_desired.xray_templates | default([]) | map(attribute='file') | list) + (remnawave_desired.profiles | default([]) | map(attribute='template') | list)) | unique }}" + - name: Собрать конфигурацию синхронизации Remnawave ansible.builtin.set_fact: remnawave_sync_payload: @@ -18,15 +24,10 @@ netbird_peers_url: "{{ remnawave_netbird_peers_url }}" netbird_connected: "{{ 'Management: Connected' in (remnawave_local_netbird.stdout | default('')) }}" xray_checker_user: "{{ remnawave_xray_checker_user }}" - xray_template: "{{ remnawave_nodes_cfg.xray_template }}" - xray_template_json: "{{ lookup('file', role_path ~ '/files/' ~ remnawave_nodes_cfg.xray_template.file) | from_json }}" - squad_name: "{{ remnawave_nodes_cfg.squad_name | default('Default-Squad') }}" - subscription_page_config: "{{ remnawave_nodes_cfg.subscription_page_config | default('Homelab') }}" - nodes: "{{ remnawave_nodes_cfg.nodes | default([]) }}" - managed_tag: "{{ remnawave_users_cfg.managed_tag | default('IAC') }}" - users: "{{ remnawave_users_cfg.users | default([]) }}" - profile_templates: - profile-selfsteal-xhttp.json: "{{ lookup('file', role_path ~ '/files/profile-selfsteal-xhttp.json') }}" + desired: "{{ remnawave_desired }}" + users_doc: "{{ remnawave_users_cfg }}" + commands: "{{ remnawave_commands }}" + files: "{{ remnawave_spec_files }}" - name: Положить скрипт синхронизации Remnawave ansible.builtin.copy: @@ -44,17 +45,24 @@ group: root mode: "0600" -- name: Синхронизировать шаблоны, профили, ноды, хосты и пользователей Remnawave +- name: Синхронизировать шаблоны, профили, ноды, хосты, сквады и пользователей ansible.builtin.command: "python3 {{ remnawave_dir }}/sync_remnawave.py {{ remnawave_dir }}/sync_config.json" environment: RW_API_TOKEN: "{{ remnawave_api_token }}" NB_API_TOKEN: "{{ lookup('env', 'NETBIRD_API_TOKEN', default='') }}" + RW_XRAY_URL_FILE: "{{ remnawave_dir }}/.xray-checker-url" register: remnawave_sync_raw changed_when: remnawave_sync_raw.rc == 0 and ((remnawave_sync_raw.stdout | from_json).changed | default(false)) +- name: Прочитать ссылку Xray Checker + ansible.builtin.slurp: + src: "{{ remnawave_dir }}/.xray-checker-url" + register: remnawave_xray_url_file + failed_when: false + no_log: true + - name: Разобрать результат синхронизации Remnawave ansible.builtin.set_fact: remnawave_sync: "{{ remnawave_sync_raw.stdout | from_json }}" - remnawave_xray_checker_url: "{{ (remnawave_sync_raw.stdout | from_json).xray_checker_subscription_url | default('') }}" + remnawave_xray_checker_url: "{{ remnawave_xray_url_file.content | default('') | b64decode }}" no_log: true - diff --git a/ansible/roles/remnanode/tasks/main.yml b/ansible/roles/remnanode/tasks/main.yml index f207b18..215623b 100644 --- a/ansible/roles/remnanode/tasks/main.yml +++ b/ansible/roles/remnanode/tasks/main.yml @@ -1,4 +1,34 @@ --- +- name: Взять описание ноды из desired.yml + ansible.builtin.set_fact: + remnanode_stack: "{{ lookup('file', playbook_dir ~ '/../roles/core-net/remnawave/files/desired.yml') | from_yaml }}" + +- name: Найти эту ноду в desired.yml + ansible.builtin.set_fact: + remnanode_node_matches: "{{ remnanode_stack.nodes | selectattr('netbird_hostname', 'equalto', inventory_hostname) | list }}" + +- name: Нода должна быть описана в desired.yml + ansible.builtin.assert: + that: + - remnanode_node_matches | length == 1 + fail_msg: "В desired.yml нет ровно одной ноды с netbird_hostname={{ inventory_hostname }}" + +- name: Подставить профиль ноды + ansible.builtin.set_fact: + remnanode_node: "{{ remnanode_node_matches[0] }}" + remnanode_profile_matches: "{{ remnanode_stack.profiles | selectattr('name', 'equalto', remnanode_node_matches[0].profile) | list }}" + +- name: Профиль ноды должен быть в desired.yml + ansible.builtin.assert: + that: + - remnanode_profile_matches | length == 1 + fail_msg: "В desired.yml нет профиля {{ remnanode_node.profile }}" + +- name: Домен и путь SelfSteal из профиля + ansible.builtin.set_fact: + remnanode_selfsteal_domain: "{{ remnanode_profile_matches[0].selfsteal_domain }}" + remnanode_xhttp_path: "{{ remnanode_profile_matches[0].xhttp_path }}" + - name: Проверить SECRET_KEY ноды и Cloudflare API ansible.builtin.assert: that: @@ -16,6 +46,7 @@ - certbot state: present update_cache: true + cache_valid_time: 86400 - name: Создать каталоги ноды ansible.builtin.file: diff --git a/ansible/roles/rus-central/netbird/files/access.yml b/ansible/roles/rus-central/netbird/files/access.yml index 9e90ae5..8b1e3bc 100644 --- a/ansible/roles/rus-central/netbird/files/access.yml +++ b/ansible/roles/rus-central/netbird/files/access.yml @@ -4,25 +4,10 @@ # Секреты setup-ключей сохраняются в Infisical (/services/netbird) автоматически # и не попадают в Git или логи CI. -# Управляемые группы пиров +# Группы exit- для нод из desired.yml добавляет sync_access.py. client_groups: - exit-clients - remnawave - - exit-ger-nod-02 - -# Выходные шлюзы (Exit Nodes) -exit_nodes: - - network_id: exit-ger-nod-02 - description: "Exit Node DE (0.0.0.0/0 via ger-nod-02)" - peer: ger-nod-02 - peer_group: exit-ger-nod-02 - client_groups: - - exit-clients - metric: 9999 - masquerade: true - keep_route: false - skip_auto_apply: false - enabled: true # DNS для клиентских групп dns_nameservers: @@ -58,10 +43,6 @@ peers: groups: - remnawave state: present - - name: "ger-nod-02" - groups: - - exit-ger-nod-02 - state: present # Zero-Trust политики доступа (заменяют дефолтную политику Default All <-> All). # Инициировать соединения в сторону внешних нод могут только панель (core-net) и клиенты (exit-clients). @@ -83,8 +64,7 @@ policies: - "9101" sources: - remnawave - destinations: - - exit-ger-nod-02 + destinations: "@remnawave_exit_groups" - name: "exit-clients-to-exit-nodes" description: "One-way traffic from Exit Node clients to Exit Nodes" @@ -98,8 +78,7 @@ policies: bidirectional: false sources: - exit-clients - destinations: - - exit-ger-nod-02 + destinations: "@remnawave_exit_groups" - name: "exit-clients-udp" description: "Allow outbound UDP from exit-clients via exit node" enabled: true @@ -108,8 +87,7 @@ policies: bidirectional: false sources: - exit-clients - destinations: - - exit-ger-nod-02 + destinations: "@remnawave_exit_groups" - name: "exit-clients-icmp" description: "Allow ICMP for exit-clients via exit node" enabled: true @@ -118,5 +96,4 @@ policies: bidirectional: true sources: - exit-clients - destinations: - - exit-ger-nod-02 + destinations: "@remnawave_exit_groups" diff --git a/ansible/roles/rus-central/netbird/files/sync_access.py b/ansible/roles/rus-central/netbird/files/sync_access.py index 57560da..3d9b07e 100644 --- a/ansible/roles/rus-central/netbird/files/sync_access.py +++ b/ansible/roles/rus-central/netbird/files/sync_access.py @@ -73,10 +73,59 @@ def group_peer_ids(grp: dict[str, Any]) -> list[str]: return [p["id"] for p in (grp.get("peers") or []) if p.get("id")] +def expand_remnawave(cfg: dict[str, Any]) -> None: + """Группы, exit-маршруты и пиры нод берутся из desired.yml, не копируются руками.""" + if "remnawave_nodes" not in cfg: + return + nodes = cfg.pop("remnawave_nodes") or [] + groups: list[str] = [] + for node in nodes: + host = str(node.get("netbird_hostname") or "").strip() + if not host: + continue + group = f"exit-{host}" + groups.append(group) + client_groups = cfg.setdefault("client_groups", []) + if group not in client_groups: + client_groups.append(group) + exits = cfg.setdefault("exit_nodes", []) + if not any(item.get("network_id") == group for item in exits): + code = str(node.get("country_code") or "").strip() + exits.append( + { + "network_id": group, + "description": f"Exit Node {code} via {host}".strip(), + "peer": host, + "peer_group": group, + "client_groups": ["exit-clients"], + "metric": 9999, + "masquerade": True, + "keep_route": False, + "skip_auto_apply": False, + "enabled": True, + } + ) + peers = cfg.setdefault("peers", []) + if not any(item.get("name") == host for item in peers): + peers.append({"name": host, "groups": [group], "state": "present"}) + cfg["_remnawave_exit_groups"] = groups + for policy in cfg.get("policies") or []: + for rule in policy.get("rules") or []: + dest = rule.get("destinations") + if dest == "@remnawave_exit_groups": + rule["destinations"] = list(groups) + elif isinstance(dest, list) and "@remnawave_exit_groups" in dest: + expanded: list[str] = [] + for item in dest: + expanded.extend(groups if item == "@remnawave_exit_groups" else [item]) + rule["destinations"] = expanded + + def sync(cfg: dict[str, Any], api: NetBirdAPI) -> dict[str, Any]: changes: list[str] = [] warnings: list[str] = [] updated_secrets: dict[str, str] = {} + expand_remnawave(cfg) peers_raw = api.get("/peers") or [] groups_raw = api.get("/groups") or [] @@ -217,6 +266,22 @@ def sync(cfg: dict[str, Any], api: NetBirdAPI) -> dict[str, Any]: api.put(f"/routes/{existing['id']}", desired) changes.append(f"route.update:{net_id}") + keep_exit_groups = cfg.get("_remnawave_exit_groups") + if keep_exit_groups is not None: + keep = set(keep_exit_groups) + for route in routes_raw: + net_id = str(route.get("network_id") or "") + if route.get("network") == "0.0.0.0/0" and net_id.startswith("exit-") and net_id not in keep and route.get("id"): + api.delete(f"/routes/{route['id']}") + changes.append(f"route.delete:{net_id}") + for gname, grp in list(groups_by_name.items()): + if not gname.startswith("exit-") or gname == "exit-clients" or gname in keep: + continue + current_ids = group_peer_ids(grp) + if current_ids: + set_group_peers(grp, []) + changes.append(f"group.clear:{gname}") + # 4. DNS Nameservers ns_raw = api.get("/dns/nameservers") or [] for item in cfg.get("dns_nameservers") or []: diff --git a/ansible/roles/rus-central/netbird/tasks/access.yml b/ansible/roles/rus-central/netbird/tasks/access.yml index c91a7a2..9ec4121 100644 --- a/ansible/roles/rus-central/netbird/tasks/access.yml +++ b/ansible/roles/rus-central/netbird/tasks/access.yml @@ -2,6 +2,11 @@ - name: Загрузить декларативную конфигурацию доступа NetBird ansible.builtin.set_fact: netbird_access_cfg: "{{ lookup('file', role_path ~ '/files/access.yml') | from_yaml }}" + netbird_remnawave_nodes: "{{ (lookup('file', playbook_dir ~ '/../roles/core-net/remnawave/files/desired.yml') | from_yaml).nodes | default([]) }}" + +- name: Передать ноды Remnawave в синхронизацию доступа + ansible.builtin.set_fact: + netbird_access_cfg: "{{ netbird_access_cfg | combine({'remnawave_nodes': netbird_remnawave_nodes}) }}" - name: Положить скрипт синхронизации доступа NetBird ansible.builtin.copy: