diff --git a/d2ypp2/scripts/linux/service_ctl.sh b/d2ypp2/scripts/linux/service_ctl.sh index 95ef287..c58850f 100644 --- a/d2ypp2/scripts/linux/service_ctl.sh +++ b/d2ypp2/scripts/linux/service_ctl.sh @@ -1170,21 +1170,89 @@ neko_default_proxy_bypass() { printf '%s' '<-loopback>;localhost;127.0.0.1;::1;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*;*.local;host.docker.internal' } +# Host IP that Neko containers should use to reach ShellCrash listeners on the host. +# host.docker.internal often resolves to 172.17.0.1 even when docker0 is down and +# the compose network gateway is a different bridge address. +resolve_neko_docker_host_ip() { + local gw="" name network bridge + if ! have docker; then + printf 'host-gateway' + return 0 + fi + for name in live-neko-1 live-neko-2 live-neko-3; do + gw="$(docker inspect "$name" --format '{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}' 2>/dev/null | awk 'NF { print; exit }')" + [ -n "$gw" ] && break + done + if [ -z "$gw" ]; then + while read -r network; do + case "$network" in *neko*) + gw="$(docker network inspect "$network" --format '{{range .IPAM.Config}}{{.Gateway}}{{end}}' 2>/dev/null | head -1)" + [ -n "$gw" ] && break + ;; + esac + done < <(docker network ls --format '{{.Name}}' 2>/dev/null) + fi + if [ -z "$gw" ] && have ip; then + while read -r bridge; do + [ -n "$bridge" ] || continue + case "$bridge" in + wg*|wgeasy|wireguard*) + continue + ;; + docker0) + state="$(ip -o link show dev "$bridge" 2>/dev/null | awk '{print $9}')" + case "$state" in + DOWN|NO-CARRIER*) continue ;; + esac + ;; + esac + gw="$(ip -4 addr show dev "$bridge" 2>/dev/null | awk '/inet / { split($2, a, "/"); print a[1]; exit }')" + case "$gw" in + 172.1[6-9].*|172.2[0-9].*|172.3[0-1].*) [ -n "$gw" ] && break ;; + esac + gw="" + done < <(ip -o link show type bridge 2>/dev/null | awk -F': ' '{print $2}' | awk '{print $1}') + fi + if [ -n "$gw" ]; then + printf '%s' "$gw" + else + printf 'host-gateway' + fi +} + +neko_proxy_url_for_port() { + local port="$1" host_ip + [ -n "$port" ] || return 1 + host_ip="$(resolve_neko_docker_host_ip)" + if [ "$host_ip" = "host-gateway" ]; then + printf 'http://host.docker.internal:%s' "$port" + else + printf 'http://%s:%s' "$host_ip" "$port" + fi +} + ensure_shellcrash_docker_proxy_access() { local port="${1:-$(shellcrash_mix_port)}" [ "${ENABLE_SHELLCRASH:-1}" = "1" ] || return 0 [ -n "$port" ] || return 0 - have nft || return 0 - nft list chain inet shellcrash input >/dev/null 2>&1 || return 0 - if ! nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr 172.16.0.0/12 tcp dport $port accept"; then - nft -f - >/dev/null 2>&1 </dev/null 2>&1; then + if ! nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr 172.16.0.0/12 tcp dport $port accept"; then + nft -f - >/dev/null 2>&1 </dev/null | grep -Fq "ip saddr 172.16.0.0/12 udp dport $port accept"; then - nft -f - >/dev/null 2>&1 </dev/null | grep -Fq "ip saddr 172.16.0.0/12 udp dport $port accept"; then + nft -f - >/dev/null 2>&1 </dev/null || + iptables -I INPUT -s "$cidr" -p tcp --dport "$port" -j ACCEPT 2>/dev/null || true + done fi } @@ -1231,7 +1299,7 @@ resolve_neko_browser_proxy() { local port port="$(shellcrash_neko_proxy_port)" if shellcrash_proxy_available "$port"; then - printf 'http://host.docker.internal:%s' "$port" + neko_proxy_url_for_port "$port" return 0 fi if system_direct_global_ready; then @@ -1240,7 +1308,7 @@ resolve_neko_browser_proxy() { shellcrash_autostart_if_needed >/dev/null 2>&1 || true port="$(shellcrash_neko_proxy_port)" if shellcrash_proxy_available "$port"; then - printf 'http://host.docker.internal:%s' "$port" + neko_proxy_url_for_port "$port" fi } @@ -2132,6 +2200,9 @@ service_neko_start() { export_neko_env sync_neko_host_assets ensure_neko_browser_launcher_executable + if neko_shellcrash_proxy_enabled; then + ensure_shellcrash_proxy_ports_for_docker >/dev/null 2>&1 || true + fi for idx in $(seq 1 "$instance_count"); do prof="$(resolve_neko_profile_dir_for_instance "$idx")" seed_neko_tampermonkey_profile "$prof" "$(resolve_neko_profile_dir_for_instance 1)" @@ -2167,6 +2238,9 @@ service_neko_reconfigure() { instance_count="$(resolve_neko_instance_count)" export_neko_env sync_neko_host_assets + if neko_shellcrash_proxy_enabled; then + ensure_shellcrash_proxy_ports_for_docker >/dev/null 2>&1 || true + fi for idx in $(seq 1 "$instance_count"); do service_names="${service_names} neko${idx}" done @@ -2210,6 +2284,7 @@ export_neko_env() { export NEKO_MAX_FPS="${NEKO_MAX_FPS:-15}" export NEKO_VIDEO_BITRATE="${NEKO_VIDEO_BITRATE:-1200}" export NEKO_BROWSER_RENDER_MODE="${NEKO_BROWSER_RENDER_MODE:-auto}" + export NEKO_DOCKER_HOST_IP="$(resolve_neko_docker_host_ip)" export NEKO_BROWSER_PROXY="$(resolve_neko_browser_proxy)" export NEKO_BROWSER_PROXY_BYPASS="${NEKO_BROWSER_PROXY_BYPASS:-$(neko_default_proxy_bypass)}" export NEKO_MANAGED_POLICIES="${NEKO_MANAGED_POLICIES:-1}" diff --git a/d2ypp2/services/neko/browser-launch.sh b/d2ypp2/services/neko/browser-launch.sh index af1a342..1b4586f 100644 --- a/d2ypp2/services/neko/browser-launch.sh +++ b/d2ypp2/services/neko/browser-launch.sh @@ -257,12 +257,14 @@ if [ "$(id -u 2>/dev/null || echo 0)" = "0" ]; then fi proxy_flags="" +quic_flags="" case "${NEKO_BROWSER_PROXY:-}" in ""|0|false|FALSE|off|OFF|none|NONE|direct|DIRECT) ;; *) proxy_bypass="${NEKO_BROWSER_PROXY_BYPASS:-<-loopback>;localhost;127.0.0.1;::1;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*;*.local;host.docker.internal}" proxy_flags="--proxy-server=${NEKO_BROWSER_PROXY} --proxy-bypass-list=${proxy_bypass}" + quic_flags="--disable-quic" ;; esac @@ -289,6 +291,7 @@ launch_browser() { "$browser_bin" \ ${render_flags} \ ${proxy_flags} \ + ${quic_flags} \ ${extension_flags} \ ${sandbox_flags} \ --window-position=0,0 \ diff --git a/d2ypp2/src/wireguard_config.py b/d2ypp2/src/wireguard_config.py index 79260ab..260f5d4 100644 --- a/d2ypp2/src/wireguard_config.py +++ b/d2ypp2/src/wireguard_config.py @@ -1343,6 +1343,31 @@ def _env_flag_enabled(env: dict[str, str], key: str, *, default: bool = False) - return raw in {"1", "true", "yes", "on", "enable", "enabled"} +def _resolve_neko_docker_host_ip() -> str: + if platform.system().lower() != "linux" or not shutil.which("docker"): + return "" + try: + proc = subprocess.run( + [ + "docker", + "inspect", + "live-neko-1", + "--format", + "{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}", + ], + capture_output=True, + text=True, + timeout=4, + check=False, + ) + except (OSError, subprocess.TimeoutExpired): + return "" + gw = (proc.stdout or "").strip().split()[0] if (proc.stdout or "").strip() else "" + if gw.startswith("172."): + return gw + return "" + + def _neko_browser_proxy_status(repo_root: Path) -> dict[str, Any]: env = _read_stack_env(repo_root) enabled = _env_flag_enabled(env, "ENABLE_NEKO_BROWSER_PROXY", default=False) @@ -1350,17 +1375,22 @@ def _neko_browser_proxy_status(repo_root: Path) -> dict[str, Any]: port = str(env.get("NEKO_SHELLCRASH_PROXY_PORT") or "7894").strip() or "7894" node = str(env.get("SHELLCRASH_NEKO_PROXY_NODE") or "proxycheap").strip() or "proxycheap" attach = str(env.get("SHELLCRASH_ATTACH_NEKO") or "auto").strip() or "auto" + docker_host_ip = _resolve_neko_docker_host_ip() resolved = "" if enabled: if explicit and explicit.lower() not in {"0", "false", "off", "none", "direct"}: resolved = explicit elif attach.lower() not in {"0", "false", "off", "none", "direct"}: - resolved = f"http://host.docker.internal:{port}" + if docker_host_ip: + resolved = f"http://{docker_host_ip}:{port}" + else: + resolved = f"http://host.docker.internal:{port}" return { "enabled": enabled, "switch": "ENABLE_NEKO_BROWSER_PROXY", "explicit_proxy": explicit, "resolved_proxy": resolved, + "docker_host_ip": docker_host_ip, "shellcrash_port": port, "shellcrash_node": node, "attach_mode": attach, diff --git a/d2ypp2/tests/test_neko_config.py b/d2ypp2/tests/test_neko_config.py index ecbf548..a789f57 100644 --- a/d2ypp2/tests/test_neko_config.py +++ b/d2ypp2/tests/test_neko_config.py @@ -32,6 +32,7 @@ class NekoConfigTests(unittest.TestCase): self.assertIn("NEKO_BROWSER_RENDER_MODE", script) self.assertIn("NEKO_BROWSER_PROXY", script) self.assertIn("--proxy-server=${NEKO_BROWSER_PROXY}", script) + self.assertIn("--disable-quic", script) self.assertIn("direct|DIRECT", script) self.assertIn("DISPLAY=\"${DISPLAY:-${ENV_DISPLAY:-:99.0}}\"", script) self.assertIn(".local/share/icons/hicolor", script) @@ -174,6 +175,9 @@ class NekoConfigTests(unittest.TestCase): self.assertIn('ENV_DISPLAY: ":88.0"', rendered) self.assertIn('DISPLAY: ":88.0"', rendered) self.assertEqual(rendered.count('"host.docker.internal:host-gateway"'), 4) + with patch.dict(os.environ, {"NEKO_DOCKER_HOST_IP": "172.18.0.1"}, clear=False): + routed = module.render(2) + self.assertEqual(routed.count('"host.docker.internal:172.18.0.1"'), 2) self.assertEqual(rendered.count("apparmor:unconfined"), 4) self.assertIn('NEKO_MANAGED_POLICIES: "1"', rendered) self.assertIn('NEKO_TAMPERMONKEY_REPAIR: "1"', rendered) @@ -233,6 +237,9 @@ class NekoConfigTests(unittest.TestCase): self.assertIn(marker, script) service_ctl_markers = ( "shellcrash_mix_port()", + "resolve_neko_docker_host_ip()", + "neko_proxy_url_for_port()", + "NEKO_DOCKER_HOST_IP", "ensure_shellcrash_proxy_ports_for_docker()", "neko_shellcrash_proxy_enabled()", "SHELLCRASH_NEKO_PROXY_NODE", diff --git a/d2ypp2/tools/deploy_full_stream.py b/d2ypp2/tools/deploy_full_stream.py index 18a3fa5..4ff01e5 100644 --- a/d2ypp2/tools/deploy_full_stream.py +++ b/d2ypp2/tools/deploy_full_stream.py @@ -52,7 +52,6 @@ def run_post_config(ssh: paramiko.SSHClient, sftp: paramiko.SFTPClient, password env_updates = { "NEKO_SHELLCRASH_PROXY_PORT": "7894", "SHELLCRASH_NEKO_PROXY_NODE": "proxycheap", - "ENABLE_NEKO_BROWSER_PROXY": "0", } env_path = f"{REMOTE_PATH}/config/system-stack.env" try: diff --git a/d2ypp2/tools/render_neko_compose.py b/d2ypp2/tools/render_neko_compose.py index 7aa616b..bfb1ca9 100644 --- a/d2ypp2/tools/render_neko_compose.py +++ b/d2ypp2/tools/render_neko_compose.py @@ -69,6 +69,11 @@ def render(count: int) -> str: "172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;" "172.31.*;192.168.*;*.local;host.docker.internal", ) + docker_host_ip = env("NEKO_DOCKER_HOST_IP", "") + if docker_host_ip and docker_host_ip not in {"host-gateway", "host.docker.internal"}: + extra_hosts_line = f' - "host.docker.internal:{docker_host_ip}"' + else: + extra_hosts_line = ' - "host.docker.internal:host-gateway"' supervisor_conf = env("NEKO_BROWSER_SUPERVISOR_CONF", "chromium.conf") tampermonkey_repair = env("NEKO_TAMPERMONKEY_REPAIR", "1") live_ops_extension = env("NEKO_LIVE_OPS_EXTENSION", "1") @@ -111,7 +116,7 @@ def render(count: int) -> str: " security_opt:", " - apparmor:unconfined", " extra_hosts:", - ' - "host.docker.internal:host-gateway"', + f" {extra_hosts_line.strip()}", f" container_name: live-neko-{idx}", " environment:", f" DISPLAY: {q(display)}",