diff --git a/ansible/roles/core-net/remnawave/files/sync_remnawave.py b/ansible/roles/core-net/remnawave/files/sync_remnawave.py index 41929d2..308d2d4 100644 --- a/ansible/roles/core-net/remnawave/files/sync_remnawave.py +++ b/ansible/roles/core-net/remnawave/files/sync_remnawave.py @@ -177,6 +177,39 @@ def sync_xray_template( return tpl_uuid +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}") + + _, 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__", node_cfg["inbound_tag"]) + .replace("__SELFSTEAL_DOMAIN__", node_cfg["selfsteal_domain"]) + .replace("__XHTTP_PATH__", node_cfg["xhttp_path"]) + ) + return json.loads(rendered) + + def ensure_profile_and_inbound( api: RemnawaveAPI, node_cfg: dict[str, Any], @@ -191,48 +224,33 @@ def ensure_profile_and_inbound( 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"]) - ) + cfg_obj = _render_profile_config(api, node_cfg, profile_templates) _, created = api.request( "POST", "/config-profiles/", - {"name": profile_name, "config": json.loads(rendered)}, + {"name": profile_name, "config": cfg_obj}, ) 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") + 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 + 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