From 0317349eed5833eb31797fff6298ec8aca558fcf Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 7 Aug 2026 17:56:21 -0500 Subject: [PATCH] Harden install-time proxy for apt/Docker and WireGuard Neko remote mode. LAN Clash deploys were timing out on Neko image pulls because the Docker daemon ignored shell http_proxy; also keep NAT1TO1 on the overlay after wg-quick apply. Co-authored-by: Cursor --- d2ypp2/config/system-stack.env.example | 3 +- d2ypp2/docs/deploy-hardening.md | 19 ++++-- d2ypp2/install.sh | 10 ++++ d2ypp2/scripts/linux/lib/common.sh | 83 +++++++++++++++++++++++++- d2ypp2/src/wireguard_config.py | 9 ++- d2ypp2/tests/test_install_bootstrap.py | 38 ++++++++++++ 6 files changed, 151 insertions(+), 11 deletions(-) diff --git a/d2ypp2/config/system-stack.env.example b/d2ypp2/config/system-stack.env.example index 3db2ae8..164b0e7 100644 --- a/d2ypp2/config/system-stack.env.example +++ b/d2ypp2/config/system-stack.env.example @@ -47,7 +47,8 @@ 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. +# Leave empty for direct. When set, installer also writes apt + Docker daemon proxy drop-ins +# so image pulls work (Docker does not inherit shell http_proxy). Clear after install if desired. # 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 diff --git a/d2ypp2/docs/deploy-hardening.md b/d2ypp2/docs/deploy-hardening.md index 977442e..b57865a 100644 --- a/d2ypp2/docs/deploy-hardening.md +++ b/d2ypp2/docs/deploy-hardening.md @@ -28,23 +28,32 @@ Fix in tree: ## Install-time proxy -When the host cannot reach Docker Hub / GitHub / PyPI directly: +When the host cannot reach Docker Hub / GitHub / PyPI / Ubuntu archives directly (typical behind GFW with a LAN Clash on Windows): ```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 +export 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 +sudo -E bash ./install.sh --local-only --profile all +# optional: clear LIVE_* afterwards so runtime stays direct ``` -Installer downloads for ShellCrash try with proxy and without (`--noproxy` / unset env). +What the installer does when `LIVE_HTTP_PROXY` is set: + +1. Exports shell `http(s)_proxy` for curl / apt / pip / git helpers. +2. Writes `/etc/apt/apt.conf.d/99live-proxy` so `apt-get` keeps working in child processes. +3. Writes `/etc/systemd/system/docker.service.d/http-proxy.conf` and reloads Docker **before** Neko/image pulls (daemon does not inherit shell proxy). +4. On Neko `compose up` timeout, re-applies the Docker proxy drop-in and retries once. + +Installer downloads for ShellCrash also 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. +- If a WireGuard overlay IP is already present during Neko install, the installer switches to `remote` automatically. +- Applying a WG conf via the hub also switches Neko to remote when the overlay address is ready, and re-runs the ShellCrash trusted-CIDR hook. ## Ports to remember diff --git a/d2ypp2/install.sh b/d2ypp2/install.sh index f079b71..b8fd9bd 100755 --- a/d2ypp2/install.sh +++ b/d2ypp2/install.sh @@ -65,6 +65,7 @@ Options: Environment overrides: LIVE_REPO_URL, LIVE_REPO_REF, LIVE_REPO_SUBDIR, LIVE_INSTALL_DIR + LIVE_HTTP_PROXY, LIVE_HTTPS_PROXY, LIVE_NO_PROXY (install-time apt/Docker proxy) HOSTNAME_ALIAS, PORT and other config/system-stack.env values USAGE } @@ -344,6 +345,15 @@ install_profile() { LIVE_STACK_REPO_ROOT="$INSTALL_DIR" \ LIVE_INSTALL_LOG_FILE="$LOG_FILE" \ ASSUME_YES="$ASSUME_YES" \ + LIVE_HTTP_PROXY="${LIVE_HTTP_PROXY:-}" \ + LIVE_HTTPS_PROXY="${LIVE_HTTPS_PROXY:-}" \ + LIVE_NO_PROXY="${LIVE_NO_PROXY:-}" \ + http_proxy="${http_proxy:-${LIVE_HTTP_PROXY:-}}" \ + https_proxy="${https_proxy:-${LIVE_HTTPS_PROXY:-${LIVE_HTTP_PROXY:-}}}" \ + HTTP_PROXY="${HTTP_PROXY:-${LIVE_HTTP_PROXY:-}}" \ + HTTPS_PROXY="${HTTPS_PROXY:-${LIVE_HTTPS_PROXY:-${LIVE_HTTP_PROXY:-}}}" \ + no_proxy="${no_proxy:-${LIVE_NO_PROXY:-}}" \ + NO_PROXY="${NO_PROXY:-${LIVE_NO_PROXY:-}}" \ bash "$stack" "$PROFILE" } diff --git a/d2ypp2/scripts/linux/lib/common.sh b/d2ypp2/scripts/linux/lib/common.sh index 072fb7b..c481c74 100644 --- a/d2ypp2/scripts/linux/lib/common.sh +++ b/d2ypp2/scripts/linux/lib/common.sh @@ -1269,8 +1269,11 @@ install_docker_stack() { if ! need_docker; then return 0 fi + # Write proxy drop-in before first docker start when LIVE_HTTP_PROXY is set. + configure_docker_http_proxy if have docker; then systemctl enable --now docker >/dev/null 2>&1 || true + configure_docker_http_proxy usermod -aG docker live 2>/dev/null || true if docker compose version >/dev/null 2>&1; then return 0 @@ -1295,6 +1298,8 @@ install_docker_stack() { apt_install docker.io docker-compose || apt_install docker.io fi systemctl enable --now docker + # Re-apply after package install so the first image pull uses the proxy. + configure_docker_http_proxy if ! docker compose version >/dev/null 2>&1; then log "Docker installed without Compose v2 package; trying Compose v2 binary" install_docker_compose_plugin_binary || true @@ -1836,6 +1841,60 @@ shellcrash_cpu_core() { detect_cpu_arch } +configure_live_apt_proxy() { + # Persist apt proxy so apt-get works even when child processes drop shell env. + local conf="/etc/apt/apt.conf.d/99live-proxy" + local http_proxy_url="${LIVE_HTTP_PROXY:-}" + local https_proxy_url="${LIVE_HTTPS_PROXY:-${LIVE_HTTP_PROXY:-}}" + if [ -z "${http_proxy_url}${https_proxy_url}" ]; then + rm -f "$conf" 2>/dev/null || true + return 0 + fi + log "Configuring apt proxy via $conf" + cat >"$conf" </dev/null || true + if have systemctl && systemctl list-unit-files docker.service >/dev/null 2>&1; then + systemctl daemon-reload >/dev/null 2>&1 || true + systemctl try-restart docker >/dev/null 2>&1 || true + fi + fi + return 0 + fi + mkdir -p "$drop_dir" + cat >"$conf" </dev/null 2>&1; then + # Drop-in is ready for the first docker package install/start. + return 0 + fi + log "Configuring Docker daemon proxy via $conf" + systemctl daemon-reload >/dev/null 2>&1 || true + if systemctl is-active --quiet docker 2>/dev/null; then + systemctl restart docker >/dev/null 2>&1 || true + fi +} + apply_live_http_proxy_env() { # Only inject when explicitly configured; never invent a proxy. if [ -n "${LIVE_HTTP_PROXY:-}" ]; then @@ -1847,8 +1906,10 @@ apply_live_http_proxy_env() { 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="${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}" export NO_PROXY="$no_proxy" + configure_live_apt_proxy + configure_docker_http_proxy fi } @@ -1856,6 +1917,8 @@ 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="" + configure_live_apt_proxy + configure_docker_http_proxy } detect_wireguard_overlay_ip() { @@ -2495,6 +2558,16 @@ install_neko_browser() { export NEKO_WEBRTC_EPR_2="${NEKO_WEBRTC_EPR_2:-$NEKO_WEBRTC_UDP_RANGE_2}" export NEKO_WEBRTC_EPR_3="${NEKO_WEBRTC_EPR_3:-$NEKO_WEBRTC_UDP_RANGE_3}" export NEKO_ACCESS_MODE="${NEKO_ACCESS_MODE:-lan}" + # If WireGuard overlay is already up and the operator did not force lan, prefer remote NAT1TO1. + if [ "${NEKO_ACCESS_MODE}" = "lan" ]; then + local wg_ip="" + wg_ip="$(detect_wireguard_overlay_ip || true)" + if [ -n "$wg_ip" ]; then + log "WireGuard overlay detected ($wg_ip); switching Neko access mode to remote" + export NEKO_ACCESS_MODE=remote + export NEKO_WIREGUARD_HOST="${NEKO_WIREGUARD_HOST:-$wg_ip}" + fi + fi # Upstream n.eko images include STUN servers by default, and ICE Lite with ICE servers is unstable. # Keep full ICE on by default even if an older system-stack.env still contains NEKO_WEBRTC_ICELITE=1. if [ "${NEKO_FORCE_ICELITE:-0}" = "1" ]; then @@ -2532,9 +2605,15 @@ install_neko_browser() { remove_legacy_neko_container remove_unreachable_neko_containers "$instance_count" compose_file="$(render_neko_compose_file)" + # Ensure daemon proxy is active before the (often large) image pull. + configure_docker_http_proxy neko_timeout="${NEKO_INSTALL_TIMEOUT:-${DOCKER_OPTIONAL_INSTALL_TIMEOUT:-900}}" if ! compose_in_service_dir_timed "$neko_timeout" "$compose_file" up -d; then - log "WARN: Neko start timed out or failed after ${neko_timeout}s; continuing. Run: sudo bash $ROOT_DIR/scripts/linux/service_ctl.sh neko start" + log "WARN: Neko start timed out or failed after ${neko_timeout}s; re-applying Docker proxy and retrying once" + configure_docker_http_proxy + if ! compose_in_service_dir_timed "$neko_timeout" "$compose_file" up -d; then + log "WARN: Neko start still failed after retry; continuing. Run: sudo bash $ROOT_DIR/scripts/linux/service_ctl.sh neko start" + fi fi } diff --git a/d2ypp2/src/wireguard_config.py b/d2ypp2/src/wireguard_config.py index 236a590..1e6a321 100644 --- a/d2ypp2/src/wireguard_config.py +++ b/d2ypp2/src/wireguard_config.py @@ -1491,16 +1491,19 @@ 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. + # Applying WG means overlay access is intended: switch Neko to remote NAT1TO1 when ready. try: env = _read_stack_env(repo_root) mode = _normalize_neko_access_mode(env.get("NEKO_ACCESS_MODE") or "lan") - if mode == "remote": + status = result.get("status") if isinstance(result.get("status"), dict) else None + access = (status or {}).get("neko_access") if isinstance(status, dict) else None + remote_ready = bool(isinstance(access, dict) and access.get("remote_ready")) + if mode == "remote" or remote_ready: 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, + status=status, ) except Exception as exc: # noqa: BLE001 - WireGuard apply already succeeded result["neko_access_error"] = str(exc) diff --git a/d2ypp2/tests/test_install_bootstrap.py b/d2ypp2/tests/test_install_bootstrap.py index df8f4bd..5eb6f2d 100644 --- a/d2ypp2/tests/test_install_bootstrap.py +++ b/d2ypp2/tests/test_install_bootstrap.py @@ -201,6 +201,44 @@ class InstallCommonShellTests(unittest.TestCase): self.assertIn("WARN: hardware probe failed; continuing", body) self.assertIn("return 0", body) + def test_live_http_proxy_configures_apt_and_docker_daemon(self) -> None: + apt_body = self._function_body("configure_live_apt_proxy", "configure_docker_http_proxy") + docker_body = self._function_body("configure_docker_http_proxy", "apply_live_http_proxy_env") + apply_body = self._function_body("apply_live_http_proxy_env", "clear_live_http_proxy_env") + clear_body = self._function_body("clear_live_http_proxy_env", "detect_wireguard_overlay_ip") + self.assertIn("/etc/apt/apt.conf.d/99live-proxy", apt_body) + self.assertIn("Acquire::http::Proxy", apt_body) + self.assertIn('/etc/systemd/system/docker.service.d', docker_body) + self.assertIn('http-proxy.conf', docker_body) + self.assertIn('Environment="HTTP_PROXY=', docker_body) + self.assertIn("systemctl daemon-reload", docker_body) + self.assertIn("configure_live_apt_proxy", apply_body) + self.assertIn("configure_docker_http_proxy", apply_body) + self.assertIn("configure_live_apt_proxy", clear_body) + self.assertIn("configure_docker_http_proxy", clear_body) + + def test_docker_stack_reapplies_proxy_before_and_after_install(self) -> None: + body = self._function_body("install_docker_stack", "prepare_python_runtime") + self.assertGreaterEqual(body.count("configure_docker_http_proxy"), 3) + + def test_neko_install_retries_after_docker_proxy_reapply(self) -> None: + body = self._function_body("install_neko_browser", "install_redroid") + self.assertIn("configure_docker_http_proxy", body) + self.assertIn("re-applying Docker proxy and retrying once", body) + self.assertIn("switching Neko access mode to remote", body) + self.assertIn("detect_wireguard_overlay_ip", body) + + +class InstallBootstrapProxyPassThroughTests(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + cls.script = (ROOT / "install.sh").read_text(encoding="utf-8") + + def test_install_profile_forwards_live_http_proxy(self) -> None: + self.assertIn('LIVE_HTTP_PROXY="${LIVE_HTTP_PROXY:-}"', self.script) + self.assertIn('LIVE_HTTPS_PROXY="${LIVE_HTTPS_PROXY:-}"', self.script) + self.assertIn("LIVE_HTTP_PROXY, LIVE_HTTPS_PROXY, LIVE_NO_PROXY", self.script) + if __name__ == "__main__": unittest.main()