fix(remnawave): restore cascade sync logic in sync_remnawave.py
This commit is contained in:
1 parent
8b76f35199
commit
0a963c4150
1 file changed
+97
-18
@@ -9,6 +9,7 @@ import re
|
|||||||
import secrets
|
import secrets
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import uuid
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
@@ -177,12 +178,87 @@ def sync_xray_template(
|
|||||||
return tpl_uuid
|
return tpl_uuid
|
||||||
|
|
||||||
|
|
||||||
|
def generate_x25519(api: RemnawaveAPI) -> tuple[str, str]:
|
||||||
|
_, key_resp = api.request("GET", "/system/tools/x25519/generate")
|
||||||
|
resp_obj = key_resp.get("response") or key_resp
|
||||||
|
keypairs = resp_obj.get("keypairs") or []
|
||||||
|
first = keypairs[0] if keypairs else resp_obj
|
||||||
|
priv = first.get("privateKey") or first.get("privKey") or ""
|
||||||
|
pub = first.get("publicKey") or first.get("pubKey") or ""
|
||||||
|
if not priv or not pub:
|
||||||
|
raise RuntimeError("Не удалось сгенерировать X25519 ключ Reality")
|
||||||
|
return priv, pub
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_cascade_credentials(
|
||||||
|
api: RemnawaveAPI,
|
||||||
|
changes: list[str],
|
||||||
|
) -> dict[str, str]:
|
||||||
|
"""Ensure DE-Yali has Cascade-RU-DE inbound and return credentials if RU-Cascade needs creation."""
|
||||||
|
_, profiles_resp = api.request("GET", "/config-profiles/")
|
||||||
|
profiles = _extract_list(profiles_resp, "configProfiles")
|
||||||
|
de_prof = next((p for p in profiles if p.get("name") == "DE-Yali"), None)
|
||||||
|
ru_prof = next((p for p in profiles if p.get("name") == "RU-Cascade"), None)
|
||||||
|
if not de_prof:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
de_uuid = de_prof["uuid"]
|
||||||
|
_, de_full = api.request("GET", f"/config-profiles/{de_uuid}")
|
||||||
|
de_obj = de_full.get("response") or de_full
|
||||||
|
de_cfg = de_obj.get("config") or {}
|
||||||
|
inbounds = de_cfg.get("inbounds") or []
|
||||||
|
has_cascade_inb = any(i.get("tag") == "Cascade-RU-DE" for i in inbounds)
|
||||||
|
|
||||||
|
if has_cascade_inb and ru_prof:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
priv, pub = generate_x25519(api)
|
||||||
|
creds = {
|
||||||
|
"uuid": str(uuid.uuid4()),
|
||||||
|
"priv": priv,
|
||||||
|
"pub": pub,
|
||||||
|
"short_id": secrets.token_hex(8),
|
||||||
|
}
|
||||||
|
cascade_inbound = {
|
||||||
|
"tag": "Cascade-RU-DE",
|
||||||
|
"listen": "2a12:bec4:1bb0:2e80::2",
|
||||||
|
"port": 1443,
|
||||||
|
"protocol": "vless",
|
||||||
|
"settings": {
|
||||||
|
"clients": [{"id": creds["uuid"], "flow": "xtls-rprx-vision"}],
|
||||||
|
"decryption": "none",
|
||||||
|
},
|
||||||
|
"sniffing": {
|
||||||
|
"enabled": True,
|
||||||
|
"destOverride": ["http", "tls", "quic"],
|
||||||
|
},
|
||||||
|
"streamSettings": {
|
||||||
|
"network": "tcp",
|
||||||
|
"security": "reality",
|
||||||
|
"realitySettings": {
|
||||||
|
"dest": "www.cloudflare.com:443",
|
||||||
|
"show": False,
|
||||||
|
"shortIds": [creds["short_id"]],
|
||||||
|
"privateKey": creds["priv"],
|
||||||
|
"serverNames": ["www.cloudflare.com"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
new_inbounds = [i for i in inbounds if i.get("tag") != "Cascade-RU-DE"]
|
||||||
|
new_inbounds.append(cascade_inbound)
|
||||||
|
de_cfg["inbounds"] = new_inbounds
|
||||||
|
api.request("PATCH", "/config-profiles/", {"uuid": de_uuid, "config": de_cfg})
|
||||||
|
changes.append("updated profile DE-Yali with Cascade-RU-DE inbound")
|
||||||
|
return creds
|
||||||
|
|
||||||
|
|
||||||
def ensure_profile_and_inbound(
|
def ensure_profile_and_inbound(
|
||||||
api: RemnawaveAPI,
|
api: RemnawaveAPI,
|
||||||
node_cfg: dict[str, Any],
|
node_cfg: dict[str, Any],
|
||||||
profile_templates: dict[str, str],
|
profile_templates: dict[str, str],
|
||||||
|
cascade_creds: dict[str, str],
|
||||||
changes: list[str],
|
changes: list[str],
|
||||||
) -> tuple[str, str]:
|
) -> tuple[str, str, list[str]]:
|
||||||
profile_name = node_cfg["profile_name"]
|
profile_name = node_cfg["profile_name"]
|
||||||
inbound_tag = node_cfg["inbound_tag"]
|
inbound_tag = node_cfg["inbound_tag"]
|
||||||
|
|
||||||
@@ -196,18 +272,7 @@ def ensure_profile_and_inbound(
|
|||||||
if not raw_tpl:
|
if not raw_tpl:
|
||||||
raise RuntimeError(f"Не найден шаблон профиля {tpl_key}")
|
raise RuntimeError(f"Не найден шаблон профиля {tpl_key}")
|
||||||
|
|
||||||
_, key_resp = api.request("GET", "/system/tools/x25519/generate")
|
priv, _ = generate_x25519(api)
|
||||||
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)
|
short_id = secrets.token_hex(8)
|
||||||
rendered = (
|
rendered = (
|
||||||
raw_tpl.replace("__REALITY_PRIVATE_KEY__", priv)
|
raw_tpl.replace("__REALITY_PRIVATE_KEY__", priv)
|
||||||
@@ -215,6 +280,9 @@ def ensure_profile_and_inbound(
|
|||||||
.replace("__INBOUND_TAG__", inbound_tag)
|
.replace("__INBOUND_TAG__", 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"])
|
||||||
|
.replace("__CASCADE_UUID__", cascade_creds.get("uuid", ""))
|
||||||
|
.replace("__CASCADE_PUBLIC_KEY__", cascade_creds.get("pub", ""))
|
||||||
|
.replace("__CASCADE_SHORT_ID__", cascade_creds.get("short_id", ""))
|
||||||
)
|
)
|
||||||
_, created = api.request(
|
_, created = api.request(
|
||||||
"POST",
|
"POST",
|
||||||
@@ -235,7 +303,13 @@ def ensure_profile_and_inbound(
|
|||||||
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}")
|
||||||
return profile_uuid, inbound_uuid
|
extra_uuids = [
|
||||||
|
i["uuid"]
|
||||||
|
for tag in node_cfg.get("extra_inbound_tags", [])
|
||||||
|
for i in inbounds
|
||||||
|
if i.get("tag") == tag
|
||||||
|
]
|
||||||
|
return profile_uuid, inbound_uuid, extra_uuids
|
||||||
|
|
||||||
|
|
||||||
def sync_node_and_host(
|
def sync_node_and_host(
|
||||||
@@ -243,6 +317,7 @@ def sync_node_and_host(
|
|||||||
node_cfg: dict[str, Any],
|
node_cfg: dict[str, Any],
|
||||||
profile_uuid: str,
|
profile_uuid: str,
|
||||||
inbound_uuid: str,
|
inbound_uuid: str,
|
||||||
|
extra_inbound_uuids: list[str],
|
||||||
tpl_uuid: str,
|
tpl_uuid: str,
|
||||||
nb_peers: dict[str, str],
|
nb_peers: dict[str, str],
|
||||||
netbird_connected: bool,
|
netbird_connected: bool,
|
||||||
@@ -263,9 +338,10 @@ def sync_node_and_host(
|
|||||||
nodes = _extract_list(nodes_resp)
|
nodes = _extract_list(nodes_resp)
|
||||||
existing_node = next((n for n in nodes if n.get("name") == node_name), None)
|
existing_node = next((n for n in nodes if n.get("name") == node_name), None)
|
||||||
|
|
||||||
|
active_inbounds = [inbound_uuid] + extra_inbound_uuids
|
||||||
desired_cp = {
|
desired_cp = {
|
||||||
"activeConfigProfileUuid": profile_uuid,
|
"activeConfigProfileUuid": profile_uuid,
|
||||||
"activeInbounds": [inbound_uuid],
|
"activeInbounds": active_inbounds,
|
||||||
}
|
}
|
||||||
|
|
||||||
if not existing_node:
|
if not existing_node:
|
||||||
@@ -295,7 +371,7 @@ def sync_node_and_host(
|
|||||||
existing_node.get("address") != target_addr
|
existing_node.get("address") != target_addr
|
||||||
or int(existing_node.get("port") or 0) != int(node_cfg["port"])
|
or int(existing_node.get("port") or 0) != int(node_cfg["port"])
|
||||||
or cur_cp.get("activeConfigProfileUuid") != profile_uuid
|
or cur_cp.get("activeConfigProfileUuid") != profile_uuid
|
||||||
or cur_inbounds != [inbound_uuid]
|
or set(cur_inbounds) != set(active_inbounds)
|
||||||
)
|
)
|
||||||
if needs_update:
|
if needs_update:
|
||||||
payload = {
|
payload = {
|
||||||
@@ -585,10 +661,12 @@ def main() -> int:
|
|||||||
changes,
|
changes,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cascade_creds = ensure_cascade_credentials(api, changes)
|
||||||
|
|
||||||
inbound_uuids: list[str] = []
|
inbound_uuids: list[str] = []
|
||||||
for node_cfg in cfg.get("nodes", []):
|
for node_cfg in cfg.get("nodes", []):
|
||||||
profile_uuid, inbound_uuid = ensure_profile_and_inbound(
|
profile_uuid, inbound_uuid, extra_inbound_uuids = ensure_profile_and_inbound(
|
||||||
api, node_cfg, cfg.get("profile_templates", {}), changes
|
api, node_cfg, cfg.get("profile_templates", {}), cascade_creds, changes
|
||||||
)
|
)
|
||||||
inbound_uuids.append(inbound_uuid)
|
inbound_uuids.append(inbound_uuid)
|
||||||
sync_node_and_host(
|
sync_node_and_host(
|
||||||
@@ -596,6 +674,7 @@ def main() -> int:
|
|||||||
node_cfg,
|
node_cfg,
|
||||||
profile_uuid,
|
profile_uuid,
|
||||||
inbound_uuid,
|
inbound_uuid,
|
||||||
|
extra_inbound_uuids,
|
||||||
tpl_uuid,
|
tpl_uuid,
|
||||||
nb_peers,
|
nb_peers,
|
||||||
bool(cfg.get("netbird_connected")),
|
bool(cfg.get("netbird_connected")),
|
||||||
|
|||||||
Reference in new issue
Block a user