From 6a665a83cc72c513e90054c32fbe8e0411cc64a5 Mon Sep 17 00:00:00 2001 From: karpenok Date: Wed, 18 Nov 2026 14:30:15 +0700 Subject: [PATCH] feat(remnawave,netbird): add isolated home squad with Except-RU-BirdSocks template and NetBird LAN access --- .../prod/host_vars/rus-central.yml | 2 + .../roles/core-net/remnawave/files/nodes.yml | 9 + .../remnawave/files/profile-cascade-ru.json | 32 +++ .../remnawave/files/sync_remnawave.py | 221 +++++++++++++----- .../files/xray-except-ru-birdsocks.json | 184 +++++++++++++++ .../roles/core-net/remnawave/tasks/sync.yml | 3 + ansible/roles/remnanode/defaults/main.yml | 1 + ansible/roles/remnanode/tasks/main.yml | 7 + .../roles/remnanode/templates/nginx.conf.j2 | 8 +- .../rus-central/netbird/files/access.yml | 17 ++ 10 files changed, 420 insertions(+), 64 deletions(-) create mode 100644 ansible/roles/core-net/remnawave/files/xray-except-ru-birdsocks.json diff --git a/ansible/inventories/prod/host_vars/rus-central.yml b/ansible/inventories/prod/host_vars/rus-central.yml index c26161c..ccaedf0 100644 --- a/ansible/inventories/prod/host_vars/rus-central.yml +++ b/ansible/inventories/prod/host_vars/rus-central.yml @@ -6,6 +6,8 @@ remnanode_allow_panel_wan: true remnanode_selfsteal_domain: "lulu.gunihome.ru" remnanode_cloudflare_zone: "gunihome.ru" remnanode_xhttp_path: "/xhttp-lulu" +remnanode_extra_ports: + - 8443 remnanode_panel_egress_ips: - "213.128.28.107" # провайдер 1 (МТС) - "185.160.137.140" # провайдер 2 (NorCom) diff --git a/ansible/roles/core-net/remnawave/files/nodes.yml b/ansible/roles/core-net/remnawave/files/nodes.yml index 5c590a6..930436e 100644 --- a/ansible/roles/core-net/remnawave/files/nodes.yml +++ b/ansible/roles/core-net/remnawave/files/nodes.yml @@ -28,7 +28,16 @@ nodes: profile_name: "RU-Cascade" profile_template: "profile-cascade-ru.json" inbound_tag: "Steal-Lulu" + extra_inbound_tags: + - "Steal-Lulu-Home" selfsteal_domain: "lulu.gunihome.ru" xhttp_path: "/xhttp-lulu" host_remark: "🇷🇺→🇩🇪 lulu" + home_host: + squad_name: "home" + inbound_tag: "Steal-Lulu-Home" + port: 8443 + xhttp_path: "/xhttp-lulu-home" + remark: "📱 🇷🇺→🇩🇪 lulu + NetBird" + xray_template_name: "Except-RU-BirdSocks" diff --git a/ansible/roles/core-net/remnawave/files/profile-cascade-ru.json b/ansible/roles/core-net/remnawave/files/profile-cascade-ru.json index a235ee8..88e541a 100644 --- a/ansible/roles/core-net/remnawave/files/profile-cascade-ru.json +++ b/ansible/roles/core-net/remnawave/files/profile-cascade-ru.json @@ -43,6 +43,38 @@ "serverNames": ["__SELFSTEAL_DOMAIN__"] } } + }, + { + "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": "__SELFSTEAL_DOMAIN__", + "mode": "stream-one", + "path": "/xhttp-lulu-home" + }, + "realitySettings": { + "dest": "/dev/shm/nginx.sock", + "show": false, + "xver": 1, + "spiderX": "", + "shortIds": ["__REALITY_SHORT_ID__"], + "privateKey": "__REALITY_PRIVATE_KEY__", + "serverNames": ["__SELFSTEAL_DOMAIN__"] + } + } } ], "outbounds": [ diff --git a/ansible/roles/core-net/remnawave/files/sync_remnawave.py b/ansible/roles/core-net/remnawave/files/sync_remnawave.py index 8238752..4f3f392 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 [] @@ -353,7 +388,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( @@ -367,7 +467,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", "") @@ -430,57 +530,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( @@ -493,10 +557,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", @@ -707,17 +781,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, @@ -729,8 +819,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( diff --git a/ansible/roles/core-net/remnawave/files/xray-except-ru-birdsocks.json b/ansible/roles/core-net/remnawave/files/xray-except-ru-birdsocks.json new file mode 100644 index 0000000..a680ac1 --- /dev/null +++ b/ansible/roles/core-net/remnawave/files/xray-except-ru-birdsocks.json @@ -0,0 +1,184 @@ +{ + "remnawave": { + "addVirtualHostAsOutbound": true + }, + "routing": { + "rules": [ + { + "type": "field", + "domain": [ + "full:lulu.gunihome.ru", + "full:central.gunihome.ru" + ], + "outboundTag": "direct" + }, + { + "type": "field", + "domain": ["domain:gunihome.ru"], + "outboundTag": "netbird-socks" + }, + { + "type": "field", + "ip": [ + "192.168.1.0/24", + "100.68.0.0/16" + ], + "outboundTag": "netbird-socks" + }, + { + "type": "field", + "domain": [ + "regexp:^([a-z0-9-]+\\.)*2ip\\.[a-z0-9.-]+$", + "domain:ifconfig.me", + "domain:www.ifconfig.me", + "domain:ifconfig.co", + "domain:www.ifconfig.co", + "domain:ifconfig.io", + "domain:www.ifconfig.io", + "domain:ifconfig.so", + "domain:checkip.amazonaws.com", + "domain:api.ipify.org", + "domain:ipify.org", + "domain:ip.sb", + "domain:www.ip.sb", + "domain:api-ipv4.ip.sb", + "domain:api.ipv6.ip.sb", + "domain:ip-api.com", + "domain:api.ipapi.is", + "domain:ipapi.co", + "domain:ipapi.com", + "domain:iplocation.net", + "domain:iplocation.io", + "domain:iplocation.com", + "domain:ipwho.is", + "domain:ipwhois.io", + "domain:ipinfo.io", + "domain:www.ipinfo.io", + "domain:ident.me", + "domain:myip.com", + "domain:whatismyipaddress.com", + "domain:wtfismyip.com", + "domain:whatismyip.com", + "domain:showmyip.com", + "domain:icanhazip.com", + "domain:geojs.io", + "domain:ipleak.net", + "domain:ipleak.org", + "domain:browserleaks.com", + "domain:dnsleaktest.com", + "domain:dnscheck.tools", + "domain:whoer.net", + "domain:hidemy.name", + "domain:sypexgeo.net", + "domain:api.sypexgeo.net", + "domain:mail.ru", + "domain:ip.mail.ru", + "full:ipv4-internet.yandex.net", + "full:ipv6-internet.yandex.net", + "full:www.cloudflare.com/cdn-cgi/trace" + ], + "outboundTag": "direct" + }, + { + "type": "field", + "domain": [ + "domain:habr.com", + "domain:4pda.to", + "domain:4pda.ru", + "domain:kemono.su", + "domain:jut.su", + "domain:kara.su", + "domain:theins.ru", + "domain:tvrain.ru", + "domain:echo.msk.ru", + "domain:the-village.ru", + "domain:snob.ru", + "domain:novayagazeta.ru", + "domain:moscowtimes.ru", + "domain:allnorilsk.ru" + ], + "outboundTag": "proxy" + }, + { + "ip": ["geoip:ru"], + "type": "field", + "outboundTag": "direct" + }, + { + "type": "field", + "domain": ["geosite:category-ru"], + "outboundTag": "direct" + }, + { + "ip": ["geoip:private"], + "type": "field", + "outboundTag": "direct" + }, + { + "type": "field", + "protocol": ["bittorrent"], + "outboundTag": "direct" + }, + { + "type": "field", + "network": "tcp,udp", + "outboundTag": "proxy" + } + ], + "domainMatcher": "hybrid", + "domainStrategy": "IPIfNonMatch" + }, + "inbounds": [ + { + "tag": "socks", + "port": 10808, + "listen": "127.0.0.1", + "protocol": "socks", + "settings": { + "udp": true, + "auth": "noauth" + }, + "sniffing": { + "enabled": true, + "routeOnly": false, + "destOverride": ["http", "tls", "quic"] + } + }, + { + "tag": "http", + "port": 10809, + "listen": "127.0.0.1", + "protocol": "http", + "settings": { + "allowTransparent": false + }, + "sniffing": { + "enabled": true, + "routeOnly": false, + "destOverride": ["http", "tls", "quic"] + } + } + ], + "outbounds": [ + { + "tag": "direct", + "protocol": "freedom" + }, + { + "tag": "netbird-socks", + "protocol": "socks", + "settings": { + "servers": [ + { + "address": "127.0.0.1", + "port": 1055 + } + ] + } + }, + { + "tag": "block", + "protocol": "blackhole" + } + ] +} diff --git a/ansible/roles/core-net/remnawave/tasks/sync.yml b/ansible/roles/core-net/remnawave/tasks/sync.yml index cb527d4..5d3cc46 100644 --- a/ansible/roles/core-net/remnawave/tasks/sync.yml +++ b/ansible/roles/core-net/remnawave/tasks/sync.yml @@ -20,6 +20,9 @@ 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 }}" + extra_xray_templates: + - name: "Except-RU-BirdSocks" + json: "{{ lookup('file', role_path ~ '/files/xray-except-ru-birdsocks.json') | 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([]) }}" diff --git a/ansible/roles/remnanode/defaults/main.yml b/ansible/roles/remnanode/defaults/main.yml index 13dfdba..98bddc2 100644 --- a/ansible/roles/remnanode/defaults/main.yml +++ b/ansible/roles/remnanode/defaults/main.yml @@ -9,6 +9,7 @@ remnanode_allow_panel_wan: false remnanode_dns_record_type: "A" remnanode_ipv6_only: false remnanode_cascade_allowed_ips: [] +remnanode_extra_ports: [] remnanode_selfsteal_domain: "yali.gunis.top" remnanode_xhttp_path: "/xhttp-yali" diff --git a/ansible/roles/remnanode/tasks/main.yml b/ansible/roles/remnanode/tasks/main.yml index 770689c..676477d 100644 --- a/ansible/roles/remnanode/tasks/main.yml +++ b/ansible/roles/remnanode/tasks/main.yml @@ -224,6 +224,13 @@ failed_when: false when: remnanode_ipv6_only | bool +- name: Разрешить дополнительные порты клиентов (если заданы) + ansible.builtin.command: "ufw allow {{ item }}/tcp" + loop: "{{ remnanode_extra_ports }}" + register: ufw_extra_ports + changed_when: "'added' in (ufw_extra_ports.stdout | lower)" + when: remnanode_extra_ports | length > 0 + - name: Разрешить 443/tcp только с входных IPv6 каскада ansible.builtin.command: "ufw allow from {{ item }} to {{ remnanode_public_ip }} port 443 proto tcp" loop: "{{ remnanode_cascade_allowed_ips }}" diff --git a/ansible/roles/remnanode/templates/nginx.conf.j2 b/ansible/roles/remnanode/templates/nginx.conf.j2 index 37ad9a6..9a3295e 100644 --- a/ansible/roles/remnanode/templates/nginx.conf.j2 +++ b/ansible/roles/remnanode/templates/nginx.conf.j2 @@ -29,13 +29,7 @@ server { try_files $uri $uri/ /index.html; } - location = {{ remnanode_xhttp_path }} { - default_type text/plain; - add_header Cache-Control "no-store" always; - return 200 "ok"; - } - - location = {{ remnanode_xhttp_path }}/ { + location ~ ^{{ remnanode_xhttp_path }}(?:-home)?/?$ { default_type text/plain; add_header Cache-Control "no-store" always; return 200 "ok"; diff --git a/ansible/roles/rus-central/netbird/files/access.yml b/ansible/roles/rus-central/netbird/files/access.yml index 9e90ae5..a925394 100644 --- a/ansible/roles/rus-central/netbird/files/access.yml +++ b/ansible/roles/rus-central/netbird/files/access.yml @@ -9,6 +9,7 @@ client_groups: - exit-clients - remnawave - exit-ger-nod-02 + - homelab-routers # Выходные шлюзы (Exit Nodes) exit_nodes: @@ -120,3 +121,19 @@ policies: - exit-clients destinations: - exit-ger-nod-02 + + - name: "exit-clients-to-homelab" + description: "Allow BirdSocks/NetBird clients to reach homelab LAN (via OPNsense) and core-net" + enabled: true + rules: + - name: "exit-clients-homelab-tcp-udp" + description: "Allow TCP/UDP from exit-clients to homelab-routers and remnawave" + enabled: true + action: accept + protocol: all + bidirectional: false + sources: + - exit-clients + destinations: + - homelab-routers + - remnawave