This commit is contained in:
eric 2026-06-10 06:20:52 -05:00
parent e7618149c6
commit 20f3501f24
5 changed files with 215 additions and 31 deletions

View File

@ -1804,12 +1804,31 @@ 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'
}
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 <<NFT || true
insert rule inet shellcrash input ip saddr 172.16.0.0/12 tcp dport $port accept
NFT
fi
if ! nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr 172.16.0.0/12 udp dport $port accept"; then
nft -f - >/dev/null 2>&1 <<NFT || true
insert rule inet shellcrash input ip saddr 172.16.0.0/12 udp dport $port accept
NFT
fi
}
shellcrash_proxy_available() {
local port="$1"
[ "${ENABLE_SHELLCRASH:-1}" = "1" ] || return 1
if systemctl list-unit-files shellcrash.service --no-legend >/dev/null 2>&1 && ! systemctl is-active --quiet shellcrash.service; then
return 1
fi
ensure_shellcrash_docker_proxy_access "$port" >/dev/null 2>&1 || true
if have ss; then
ss -ltn 2>/dev/null | awk -v p=":$port" '
$4 ~ p"$" && $4 !~ /(^|:)127\.0\.0\.1:/ && $4 !~ /\[::1\]:/ { found=1 }
@ -1835,11 +1854,16 @@ resolve_neko_browser_proxy() {
return 0
;;
esac
if ! flag_enabled "$SHELLCRASH_ATTACH_WHEN_DIRECT_OK" && system_direct_global_ready; then
local port
port="$(shellcrash_mix_port)"
if shellcrash_proxy_available "$port"; then
printf 'http://host.docker.internal:%s' "$port"
return 0
fi
if system_direct_global_ready; then
return 0
fi
shellcrash_autostart_if_needed >/dev/null 2>&1 || true
local port
port="$(shellcrash_mix_port)"
if shellcrash_proxy_available "$port"; then
printf 'http://host.docker.internal:%s' "$port"

View File

@ -1146,12 +1146,31 @@ 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'
}
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 <<NFT || true
insert rule inet shellcrash input ip saddr 172.16.0.0/12 tcp dport $port accept
NFT
fi
if ! nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr 172.16.0.0/12 udp dport $port accept"; then
nft -f - >/dev/null 2>&1 <<NFT || true
insert rule inet shellcrash input ip saddr 172.16.0.0/12 udp dport $port accept
NFT
fi
}
shellcrash_proxy_available() {
local port="$1"
[ "${ENABLE_SHELLCRASH:-1}" = "1" ] || return 1
if unit_exists shellcrash.service && ! unit_active shellcrash.service; then
return 1
fi
ensure_shellcrash_docker_proxy_access "$port" >/dev/null 2>&1 || true
if have ss; then
ss -ltn 2>/dev/null | awk -v p=":$port" '
$4 ~ p"$" && $4 !~ /(^|:)127\.0\.0\.1:/ && $4 !~ /\[::1\]:/ { found=1 }
@ -1177,15 +1196,31 @@ resolve_neko_browser_proxy() {
return 0
;;
esac
if ! flag_enabled "$SHELLCRASH_ATTACH_WHEN_DIRECT_OK" && system_direct_global_ready; then
return 0
fi
shellcrash_autostart_if_needed >/dev/null 2>&1 || true
local port
port="$(shellcrash_mix_port)"
if shellcrash_proxy_available "$port"; then
printf 'http://host.docker.internal:%s' "$port"
return 0
fi
if system_direct_global_ready; then
return 0
fi
shellcrash_autostart_if_needed >/dev/null 2>&1 || true
port="$(shellcrash_mix_port)"
if shellcrash_proxy_available "$port"; then
printf 'http://host.docker.internal:%s' "$port"
fi
}
service_neko_reconfigure_for_shellcrash_change() {
case "$SHELLCRASH_ATTACH_NEKO" in
0|false|FALSE|off|OFF|none|NONE|direct|DIRECT)
return 0
;;
esac
have docker || return 0
docker ps --format '{{.Names}}' 2>/dev/null | grep -Eq '^live-neko-[0-9]+$' || return 0
service_neko_reconfigure >/dev/null 2>&1 || true
}
ensure_neko_browser_launcher_executable() {
@ -1468,6 +1503,8 @@ service_shellcrash_start() {
kv message "ShellCrash is not installed"
exit 1
fi
ensure_shellcrash_docker_proxy_access "$(shellcrash_mix_port)" >/dev/null 2>&1 || true
service_neko_reconfigure_for_shellcrash_change
kv message "ShellCrash started"
}
@ -1492,6 +1529,8 @@ service_shellcrash_restart() {
kv message "ShellCrash is not installed"
exit 1
fi
ensure_shellcrash_docker_proxy_access "$(shellcrash_mix_port)" >/dev/null 2>&1 || true
service_neko_reconfigure_for_shellcrash_change
kv message "ShellCrash restarted"
}
@ -1501,6 +1540,7 @@ service_shellcrash_stop() {
elif [ -x "$SHELLCRASH_DIR/start.sh" ]; then
"$SHELLCRASH_DIR/start.sh" stop
fi
service_neko_reconfigure_for_shellcrash_change
kv message "ShellCrash stopped"
}
@ -1620,6 +1660,8 @@ EOF
exit 1
fi
ensure_shellcrash_web_panel >/dev/null 2>&1 || true
ensure_shellcrash_docker_proxy_access "$(shellcrash_mix_port)" >/dev/null 2>&1 || true
service_neko_reconfigure_for_shellcrash_change
selected=""
if have python3; then
@ -2569,6 +2611,7 @@ render_frigate_usb_config() {
printf ' usb_%s:\n' "$cam"
printf ' enabled: true\n'
printf ' ffmpeg:\n'
printf ' hwaccel_args: []\n'
printf ' inputs:\n'
printf ' - path: "%s"\n' "$dev"
printf ' input_args:\n'
@ -2593,6 +2636,27 @@ render_frigate_usb_config() {
chmod a+rw "$cfg" 2>/dev/null || true
}
frigate_container_usb_paths_stale() {
local devices="$1" state dev
state="$(docker_container_status live-frigate || true)"
[[ "$state" == running* ]] || return 1
have docker || return 1
while IFS='|' read -r dev _name; do
[ -n "$dev" ] || continue
case "$dev" in
/dev/v4l/*)
[ -e "$dev" ] || continue
if ! docker exec live-frigate sh -c 'test -e "$1"' sh "$dev" >/dev/null 2>&1; then
return 0
fi
;;
esac
done <<EOF
$devices
EOF
return 1
}
ensure_frigate_default_config() {
mkdir -p "$FRIGATE_CONFIG_DIR" "$FRIGATE_MEDIA_DIR"
if [ ! -s "$FRIGATE_CONFIG_DIR/config.yml" ] && [ -s "$ROOT_DIR/services/frigate/config/config.yml" ]; then
@ -2668,6 +2732,11 @@ service_frigate_auto() {
render_frigate_usb_compose_override "$devices"
state="$(docker_container_status live-frigate || true)"
if [[ "$state" == running* ]]; then
if frigate_container_usb_paths_stale "$devices"; then
compose_frigate up -d --force-recreate
kv message "Frigate USB container mounts refreshed; usb_cameras=${count}"
return 0
fi
compose_frigate up -d
kv message "Frigate USB config applied; usb_cameras=${count}"
return 0

View File

@ -248,6 +248,8 @@ def clear_process_proxy_env(*, mode: str = "direct") -> None:
def shellcrash_policy() -> dict[str, Any]:
attach_neko = os.environ.get("SHELLCRASH_ATTACH_NEKO", "auto")
attach_ffmpeg = os.environ.get("SHELLCRASH_ATTACH_FFMPEG", "auto")
return {
"manual_only": _env_flag("SHELLCRASH_MANUAL_ONLY", True),
"autostart_allowed": _env_flag("SHELLCRASH_AUTOSTART_ON_DIRECT_FAIL", True),
@ -255,11 +257,22 @@ def shellcrash_policy() -> dict[str, Any]:
"configured_autostart": _env_flag("SHELLCRASH_AUTOSTART", False),
"auto_start_on_web_panel": _env_flag("SHELLCRASH_AUTO_START_ON_WEB_PANEL", False),
"attach_when_direct_ok": _env_flag("SHELLCRASH_ATTACH_WHEN_DIRECT_OK", False),
"attach_neko": os.environ.get("SHELLCRASH_ATTACH_NEKO", "auto"),
"attach_ffmpeg": os.environ.get("SHELLCRASH_ATTACH_FFMPEG", "auto"),
"attach_neko": attach_neko,
"attach_neko_mode": _attach_mode(attach_neko),
"attach_ffmpeg": attach_ffmpeg,
"attach_ffmpeg_mode": _attach_mode(attach_ffmpeg),
}
def _attach_mode(raw: str | None) -> str:
value = (raw or "auto").strip().lower()
if value in {"0", "false", "no", "off", "none", "direct", "disable", "disabled"}:
return "off"
if value in {"1", "true", "yes", "on", "enable", "enabled", "force", "forced", "proxy", "shellcrash"}:
return "force"
return "auto"
def system_network_probe(*, include_proxy: bool = True) -> dict[str, Any]:
direct = _probe_targets(None)
shell = shellcrash_runtime()
@ -270,8 +283,20 @@ def system_network_probe(*, include_proxy: bool = True) -> dict[str, Any]:
douyin_direct_ok = _douyin_ok(direct)
global_proxy_ok = _global_ok(proxied)
policy = shellcrash_policy()
attach_ffmpeg_mode = str(policy.get("attach_ffmpeg_mode") or "auto")
shellcrash_ready = bool(shell.get("running") and global_proxy_ok and attach_ffmpeg_mode != "off")
effective_mode = "failed"
network_profile = "pure_china"
if shellcrash_ready:
effective_mode = "shellcrash"
network_profile = "shellcrash_proxy"
elif global_direct_ok:
effective_mode = "direct"
network_profile = "soft_router"
recommendation = "direct"
if global_direct_ok:
if shellcrash_ready:
recommendation = "keep_shellcrash_attached"
elif global_direct_ok:
recommendation = "keep_shellcrash_stopped"
elif shell_cfg.get("available"):
recommendation = "autostart_shellcrash_from_config"
@ -281,10 +306,12 @@ def system_network_probe(*, include_proxy: bool = True) -> dict[str, Any]:
recommendation = "stage_shellcrash_config_before_youtube"
policy["recommendation"] = recommendation
return {
"ok": bool(global_direct_ok or global_proxy_ok),
"mode": "direct" if global_direct_ok else ("shellcrash" if global_proxy_ok else "failed"),
"ok": bool(global_direct_ok or shellcrash_ready),
"mode": effective_mode,
"network_profile": network_profile,
"direct_ready": bool(global_direct_ok),
"global_direct_ok": bool(global_direct_ok),
"global_proxy_ok": bool(global_proxy_ok),
"douyin_direct_ok": bool(douyin_direct_ok),
"direct": direct,
"proxied": proxied,
@ -294,7 +321,9 @@ def system_network_probe(*, include_proxy: bool = True) -> dict[str, Any]:
"shellcrash_policy": policy,
"platform": {"system": platform.system(), "machine": platform.machine()},
"summary": (
"Google/YouTube direct egress is available; ShellCrash should stay stopped by default"
"ShellCrash is running and its proxy reaches Google/YouTube; attach browser and FFmpeg to ShellCrash"
if shellcrash_ready
else "Google/YouTube direct egress is available; ShellCrash should stay stopped by default"
if global_direct_ok
else (
"Direct Google/YouTube egress failed; ShellCrash config is available for conditional autostart"
@ -311,34 +340,60 @@ def youtube_network_preflight() -> dict[str, Any]:
direct = _probe_targets(None)
policy = shellcrash_policy()
if _global_ok(direct):
clear_process_proxy_env(mode="direct")
return {
"ok": True,
"mode": "direct",
"message": "YouTube network preflight passed by direct Google/YouTube egress; ShellCrash not attached",
"direct": direct,
"shellcrash": shellcrash_runtime(),
"shellcrash_policy": policy,
"douyin_direct_ok": _douyin_ok(direct),
"platform": {"system": platform.system(), "machine": platform.machine()},
}
attach_ffmpeg_mode = str(policy.get("attach_ffmpeg_mode") or "auto")
global_direct_ok = _global_ok(direct)
shell = shellcrash_runtime()
autostart = {}
if not shell.get("running"):
autostart = autostart_shellcrash_if_configured()
if autostart.get("attempted"):
shell = shellcrash_runtime()
proxy = f"http://127.0.0.1:{shell.get('mixed_port') or '7890'}"
proxied = _probe_targets(proxy) if shell.get("running") else []
proxied = _probe_targets(proxy) if shell.get("running") and attach_ffmpeg_mode != "off" else []
if _global_ok(proxied):
env = _proxy_env(proxy)
apply_process_proxy_env(env)
return {
"ok": True,
"mode": "shellcrash",
"message": f"YouTube network preflight passed through manually running ShellCrash {proxy}",
"network_profile": "shellcrash_proxy",
"message": f"ShellCrash is running and proxy probe passed; FFmpeg attached to {proxy}",
"direct": direct,
"proxied": proxied,
"shellcrash": shell,
"shellcrash_autostart": {},
"shellcrash_policy": policy,
"proxy_env": env,
"douyin_direct_ok": _douyin_ok(direct),
"platform": {"system": platform.system(), "machine": platform.machine()},
}
if global_direct_ok and attach_ffmpeg_mode != "force":
clear_process_proxy_env(mode="direct")
return {
"ok": True,
"mode": "direct",
"network_profile": "soft_router",
"message": "YouTube network preflight passed by direct Google/YouTube egress; ShellCrash not attached",
"direct": direct,
"proxied": proxied,
"shellcrash": shell,
"shellcrash_policy": policy,
"douyin_direct_ok": _douyin_ok(direct),
"platform": {"system": platform.system(), "machine": platform.machine()},
}
autostart = {}
if attach_ffmpeg_mode != "off" and not shell.get("running"):
autostart = autostart_shellcrash_if_configured()
if autostart.get("attempted"):
shell = shellcrash_runtime()
proxy = f"http://127.0.0.1:{shell.get('mixed_port') or '7890'}"
proxied = _probe_targets(proxy) if shell.get("running") and attach_ffmpeg_mode != "off" else []
if _global_ok(proxied):
env = _proxy_env(proxy)
apply_process_proxy_env(env)
return {
"ok": True,
"mode": "shellcrash",
"network_profile": "shellcrash_proxy",
"message": f"YouTube network preflight passed through ShellCrash {proxy}",
"direct": direct,
"proxied": proxied,
"shellcrash": shell,
@ -358,6 +413,7 @@ def youtube_network_preflight() -> dict[str, Any]:
return {
"ok": False,
"mode": "failed",
"network_profile": "pure_china",
"message": reason,
"direct": direct,
"proxied": proxied,

View File

@ -214,6 +214,8 @@ class NekoConfigTests(unittest.TestCase):
self.assertIn("resolve_neko_browser_proxy()", script)
self.assertIn("shellcrash_mix_port()", script)
self.assertIn("shellcrash_proxy_available()", script)
self.assertIn("ensure_shellcrash_docker_proxy_access()", script)
self.assertIn("ip saddr 172.16.0.0/12 tcp dport", script)
self.assertIn('chmod +x "$launch"', script)
self.assertIn("copy_neko_tampermonkey_seed_dir()", script)
self.assertIn("copy_neko_tampermonkey_storage_dir()", script)
@ -230,6 +232,7 @@ class NekoConfigTests(unittest.TestCase):
self.assertIn("remove_unreachable_neko_containers", script)
self.assertIn("service_neko_restart()", service_ctl)
self.assertIn("service_neko_reconfigure", service_ctl)
self.assertIn("service_neko_reconfigure_for_shellcrash_change", service_ctl)
self.assertIn("neko_http_probe_code()", service_ctl)
self.assertIn("published=", service_ctl)

View File

@ -46,6 +46,37 @@ class NetworkPreflightTests(unittest.TestCase):
self.assertEqual(os.environ.get("LIVE_YOUTUBE_NETWORK_MODE"), "shellcrash")
self.assertIn(".douyin.com", os.environ.get("NO_PROXY", ""))
def test_running_shellcrash_proxy_wins_over_shellcrash_assisted_direct_probe(self) -> None:
def fake_probe(url: str, **kwargs):
proxy = kwargs.get("proxy")
return {"ok": True, "url": url, "proxy": proxy or "", "elapsed_ms": 5, "status": 204}
with patch.dict(os.environ, {}, clear=False):
with patch("src.network_preflight._probe_url", side_effect=fake_probe):
with patch("src.network_preflight.shellcrash_runtime", return_value={"running": True, "mixed_port": "7890"}):
result = network_preflight.youtube_network_preflight()
self.assertTrue(result["ok"])
self.assertEqual(result["mode"], "shellcrash")
self.assertEqual(result["network_profile"], "shellcrash_proxy")
self.assertEqual(os.environ.get("LIVE_YOUTUBE_NETWORK_MODE"), "shellcrash")
self.assertEqual(os.environ.get("HTTP_PROXY"), "http://127.0.0.1:7890")
def test_ffmpeg_attach_off_keeps_soft_router_direct_even_if_shellcrash_runs(self) -> None:
def fake_probe(url: str, **kwargs):
return {"ok": True, "url": url, "proxy": kwargs.get("proxy") or "", "elapsed_ms": 5, "status": 204}
with patch.dict(os.environ, {"SHELLCRASH_ATTACH_FFMPEG": "off"}, clear=False):
with patch("src.network_preflight._probe_url", side_effect=fake_probe):
with patch("src.network_preflight.shellcrash_runtime", return_value={"running": True, "mixed_port": "7890"}):
result = network_preflight.youtube_network_preflight()
self.assertTrue(result["ok"])
self.assertEqual(result["mode"], "direct")
self.assertEqual(result["network_profile"], "soft_router")
self.assertEqual(os.environ.get("LIVE_YOUTUBE_NETWORK_MODE"), "direct")
self.assertNotIn("HTTP_PROXY", os.environ)
def test_failed_preflight_keeps_shellcrash_manual(self) -> None:
def fake_probe(url: str, **kwargs):
return {"ok": False, "url": url, "elapsed_ms": 5, "error": "blocked"}
@ -111,6 +142,7 @@ class NetworkPreflightTests(unittest.TestCase):
self.assertIn("youtube", targets)
self.assertIn("douyin", targets)
self.assertEqual(result["mode"], "direct")
self.assertEqual(result["network_profile"], "soft_router")
self.assertEqual(result["shellcrash_policy"]["recommendation"], "keep_shellcrash_stopped")