diff --git a/d2ypp2/config/system-stack.env.example b/d2ypp2/config/system-stack.env.example index e43b1ba..9219b94 100644 --- a/d2ypp2/config/system-stack.env.example +++ b/d2ypp2/config/system-stack.env.example @@ -41,6 +41,16 @@ SHELLCRASH_ATTACH_FFMPEG=auto # Docker bridge CIDR allowed to reach ShellCrash listeners. This keeps Neko # transparent mode working when the browser proxy switch is off and UFW denies INPUT. SHELLCRASH_DOCKER_CIDR=172.16.0.0/12 +# WireGuard overlay used by the project (wg-easy / 腾讯云中继). ShellCrash WAN +# firewall rejects 9999/7890 unless these trusted CIDRs are accepted first. +SHELLCRASH_WG_CIDR=10.88.0.0/24 +# Optional override (comma-separated). Empty = Docker + WG + RFC1918 defaults. +# SHELLCRASH_TRUSTED_CIDRS=172.16.0.0/12,10.88.0.0/24,192.168.0.0/16,10.0.0.0/8 +# Optional install-time HTTP(S) proxy (e.g. Windows Clash http://192.168.21.36:7897). +# Leave empty for direct; clear after install so runtime does not keep the proxy. +# LIVE_HTTP_PROXY= +# LIVE_HTTPS_PROXY= +# LIVE_NO_PROXY=localhost,127.0.0.1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,*.lan,*.local # Neko 浏览器 HTTP 代理总开关;默认 0=关闭(走宿主机/软路由直连)。 # 当直连 Google/YouTube 失败且 ShellCrash 代理可用时,控制台/启动脚本会自动设为 1(除非 NEKO_BROWSER_PROXY_MANUAL=1)。 ENABLE_NEKO_BROWSER_PROXY=0 diff --git a/d2ypp2/docs/deploy-hardening.md b/d2ypp2/docs/deploy-hardening.md new file mode 100644 index 0000000..977442e --- /dev/null +++ b/d2ypp2/docs/deploy-hardening.md @@ -0,0 +1,59 @@ +# Deploy hardening notes (x86 / ARM) + +Lessons from production deploys on Ubuntu (x86_64 and aarch64) behind Soft Router / WireGuard. + +## Architecture + +Install scripts detect CPU once and reuse labels: + +| `uname -m` | ShellCrash `cpucore` | Docker Compose asset | +|------------|----------------------|----------------------| +| `x86_64` / `amd64` | `amd64` | `x86_64` | +| `aarch64` / `arm64` | `arm64` | `aarch64` | +| `armv7*` / `armhf` | `armv7` | `armv7` | + +`harden_shellcrash_runtime` refreshes `cpucore` on every install so a copied image cannot keep the wrong binary label. + +## ShellCrash dashboard blocked on WireGuard + +Symptom: ShellCrash shows online and has YAML, but `http://10.88.0.x:9999/ui/` fails from WG peers while localhost works. + +Cause: ShellCrash nftables WAN firewall accepts only physical LAN (`HOST_IP`) then **rejects** `7890` / `9999`. WireGuard (`10.88.0.0/24`) is not in that accept set. + +Fix in tree: + +- `SHELLCRASH_WG_CIDR` / `SHELLCRASH_TRUSTED_CIDRS` (see `config/system-stack.env.example`) +- `ensure_shellcrash_proxy_ports_for_docker` also opens dashboard `db_port` +- systemd drop-in `shellcrash.service.d/allow-trusted.conf` + `/usr/local/sbin/shellcrash-allow-trusted.sh` re-applies after every ShellCrash start + +## Install-time proxy + +When the host cannot reach Docker Hub / GitHub / PyPI directly: + +```bash +export LIVE_HTTP_PROXY=http://192.168.21.36:7897 +export LIVE_HTTPS_PROXY="$LIVE_HTTP_PROXY" +sudo -E bash ./install-all.sh +# then clear LIVE_* and host proxy files so runtime stays direct +``` + +Installer downloads for ShellCrash try with proxy and without (`--noproxy` / unset env). + +## WireGuard + Neko + +- Overlay address for this fleet is typically `10.88.0.0/24` via `129.204.21.129:51820`. +- `NEKO_ACCESS_MODE=remote` should use the WG address for `NEKO_WEBRTC_NAT1TO1`, not the physical LAN IP. +- `resolve_neko_nat1to1` auto-picks `wg*` / `10.88.0.x` when remote mode is set. +- Applying a WG conf also re-runs the ShellCrash trusted-CIDR hook. + +## Ports to remember + +| Service | Default | +|---------|---------| +| Control plane | `8001` | +| Caddy edge | `80` → `8001` | +| Homepage | `3080` (not `80`) | +| ShellCrash UI | `9999` | +| ShellCrash mixed | `7890` | +| WebTTY | `7681` | +| File Browser | `8082` | diff --git a/d2ypp2/docs/wg-easy-operations.md b/d2ypp2/docs/wg-easy-operations.md index adee55c..b200550 100644 --- a/d2ypp2/docs/wg-easy-operations.md +++ b/d2ypp2/docs/wg-easy-operations.md @@ -18,6 +18,12 @@ - wandaB:`wgeasy` 使用 `Endpoint = 129.204.21.129:51820`,`Address = 10.88.0.2/32`;到 `10.88.0.1` 的延迟约 25ms。 - Neko 访问优先级:WireGuard `10.88.0.0/24` 优先,frp 公网端口兜底,EasyTier `10.126.126.0/24` 作为备用运维链路。 +## ShellCrash dashboard over WireGuard + +ShellCrash `firewall_area=1` rejects WAN access to `7890` / `9999` after accepting only the physical LAN. WireGuard peers (`10.88.0.0/24`) must be added to the trusted accept set or the UI shows “online” locally but `http://10.88.0.x:9999/ui/` fails from remote. + +See `docs/deploy-hardening.md` for `SHELLCRASH_TRUSTED_CIDRS` / `shellcrash-allow-trusted.sh`. + ## 推荐参数 WireGuard peer 推荐: diff --git a/d2ypp2/scripts/linux/lib/common.sh b/d2ypp2/scripts/linux/lib/common.sh index cd04966..394a440 100644 --- a/d2ypp2/scripts/linux/lib/common.sh +++ b/d2ypp2/scripts/linux/lib/common.sh @@ -314,6 +314,10 @@ load_env() { SHELLCRASH_ATTACH_NEKO="${SHELLCRASH_ATTACH_NEKO:-auto}" SHELLCRASH_ATTACH_FFMPEG="${SHELLCRASH_ATTACH_FFMPEG:-auto}" SHELLCRASH_DOCKER_CIDR="${SHELLCRASH_DOCKER_CIDR:-172.16.0.0/12}" + # Extra CIDRs allowed past ShellCrash WAN reject (dashboard 9999 / mixed 7890). + # Defaults cover Docker bridges, RFC1918 LAN, and the project WireGuard overlay. + SHELLCRASH_WG_CIDR="${SHELLCRASH_WG_CIDR:-10.88.0.0/24}" + SHELLCRASH_TRUSTED_CIDRS="${SHELLCRASH_TRUSTED_CIDRS:-}" ENABLE_NEKO_BROWSER_PROXY="${ENABLE_NEKO_BROWSER_PROXY:-0}" NEKO_SHELLCRASH_PROXY_PORT="${NEKO_SHELLCRASH_PROXY_PORT:-7894}" SHELLCRASH_NEKO_PROXY_NODE="${SHELLCRASH_NEKO_PROXY_NODE:-proxycheap}" @@ -330,8 +334,13 @@ load_env() { BROWSER_PROFILE_DIR="${BROWSER_PROFILE_DIR:-/var/lib/live-browser/profile}" DISABLE_IPV6="${DISABLE_IPV6:-1}" LIVE_DNS_SERVERS="${LIVE_DNS_SERVERS:-223.5.5.5 119.29.29.29 1.1.1.1 8.8.8.8}" + # Optional install-time proxy (e.g. Windows Clash on LAN). Leave empty for direct. + LIVE_HTTP_PROXY="${LIVE_HTTP_PROXY:-}" + LIVE_HTTPS_PROXY="${LIVE_HTTPS_PROXY:-${LIVE_HTTP_PROXY:-}}" + LIVE_NO_PROXY="${LIVE_NO_PROXY:-localhost,127.0.0.1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,*.lan,*.local}" ANDROID_PANEL_PORT="${ANDROID_PANEL_PORT:-5000}" ANDROID_PANEL_DIR="${ANDROID_PANEL_DIR:-/opt/live-modules/web-scrcpy}" + apply_live_http_proxy_env ANDROID_PANEL_VIDEO_BIT_RATE="${ANDROID_PANEL_VIDEO_BIT_RATE:-1024000}" ENABLE_LIVE_EDGE_PROXY="${ENABLE_LIVE_EDGE_PROXY:-1}" LIVE_NOFILE_SOFT="${LIVE_NOFILE_SOFT:-524288}" @@ -1231,14 +1240,12 @@ install_nodejs() { } install_docker_compose_plugin_binary() { - local raw arch version url target - raw="$(uname -m)" - case "$raw" in - x86_64|amd64) arch="x86_64" ;; - aarch64|arm64) arch="aarch64" ;; - armv7l|armv6l) arch="armv7" ;; + local arch version url target + arch="$(detect_docker_compose_arch)" + case "$arch" in + x86_64|aarch64|armv7) ;; *) - log "WARN: unsupported arch for docker compose plugin binary: $raw" + log "WARN: unsupported arch for docker compose plugin binary: $(uname -m)" return 1 ;; esac @@ -1606,6 +1613,12 @@ resolve_neko_nat1to1() { return 0 fi fi + # Prefer live WireGuard overlay address over physical LAN (avoids WebRTC black screen). + candidate="$(detect_wireguard_overlay_ip || true)" + if [ -n "$candidate" ] && neko_nat1to1_is_usable "$candidate"; then + printf '%s' "$candidate" + return 0 + fi ;; *) [ -n "$lan_ip" ] && printf '%s' "$lan_ip" @@ -1791,15 +1804,87 @@ configure_wifi_profile() { fi } -shellcrash_cpu_core() { +# Canonical CPU labels used across ShellCrash / Caddy / ttyd (amd64|arm64|armv7). +detect_cpu_arch() { case "$(uname -m)" in - aarch64|arm64) printf '%s\n' arm64 ;; - armv7l|armv7*) printf '%s\n' armv7 ;; x86_64|amd64) printf '%s\n' amd64 ;; + aarch64|arm64) printf '%s\n' arm64 ;; + armv7l|armv7*|armhf) printf '%s\n' armv7 ;; + armv6l|armv6*) printf '%s\n' armv7 ;; *) printf '%s\n' "$(uname -m)" ;; esac } +# Docker Compose GitHub asset arch (x86_64|aarch64|armv7). +detect_docker_compose_arch() { + case "$(detect_cpu_arch)" in + amd64) printf '%s\n' x86_64 ;; + arm64) printf '%s\n' aarch64 ;; + armv7) printf '%s\n' armv7 ;; + *) printf '%s\n' "$(uname -m)" ;; + esac +} + +shellcrash_cpu_core() { + detect_cpu_arch +} + +apply_live_http_proxy_env() { + # Only inject when explicitly configured; never invent a proxy. + if [ -n "${LIVE_HTTP_PROXY:-}" ]; then + export http_proxy="$LIVE_HTTP_PROXY" + export HTTP_PROXY="$LIVE_HTTP_PROXY" + fi + if [ -n "${LIVE_HTTPS_PROXY:-}" ]; then + export https_proxy="$LIVE_HTTPS_PROXY" + export HTTPS_PROXY="$LIVE_HTTPS_PROXY" + fi + if [ -n "${LIVE_HTTP_PROXY:-}${LIVE_HTTPS_PROXY:-}" ]; then + export no_proxy="${LIVE_NO_PROXY:-localhost,127.0.0.1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16}" + export NO_PROXY="$no_proxy" + fi +} + +clear_live_http_proxy_env() { + unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy no_proxy NO_PROXY || true + LIVE_HTTP_PROXY="" + LIVE_HTTPS_PROXY="" +} + +detect_wireguard_overlay_ip() { + local path name ip preferred="${SHELLCRASH_WG_CIDR:-10.88.0.0/24}" + preferred="${preferred%%/*}" + preferred="${preferred%.*}" + for path in /sys/class/net/*; do + [ -e "$path" ] || continue + name="${path##*/}" + case "$name" in + wg*|wgeasy|wireguard*) ;; + *) continue ;; + esac + ip="$(ip -4 addr show dev "$name" 2>/dev/null | awk '$1=="inet" {sub(/\/.*/, "", $2); print $2; exit}')" + [ -n "$ip" ] || continue + if [ -n "$preferred" ] && printf '%s' "$ip" | grep -q "^${preferred}\\."; then + printf '%s\n' "$ip" + return 0 + fi + done + for path in /sys/class/net/*; do + [ -e "$path" ] || continue + name="${path##*/}" + case "$name" in + wg*|wgeasy|wireguard*) ;; + *) continue ;; + esac + ip="$(ip -4 addr show dev "$name" 2>/dev/null | awk '$1=="inet" {sub(/\/.*/, "", $2); print $2; exit}')" + if [ -n "$ip" ]; then + printf '%s\n' "$ip" + return 0 + fi + done + return 1 +} + shellcrash_set_cfg_default() { local cfg="$1" key="$2" value="$3" if ! grep -q "^${key}=" "$cfg" 2>/dev/null; then @@ -1926,9 +2011,25 @@ shellcrash_docker_cidr() { printf '%s' "$cidr" } -ensure_ufw_shellcrash_docker_access() { - local port="$1" proto="$2" comment="$3" cidr - cidr="$(shellcrash_docker_cidr)" +# Emit trusted source CIDRs (one per line) allowed past ShellCrash WAN reject. +shellcrash_trusted_cidrs() { + local raw item + if [ -n "${SHELLCRASH_TRUSTED_CIDRS:-}" ]; then + raw="$SHELLCRASH_TRUSTED_CIDRS" + else + raw="$(shellcrash_docker_cidr),${SHELLCRASH_WG_CIDR:-10.88.0.0/24},192.168.0.0/16,10.0.0.0/8" + fi + raw="$(printf '%s' "$raw" | tr ',;' ' ')" + for item in $raw; do + case "$item" in + ''|*[!0-9./]*) continue ;; + *) printf '%s\n' "$item" ;; + esac + done | awk 'NF && !seen[$0]++' +} + +ensure_ufw_shellcrash_cidr_access() { + local port="$1" proto="$2" comment="$3" cidr="$4" have ufw || return 1 ufw status 2>/dev/null | grep -q '^Status: active' || return 1 ufw_shellcrash_docker_rule_exists "$port" "$proto" "$cidr" && return 0 @@ -1936,6 +2037,10 @@ ensure_ufw_shellcrash_docker_access() { ufw allow in proto "$proto" from "$cidr" to any port "$port" >/dev/null 2>&1 } +ensure_ufw_shellcrash_docker_access() { + ensure_ufw_shellcrash_cidr_access "$1" "$2" "$3" "$(shellcrash_docker_cidr)" +} + ufw_shellcrash_docker_rule_exists() { local port="$1" proto="$2" cidr="$3" ufw status 2>/dev/null | awk -v target="${port}/${proto}" -v cidr="$cidr" ' @@ -1944,8 +2049,8 @@ ufw_shellcrash_docker_rule_exists() { ' } -ensure_shellcrash_docker_port_access() { - local port="$1" proto="$2" comment="${3:-ShellCrash from Docker}" cidr ufw_rule_ok=0 +ensure_shellcrash_cidr_port_access() { + local port="$1" proto="$2" comment="${3:-ShellCrash trusted}" cidr="$4" ufw_rule_ok=0 [ "${ENABLE_SHELLCRASH:-1}" = "1" ] || return 0 case "$port" in ''|*[!0-9]*) return 0 ;; @@ -1954,15 +2059,20 @@ ensure_shellcrash_docker_port_access() { tcp|udp) ;; *) return 0 ;; esac - cidr="$(shellcrash_docker_cidr)" - if ensure_ufw_shellcrash_docker_access "$port" "$proto" "$comment"; then + case "$cidr" in + ''|*[!0-9./]*) return 0 ;; + esac + if ensure_ufw_shellcrash_cidr_access "$port" "$proto" "$comment" "$cidr"; then ufw_rule_ok=1 fi if have nft && nft list chain inet shellcrash input >/dev/null 2>&1; then - if ! nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr $cidr $proto dport $port accept"; then - nft -f - >/dev/null 2>&1 </dev/null | grep -Fq "ip saddr $cidr accept"; then + if ! nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr $cidr $proto dport $port accept"; then + nft -f - >/dev/null 2>&1 <"$hook" <<'EOS' +#!/bin/bash +# Re-allow trusted CIDRs after ShellCrash rebuilds its nftables WAN reject rules. +# Keeps WireGuard/LAN/Docker able to reach dashboard :9999 and mixed-port :7890. +set -euo pipefail +for i in 1 2 3 4 5 6 7 8 9 10 11 12; do + nft list table inet shellcrash >/dev/null 2>&1 && break + sleep 1 +done +nft list table inet shellcrash >/dev/null 2>&1 || exit 0 +cidrs="${SHELLCRASH_TRUSTED_CIDRS:-172.16.0.0/12,10.88.0.0/24,192.168.0.0/16,10.0.0.0/8}" +IFS=',; ' read -r -a arr <<<"$cidrs" +for cidr in "${arr[@]}"; do + case "$cidr" in + ''|*[!0-9./]*) continue ;; + esac + nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr $cidr accept" && continue + nft insert rule inet shellcrash input ip saddr "$cidr" accept comment \"shellcrash-trusted\" 2>/dev/null || true +done +EOS + chmod +x "$hook" + cat >/etc/systemd/system/shellcrash.service.d/allow-trusted.conf </dev/null 2>&1 || true +} + shellcrash_proxy_available() { local port="$1" [ "${ENABLE_SHELLCRASH:-1}" = "1" ] || return 1 @@ -2087,6 +2246,34 @@ if old in text: PY } +patch_shellcrash_nftables_redirect() { + local fw="$SHELLCRASH_DIR/starts/fw_nftables.sh" + [ -f "$fw" ] || return 0 + command -v python3 >/dev/null 2>&1 || return 0 + python3 - "$fw" <<'PY' || true +import sys +from pathlib import Path + +path = Path(sys.argv[1]) +text = path.read_text(encoding="utf-8", errors="replace") +text = text.replace( + 'nft add rule inet shellcrash "$1" "$JUMP"', + 'nft add rule inet shellcrash "$1" $JUMP', +) +needle = "start_nftables() { #nftables配置总入口\n" +insert = ( + needle + + " modprobe nft_redir >/dev/null 2>&1 || true\n" + + " modprobe nft_chain_nat >/dev/null 2>&1 || true\n" + + " modprobe nf_nat >/dev/null 2>&1 || true\n" +) +if "modprobe nft_redir" not in text and needle in text: + text = text.replace(needle, insert, 1) +path.write_text(text, encoding="utf-8") +PY + chmod +x "$fw" 2>/dev/null || true +} + harden_shellcrash_runtime() { if [ ! -d "$SHELLCRASH_DIR" ]; then return 0 @@ -2097,7 +2284,12 @@ harden_shellcrash_runtime() { [ -f "$cfg" ] || printf '%s\n' "#ShellCrash配置文件,不明勿动!" >"$cfg" shellcrash_set_cfg_default "$cfg" crashcore meta - shellcrash_set_cfg_default "$cfg" cpucore "$(shellcrash_cpu_core)" + # Always refresh cpucore so a copied image cannot keep the wrong arch binary label. + if grep -q '^cpucore=' "$cfg" 2>/dev/null; then + sed -i "s/^cpucore=.*/cpucore=$(shellcrash_cpu_core)/" "$cfg" || true + else + shellcrash_set_cfg_default "$cfg" cpucore "$(shellcrash_cpu_core)" + fi shellcrash_set_cfg_default "$cfg" redir_mod "Redir模式" shellcrash_set_cfg_default "$cfg" firewall_area 1 shellcrash_set_cfg_default "$cfg" dns_mod redir_host @@ -2111,6 +2303,8 @@ harden_shellcrash_runtime() { shellcrash_set_cfg_default "$cfg" network_check OFF patch_shellcrash_dns_repair + patch_shellcrash_nftables_redirect + install_shellcrash_trusted_firewall_hook if ! grep -q '^crashcore=.*singbox' "$cfg" 2>/dev/null; then cat >"$SHELLCRASH_DIR/configs/command.env" </dev/null | grep -q '^shellcrash.service'; then if shellcrash_should_autostart_on_direct_fail; then systemctl reset-failed shellcrash.service >/dev/null 2>&1 || true + systemctl unmask shellcrash.service >/dev/null 2>&1 || true systemctl enable --now shellcrash.service >/dev/null 2>&1 || true else systemctl disable --now shellcrash.service >/dev/null 2>&1 || systemctl stop shellcrash.service >/dev/null 2>&1 || true @@ -2142,6 +2337,8 @@ EOF fi fi shellcrash_sync_runtime_config || log "WARN: ShellCrash config.yaml is not staged yet; apply a YAML profile before starting" + ensure_shellcrash_proxy_ports_for_docker >/dev/null 2>&1 || true + /usr/local/sbin/shellcrash-allow-trusted.sh >/dev/null 2>&1 || true grant_shellcrash_web_write_access } @@ -2154,20 +2351,33 @@ install_shellcrash() { harden_shellcrash_runtime return 0 fi - log "Installing ShellCrash" + log "Installing ShellCrash (arch=$(shellcrash_cpu_core))" local url ok=0 rm -f /tmp/install_shellcrash.sh + download_shellcrash_installer() { + local target="$1" + if have wget; then + wget -q --no-check-certificate -O /tmp/install_shellcrash.sh "$target/install_en.sh" && return 0 + env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u all_proxy \ + wget -q --no-check-certificate -O /tmp/install_shellcrash.sh "$target/install_en.sh" && return 0 + fi + if have curl; then + curl -fsSL --noproxy '*' -o /tmp/install_shellcrash.sh "$target/install_en.sh" && return 0 + curl -fsSL -o /tmp/install_shellcrash.sh "$target/install_en.sh" && return 0 + fi + return 1 + } for url in \ 'https://testingcf.jsdelivr.net/gh/juewuy/ShellCrash@dev' \ 'https://raw.githubusercontent.com/juewuy/ShellCrash/dev' do - if retry_run 2 4 wget -q --no-check-certificate -O /tmp/install_shellcrash.sh "$url/install_en.sh" && [ -s /tmp/install_shellcrash.sh ]; then + if retry_run 2 4 download_shellcrash_installer "$url" && [ -s /tmp/install_shellcrash.sh ]; then ok=1 break fi done if [ "$ok" != "1" ] || [ ! -s /tmp/install_shellcrash.sh ]; then - log "ShellCrash installer download failed — continuing hub install (proxy later or set ENABLE_SHELLCRASH=0)" + log "ShellCrash installer download failed — continuing hub install (set LIVE_HTTP_PROXY or ENABLE_SHELLCRASH=0)" return 0 fi if ! printf '1\n1\n1\n1\n' | bash /tmp/install_shellcrash.sh; then diff --git a/d2ypp2/scripts/linux/service_ctl.sh b/d2ypp2/scripts/linux/service_ctl.sh index 7303e60..ded5178 100644 --- a/d2ypp2/scripts/linux/service_ctl.sh +++ b/d2ypp2/scripts/linux/service_ctl.sh @@ -32,7 +32,7 @@ KMSCUBE_LAUNCHER="$ROOT_DIR/scripts/linux/run_kmscube.sh" HOSTNAME_ALIAS="${HOSTNAME_ALIAS:-live}" PORT="${PORT:-8001}" WEBTTY_PORT="${WEBTTY_PORT:-7681}" -HOMEPAGE_PORT="${HOMEPAGE_PORT:-80}" +HOMEPAGE_PORT="${HOMEPAGE_PORT:-3080}" FILEBROWSER_PORT="${FILEBROWSER_PORT:-8082}" NETDATA_PORT="${NETDATA_PORT:-19999}" SRS_HTTP_PORT="${SRS_HTTP_PORT:-8080}" @@ -49,6 +49,8 @@ SHELLCRASH_ATTACH_WHEN_DIRECT_OK="${SHELLCRASH_ATTACH_WHEN_DIRECT_OK:-0}" SHELLCRASH_ATTACH_NEKO="${SHELLCRASH_ATTACH_NEKO:-auto}" SHELLCRASH_ATTACH_FFMPEG="${SHELLCRASH_ATTACH_FFMPEG:-auto}" SHELLCRASH_DOCKER_CIDR="${SHELLCRASH_DOCKER_CIDR:-172.16.0.0/12}" +SHELLCRASH_WG_CIDR="${SHELLCRASH_WG_CIDR:-10.88.0.0/24}" +SHELLCRASH_TRUSTED_CIDRS="${SHELLCRASH_TRUSTED_CIDRS:-}" ENABLE_NEKO_BROWSER_PROXY="${ENABLE_NEKO_BROWSER_PROXY:-0}" NEKO_SHELLCRASH_PROXY_PORT="${NEKO_SHELLCRASH_PROXY_PORT:-7894}" SHELLCRASH_NEKO_PROXY_NODE="${SHELLCRASH_NEKO_PROXY_NODE:-proxycheap}" @@ -1017,6 +1019,20 @@ resolve_neko_nat1to1() { return 0 fi fi + candidate="$(detect_wireguard_overlay_ip 2>/dev/null || true)" + if [ -z "$candidate" ]; then + candidate="$(ip -4 addr show 2>/dev/null | awk ' + /^[0-9]+: / { iface=$2; sub(/:$/, "", iface) } + $1=="inet" { + ip=$2; sub(/\/.*/, "", ip) + if (iface ~ /^(wg|wgeasy|wireguard)/ && ip ~ /^10\.88\.0\./) { print ip; exit } + } + ')" + fi + if [ -n "$candidate" ] && neko_nat1to1_is_usable "$candidate"; then + printf '%s' "$candidate" + return 0 + fi ;; *) [ -n "$lan_ip" ] && printf '%s' "$lan_ip" @@ -1413,9 +1429,25 @@ shellcrash_docker_cidr() { printf '%s' "$cidr" } +shellcrash_trusted_cidrs() { + local raw item + if [ -n "${SHELLCRASH_TRUSTED_CIDRS:-}" ]; then + raw="$SHELLCRASH_TRUSTED_CIDRS" + else + raw="$(shellcrash_docker_cidr),${SHELLCRASH_WG_CIDR:-10.88.0.0/24},192.168.0.0/16,10.0.0.0/8" + fi + raw="$(printf '%s' "$raw" | tr ',;' ' ')" + for item in $raw; do + case "$item" in + ''|*[!0-9./]*) continue ;; + *) printf '%s\n' "$item" ;; + esac + done | awk 'NF && !seen[$0]++' +} + ensure_ufw_shellcrash_docker_access() { - local port="$1" proto="$2" comment="$3" cidr - cidr="$(shellcrash_docker_cidr)" + local port="$1" proto="$2" comment="$3" cidr="$4" + cidr="${cidr:-$(shellcrash_docker_cidr)}" have ufw || return 1 ufw status 2>/dev/null | grep -q '^Status: active' || return 1 ufw_shellcrash_docker_rule_exists "$port" "$proto" "$cidr" && return 0 @@ -1441,21 +1473,26 @@ ensure_shellcrash_docker_port_access() { tcp|udp) ;; *) return 0 ;; esac - cidr="$(shellcrash_docker_cidr)" - if ensure_ufw_shellcrash_docker_access "$port" "$proto" "$comment"; then - ufw_rule_ok=1 - fi - if have nft && nft list chain inet shellcrash input >/dev/null 2>&1; then - if ! nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr $cidr $proto dport $port accept"; then - nft -f - >/dev/null 2>&1 </dev/null || - iptables -I INPUT -s "$cidr" -p "$proto" --dport "$port" -j ACCEPT 2>/dev/null || true - fi + if have nft && nft list chain inet shellcrash input >/dev/null 2>&1; then + if ! nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr $cidr accept"; then + nft -f - >/dev/null 2>&1 </dev/null || + iptables -I INPUT -s "$cidr" -p "$proto" --dport "$port" -j ACCEPT 2>/dev/null || true + fi + done </dev/null || echo 9999)" tcp "ShellCrash dashboard from trusted" if neko_shellcrash_proxy_enabled; then ensure_shellcrash_docker_proxy_access "$(shellcrash_neko_proxy_port)" "Neko ShellCrash proxy from Docker" fi @@ -1594,7 +1632,7 @@ shellcrash_set_cfg() { shellcrash_cpu_core() { case "$(uname -m)" in aarch64|arm64) printf '%s\n' arm64 ;; - armv7l|armv7*) printf '%s\n' armv7 ;; + armv7l|armv7*|armhf|armv6l) printf '%s\n' armv7 ;; x86_64|amd64) printf '%s\n' amd64 ;; *) printf '%s\n' "$(uname -m)" ;; esac diff --git a/d2ypp2/scripts/linux/wireguard_watch.sh b/d2ypp2/scripts/linux/wireguard_watch.sh index 91a4727..04cf4f8 100644 --- a/d2ypp2/scripts/linux/wireguard_watch.sh +++ b/d2ypp2/scripts/linux/wireguard_watch.sh @@ -68,6 +68,10 @@ restart_interface() { systemctl restart "wg-quick@${iface}.service" elif systemctl list-units "wg-quick@${iface}.service" --no-legend 2>/dev/null | grep -q .; then systemctl restart "wg-quick@${iface}.service" + elif command -v wg-quick >/dev/null 2>&1 && [ -f "/etc/wireguard/${iface}.conf" ]; then + log "WARN: $iface unit missing; falling back to wg-quick down/up" + wg-quick down "$iface" >/dev/null 2>&1 || true + wg-quick up "$iface" else log "WARN: $iface is stale but wg-quick@${iface}.service is not installed" return 1 diff --git a/d2ypp2/src/wireguard_config.py b/d2ypp2/src/wireguard_config.py index 16996e7..236a590 100644 --- a/d2ypp2/src/wireguard_config.py +++ b/d2ypp2/src/wireguard_config.py @@ -1491,4 +1491,20 @@ def apply_wireguard_config( result["applied"] = True result["system_path"] = str(target) result["status"] = wireguard_runtime_status(repo_root=repo_root) + # Best-effort: when Neko is already in remote mode, refresh NAT1TO1 to the WG address. + try: + env = _read_stack_env(repo_root) + mode = _normalize_neko_access_mode(env.get("NEKO_ACCESS_MODE") or "lan") + if mode == "remote": + result["neko_access"] = configure_neko_wireguard_access( + repo_root=repo_root, + mode="remote", + restart=False, + status=result.get("status") if isinstance(result.get("status"), dict) else None, + ) + except Exception as exc: # noqa: BLE001 - WireGuard apply already succeeded + result["neko_access_error"] = str(exc) + allow_script = Path("/usr/local/sbin/shellcrash-allow-trusted.sh") + if allow_script.is_file(): + _run_command(["bash", str(allow_script)], timeout=8.0) return result