From 94216f181e0193579362d66744e8b28f7d701b51 Mon Sep 17 00:00:00 2001 From: karpenok Date: Wed, 18 Nov 2026 14:44:01 +0700 Subject: [PATCH] fix(remnawave): restore home squad sync and remove Vision flow mismatch --- .../remnawave/files/sync_remnawave.py | 221 +++++++++++++----- 1 file changed, 164 insertions(+), 57 deletions(-) diff --git a/ansible/roles/core-net/remnawave/files/sync_remnawave.py b/ansible/roles/core-net/remnawave/files/sync_remnawave.py index 8e60879..fb3c259 100644 --- a/ansible/roles/core-net/remnawave/files/sync_remnawave.py +++ b/ansible/roles/core-net/remnawave/files/sync_remnawave.py @@ -219,6 +219,41 @@ def ensure_cascade_credentials( if ru_prof: _, ru_full = api.request("GET", f"/config-profiles/{ru_prof['uuid']}") ru_cfg = (ru_full.get("response") or ru_full).get("config") or {} + ru_inbounds = ru_cfg.get("inbounds") or [] + ru_has_home = any(i.get("tag") == "Steal-Lulu-Home" for i in ru_inbounds) + if not ru_has_home: + steal_lulu = next((i for i in ru_inbounds if i.get("tag") == "Steal-Lulu"), {}) + rs_lulu = (steal_lulu.get("streamSettings") or {}).get("realitySettings") or {} + home_inbound = { + "tag": "Steal-Lulu-Home", + "listen": "31.177.108.210", + "port": 8443, + "protocol": "vless", + "settings": {"clients": [], "decryption": "none"}, + "sniffing": {"enabled": True, "destOverride": ["http", "tls", "quic"]}, + "streamSettings": { + "network": "xhttp", + "security": "reality", + "xhttpSettings": { + "host": "lulu.gunihome.ru", + "mode": "stream-one", + "path": "/xhttp-lulu-home", + }, + "realitySettings": { + "dest": "/dev/shm/nginx.sock", + "show": False, + "xver": 1, + "spiderX": "", + "shortIds": rs_lulu.get("shortIds") or [secrets.token_hex(8)], + "privateKey": rs_lulu.get("privateKey") or generate_x25519(api)[0], + "serverNames": ["lulu.gunihome.ru"], + }, + }, + } + ru_cfg["inbounds"] = ru_inbounds + [home_inbound] + api.request("PATCH", "/config-profiles/", {"uuid": ru_prof["uuid"], "config": ru_cfg}) + changes.append("added Steal-Lulu-Home inbound to RU-Cascade") + for ob in ru_cfg.get("outbounds") or []: if ob.get("tag") == "CASCADE-DE": vnext = (ob.get("settings") or {}).get("vnext") or [] @@ -360,7 +395,72 @@ def ensure_profile_and_inbound( for i in inbounds if i.get("tag") == tag ] - return profile_uuid, inbound_uuid, extra_uuids + return profile_uuid, inbound_uuid, extra_uuids, inbounds + + +def _upsert_host( + api: RemnawaveAPI, + address: str, + port: int, + path: str, + remark: str, + profile_uuid: str, + inbound_uuid: str, + node_uuid: str, + tpl_uuid: str, + changes: list[str], +) -> None: + _, hosts_resp = api.request("GET", "/hosts/") + hosts = _extract_list(hosts_resp) + existing_host = next( + (h for h in hosts if h.get("address") == address and int(h.get("port") or 0) == port), + None, + ) + + if not existing_host: + host_payload = { + "remark": remark, + "address": address, + "port": port, + "path": path, + "sni": address, + "host": address, + "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 {address}:{port}") + changes.append(f"created host {address}:{port}") + else: + host_uuid = existing_host["uuid"] + patch_fields: dict[str, Any] = {"uuid": host_uuid} + if existing_host.get("remark") != remark: + patch_fields["remark"] = 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 {address}:{port}") def sync_node_and_host( @@ -374,7 +474,7 @@ def sync_node_and_host( netbird_connected: bool, changes: list[str], warnings: list[str], -) -> None: +) -> str: node_name = node_cfg["name"] target_addr = node_cfg["address"] nb_host = node_cfg.get("netbird_hostname", "") @@ -437,57 +537,21 @@ def sync_node_and_host( changes.append(f"updated node {node_name} ({target_addr})") if not node_cfg.get("create_host", True): - return + return node_uuid - 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}") + _upsert_host( + api, + node_cfg["selfsteal_domain"], + 443, + node_cfg["xhttp_path"], + node_cfg["host_remark"], + profile_uuid, + inbound_uuid, + node_uuid, + tpl_uuid, + changes, + ) + return node_uuid def sync_squad( @@ -500,10 +564,20 @@ def sync_squad( squads = _extract_list(squads_resp, "internalSquads") squad_map = {s["name"]: s["uuid"] for s in squads if s.get("name") and s.get("uuid")} + desired_inbounds = list(dict.fromkeys(inbound_uuids)) target = next((s for s in squads if s.get("name") == squad_name), None) - if target: + if not target: + _, created = api.request( + "POST", + "/internal-squads/", + {"name": squad_name, "inbounds": desired_inbounds}, + ) + new_uuid = (created.get("response") or created).get("uuid", "") + if new_uuid: + squad_map[squad_name] = new_uuid + changes.append(f"created squad {squad_name}") + else: 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", @@ -714,17 +788,33 @@ def main() -> int: cfg["xray_template_json"], changes, ) + tpl_map = {cfg["xray_template"]["name"]: tpl_uuid} + for extra_tpl in cfg.get("extra_xray_templates", []): + tpl_map[extra_tpl["name"]] = sync_xray_template( + api, + extra_tpl["name"], + extra_tpl["json"], + changes, + ) cascade_creds = ensure_cascade_credentials(api, changes) inbound_uuids: list[str] = [] + home_squad_inbounds: dict[str, list[str]] = {} for node_cfg in cfg.get("nodes", []): - profile_uuid, inbound_uuid, extra_inbound_uuids = ensure_profile_and_inbound( + profile_uuid, inbound_uuid, extra_inbound_uuids, all_inbounds = ensure_profile_and_inbound( api, node_cfg, cfg.get("profile_templates", {}), cascade_creds, changes ) + home_cfg = node_cfg.get("home_host") + home_tag = home_cfg.get("inbound_tag") if home_cfg else None + home_inb_uuid = next((i["uuid"] for i in all_inbounds if i.get("tag") == home_tag), "") if home_tag else "" + inbound_uuids.append(inbound_uuid) - inbound_uuids.extend(extra_inbound_uuids) - sync_node_and_host( + for eu in extra_inbound_uuids: + if eu != home_inb_uuid: + inbound_uuids.append(eu) + + node_uuid = sync_node_and_host( api, node_cfg, profile_uuid, @@ -736,8 +826,25 @@ def main() -> int: changes, warnings, ) + if home_cfg and home_inb_uuid: + sq = home_cfg["squad_name"] + home_squad_inbounds.setdefault(sq, []).append(home_inb_uuid) + _upsert_host( + api, + node_cfg["selfsteal_domain"], + int(home_cfg["port"]), + home_cfg["xhttp_path"], + home_cfg["remark"], + profile_uuid, + home_inb_uuid, + node_uuid, + tpl_map.get(home_cfg["xray_template_name"], ""), + changes, + ) squad_map = sync_squad(api, cfg.get("squad_name", "Default-Squad"), inbound_uuids, changes) + for sq_name, sq_inbs in home_squad_inbounds.items(): + squad_map.update(sync_squad(api, sq_name, sq_inbs, 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(