fix(remnawave): update existing config profile when expected inbound tag is missing
This commit is contained in:
1 parent
f218ca85e1
commit
9be350ed97
1 file changed
+34
-16
@@ -177,20 +177,11 @@ def sync_xray_template(
|
|||||||
return tpl_uuid
|
return tpl_uuid
|
||||||
|
|
||||||
|
|
||||||
def ensure_profile_and_inbound(
|
def _render_profile_config(
|
||||||
api: RemnawaveAPI,
|
api: RemnawaveAPI,
|
||||||
node_cfg: dict[str, Any],
|
node_cfg: dict[str, Any],
|
||||||
profile_templates: dict[str, str],
|
profile_templates: dict[str, str],
|
||||||
changes: list[str],
|
) -> dict[str, Any]:
|
||||||
) -> 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"]
|
tpl_key = node_cfg["profile_template"]
|
||||||
raw_tpl = profile_templates.get(tpl_key)
|
raw_tpl = profile_templates.get(tpl_key)
|
||||||
if not raw_tpl:
|
if not raw_tpl:
|
||||||
@@ -212,26 +203,53 @@ def ensure_profile_and_inbound(
|
|||||||
rendered = (
|
rendered = (
|
||||||
raw_tpl.replace("__REALITY_PRIVATE_KEY__", priv)
|
raw_tpl.replace("__REALITY_PRIVATE_KEY__", priv)
|
||||||
.replace("__REALITY_SHORT_ID__", short_id)
|
.replace("__REALITY_SHORT_ID__", short_id)
|
||||||
.replace("__INBOUND_TAG__", inbound_tag)
|
.replace("__INBOUND_TAG__", node_cfg["inbound_tag"])
|
||||||
.replace("__SELFSTEAL_DOMAIN__", node_cfg["selfsteal_domain"])
|
.replace("__SELFSTEAL_DOMAIN__", node_cfg["selfsteal_domain"])
|
||||||
.replace("__XHTTP_PATH__", node_cfg["xhttp_path"])
|
.replace("__XHTTP_PATH__", node_cfg["xhttp_path"])
|
||||||
)
|
)
|
||||||
|
return json.loads(rendered)
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
cfg_obj = _render_profile_config(api, node_cfg, profile_templates)
|
||||||
_, created = api.request(
|
_, created = api.request(
|
||||||
"POST",
|
"POST",
|
||||||
"/config-profiles/",
|
"/config-profiles/",
|
||||||
{"name": profile_name, "config": json.loads(rendered)},
|
{"name": profile_name, "config": cfg_obj},
|
||||||
)
|
)
|
||||||
profile_uuid = (created.get("response") or created).get("uuid", "")
|
profile_uuid = (created.get("response") or created).get("uuid", "")
|
||||||
if not profile_uuid:
|
if not profile_uuid:
|
||||||
raise RuntimeError(f"Не удалось создать config profile {profile_name}")
|
raise RuntimeError(f"Не удалось создать config profile {profile_name}")
|
||||||
changes.append(f"created profile {profile_name}")
|
changes.append(f"created profile {profile_name}")
|
||||||
|
|
||||||
|
def _get_inbounds() -> list[dict[str, Any]]:
|
||||||
_, profile_full = api.request("GET", f"/config-profiles/{profile_uuid}")
|
_, profile_full = api.request("GET", f"/config-profiles/{profile_uuid}")
|
||||||
inbounds = _extract_list(profile_full, "inbounds")
|
inbs = _extract_list(profile_full, "inbounds")
|
||||||
if not inbounds:
|
if not inbs:
|
||||||
_, inb_resp = api.request("GET", f"/config-profiles/{profile_uuid}/inbounds")
|
_, inb_resp = api.request("GET", f"/config-profiles/{profile_uuid}/inbounds")
|
||||||
inbounds = _extract_list(inb_resp, "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), "")
|
inbound_uuid = next((i["uuid"] for i in inbounds if i.get("tag") == inbound_tag), "")
|
||||||
if not inbound_uuid:
|
if not inbound_uuid:
|
||||||
raise RuntimeError(f"Не найден inbound {inbound_tag} в профиле {profile_uuid}")
|
raise RuntimeError(f"Не найден inbound {inbound_tag} в профиле {profile_uuid}")
|
||||||
|
|||||||
Reference in new issue
Block a user