Strip removed global-client-fingerprint, force 0.0.0.0:9999, fetch GeoDB via jsdelivr when GitHub is blocked, raise start timeout, and allow 10.66.66.0/24 peers; also accept LAN Clash proxies for Neko. Co-authored-by: Cursor <cursoragent@cursor.com>
3764 lines
123 KiB
Bash
3764 lines
123 KiB
Bash
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
SERVICE_ID="${1:-}"
|
||
ACTION="${2:-status}"
|
||
ACTION_ARG1="${3:-}"
|
||
ACTION_ARG2="${4:-}"
|
||
|
||
ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
|
||
CONFIG_DIR="$ROOT_DIR/config"
|
||
STACK_ENV="$CONFIG_DIR/system-stack.env"
|
||
HW_ENV="$CONFIG_DIR/hardware.env"
|
||
SRS_COMPOSE="$ROOT_DIR/services/srs/docker-compose.yml"
|
||
HOMEPAGE_COMPOSE="$ROOT_DIR/services/homepage/docker-compose.yml"
|
||
FILEBROWSER_COMPOSE="$ROOT_DIR/services/filebrowser/docker-compose.yml"
|
||
NEKO_COMPOSE="$ROOT_DIR/services/neko/docker-compose.yml"
|
||
MITMPROXY_COMPOSE="$ROOT_DIR/services/mitmproxy/docker-compose.yml"
|
||
VSCODE_SERVER_COMPOSE="$ROOT_DIR/services/vscode-server/docker-compose.yml"
|
||
UPTIME_KUMA_COMPOSE="$ROOT_DIR/services/uptime-kuma/docker-compose.yml"
|
||
OPENCLAW_COMPOSE="$ROOT_DIR/services/openclaw/docker-compose.yml"
|
||
HOMEASSISTANT_COMPOSE="$ROOT_DIR/services/homeassistant/docker-compose.yml"
|
||
FRIGATE_COMPOSE="$ROOT_DIR/services/frigate/docker-compose.yml"
|
||
FRIGATE_USB_COMPOSE="$ROOT_DIR/services/frigate/docker-compose.usb.yml"
|
||
NTFY_COMPOSE="$ROOT_DIR/services/ntfy/docker-compose.yml"
|
||
COTURN_COMPOSE="$ROOT_DIR/services/coturn/docker-compose.yml"
|
||
BROWSER_LAUNCHER="$ROOT_DIR/scripts/linux/launch_browser.sh"
|
||
KMSCUBE_LAUNCHER="$ROOT_DIR/scripts/linux/run_kmscube.sh"
|
||
|
||
# SFTP/Windows editors often leave CR; sourcing then breaks ports as "9200\r".
|
||
if [ -f "$STACK_ENV" ]; then
|
||
sed -i 's/\r$//' "$STACK_ENV" 2>/dev/null || true
|
||
set -a && . "$STACK_ENV" && set +a
|
||
fi
|
||
if [ -f "$HW_ENV" ]; then
|
||
sed -i 's/\r$//' "$HW_ENV" 2>/dev/null || true
|
||
set -a && . "$HW_ENV" && set +a
|
||
fi
|
||
|
||
HOSTNAME_ALIAS="${HOSTNAME_ALIAS:-live}"
|
||
PORT="${PORT:-8001}"
|
||
WEBTTY_PORT="${WEBTTY_PORT:-7681}"
|
||
HOMEPAGE_PORT="${HOMEPAGE_PORT:-3080}"
|
||
FILEBROWSER_PORT="${FILEBROWSER_PORT:-8082}"
|
||
NETDATA_PORT="${NETDATA_PORT:-19999}"
|
||
SRS_HTTP_PORT="${SRS_HTTP_PORT:-8080}"
|
||
SRS_RTMP_PORT="${SRS_RTMP_PORT:-1935}"
|
||
SRS_API_PORT="${SRS_API_PORT:-1985}"
|
||
SRS_SRT_PORT="${SRS_SRT_PORT:-10080}"
|
||
SRS_WEBRTC_PORT="${SRS_WEBRTC_PORT:-8088}"
|
||
SHELLCRASH_DIR="${SHELLCRASH_DIR:-/etc/ShellCrash}"
|
||
SHELLCRASH_AUTOSTART="${SHELLCRASH_AUTOSTART:-0}"
|
||
SHELLCRASH_AUTOSTART_ON_DIRECT_FAIL="${SHELLCRASH_AUTOSTART_ON_DIRECT_FAIL:-1}"
|
||
SHELLCRASH_MANUAL_ONLY="${SHELLCRASH_MANUAL_ONLY:-1}"
|
||
SHELLCRASH_AUTO_START_ON_WEB_PANEL="${SHELLCRASH_AUTO_START_ON_WEB_PANEL:-0}"
|
||
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}"
|
||
NETWORK_PROBE_GOOGLE_URL="${NETWORK_PROBE_GOOGLE_URL:-https://www.google.com/generate_204}"
|
||
NETWORK_PROBE_YOUTUBE_URL="${NETWORK_PROBE_YOUTUBE_URL:-https://www.youtube.com/generate_204}"
|
||
NETWORK_PROBE_DOUYIN_URL="${NETWORK_PROBE_DOUYIN_URL:-https://live.douyin.com/}"
|
||
WEBTTY_USER="${WEBTTY_USER:-live}"
|
||
WEBTTY_PASS="${WEBTTY_PASS:-12345678}"
|
||
FILEBROWSER_USER="${FILEBROWSER_USER:-live}"
|
||
FILEBROWSER_PASS="${FILEBROWSER_PASS:-12345678}"
|
||
WIFI_CONNECTION_NAME="${WIFI_CONNECTION_NAME:-live-wifi}"
|
||
WIFI_SSID="${WIFI_SSID:-live}"
|
||
ANDROID_GATEWAY_NAME="${ANDROID_GATEWAY_NAME:-live-android-gateway}"
|
||
ANDROID_UPLINK_IF="${ANDROID_UPLINK_IF:-eth0}"
|
||
ANDROID_DOWNLINK_IF="${ANDROID_DOWNLINK_IF:-}"
|
||
ANDROID_GATEWAY_CIDR="${ANDROID_GATEWAY_CIDR:-192.168.51.1/24}"
|
||
ANDROID_PANEL_PORT="${ANDROID_PANEL_PORT:-5000}"
|
||
NEKO_HTTP_PORT="${NEKO_HTTP_PORT:-9200}"
|
||
NEKO_HTTP_PORT_1="${NEKO_HTTP_PORT_1:-$NEKO_HTTP_PORT}"
|
||
NEKO_HTTP_PORT_2="${NEKO_HTTP_PORT_2:-9201}"
|
||
NEKO_HTTP_PORT_3="${NEKO_HTTP_PORT_3:-9202}"
|
||
NEKO_INSTANCE_COUNT="${NEKO_INSTANCE_COUNT:-2}"
|
||
NEKO_CONTAINER_USER="${NEKO_CONTAINER_USER:-neko}"
|
||
NEKO_CONTAINER_HOME="${NEKO_CONTAINER_HOME:-/home/$NEKO_CONTAINER_USER}"
|
||
NEKO_CONTAINER_UID="${NEKO_CONTAINER_UID:-1000}"
|
||
NEKO_CONTAINER_GID="${NEKO_CONTAINER_GID:-1000}"
|
||
NEKO_DISPLAY="${NEKO_DISPLAY:-:99.0}"
|
||
NEKO_WEBRTC_TCPMUX="${NEKO_WEBRTC_TCPMUX:-52300}"
|
||
NEKO_WEBRTC_UDPMUX="${NEKO_WEBRTC_UDPMUX:-52300}"
|
||
NEKO_WEBRTC_TCPMUX_1="${NEKO_WEBRTC_TCPMUX_1:-$NEKO_WEBRTC_TCPMUX}"
|
||
NEKO_WEBRTC_TCPMUX_2="${NEKO_WEBRTC_TCPMUX_2:-52301}"
|
||
NEKO_WEBRTC_TCPMUX_3="${NEKO_WEBRTC_TCPMUX_3:-52302}"
|
||
NEKO_WEBRTC_UDPMUX_1="${NEKO_WEBRTC_UDPMUX_1:-$NEKO_WEBRTC_UDPMUX}"
|
||
NEKO_WEBRTC_UDPMUX_2="${NEKO_WEBRTC_UDPMUX_2:-52301}"
|
||
NEKO_WEBRTC_UDPMUX_3="${NEKO_WEBRTC_UDPMUX_3:-52302}"
|
||
# Default ends at .98: ShellCrash CrashCore has been observed binding UDP 52099.
|
||
NEKO_WEBRTC_UDP_RANGE="${NEKO_WEBRTC_UDP_RANGE:-52000-52098}"
|
||
NEKO_WEBRTC_UDP_RANGE_1="${NEKO_WEBRTC_UDP_RANGE_1:-$NEKO_WEBRTC_UDP_RANGE}"
|
||
NEKO_WEBRTC_UDP_RANGE_2="${NEKO_WEBRTC_UDP_RANGE_2:-52101-52199}"
|
||
NEKO_WEBRTC_UDP_RANGE_3="${NEKO_WEBRTC_UDP_RANGE_3:-52201-52299}"
|
||
NEKO_WEBRTC_EPR="${NEKO_WEBRTC_EPR:-$NEKO_WEBRTC_UDP_RANGE_1}"
|
||
NEKO_WEBRTC_EPR_1="${NEKO_WEBRTC_EPR_1:-$NEKO_WEBRTC_EPR}"
|
||
NEKO_WEBRTC_EPR_2="${NEKO_WEBRTC_EPR_2:-$NEKO_WEBRTC_UDP_RANGE_2}"
|
||
NEKO_WEBRTC_EPR_3="${NEKO_WEBRTC_EPR_3:-$NEKO_WEBRTC_UDP_RANGE_3}"
|
||
NEKO_ACCESS_MODE="${NEKO_ACCESS_MODE:-lan}"
|
||
NEKO_START_URL="${NEKO_START_URL:-https://accounts.google.com/ServiceLogin?continue=https://www.youtube.com/}"
|
||
NEKO_IMAGE_X86="${NEKO_IMAGE_X86:-ghcr.io/m1k1o/neko/google-chrome:3.0.0}"
|
||
NEKO_IMAGE_ARM64="${NEKO_IMAGE_ARM64:-ghcr.io/m1k1o/neko/chromium:3.0.0}"
|
||
case "$NEKO_IMAGE_X86" in
|
||
ghcr.io/m1k1o/neko/google-chrome:latest) NEKO_IMAGE_X86="ghcr.io/m1k1o/neko/google-chrome:3.0.0" ;;
|
||
esac
|
||
case "$NEKO_IMAGE_ARM64" in
|
||
ghcr.io/m1k1o/neko/chromium:latest) NEKO_IMAGE_ARM64="ghcr.io/m1k1o/neko/chromium:3.0.0" ;;
|
||
esac
|
||
NEKO_SHM_SIZE="${NEKO_SHM_SIZE:-512mb}"
|
||
NEKO_DESKTOP_SCREEN="${NEKO_DESKTOP_SCREEN:-1280x720@15}"
|
||
NEKO_MAX_FPS="${NEKO_MAX_FPS:-15}"
|
||
NEKO_VIDEO_BITRATE="${NEKO_VIDEO_BITRATE:-1200}"
|
||
NEKO_BROWSER_RENDER_MODE="${NEKO_BROWSER_RENDER_MODE:-auto}"
|
||
NEKO_TAMPERMONKEY_REPAIR="${NEKO_TAMPERMONKEY_REPAIR:-1}"
|
||
ENABLE_NEKO="${ENABLE_NEKO:-1}"
|
||
ENABLE_MITMPROXY="${ENABLE_MITMPROXY:-0}"
|
||
MITMPROXY_WEB_PORT="${MITMPROXY_WEB_PORT:-8086}"
|
||
MITMPROXY_PROXY_PORT="${MITMPROXY_PROXY_PORT:-8087}"
|
||
ENABLE_VSCODE_SERVER="${ENABLE_VSCODE_SERVER:-0}"
|
||
VSCODE_SERVER_PORT="${VSCODE_SERVER_PORT:-8440}"
|
||
VSCODE_SERVER_PASSWORD="${VSCODE_SERVER_PASSWORD:-12345678}"
|
||
ENABLE_UPTIME_KUMA="${ENABLE_UPTIME_KUMA:-0}"
|
||
UPTIME_KUMA_PORT="${UPTIME_KUMA_PORT:-3001}"
|
||
ENABLE_PULSEAUDIO="${ENABLE_PULSEAUDIO:-0}"
|
||
PULSEAUDIO_TCP_PORT="${PULSEAUDIO_TCP_PORT:-4713}"
|
||
ENABLE_KMSCUBE="${ENABLE_KMSCUBE:-1}"
|
||
KMSCUBE_DRM_DEVICE="${KMSCUBE_DRM_DEVICE:-/dev/dri/card0}"
|
||
ENABLE_OPENCLAW="${ENABLE_OPENCLAW:-0}"
|
||
OPENCLAW_IMAGE="${OPENCLAW_IMAGE:-ghcr.io/openclaw/openclaw:latest}"
|
||
OPENCLAW_GATEWAY_PORT="${OPENCLAW_GATEWAY_PORT:-18789}"
|
||
OPENCLAW_BRIDGE_PORT="${OPENCLAW_BRIDGE_PORT:-18790}"
|
||
OPENCLAW_GATEWAY_TOKEN="${OPENCLAW_GATEWAY_TOKEN:-12345678}"
|
||
OPENCLAW_ALLOW_INSECURE_PRIVATE_WS="${OPENCLAW_ALLOW_INSECURE_PRIVATE_WS:-1}"
|
||
OPENCLAW_CONFIG_DIR="${OPENCLAW_CONFIG_DIR:-/var/lib/live-openclaw/config}"
|
||
OPENCLAW_WORKSPACE_DIR="${OPENCLAW_WORKSPACE_DIR:-/var/lib/live-openclaw/workspace}"
|
||
ENABLE_HOMEASSISTANT="${ENABLE_HOMEASSISTANT:-0}"
|
||
HOMEASSISTANT_AUTOSTART="${HOMEASSISTANT_AUTOSTART:-0}"
|
||
HOMEASSISTANT_IMAGE="${HOMEASSISTANT_IMAGE:-ghcr.io/home-assistant/home-assistant:stable}"
|
||
HOMEASSISTANT_PORT="${HOMEASSISTANT_PORT:-8123}"
|
||
HOMEASSISTANT_CONFIG_DIR="${HOMEASSISTANT_CONFIG_DIR:-/var/lib/live-homeassistant/config}"
|
||
ENABLE_FRIGATE="${ENABLE_FRIGATE:-0}"
|
||
FRIGATE_USB_AUTOSTART="${FRIGATE_USB_AUTOSTART:-0}"
|
||
FRIGATE_USB_SCAN_INTERVAL="${FRIGATE_USB_SCAN_INTERVAL:-60}"
|
||
FRIGATE_IMAGE="${FRIGATE_IMAGE:-ghcr.io/blakeblackshear/frigate:stable}"
|
||
FRIGATE_WEB_PORT="${FRIGATE_WEB_PORT:-5001}"
|
||
FRIGATE_RTSP_PORT="${FRIGATE_RTSP_PORT:-8554}"
|
||
FRIGATE_WEBRTC_PORT="${FRIGATE_WEBRTC_PORT:-8555}"
|
||
FRIGATE_SHM_SIZE="${FRIGATE_SHM_SIZE:-256mb}"
|
||
FRIGATE_CONFIG_DIR="${FRIGATE_CONFIG_DIR:-/var/lib/live-frigate/config}"
|
||
FRIGATE_MEDIA_DIR="${FRIGATE_MEDIA_DIR:-/var/lib/live-frigate/media}"
|
||
FRIGATE_PRIVILEGED="${FRIGATE_PRIVILEGED:-true}"
|
||
ENABLE_WOL="${ENABLE_WOL:-1}"
|
||
WOL_BROADCAST="${WOL_BROADCAST:-255.255.255.255}"
|
||
WOL_PORT="${WOL_PORT:-9}"
|
||
WOL_PROBE_INTERVAL="${WOL_PROBE_INTERVAL:-30}"
|
||
WOL_PROBE_CIDR="${WOL_PROBE_CIDR:-}"
|
||
ENABLE_NTFY="${ENABLE_NTFY:-0}"
|
||
NTFY_IMAGE="${NTFY_IMAGE:-binwiederhier/ntfy:latest}"
|
||
NTFY_PORT="${NTFY_PORT:-8089}"
|
||
NTFY_CONFIG_DIR="${NTFY_CONFIG_DIR:-/var/lib/live-ntfy/config}"
|
||
NTFY_CACHE_DIR="${NTFY_CACHE_DIR:-/var/lib/live-ntfy/cache}"
|
||
export HOMEASSISTANT_IMAGE HOMEASSISTANT_PORT HOMEASSISTANT_CONFIG_DIR
|
||
export FRIGATE_IMAGE FRIGATE_WEB_PORT FRIGATE_RTSP_PORT FRIGATE_WEBRTC_PORT FRIGATE_SHM_SIZE FRIGATE_CONFIG_DIR FRIGATE_MEDIA_DIR FRIGATE_PRIVILEGED
|
||
export NTFY_IMAGE NTFY_PORT NTFY_CONFIG_DIR NTFY_CACHE_DIR
|
||
ENABLE_COTURN="${ENABLE_COTURN:-0}"
|
||
COTURN_IMAGE="${COTURN_IMAGE:-coturn/coturn:latest}"
|
||
COTURN_PORT="${COTURN_PORT:-3478}"
|
||
COTURN_TLS_PORT="${COTURN_TLS_PORT:-5349}"
|
||
COTURN_MIN_PORT="${COTURN_MIN_PORT:-49160}"
|
||
COTURN_MAX_PORT="${COTURN_MAX_PORT:-49200}"
|
||
COTURN_REALM="${COTURN_REALM:-live.local}"
|
||
COTURN_STATIC_AUTH_SECRET="${COTURN_STATIC_AUTH_SECRET:-}"
|
||
ENABLE_REDROID="${ENABLE_REDROID:-0}"
|
||
REDROID_DEFAULT_PORT="${REDROID_DEFAULT_PORT:-5555}"
|
||
REDROID_INSTANCE_PREFIX="${REDROID_INSTANCE_PREFIX:-redroid13}"
|
||
REDROID_DATA_PREFIX="${REDROID_DATA_PREFIX:-/var/lib/live-redroid}"
|
||
REDROID_GPU_MODE="${REDROID_GPU_MODE:-guest}"
|
||
REDROID_DEVICE_PROFILE="${REDROID_DEVICE_PROFILE:-pixel-7-pro}"
|
||
REDROID_BOOT_ARGS="${REDROID_BOOT_ARGS:-androidboot.redroid_magisk=1}"
|
||
REDROID_EXPECT_GMS="${REDROID_EXPECT_GMS:-1}"
|
||
REDROID_EXPECT_MAGISK="${REDROID_EXPECT_MAGISK:-1}"
|
||
REDROID_IMAGE_X86="${REDROID_IMAGE_X86:-fahaddz/redroid:13}"
|
||
REDROID_IMAGE_X86_ALT="${REDROID_IMAGE_X86_ALT:-fahaddz/redroid:13}"
|
||
REDROID_IMAGE_ARM64="${REDROID_IMAGE_ARM64:-fahaddz/redroid:13-arm-pi5}"
|
||
REDROID_IMAGE_ARM64_ALT="${REDROID_IMAGE_ARM64_ALT:-fahaddz/redroid:13-arm-pi5}"
|
||
if [ "$REDROID_IMAGE_ARM64" = "cnflysky/redroid-rk3588:lineage-20" ]; then
|
||
REDROID_IMAGE_ARM64="fahaddz/redroid:13-arm-pi5"
|
||
fi
|
||
if [ "$REDROID_IMAGE_ARM64_ALT" = "cnflysky/redroid-rk3588:lineage-20" ]; then
|
||
REDROID_IMAGE_ARM64_ALT="fahaddz/redroid:13-arm-pi5"
|
||
fi
|
||
if [ "${REDROID_IMAGE:-}" = "cnflysky/redroid-rk3588:lineage-20" ]; then
|
||
REDROID_IMAGE="fahaddz/redroid:13-arm-pi5"
|
||
fi
|
||
case " ${REDROID_BOOT_ARGS} " in
|
||
*" androidboot.redroid_gpu_mode="*)
|
||
;;
|
||
*)
|
||
REDROID_BOOT_ARGS="${REDROID_BOOT_ARGS} androidboot.redroid_gpu_mode=${REDROID_GPU_MODE:-guest}"
|
||
;;
|
||
esac
|
||
CONSOLE_BASE_URL="http://${HOSTNAME_ALIAS}.local:${PORT}"
|
||
|
||
kv() {
|
||
printf '%s=%s\n' "$1" "$2"
|
||
}
|
||
|
||
have() {
|
||
command -v "$1" >/dev/null 2>&1
|
||
}
|
||
|
||
flag_enabled() {
|
||
case "${1:-}" in
|
||
1|true|TRUE|yes|YES|on|ON|enable|enabled) return 0 ;;
|
||
*) return 1 ;;
|
||
esac
|
||
}
|
||
|
||
stack_env_upsert() {
|
||
local key="$1" value="$2"
|
||
[ -n "$key" ] || return 1
|
||
mkdir -p "$(dirname "$STACK_ENV")"
|
||
touch "$STACK_ENV"
|
||
sed -i 's/\r$//' "$STACK_ENV" 2>/dev/null || true
|
||
value="${value//$'\r'/}"
|
||
local prefix="${key}=" next_line="${key}=${value}"
|
||
local line kept="" found=0
|
||
: >"${STACK_ENV}.tmp"
|
||
while IFS= read -r line || [ -n "$line" ]; do
|
||
case "$line" in
|
||
"${prefix}"*)
|
||
if [ "$found" -eq 0 ]; then
|
||
printf '%s\n' "$next_line" >>"${STACK_ENV}.tmp"
|
||
found=1
|
||
fi
|
||
;;
|
||
*)
|
||
printf '%s\n' "$line" >>"${STACK_ENV}.tmp"
|
||
;;
|
||
esac
|
||
done <"$STACK_ENV"
|
||
if [ "$found" -eq 0 ]; then
|
||
printf '%s\n' "$next_line" >>"${STACK_ENV}.tmp"
|
||
fi
|
||
mv "${STACK_ENV}.tmp" "$STACK_ENV"
|
||
sed -i 's/\r$//' "$STACK_ENV" 2>/dev/null || true
|
||
}
|
||
|
||
neko_browser_proxy_auto_enable_if_needed() {
|
||
flag_enabled "${NEKO_BROWSER_PROXY_MANUAL:-0}" && return 0
|
||
flag_enabled "${ENABLE_NEKO_BROWSER_PROXY:-0}" && return 0
|
||
case "${SHELLCRASH_ATTACH_NEKO:-auto}" in
|
||
0|false|FALSE|off|OFF|none|NONE|direct|DIRECT) return 0 ;;
|
||
esac
|
||
system_direct_global_ready && return 0
|
||
local port
|
||
port="$(shellcrash_neko_proxy_port)"
|
||
if ! shellcrash_proxy_available "$port"; then
|
||
shellcrash_autostart_if_needed >/dev/null 2>&1 || true
|
||
port="$(shellcrash_neko_proxy_port)"
|
||
shellcrash_proxy_available "$port" || return 0
|
||
fi
|
||
stack_env_upsert ENABLE_NEKO_BROWSER_PROXY 1
|
||
export ENABLE_NEKO_BROWSER_PROXY=1
|
||
}
|
||
|
||
allow_tcp_port_if_firewall_active() {
|
||
local port="$1"
|
||
if have ufw && ufw status 2>/dev/null | grep -q '^Status: active'; then
|
||
ufw allow "${port}/tcp" >/dev/null 2>&1 || true
|
||
fi
|
||
}
|
||
|
||
allow_udp_port_if_firewall_active() {
|
||
local port="$1"
|
||
if have ufw && ufw status 2>/dev/null | grep -q '^Status: active'; then
|
||
ufw allow "${port}/udp" >/dev/null 2>&1 || true
|
||
fi
|
||
}
|
||
|
||
allow_udp_range_if_firewall_active() {
|
||
local port_range="$1"
|
||
local ufw_range="${port_range//-/:}"
|
||
if have ufw && ufw status 2>/dev/null | grep -q '^Status: active'; then
|
||
ufw allow "${ufw_range}/udp" >/dev/null 2>&1 || true
|
||
fi
|
||
}
|
||
|
||
prune_neko_profile() {
|
||
local prof="$1" owner
|
||
[ -n "$prof" ] || return 0
|
||
mkdir -p "$prof"
|
||
rm -rf \
|
||
"$prof/SingletonLock" \
|
||
"$prof/SingletonSocket" \
|
||
"$prof/SingletonCookie" \
|
||
"$prof/SingletonStartupLock" \
|
||
"$prof/lockfile" \
|
||
"$prof/DevToolsActivePort" \
|
||
"$prof/BrowserMetrics" \
|
||
"$prof/Crash Reports" \
|
||
"$prof/Crashpad" \
|
||
"$prof/ShaderCache" \
|
||
"$prof/GrShaderCache" \
|
||
"$prof/GraphiteDawnCache" \
|
||
"$prof/Default/Cache" \
|
||
"$prof/Default/Code Cache" \
|
||
"$prof/Default/GPUCache" \
|
||
"$prof/Default/DawnGraphiteCache" \
|
||
"$prof/Default/DawnWebGPUCache" \
|
||
2>/dev/null || true
|
||
mkdir -p \
|
||
"$prof/Default" \
|
||
"$prof/Crash Reports/pending" \
|
||
"$prof/Default/WebStorage/3/IndexedDB" \
|
||
"$prof/Default/IndexedDB" \
|
||
"$prof/Default/Service Worker/CacheStorage" \
|
||
"$prof/Default/Local Extension Settings" \
|
||
"$prof/Default/Managed Extension Settings" \
|
||
2>/dev/null || true
|
||
chown_neko_profile_tree "$prof"
|
||
chmod -R 0777 "$prof" 2>/dev/null || true
|
||
find "$prof" -type d -exec chmod 0777 {} \; 2>/dev/null || true
|
||
find "$prof" -type f -exec chmod 0666 {} \; 2>/dev/null || true
|
||
}
|
||
|
||
neko_profile_owner() {
|
||
printf '%s' "${NEKO_CONTAINER_UID:-1000}:${NEKO_CONTAINER_GID:-1000}"
|
||
}
|
||
|
||
chown_neko_profile_tree() {
|
||
local target="$1" owner
|
||
[ -n "$target" ] || return 0
|
||
owner="$(neko_profile_owner)"
|
||
if [ "$(id -u 2>/dev/null || echo 1)" = "0" ]; then
|
||
chown -R "$owner" "$target" 2>/dev/null || true
|
||
return 0
|
||
fi
|
||
chown -R "$owner" "$target" 2>/dev/null || true
|
||
}
|
||
|
||
prepare_neko_profile_dirs_for_compose() {
|
||
local idx instance_count prof
|
||
mkdir -p "$ROOT_DIR/services/neko/data"
|
||
chown_neko_profile_tree "$ROOT_DIR/services/neko/data"
|
||
chmod -R a+rwX "$ROOT_DIR/services/neko/data" 2>/dev/null || true
|
||
instance_count="$(resolve_neko_instance_count)"
|
||
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)"
|
||
prune_neko_profile "$prof"
|
||
done
|
||
}
|
||
|
||
filtered_pgrep() {
|
||
local pattern="$1"
|
||
pgrep -fa "$pattern" 2>/dev/null | grep -v -F "$0" | grep -v -F "pgrep -fa" || true
|
||
}
|
||
|
||
unit_exists() {
|
||
systemctl list-unit-files "$1" --no-legend 2>/dev/null | grep -q "^$1"
|
||
}
|
||
|
||
unit_active() {
|
||
systemctl is-active --quiet "$1"
|
||
}
|
||
|
||
compose_bin() {
|
||
if have docker && docker compose version >/dev/null 2>&1; then
|
||
echo "docker compose"
|
||
return 0
|
||
fi
|
||
if have docker-compose; then
|
||
echo "docker-compose"
|
||
return 0
|
||
fi
|
||
return 1
|
||
}
|
||
|
||
compose_run() {
|
||
local compose_file="$1"
|
||
shift
|
||
local cmd
|
||
cmd="$(compose_bin)" || return 127
|
||
if [ "$cmd" = "docker compose" ]; then
|
||
docker compose -f "$compose_file" "$@"
|
||
else
|
||
docker-compose -f "$compose_file" "$@"
|
||
fi
|
||
}
|
||
|
||
# docker-compose v1 将 ./卷 解析为当前工作目录,必须在 compose 文件所在目录执行(与 Neko 一致)
|
||
compose_in_service_dir() {
|
||
local compose_file="$1"
|
||
shift
|
||
local d base cmd
|
||
d="$(cd "$(dirname "$compose_file")" && pwd)"
|
||
base="$(basename "$compose_file")"
|
||
(
|
||
cd "$d" || exit 1
|
||
cmd="$(compose_bin)" || exit 1
|
||
if [ "$cmd" = "docker compose" ]; then
|
||
docker compose -f "$base" "$@"
|
||
else
|
||
docker-compose -f "$base" "$@"
|
||
fi
|
||
)
|
||
}
|
||
|
||
compose_frigate() {
|
||
local d base cmd
|
||
d="$(cd "$(dirname "$FRIGATE_COMPOSE")" && pwd)"
|
||
base="$(basename "$FRIGATE_COMPOSE")"
|
||
(
|
||
cd "$d" || exit 1
|
||
cmd="$(compose_bin)" || exit 1
|
||
if [ "$cmd" = "docker compose" ]; then
|
||
if [ -s "$FRIGATE_USB_COMPOSE" ]; then
|
||
docker compose -f "$base" -f "$FRIGATE_USB_COMPOSE" "$@"
|
||
else
|
||
docker compose -f "$base" "$@"
|
||
fi
|
||
else
|
||
if [ -s "$FRIGATE_USB_COMPOSE" ]; then
|
||
docker-compose -f "$base" -f "$FRIGATE_USB_COMPOSE" "$@"
|
||
else
|
||
docker-compose -f "$base" "$@"
|
||
fi
|
||
fi
|
||
)
|
||
}
|
||
|
||
docker_container_status() {
|
||
local name="$1"
|
||
local output
|
||
if ! have docker; then
|
||
return 1
|
||
fi
|
||
if ! output="$(docker ps -a --filter "name=^/${name}$" --format '{{.State}}|{{.Status}}' 2>&1)"; then
|
||
printf 'error=%s\n' "$(printf '%s' "$output" | tr '\n' ' ' | cut -c1-240)"
|
||
return 0
|
||
fi
|
||
printf '%s\n' "$output" | head -n 1
|
||
}
|
||
|
||
docker_container_env_value() {
|
||
local name="$1" key="$2"
|
||
if ! have docker; then
|
||
return 1
|
||
fi
|
||
docker inspect "$name" --format '{{range .Config.Env}}{{println .}}{{end}}' 2>/dev/null |
|
||
awk -v key="$key" 'index($0, key "=") == 1 { sub(/^[^=]*=/, ""); print; exit }'
|
||
}
|
||
|
||
docker_container_force_remove() {
|
||
local name="$1" pid
|
||
docker rm -f "$name" >/dev/null 2>&1 && return 0
|
||
pid="$(docker inspect -f '{{.State.Pid}}' "$name" 2>/dev/null || true)"
|
||
docker update --restart=no "$name" >/dev/null 2>&1 || true
|
||
case "$pid" in
|
||
""|0|*[!0-9]*)
|
||
;;
|
||
*)
|
||
kill -TERM "$pid" 2>/dev/null || true
|
||
sleep 2
|
||
if kill -0 "$pid" 2>/dev/null; then
|
||
kill -KILL "$pid" 2>/dev/null || true
|
||
fi
|
||
;;
|
||
esac
|
||
docker rm -f "$name" >/dev/null
|
||
}
|
||
|
||
resolve_neko_image() {
|
||
if [ -n "${NEKO_IMAGE:-}" ]; then
|
||
printf '%s' "$NEKO_IMAGE"
|
||
return 0
|
||
fi
|
||
case "$(uname -m)" in
|
||
x86_64|amd64)
|
||
printf '%s' "$NEKO_IMAGE_X86"
|
||
;;
|
||
aarch64|arm64)
|
||
printf '%s' "$NEKO_IMAGE_ARM64"
|
||
;;
|
||
*)
|
||
printf '%s' "$NEKO_IMAGE_ARM64"
|
||
;;
|
||
esac
|
||
}
|
||
|
||
resolve_neko_supervisor_conf() {
|
||
case "${NEKO_BROWSER_SUPERVISOR_CONF:-${NEKO_BROWSER_FAMILY:-}}" in
|
||
google-chrome|google-chrome.conf|chrome|chrome.conf)
|
||
printf '%s' "google-chrome.conf"
|
||
return 0
|
||
;;
|
||
chromium|chromium.conf)
|
||
printf '%s' "chromium.conf"
|
||
return 0
|
||
;;
|
||
esac
|
||
|
||
case "$(resolve_neko_image)" in
|
||
*google-chrome*)
|
||
printf '%s' "google-chrome.conf"
|
||
return 0
|
||
;;
|
||
*chromium*)
|
||
printf '%s' "chromium.conf"
|
||
return 0
|
||
;;
|
||
esac
|
||
|
||
case "$(uname -m)" in
|
||
x86_64|amd64)
|
||
printf '%s' "google-chrome.conf"
|
||
;;
|
||
aarch64|arm64)
|
||
printf '%s' "chromium.conf"
|
||
;;
|
||
*)
|
||
printf '%s' "chromium.conf"
|
||
;;
|
||
esac
|
||
}
|
||
|
||
resolve_neko_profile_dir() {
|
||
local family
|
||
family="$(resolve_neko_supervisor_conf)"
|
||
family="${family%.conf}"
|
||
printf '%s' "$ROOT_DIR/services/neko/data/${family}-profile-v2"
|
||
}
|
||
|
||
resolve_neko_profile_dir_for_instance() {
|
||
local idx="${1:-1}"
|
||
local base override
|
||
base="${NEKO_PROFILE_HOST_DIR:-$(resolve_neko_profile_dir)}"
|
||
eval "override=\"\${NEKO_PROFILE_HOST_DIR_${idx}:-}\""
|
||
if [ -n "$override" ]; then
|
||
printf '%s' "$override"
|
||
return 0
|
||
fi
|
||
case "$idx" in
|
||
1) printf '%s' "$base" ;;
|
||
*) printf '%s-%s' "$base" "$idx" ;;
|
||
esac
|
||
}
|
||
|
||
neko_managed_policies_enabled() {
|
||
case "${NEKO_MANAGED_POLICIES:-1}" in
|
||
1|true|TRUE|yes|YES|on|ON|enable|enabled)
|
||
return 0
|
||
;;
|
||
*)
|
||
return 1
|
||
;;
|
||
esac
|
||
}
|
||
|
||
copy_neko_tampermonkey_seed_dir() {
|
||
local src="${1:-}" dst="${2:-}" marker="${3:-}"
|
||
[ -e "$src" ] || return 0
|
||
if [ -e "$dst" ]; then
|
||
case "${NEKO_TAMPERMONKEY_REPAIR:-1}" in
|
||
1|true|TRUE|yes|YES|on|ON|enable|enabled)
|
||
;;
|
||
*)
|
||
if [ -z "$marker" ] || grep -R -a -q "$marker" "$dst" 2>/dev/null; then
|
||
return 0
|
||
fi
|
||
;;
|
||
esac
|
||
rm -rf "$dst" 2>/dev/null || true
|
||
fi
|
||
mkdir -p "$(dirname "$dst")"
|
||
cp -a "$src" "$dst" 2>/dev/null || true
|
||
rm -f "$dst/LOCK" 2>/dev/null || true
|
||
}
|
||
|
||
copy_neko_tampermonkey_storage_dir() {
|
||
local src="${1:-}" dst="${2:-}" marker="${3:-YouTube Studio Auto Dismiss}"
|
||
[ -d "$src" ] || return 0
|
||
grep -R -a -q "$marker" "$src" 2>/dev/null || return 0
|
||
if [ -e "$dst" ]; then
|
||
if grep -R -a -q "$marker" "$dst" 2>/dev/null; then
|
||
chmod -R 0777 "$dst" 2>/dev/null || true
|
||
return 0
|
||
fi
|
||
case "${NEKO_TAMPERMONKEY_REPAIR:-1}" in
|
||
1|true|TRUE|yes|YES|on|ON|enable|enabled)
|
||
rm -rf "$dst" 2>/dev/null || true
|
||
;;
|
||
*)
|
||
return 0
|
||
;;
|
||
esac
|
||
fi
|
||
mkdir -p "$(dirname "$dst")"
|
||
cp -a "$src" "$dst" 2>/dev/null || true
|
||
rm -f "$dst/LOCK" 2>/dev/null || true
|
||
chmod -R 0777 "$dst" 2>/dev/null || true
|
||
}
|
||
|
||
seed_neko_tampermonkey_profile() {
|
||
local target="${1:-}" donor="${2:-}" rel src dst bundled extension_donor storage_donor
|
||
[ -n "$target" ] || return 0
|
||
bundled="$ROOT_DIR/services/neko/tampermonkey-seed"
|
||
extension_donor="$donor"
|
||
storage_donor="$donor"
|
||
case "${NEKO_TAMPERMONKEY_REPAIR:-1}" in
|
||
1|true|TRUE|yes|YES|on|ON|enable|enabled)
|
||
if [ -d "$bundled/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo" ]; then
|
||
extension_donor="$bundled"
|
||
fi
|
||
;;
|
||
esac
|
||
if [ -z "$extension_donor" ] || [ ! -d "$extension_donor/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo" ]; then
|
||
extension_donor="$(find_neko_tampermonkey_seed_profile "$target")"
|
||
fi
|
||
if [ -z "$storage_donor" ] || ! grep -R -a -q "YouTube Studio Auto Dismiss" "$storage_donor/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo" 2>/dev/null; then
|
||
storage_donor="$(find_neko_tampermonkey_seed_profile "$target")"
|
||
fi
|
||
[ "$target" = "$extension_donor" ] && [ "$target" = "$storage_donor" ] && return 0
|
||
[ -d "$extension_donor/Default" ] || return 0
|
||
mkdir -p "$target/Default"
|
||
rel="Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
||
src="$extension_donor/Default/$rel"
|
||
dst="$target/Default/$rel"
|
||
copy_neko_tampermonkey_seed_dir "$src" "$dst" "Tampermonkey"
|
||
rel="Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
||
if [ -n "$storage_donor" ] && [ "$target" != "$storage_donor" ]; then
|
||
copy_neko_tampermonkey_storage_dir "$storage_donor/Default/$rel" "$target/Default/$rel" "YouTube Studio Auto Dismiss"
|
||
fi
|
||
rm -rf "$target/Default/Managed Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo" 2>/dev/null || true
|
||
sync_neko_tampermonkey_preferences "$target" "$extension_donor"
|
||
}
|
||
|
||
find_neko_tampermonkey_seed_profile() {
|
||
local target="${1:-}" base data_dir bundled cand
|
||
if [ -n "${NEKO_TAMPERMONKEY_SEED_PROFILE:-}" ] && [ -d "$NEKO_TAMPERMONKEY_SEED_PROFILE/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo" ]; then
|
||
printf '%s' "$NEKO_TAMPERMONKEY_SEED_PROFILE"
|
||
return 0
|
||
fi
|
||
base="$(resolve_neko_profile_dir_for_instance 1)"
|
||
data_dir="$ROOT_DIR/services/neko/data"
|
||
bundled="$ROOT_DIR/services/neko/tampermonkey-seed"
|
||
for cand in "$base" "$base"-* "$data_dir"/*profile-v2 "$data_dir"/*profile-v2-*; do
|
||
[ -n "$cand" ] || continue
|
||
[ "$cand" != "$target" ] || continue
|
||
[ -d "$cand/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo" ] || continue
|
||
grep -R -a -q "YouTube Studio Auto Dismiss" "$cand/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo" 2>/dev/null || continue
|
||
printf '%s' "$cand"
|
||
return 0
|
||
done
|
||
if [ -d "$bundled/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo" ]; then
|
||
printf '%s' "$bundled"
|
||
return 0
|
||
fi
|
||
}
|
||
|
||
sync_neko_tampermonkey_preferences() {
|
||
local target="${1:-}" donor="${2:-}"
|
||
[ -n "$target" ] && [ -n "$donor" ] || return 0
|
||
command -v python3 >/dev/null 2>&1 || return 0
|
||
DONOR_PROFILE="$donor" TARGET_PROFILE="$target" python3 - <<'PY'
|
||
import json
|
||
import os
|
||
from pathlib import Path
|
||
|
||
ext_id = "dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
||
donor_pref = Path(os.environ["DONOR_PROFILE"]) / "Default" / "Preferences"
|
||
target_pref = Path(os.environ["TARGET_PROFILE"]) / "Default" / "Preferences"
|
||
|
||
def load(path: Path) -> dict:
|
||
if not path.exists():
|
||
return {}
|
||
try:
|
||
return json.loads(path.read_text(encoding="utf-8"))
|
||
except Exception:
|
||
return {}
|
||
|
||
donor = load(donor_pref)
|
||
source = donor.get("extensions", {}).get("settings", {}).get(ext_id)
|
||
if not isinstance(source, dict):
|
||
raise SystemExit(0)
|
||
target = load(target_pref)
|
||
settings = target.setdefault("extensions", {}).setdefault("settings", {})
|
||
|
||
def should_replace_current(value: object) -> bool:
|
||
if not isinstance(value, dict):
|
||
return True
|
||
if value.get("state") != 1:
|
||
return True
|
||
if value.get("from_webstore") is not True:
|
||
return True
|
||
path = str(value.get("path") or "")
|
||
if path.startswith("/"):
|
||
return True
|
||
if value.get("location") == 8:
|
||
return True
|
||
return False
|
||
|
||
if should_replace_current(settings.get(ext_id)):
|
||
settings[ext_id] = source
|
||
target_pref.parent.mkdir(parents=True, exist_ok=True)
|
||
target_pref.write_text(json.dumps(target, ensure_ascii=False, separators=(",", ":")), encoding="utf-8")
|
||
PY
|
||
}
|
||
|
||
resolve_neko_instance_count() {
|
||
local requested="${NEKO_INSTANCE_COUNT:-2}"
|
||
local cpu count max_count
|
||
max_count="${NEKO_MAX_INSTANCE_COUNT:-64}"
|
||
case "$max_count" in
|
||
''|*[!0-9]*) max_count=64 ;;
|
||
esac
|
||
[ "$max_count" -lt 1 ] && max_count=1
|
||
[ "$max_count" -gt 64 ] && max_count=64
|
||
cpu="$(nproc 2>/dev/null || echo 4)"
|
||
case "$requested" in
|
||
auto|"")
|
||
if [ "$cpu" -le 4 ]; then
|
||
count=2
|
||
else
|
||
count=3
|
||
fi
|
||
;;
|
||
*[!0-9]*) count=2 ;;
|
||
*)
|
||
count="$requested"
|
||
;;
|
||
esac
|
||
if [ "$count" -lt 1 ]; then
|
||
count=1
|
||
fi
|
||
if [ "$count" -gt "$max_count" ]; then
|
||
count="$max_count"
|
||
fi
|
||
printf '%s' "$count"
|
||
}
|
||
|
||
resolve_neko_http_port_for_instance() {
|
||
local idx="${1:-1}" value base
|
||
eval "value=\"\${NEKO_HTTP_PORT_${idx}:-}\""
|
||
if [ -n "$value" ]; then
|
||
printf '%s' "$value"
|
||
return 0
|
||
fi
|
||
base="${NEKO_HTTP_PORT:-9200}"
|
||
printf '%s' $((base + idx - 1))
|
||
}
|
||
|
||
resolve_neko_tcpmux_for_instance() {
|
||
local idx="${1:-1}" value base
|
||
eval "value=\"\${NEKO_WEBRTC_TCPMUX_${idx}:-}\""
|
||
if [ -n "$value" ]; then
|
||
printf '%s' "$value"
|
||
return 0
|
||
fi
|
||
base="${NEKO_WEBRTC_TCPMUX:-52300}"
|
||
printf '%s' $((base + idx - 1))
|
||
}
|
||
|
||
resolve_neko_udpmux_for_instance() {
|
||
local idx="${1:-1}" value base
|
||
eval "value=\"\${NEKO_WEBRTC_UDPMUX_${idx}:-}\""
|
||
if [ -n "$value" ]; then
|
||
printf '%s' "$value"
|
||
return 0
|
||
fi
|
||
base="${NEKO_WEBRTC_UDPMUX:-52300}"
|
||
printf '%s' $((base + idx - 1))
|
||
}
|
||
|
||
resolve_neko_udp_range_for_instance() {
|
||
local idx="${1:-1}" value start
|
||
eval "value=\"\${NEKO_WEBRTC_UDP_RANGE_${idx}:-}\""
|
||
if [ -n "$value" ]; then
|
||
printf '%s' "$value"
|
||
return 0
|
||
fi
|
||
if [ "$idx" -eq 1 ] && [ -n "${NEKO_WEBRTC_UDP_RANGE:-}" ]; then
|
||
printf '%s' "$NEKO_WEBRTC_UDP_RANGE"
|
||
return 0
|
||
fi
|
||
case "$idx" in
|
||
2) start=52101 ;;
|
||
3) start=52201 ;;
|
||
*) start=$((52400 + ((idx - 4) * 100))) ;;
|
||
esac
|
||
[ "$idx" -eq 1 ] && start=52000
|
||
printf '%s-%s' "$start" "$((start + 99))"
|
||
}
|
||
|
||
render_neko_compose_file() {
|
||
local count out
|
||
count="$(resolve_neko_instance_count)"
|
||
out="$ROOT_DIR/services/neko/docker-compose.generated.yml"
|
||
# Autofix skips CrashCore/foreign UDP holders inside ICE ranges and strips CRLF in stack env.
|
||
python3 "$ROOT_DIR/tools/render_neko_compose.py" \
|
||
--count "$count" \
|
||
--output "$out" \
|
||
--stack-env "$STACK_ENV"
|
||
# Reload ranges if renderer persisted changes.
|
||
if [ -f "$STACK_ENV" ]; then
|
||
sed -i 's/\r$//' "$STACK_ENV" 2>/dev/null || true
|
||
set -a && . "$STACK_ENV" && set +a
|
||
fi
|
||
printf '%s' "$out"
|
||
}
|
||
|
||
sync_neko_host_assets() {
|
||
local dst="${NEKO_HOST_ASSET_DIR:-}" src item
|
||
[ -n "$dst" ] || return 0
|
||
case "$dst" in
|
||
/|/home/eric/neko|/home/eric/neko-*|/home/eric/neko/*|"$ROOT_DIR"|"$ROOT_DIR"/*)
|
||
kv message "Refusing unsafe NEKO_HOST_ASSET_DIR=$dst"
|
||
exit 1
|
||
;;
|
||
esac
|
||
src="$ROOT_DIR/services/neko"
|
||
mkdir -p "$dst"
|
||
for item in external-extensions live-ops-extension no-managed-policies policies provisioning tampermonkey-seed; do
|
||
if [ -d "$src/$item" ]; then
|
||
mkdir -p "$dst/$item"
|
||
if have rsync; then
|
||
rsync -a --delete "$src/$item/" "$dst/$item/"
|
||
else
|
||
cp -a "$src/$item/." "$dst/$item/"
|
||
fi
|
||
fi
|
||
done
|
||
for item in browser-launch.sh chromium.conf google-chrome.conf; do
|
||
[ -f "$src/$item" ] || continue
|
||
install -m 0644 "$src/$item" "$dst/$item"
|
||
done
|
||
chmod +x "$dst/browser-launch.sh" 2>/dev/null || true
|
||
chmod -R a+rX "$dst" 2>/dev/null || true
|
||
}
|
||
|
||
remove_legacy_neko_container() {
|
||
if have docker && docker ps -a --format '{{.Names}}' 2>/dev/null | grep -qx 'live-neko-chromium'; then
|
||
docker stop live-neko-chromium >/dev/null 2>&1 || true
|
||
docker rm live-neko-chromium >/dev/null 2>&1 || true
|
||
fi
|
||
}
|
||
|
||
remove_neko_recreate_conflicts() {
|
||
local count="${1:-}" idx name
|
||
[ -n "$count" ] || count="$(resolve_neko_instance_count)"
|
||
have docker || return 0
|
||
for idx in $(seq 1 "$count"); do
|
||
name="live-neko-$idx"
|
||
docker ps -a --format '{{.Names}}' 2>/dev/null |
|
||
awk -v name="$name" '$0 == name || $0 ~ "^[0-9a-f]+_" name "$" { print }' |
|
||
while read -r conflict; do
|
||
[ -n "$conflict" ] || continue
|
||
docker_container_force_remove "$conflict"
|
||
done
|
||
done
|
||
}
|
||
|
||
docker_container_network_attached() {
|
||
local name="$1"
|
||
[ -n "$name" ] || return 1
|
||
docker inspect -f '{{if .NetworkSettings.Networks}}1{{else}}0{{end}}' "$name" 2>/dev/null | grep -qx '1'
|
||
}
|
||
|
||
docker_container_http_port_published() {
|
||
local name="$1" port="$2"
|
||
[ -n "$name" ] && [ -n "$port" ] || return 1
|
||
docker port "$name" 8080/tcp 2>/dev/null |
|
||
awk -F: -v port="$port" 'NF && $NF == port { found = 1 } END { exit(found ? 0 : 1) }'
|
||
}
|
||
|
||
neko_http_probe_code() {
|
||
local port="$1" code
|
||
if have curl; then
|
||
code="$(curl -kfsS -m 3 -o /dev/null -w '%{http_code}' "http://127.0.0.1:${port}/" 2>/dev/null || true)"
|
||
printf '%s' "${code:-000}"
|
||
return 0
|
||
fi
|
||
if have wget; then
|
||
if wget -q -O /dev/null --timeout=3 "http://127.0.0.1:${port}/" 2>/dev/null; then
|
||
printf '200'
|
||
else
|
||
printf '000'
|
||
fi
|
||
return 0
|
||
fi
|
||
printf 'skip'
|
||
}
|
||
|
||
neko_http_code_ok() {
|
||
case "${1:-}" in
|
||
2*|3*|skip) return 0 ;;
|
||
*) return 1 ;;
|
||
esac
|
||
}
|
||
|
||
remove_unreachable_neko_containers() {
|
||
local count="${1:-}" idx name port state
|
||
[ -n "$count" ] || count="$(resolve_neko_instance_count)"
|
||
have docker || return 0
|
||
for idx in $(seq 1 "$count"); do
|
||
name="live-neko-$idx"
|
||
port="$(resolve_neko_http_port_for_instance "$idx")"
|
||
state="$(docker_container_status "$name" || true)"
|
||
[ -n "$state" ] || continue
|
||
if echo "$state" | grep -Eqi 'running|created|restarting|dead'; then
|
||
if ! docker_container_network_attached "$name" || ! docker_container_http_port_published "$name" "$port"; then
|
||
docker_container_force_remove "$name"
|
||
fi
|
||
fi
|
||
done
|
||
}
|
||
|
||
is_physical_lan_iface_name() {
|
||
local name="${1:-}"
|
||
case "$name" in
|
||
""|lo|docker*|br-*|veth*|virbr*|tun*|tap*|zt*|tailscale*|wg*|wireguard*|sit*|dummy*|p2p-dev-*|mon.*)
|
||
return 1
|
||
;;
|
||
esac
|
||
return 0
|
||
}
|
||
|
||
detect_preferred_lan_ip() {
|
||
local route src iface candidate path name
|
||
|
||
route="$(ip -4 route get 1.1.1.1 2>/dev/null || true)"
|
||
src="$(printf '%s\n' "$route" | awk '{for (i=1; i<=NF; i++) if ($i=="src") {print $(i+1); exit}}')"
|
||
iface="$(printf '%s\n' "$route" | awk '{for (i=1; i<=NF; i++) if ($i=="dev") {print $(i+1); exit}}')"
|
||
if [ -n "$src" ] && is_physical_lan_iface_name "$iface"; then
|
||
printf '%s\n' "$src"
|
||
return 0
|
||
fi
|
||
|
||
iface="$(ip route show default 2>/dev/null | awk 'NR==1 {print $5}')"
|
||
if is_physical_lan_iface_name "$iface"; then
|
||
src="$(ip -4 addr show dev "$iface" 2>/dev/null | awk '$1=="inet" {sub(/\/.*/, "", $2); print $2; exit}')"
|
||
if [ -n "$src" ]; then
|
||
printf '%s\n' "$src"
|
||
return 0
|
||
fi
|
||
fi
|
||
|
||
for path in /sys/class/net/*; do
|
||
[ -e "$path" ] || continue
|
||
name="${path##*/}"
|
||
is_physical_lan_iface_name "$name" || continue
|
||
candidate="$(ip -4 addr show dev "$name" 2>/dev/null | awk '$1=="inet" {sub(/\/.*/, "", $2); print $2; exit}')"
|
||
if [ -n "$candidate" ]; then
|
||
printf '%s\n' "$candidate"
|
||
return 0
|
||
fi
|
||
done
|
||
|
||
hostname -I 2>/dev/null | tr ' ' '\n' | grep -E '^[0-9]+\.' | grep -Ev '^(127\.|169\.254\.)' | head -n 1
|
||
}
|
||
|
||
sync_mdns_hosts_entry() {
|
||
local preferred_lan_ip
|
||
preferred_lan_ip="$(detect_preferred_lan_ip || true)"
|
||
python3 - <<PY
|
||
from pathlib import Path
|
||
|
||
hosts = Path("/etc/hosts")
|
||
host_alias = "${HOSTNAME_ALIAS}"
|
||
preferred_lan_ip = "${preferred_lan_ip}".strip()
|
||
host_lines = hosts.read_text(encoding="utf-8").splitlines()
|
||
next_host_lines = []
|
||
loopback_written = False
|
||
lan_written = False
|
||
|
||
for raw in host_lines:
|
||
stripped = raw.strip()
|
||
if not stripped or stripped.startswith("#"):
|
||
next_host_lines.append(raw)
|
||
continue
|
||
parts = raw.split()
|
||
names = parts[1:]
|
||
if parts[0] == "127.0.1.1":
|
||
next_host_lines.append(f"127.0.1.1\t{host_alias}")
|
||
loopback_written = True
|
||
continue
|
||
if f"{host_alias}.local" in names:
|
||
if preferred_lan_ip and not lan_written:
|
||
next_host_lines.append(f"{preferred_lan_ip}\t{host_alias}.local")
|
||
lan_written = True
|
||
continue
|
||
next_host_lines.append(raw)
|
||
|
||
if not loopback_written:
|
||
next_host_lines.append(f"127.0.1.1\t{host_alias}")
|
||
if preferred_lan_ip and not lan_written:
|
||
next_host_lines.append(f"{preferred_lan_ip}\t{host_alias}.local")
|
||
|
||
hosts.write_text("\\n".join(next_host_lines) + "\\n", encoding="utf-8")
|
||
PY
|
||
}
|
||
|
||
neko_nat1to1_iface_for_ip() {
|
||
local candidate="$1"
|
||
[ -n "$candidate" ] || return 1
|
||
ip -4 addr show 2>/dev/null | awk -v want="$candidate" '
|
||
/^[0-9]+: / {
|
||
iface=$2
|
||
sub(/:$/, "", iface)
|
||
}
|
||
$1=="inet" {
|
||
ip=$2
|
||
sub(/\/.*/, "", ip)
|
||
if (ip==want) {
|
||
print iface
|
||
exit
|
||
}
|
||
}
|
||
'
|
||
}
|
||
|
||
neko_nat1to1_is_overlay_iface() {
|
||
local iface="${1:-}"
|
||
case "$iface" in
|
||
""|lo|docker*|br-*|veth*|virbr*|sit*|dummy*) return 1 ;;
|
||
tun*|tap*|zt*|tailscale*|wg*|wireguard*) return 0 ;;
|
||
*) return 1 ;;
|
||
esac
|
||
}
|
||
|
||
resolve_neko_nat1to1() {
|
||
local mode="${NEKO_ACCESS_MODE:-lan}" candidate="" lan_ip="" iface=""
|
||
lan_ip="$(detect_preferred_lan_ip || true)"
|
||
case "$mode" in
|
||
remote)
|
||
# VPN/remote viewers must use the WireGuard overlay address, not the LAN IP.
|
||
candidate="${NEKO_WIREGUARD_HOST:-}"
|
||
if [ -n "$candidate" ] && neko_nat1to1_is_usable "$candidate"; then
|
||
printf '%s' "$candidate"
|
||
return 0
|
||
fi
|
||
candidate="${NEKO_WEBRTC_NAT1TO1:-}"
|
||
if [ -n "$candidate" ] && neko_nat1to1_is_usable "$candidate"; then
|
||
iface="$(neko_nat1to1_iface_for_ip "$candidate")"
|
||
if neko_nat1to1_is_overlay_iface "$iface"; then
|
||
printf '%s' "$candidate"
|
||
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"
|
||
return 0
|
||
;;
|
||
esac
|
||
[ -n "$lan_ip" ] && printf '%s' "$lan_ip"
|
||
}
|
||
|
||
neko_shellcrash_proxy_port_from_url() {
|
||
local url="${1:-}" port=""
|
||
port="$(printf '%s' "$url" | sed -n 's|.*:\([0-9][0-9]*\)$|\1|p')"
|
||
case "$port" in
|
||
"$(shellcrash_neko_proxy_port)"|"$(shellcrash_mix_port)"|"$(shellcrash_redir_port)"|"$(shellcrash_tproxy_port)")
|
||
printf '%s' "$port"
|
||
return 0
|
||
;;
|
||
esac
|
||
return 1
|
||
}
|
||
|
||
neko_nat1to1_is_usable() {
|
||
local candidate="$1"
|
||
local iface
|
||
[ -n "$candidate" ] || return 1
|
||
iface="$(neko_nat1to1_iface_for_ip "$candidate")"
|
||
case "$iface" in
|
||
""|lo|docker*|br-*|veth*|virbr*|sit*|dummy*)
|
||
return 1
|
||
;;
|
||
*)
|
||
# Explicit NAT1TO1 may be a physical LAN or WireGuard address, but auto-detection
|
||
# stays on the physical LAN path via detect_preferred_lan_ip().
|
||
return 0
|
||
;;
|
||
esac
|
||
}
|
||
|
||
service_mdns_status() {
|
||
local detail hostname ipv6
|
||
hostname="$(hostname)"
|
||
ipv6="unknown"
|
||
if grep -Eq '^[[:space:]]*use-ipv6=no' /etc/avahi/avahi-daemon.conf 2>/dev/null; then
|
||
ipv6="disabled"
|
||
fi
|
||
detail="hostname=${hostname}; avahi_ipv6=${ipv6}"
|
||
kv available 1
|
||
kv running "$(unit_active avahi-daemon && echo 1 || echo 0)"
|
||
kv status "$(unit_active avahi-daemon && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port 22
|
||
kv url "ssh ${WEBTTY_USER}@${HOSTNAME_ALIAS}.local"
|
||
kv detail "$detail"
|
||
}
|
||
|
||
service_mdns_start() {
|
||
hostnamectl set-hostname "$HOSTNAME_ALIAS"
|
||
sync_mdns_hosts_entry
|
||
systemctl restart avahi-daemon
|
||
kv message "mDNS hostname refreshed"
|
||
}
|
||
|
||
service_mdns_stop() {
|
||
systemctl stop avahi-daemon
|
||
kv message "avahi-daemon stopped"
|
||
}
|
||
|
||
wifi_detect_iface() {
|
||
local iface path name
|
||
if have iw; then
|
||
while read -r iface; do
|
||
[ -n "$iface" ] || continue
|
||
case "$iface" in
|
||
p2p-dev-*|mon.*) continue ;;
|
||
esac
|
||
printf '%s\n' "$iface"
|
||
return 0
|
||
done < <(iw dev 2>/dev/null | awk '$1=="Interface"{print $2}')
|
||
fi
|
||
for path in /sys/class/net/*; do
|
||
[ -e "$path" ] || continue
|
||
name="${path##*/}"
|
||
case "$name" in
|
||
lo|docker*|br-*|veth*|virbr*|tun*|tap*|p2p-dev-*|mon.*) continue ;;
|
||
esac
|
||
if [ -d "$path/wireless" ]; then
|
||
printf '%s\n' "$name"
|
||
return 0
|
||
fi
|
||
done
|
||
return 1
|
||
}
|
||
|
||
service_wifi_status() {
|
||
if ! have nmcli; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "NetworkManager (nmcli) is not available"
|
||
return 0
|
||
fi
|
||
local active detail iface profile_exists running status
|
||
iface="$(wifi_detect_iface || true)"
|
||
active="$(nmcli -t -f NAME,DEVICE connection show --active 2>/dev/null | awk -F: -v want="$WIFI_CONNECTION_NAME" '$1==want{print $2; exit}')"
|
||
profile_exists="$(nmcli -t -f NAME connection show 2>/dev/null | grep -Fx "$WIFI_CONNECTION_NAME" || true)"
|
||
detail="ssid=${WIFI_SSID};iface=${iface:-none};profile=$([ -n "$profile_exists" ] && echo present || echo missing)"
|
||
if [ -z "$iface" ]; then
|
||
detail="${detail};warning=no_wireless_interface_detected"
|
||
fi
|
||
if [ -n "$active" ]; then
|
||
running=1
|
||
status=online
|
||
detail="${detail};active_device=${active}"
|
||
elif [ -n "$profile_exists" ]; then
|
||
running=0
|
||
status=configured
|
||
else
|
||
running=0
|
||
status=provisioning
|
||
fi
|
||
kv available 1
|
||
kv running "$running"
|
||
kv status "$status"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port ""
|
||
kv url ""
|
||
kv detail "$detail"
|
||
}
|
||
|
||
service_wifi_start() {
|
||
if ! have nmcli; then
|
||
kv message "NetworkManager (nmcli) is not available"
|
||
exit 1
|
||
fi
|
||
local iface ifname
|
||
iface="$(wifi_detect_iface || true)"
|
||
ifname="${iface:-*}"
|
||
if ! nmcli connection show "$WIFI_CONNECTION_NAME" >/dev/null 2>&1; then
|
||
nmcli connection add type wifi ifname "$ifname" con-name "$WIFI_CONNECTION_NAME" ssid "$WIFI_SSID" \
|
||
wifi-sec.key-mgmt wpa-psk wifi-sec.psk "${WIFI_PSK:-12345678}" ipv4.method auto connection.autoconnect yes
|
||
else
|
||
nmcli connection modify "$WIFI_CONNECTION_NAME" \
|
||
connection.interface-name "$ifname" \
|
||
802-11-wireless.ssid "$WIFI_SSID" \
|
||
wifi-sec.key-mgmt wpa-psk \
|
||
wifi-sec.psk "${WIFI_PSK:-12345678}" \
|
||
connection.autoconnect yes \
|
||
ipv4.method auto >/dev/null 2>&1 || true
|
||
fi
|
||
nmcli connection up "$WIFI_CONNECTION_NAME" >/dev/null 2>&1 || true
|
||
kv message "Wi-Fi profile ensured (iface=${iface:-*})"
|
||
}
|
||
|
||
service_wifi_stop() {
|
||
nmcli connection down "$WIFI_CONNECTION_NAME" >/dev/null 2>&1 || true
|
||
kv message "Wi-Fi profile disconnected"
|
||
}
|
||
|
||
shellcrash_cfg_file() {
|
||
printf '%s\n' "$SHELLCRASH_DIR/configs/ShellCrash.cfg"
|
||
}
|
||
|
||
shellcrash_get_cfg() {
|
||
local key="$1" cfg
|
||
cfg="$(shellcrash_cfg_file)"
|
||
[ -f "$cfg" ] || return 1
|
||
awk -F= -v k="$key" '$1 == k { sub(/^[^=]*=/, ""); value=$0; found=1 } END { if (found) print value; exit(found ? 0 : 1) }' "$cfg"
|
||
}
|
||
|
||
shellcrash_cfg_port() {
|
||
local key="$1" default="$2" port
|
||
port="$(shellcrash_get_cfg "$key" 2>/dev/null || true)"
|
||
case "$port" in
|
||
''|*[!0-9]*) port="$default" ;;
|
||
esac
|
||
printf '%s' "$port"
|
||
}
|
||
|
||
shellcrash_mix_port() {
|
||
shellcrash_cfg_port mix_port 7890
|
||
}
|
||
|
||
shellcrash_redir_port() {
|
||
shellcrash_cfg_port redir_port 7892
|
||
}
|
||
|
||
shellcrash_tproxy_port() {
|
||
shellcrash_cfg_port tproxy_port 7893
|
||
}
|
||
|
||
shellcrash_dns_port() {
|
||
shellcrash_cfg_port dns_port 1053
|
||
}
|
||
|
||
shellcrash_neko_proxy_port() {
|
||
local port="${NEKO_SHELLCRASH_PROXY_PORT:-7894}"
|
||
case "$port" in
|
||
''|*[!0-9]*) port=7894 ;;
|
||
esac
|
||
printf '%s' "$port"
|
||
}
|
||
|
||
neko_browser_proxy_switch_enabled() {
|
||
flag_enabled "${ENABLE_NEKO_BROWSER_PROXY:-0}"
|
||
}
|
||
|
||
neko_browser_proxy_manual_enabled() {
|
||
flag_enabled "${NEKO_BROWSER_PROXY_MANUAL:-0}" && return 0
|
||
case "${NEKO_BROWSER_PROXY:-}" in
|
||
""|0|false|FALSE|off|OFF|none|NONE|direct|DIRECT) return 1 ;;
|
||
*) neko_browser_proxy_url_is_usable "$NEKO_BROWSER_PROXY" ;;
|
||
esac
|
||
}
|
||
|
||
neko_shellcrash_proxy_enabled() {
|
||
neko_browser_proxy_switch_enabled || neko_browser_proxy_manual_enabled || return 1
|
||
[ "${ENABLE_NEKO:-1}" = "1" ] || return 1
|
||
case "$SHELLCRASH_ATTACH_NEKO" in
|
||
0|false|FALSE|off|OFF|none|NONE|direct|DIRECT) return 1 ;;
|
||
esac
|
||
return 0
|
||
}
|
||
|
||
shellcrash_config_available() {
|
||
[ -s "$SHELLCRASH_DIR/yamls/config.yaml" ] || [ -s "/tmp/ShellCrash/config.yaml" ]
|
||
}
|
||
|
||
system_probe_url() {
|
||
local url="$1"
|
||
[ -n "$url" ] || return 1
|
||
if have curl; then
|
||
curl -kfsSL --connect-timeout 3 --max-time 5 --noproxy '*' -o /dev/null "$url" 2>/dev/null && return 0
|
||
elif have wget; then
|
||
env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY -u all_proxy -u ALL_PROXY \
|
||
wget -q --no-check-certificate --timeout=5 -O /dev/null "$url" 2>/dev/null && return 0
|
||
fi
|
||
return 1
|
||
}
|
||
|
||
system_direct_global_ready() {
|
||
flag_enabled "${SHELLCRASH_FORCE_PROXY:-0}" && return 1
|
||
system_probe_url "$NETWORK_PROBE_GOOGLE_URL" && return 0
|
||
system_probe_url "$NETWORK_PROBE_YOUTUBE_URL" && return 0
|
||
return 1
|
||
}
|
||
|
||
shellcrash_status_direct_probe() {
|
||
if flag_enabled "${SHELLCRASH_STATUS_PROBE_DIRECT:-0}"; then
|
||
system_direct_global_ready && printf '%s' ok || printf '%s' fail
|
||
else
|
||
printf '%s' skipped
|
||
fi
|
||
}
|
||
|
||
shellcrash_should_autostart_on_direct_fail() {
|
||
flag_enabled "$SHELLCRASH_AUTOSTART_ON_DIRECT_FAIL" || return 1
|
||
shellcrash_config_available || return 1
|
||
system_direct_global_ready && return 1
|
||
return 0
|
||
}
|
||
|
||
shellcrash_set_systemd_autostart_for_policy() {
|
||
if shellcrash_should_autostart_on_direct_fail; then
|
||
systemctl enable shellcrash.service >/dev/null 2>&1 || true
|
||
else
|
||
systemctl disable shellcrash.service >/dev/null 2>&1 || true
|
||
fi
|
||
}
|
||
|
||
shellcrash_autostart_if_needed() {
|
||
shellcrash_should_autostart_on_direct_fail || return 1
|
||
shellcrash_sync_runtime_config >/dev/null 2>&1 || return 1
|
||
shellcrash_prepare_runtime >/dev/null 2>&1 || return 1
|
||
if unit_exists shellcrash.service; then
|
||
systemctl reset-failed shellcrash.service >/dev/null 2>&1 || true
|
||
systemctl enable --now shellcrash.service >/dev/null 2>&1 || return 1
|
||
elif [ -x "$SHELLCRASH_DIR/start.sh" ]; then
|
||
"$SHELLCRASH_DIR/start.sh" start >/dev/null 2>&1 || return 1
|
||
else
|
||
return 1
|
||
fi
|
||
return 0
|
||
}
|
||
|
||
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
|
||
case "$gw" in
|
||
""|invalid\ IP|invalid|host-gateway|host.docker.internal)
|
||
printf 'host-gateway'
|
||
;;
|
||
*)
|
||
if neko_docker_bridge_ipv4_is_usable "$gw"; then
|
||
printf '%s' "$gw"
|
||
else
|
||
printf 'host-gateway'
|
||
fi
|
||
;;
|
||
esac
|
||
}
|
||
|
||
neko_docker_bridge_ipv4_is_usable() {
|
||
local ip="${1:-}"
|
||
case "$ip" in
|
||
""|invalid|invalid\ IP|host-gateway|host.docker.internal|none|null|undefined|unknown)
|
||
return 1
|
||
;;
|
||
esac
|
||
case "$ip" in
|
||
172.1[6-9].*|172.2[0-9].*|172.3[0-1].*) return 0 ;;
|
||
esac
|
||
return 1
|
||
}
|
||
|
||
neko_browser_proxy_url_is_usable() {
|
||
local url="${1:-}" host=""
|
||
case "$url" in
|
||
""|0|false|FALSE|off|OFF|none|NONE|direct|DIRECT)
|
||
return 1
|
||
;;
|
||
esac
|
||
host="$(printf '%s' "$url" | sed -n 's|^[a-zA-Z][a-zA-Z0-9+.-]*://\([^/:]*\).*|\1|p')"
|
||
case "$host" in
|
||
""|invalid|invalid\ IP|host-gateway|none|null|undefined|unknown)
|
||
return 1
|
||
;;
|
||
host.docker.internal)
|
||
return 0
|
||
;;
|
||
10.*|192.168.*|172.1[6-9].*|172.2[0-9].*|172.3[0-1].*)
|
||
return 0
|
||
;;
|
||
esac
|
||
neko_docker_bridge_ipv4_is_usable "$host"
|
||
}
|
||
|
||
neko_proxy_url_for_port() {
|
||
local port="$1"
|
||
[ -n "$port" ] || return 1
|
||
# Use host.docker.internal; compose extra_hosts maps it to the reachable host gateway.
|
||
printf 'http://host.docker.internal:%s' "$port"
|
||
}
|
||
|
||
shellcrash_docker_cidr() {
|
||
local cidr="${SHELLCRASH_DOCKER_CIDR:-172.16.0.0/12}"
|
||
case "$cidr" in
|
||
''|*[!0-9./]*) cidr=172.16.0.0/12 ;;
|
||
esac
|
||
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},10.66.66.0/24,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="$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
|
||
ufw allow in proto "$proto" from "$cidr" to any port "$port" comment "$comment" >/dev/null 2>&1 ||
|
||
ufw allow in proto "$proto" from "$cidr" to any port "$port" >/dev/null 2>&1
|
||
}
|
||
|
||
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" '
|
||
index($0, target) && index($0, "ALLOW IN") && index($0, cidr) { found=1 }
|
||
END { exit(found ? 0 : 1) }
|
||
'
|
||
}
|
||
|
||
ensure_shellcrash_docker_port_access() {
|
||
local port="$1" proto="$2" comment="${3:-ShellCrash from Docker}" cidr ufw_rule_ok=0
|
||
[ "${ENABLE_SHELLCRASH:-1}" = "1" ] || return 0
|
||
case "$port" in
|
||
''|*[!0-9]*) return 0 ;;
|
||
esac
|
||
case "$proto" in
|
||
tcp|udp) ;;
|
||
*) return 0 ;;
|
||
esac
|
||
while IFS= read -r cidr; do
|
||
[ -n "$cidr" ] || continue
|
||
ufw_rule_ok=0
|
||
if ensure_ufw_shellcrash_docker_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 accept"; then
|
||
nft -f - >/dev/null 2>&1 <<NFT || true
|
||
insert rule inet shellcrash input ip saddr $cidr accept comment "shellcrash-trusted"
|
||
NFT
|
||
fi
|
||
fi
|
||
if [ "$ufw_rule_ok" != "1" ] && have iptables; then
|
||
iptables -C INPUT -s "$cidr" -p "$proto" --dport "$port" -j ACCEPT 2>/dev/null ||
|
||
iptables -I INPUT -s "$cidr" -p "$proto" --dport "$port" -j ACCEPT 2>/dev/null || true
|
||
fi
|
||
done <<EOF
|
||
$(shellcrash_trusted_cidrs)
|
||
EOF
|
||
}
|
||
|
||
ensure_shellcrash_docker_proxy_access() {
|
||
local port="${1:-$(shellcrash_mix_port)}" comment="${2:-ShellCrash proxy from Docker}"
|
||
ensure_shellcrash_docker_port_access "$port" tcp "$comment"
|
||
ensure_shellcrash_docker_port_access "$port" udp "$comment"
|
||
}
|
||
|
||
ensure_shellcrash_docker_transparent_access() {
|
||
ensure_shellcrash_docker_port_access "$(shellcrash_redir_port)" tcp "ShellCrash redir from Docker"
|
||
ensure_shellcrash_docker_port_access "$(shellcrash_tproxy_port)" tcp "ShellCrash tproxy from Docker"
|
||
ensure_shellcrash_docker_port_access "$(shellcrash_tproxy_port)" udp "ShellCrash tproxy from Docker"
|
||
ensure_shellcrash_docker_port_access "$(shellcrash_dns_port)" tcp "ShellCrash DNS from Docker"
|
||
ensure_shellcrash_docker_port_access "$(shellcrash_dns_port)" udp "ShellCrash DNS from Docker"
|
||
}
|
||
|
||
ensure_shellcrash_proxy_ports_for_docker() {
|
||
ensure_shellcrash_docker_proxy_access "$(shellcrash_mix_port)"
|
||
ensure_shellcrash_docker_transparent_access
|
||
ensure_shellcrash_docker_port_access "$(shellcrash_cfg_port db_port 9999 2>/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
|
||
}
|
||
|
||
shellcrash_docker_firewall_detail() {
|
||
printf 'cidr=%s,mixed=%s,redir=%s,tproxy=%s,dns=%s' \
|
||
"$(shellcrash_docker_cidr)" \
|
||
"$(shellcrash_mix_port)" \
|
||
"$(shellcrash_redir_port)" \
|
||
"$(shellcrash_tproxy_port)" \
|
||
"$(shellcrash_dns_port)"
|
||
}
|
||
|
||
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_proxy_ports_for_docker >/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 }
|
||
END { exit(found ? 0 : 1) }
|
||
' && return 0
|
||
fi
|
||
return 1
|
||
}
|
||
|
||
neko_shellcrash_attach_disabled() {
|
||
case "${SHELLCRASH_ATTACH_NEKO:-auto}" in
|
||
0|false|FALSE|off|OFF|none|NONE|direct|DIRECT) return 0 ;;
|
||
*) return 1 ;;
|
||
esac
|
||
}
|
||
|
||
# ENABLE_NEKO_BROWSER_PROXY=1 -> proxycheap listener (NEKO_SHELLCRASH_PROXY_PORT, default 7894).
|
||
# ENABLE_NEKO_BROWSER_PROXY=0 -> ShellCrash mixed port (7890) when direct egress is blocked.
|
||
# When the host already has soft-router/direct Google access, Neko stays on direct for both switch states.
|
||
resolve_neko_browser_proxy_primary_port() {
|
||
if neko_browser_proxy_switch_enabled; then
|
||
shellcrash_neko_proxy_port
|
||
else
|
||
shellcrash_mix_port
|
||
fi
|
||
}
|
||
|
||
resolve_neko_browser_proxy_fallback_port() {
|
||
if neko_browser_proxy_switch_enabled; then
|
||
shellcrash_mix_port
|
||
else
|
||
shellcrash_neko_proxy_port
|
||
fi
|
||
}
|
||
|
||
resolve_neko_browser_proxy() {
|
||
case "${NEKO_BROWSER_PROXY:-}" in
|
||
0|false|FALSE|off|OFF|none|NONE|direct|DIRECT)
|
||
neko_shellcrash_attach_disabled && printf '%s' "$NEKO_BROWSER_PROXY" && return 0
|
||
;;
|
||
?*)
|
||
if neko_browser_proxy_url_is_usable "$NEKO_BROWSER_PROXY"; then
|
||
if neko_shellcrash_proxy_port_from_url "$NEKO_BROWSER_PROXY" >/dev/null 2>&1; then
|
||
local shell_port
|
||
shell_port="$(neko_shellcrash_proxy_port_from_url "$NEKO_BROWSER_PROXY")"
|
||
if shellcrash_proxy_available "$shell_port"; then
|
||
neko_proxy_url_for_port "$shell_port"
|
||
return 0
|
||
fi
|
||
system_direct_global_ready && return 0
|
||
else
|
||
printf '%s' "$NEKO_BROWSER_PROXY"
|
||
return 0
|
||
fi
|
||
fi
|
||
;;
|
||
esac
|
||
neko_shellcrash_attach_disabled && return 0
|
||
system_direct_global_ready && return 0
|
||
local port fallback
|
||
port="$(resolve_neko_browser_proxy_primary_port)"
|
||
if shellcrash_proxy_available "$port"; then
|
||
neko_proxy_url_for_port "$port"
|
||
return 0
|
||
fi
|
||
fallback="$(resolve_neko_browser_proxy_fallback_port)"
|
||
if [ -n "$fallback" ] && [ "$fallback" != "$port" ] && shellcrash_proxy_available "$fallback"; then
|
||
neko_proxy_url_for_port "$fallback"
|
||
return 0
|
||
fi
|
||
shellcrash_autostart_if_needed >/dev/null 2>&1 || true
|
||
for port in "$(resolve_neko_browser_proxy_primary_port)" "$(resolve_neko_browser_proxy_fallback_port)"; do
|
||
[ -n "$port" ] || continue
|
||
if shellcrash_proxy_available "$port"; then
|
||
neko_proxy_url_for_port "$port"
|
||
return 0
|
||
fi
|
||
done
|
||
}
|
||
|
||
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
|
||
if ! shellcrash_proxy_available "$(shellcrash_neko_proxy_port)" \
|
||
&& ! shellcrash_proxy_available "$(shellcrash_mix_port)" \
|
||
&& system_direct_global_ready; then
|
||
case "${NEKO_BROWSER_PROXY:-}" in
|
||
""|0|false|FALSE|off|OFF|none|NONE|direct|DIRECT) ;;
|
||
*)
|
||
stack_env_upsert NEKO_BROWSER_PROXY ""
|
||
export NEKO_BROWSER_PROXY=""
|
||
;;
|
||
esac
|
||
fi
|
||
service_neko_reconfigure >/dev/null 2>&1 || true
|
||
}
|
||
|
||
ensure_neko_browser_launcher_executable() {
|
||
local launch="$ROOT_DIR/services/neko/browser-launch.sh"
|
||
[ -f "$launch" ] || return 0
|
||
chmod +x "$launch" 2>/dev/null || true
|
||
}
|
||
|
||
shellcrash_set_cfg() {
|
||
local key="$1" value="$2" cfg
|
||
cfg="$(shellcrash_cfg_file)"
|
||
mkdir -p "$SHELLCRASH_DIR/configs"
|
||
[ -f "$cfg" ] || printf '%s\n' "#ShellCrash配置文件,不明勿动!" >"$cfg"
|
||
sed -i "/^${key}=.*/d" "$cfg"
|
||
printf '%s=%s\n' "$key" "$value" >>"$cfg"
|
||
}
|
||
|
||
shellcrash_cpu_core() {
|
||
case "$(uname -m)" in
|
||
aarch64|arm64) printf '%s\n' arm64 ;;
|
||
armv7l|armv7*|armhf|armv6l) printf '%s\n' armv7 ;;
|
||
x86_64|amd64) printf '%s\n' amd64 ;;
|
||
*) printf '%s\n' "$(uname -m)" ;;
|
||
esac
|
||
}
|
||
|
||
shellcrash_web_port() {
|
||
local port
|
||
port="$(shellcrash_get_cfg db_port 2>/dev/null || true)"
|
||
case "$port" in
|
||
''|*[!0-9]*) port=9999 ;;
|
||
esac
|
||
printf '%s\n' "$port"
|
||
}
|
||
|
||
shellcrash_web_host() {
|
||
detect_preferred_lan_ip 2>/dev/null || printf '%s.local\n' "$HOSTNAME_ALIAS"
|
||
}
|
||
|
||
shellcrash_web_url() {
|
||
printf 'http://%s:%s/ui/\n' "$(shellcrash_web_host)" "$(shellcrash_web_port)"
|
||
}
|
||
|
||
# Strip removed keys and force dashboard/mixed listeners so :9999 is reachable.
|
||
shellcrash_patch_runtime_yaml() {
|
||
local file="$1"
|
||
[ -s "$file" ] || return 1
|
||
command -v python3 >/dev/null 2>&1 || return 0
|
||
python3 - "$file" <<'PY' || true
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
path = Path(sys.argv[1])
|
||
lines = []
|
||
found_ec = found_mp = False
|
||
for line in path.read_text(encoding="utf-8", errors="replace").splitlines():
|
||
if line.startswith("global-client-fingerprint:"):
|
||
continue
|
||
if line.startswith("external-controller:"):
|
||
lines.append("external-controller: 0.0.0.0:9999")
|
||
found_ec = True
|
||
continue
|
||
if line.startswith("mixed-port:"):
|
||
lines.append("mixed-port: 7890")
|
||
found_mp = True
|
||
continue
|
||
lines.append(line)
|
||
if not found_ec:
|
||
lines.append("external-controller: 0.0.0.0:9999")
|
||
if not found_mp:
|
||
lines.append("mixed-port: 7890")
|
||
path.write_text("\n".join(lines).rstrip() + "\n", encoding="utf-8")
|
||
PY
|
||
}
|
||
|
||
# Prefer jsdelivr when GitHub is blocked; CrashCore otherwise exits after failed MMDB download.
|
||
ensure_shellcrash_geo_databases() {
|
||
local sc="${SHELLCRASH_DIR:-/etc/ShellCrash}"
|
||
local js="https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release"
|
||
local dest name min sz
|
||
mkdir -p "$sc"
|
||
|
||
_shellcrash_geo_download() {
|
||
local url="$1" out="$2"
|
||
if command -v curl >/dev/null 2>&1; then
|
||
curl -fsSL --connect-timeout 20 --max-time 120 -o "$out" "$url" && return 0
|
||
env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u all_proxy \
|
||
curl -fsSL --connect-timeout 20 --max-time 120 -o "$out" "$url" && return 0
|
||
fi
|
||
if command -v wget >/dev/null 2>&1; then
|
||
wget -q --no-check-certificate -O "$out" "$url" && return 0
|
||
fi
|
||
return 1
|
||
}
|
||
|
||
for pair in "geosite.dat:GeoSite.dat:1000000" "geoip.dat:GeoIP.dat:1000000" "geoip.metadb:Country.mmdb:1000000"; do
|
||
name="${pair%%:*}"
|
||
dest="$sc/${pair#*:}"
|
||
dest="${dest%:*}"
|
||
min="${pair##*:}"
|
||
if [ -s "$dest" ]; then
|
||
sz="$(wc -c <"$dest" | tr -d '[:space:]')"
|
||
case "$sz" in
|
||
''|*[!0-9]*) ;;
|
||
*) [ "$sz" -ge "$min" ] && continue ;;
|
||
esac
|
||
fi
|
||
echo "Fetching ShellCrash geo database $name" >&2
|
||
if _shellcrash_geo_download "$js/$name" "$dest.tmp"; then
|
||
mv -f "$dest.tmp" "$dest"
|
||
chmod 644 "$dest" 2>/dev/null || true
|
||
else
|
||
rm -f "$dest.tmp"
|
||
echo "WARN: could not download ShellCrash $name" >&2
|
||
fi
|
||
done
|
||
}
|
||
|
||
shellcrash_sync_runtime_config() {
|
||
local src="${1:-$SHELLCRASH_DIR/yamls/config.yaml}"
|
||
local dst="/tmp/ShellCrash/config.yaml"
|
||
mkdir -p "$SHELLCRASH_DIR/yamls" /tmp/ShellCrash
|
||
if [ -s "$src" ]; then
|
||
shellcrash_patch_runtime_yaml "$src" || true
|
||
install -m 0644 "$src" "$dst"
|
||
shellcrash_patch_runtime_yaml "$dst" || true
|
||
return 0
|
||
fi
|
||
if [ -s "$dst" ]; then
|
||
shellcrash_patch_runtime_yaml "$dst" || true
|
||
return 0
|
||
fi
|
||
return 1
|
||
}
|
||
|
||
patch_shellcrash_dns_repair() {
|
||
local fw="$SHELLCRASH_DIR/starts/fw_start.sh"
|
||
[ -f "$fw" ] || return 0
|
||
grep -q 'SHELLCRASH_DNS_REPAIR' "$fw" 2>/dev/null && 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")
|
||
old = """[ "$firewall_area" = 2 -o "$firewall_area" = 3 ] && [ -z "$(grep '127.0.0.1' /etc/resolv.conf 2>/dev/null)" ] && [ "$systype" != 'container' ] && {"""
|
||
new = """[ "${SHELLCRASH_DNS_REPAIR:-0}" = "1" ] && [ "$firewall_area" = 2 -o "$firewall_area" = 3 ] && [ -z "$(grep '127.0.0.1' /etc/resolv.conf 2>/dev/null)" ] && [ "$systype" != 'container' ] && {"""
|
||
if old in text:
|
||
path.write_text(text.replace(old, new), encoding="utf-8")
|
||
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
|
||
}
|
||
|
||
shellcrash_prepare_runtime() {
|
||
ensure_shellcrash_geo_databases || true
|
||
if ! shellcrash_sync_runtime_config; then
|
||
return 1
|
||
fi
|
||
if [ ! -x /tmp/ShellCrash/CrashCore ] && [ -x "$SHELLCRASH_DIR/starts/bfstart.sh" ]; then
|
||
CRASHDIR="$SHELLCRASH_DIR" "$SHELLCRASH_DIR/starts/bfstart.sh" >/dev/null 2>&1 || true
|
||
fi
|
||
patch_shellcrash_dns_repair
|
||
patch_shellcrash_nftables_redirect
|
||
[ -x /tmp/ShellCrash/CrashCore ]
|
||
}
|
||
|
||
shellcrash_systemd_start() {
|
||
systemctl reset-failed shellcrash.service >/dev/null 2>&1 || true
|
||
if shellcrash_should_autostart_on_direct_fail; then
|
||
systemctl enable --now shellcrash.service
|
||
else
|
||
systemctl disable shellcrash.service >/dev/null 2>&1 || true
|
||
systemctl start shellcrash.service
|
||
systemctl disable shellcrash.service >/dev/null 2>&1 || true
|
||
fi
|
||
}
|
||
|
||
shellcrash_systemd_restart() {
|
||
systemctl reset-failed shellcrash.service >/dev/null 2>&1 || true
|
||
if shellcrash_should_autostart_on_direct_fail; then
|
||
systemctl enable shellcrash.service >/dev/null 2>&1 || true
|
||
else
|
||
systemctl disable shellcrash.service >/dev/null 2>&1 || true
|
||
fi
|
||
systemctl restart shellcrash.service
|
||
shellcrash_set_systemd_autostart_for_policy
|
||
}
|
||
|
||
shellcrash_web_panel_installed() {
|
||
local index="$SHELLCRASH_DIR/ui/index.html"
|
||
[ -s "$index" ] || return 1
|
||
if grep -qE '您还未安装本地面板|ShellCrash面板提示|board\.zash\.run\.place' "$index" 2>/dev/null; then
|
||
return 1
|
||
fi
|
||
if [ -f "$SHELLCRASH_DIR/ui/CNAME" ]; then
|
||
return 0
|
||
fi
|
||
find "$SHELLCRASH_DIR/ui" -maxdepth 2 -type f \( -name '*.js' -o -name '*.css' \) 2>/dev/null | grep -q .
|
||
}
|
||
|
||
shellcrash_fetch_dashboard() {
|
||
local dest="$1" url
|
||
shift
|
||
for url in "$@"; do
|
||
if have curl; then
|
||
curl -kfsSL --connect-timeout 10 --max-time 120 --proxy http://127.0.0.1:7890 -o "$dest" "$url" 2>/dev/null && return 0
|
||
curl -kfsSL --connect-timeout 10 --max-time 120 -o "$dest" "$url" 2>/dev/null && return 0
|
||
elif have wget; then
|
||
HTTPS_PROXY=http://127.0.0.1:7890 HTTP_PROXY=http://127.0.0.1:7890 wget -q --no-check-certificate --timeout=20 -O "$dest" "$url" 2>/dev/null && return 0
|
||
wget -q --no-check-certificate --timeout=20 -O "$dest" "$url" 2>/dev/null && return 0
|
||
fi
|
||
done
|
||
return 1
|
||
}
|
||
|
||
ensure_shellcrash_web_panel() {
|
||
local tmp panel_tmp ui_real base_real host port
|
||
if [ ! -d "$SHELLCRASH_DIR" ]; then
|
||
kv message "ShellCrash is not installed"
|
||
return 1
|
||
fi
|
||
|
||
mkdir -p "$SHELLCRASH_DIR/ui" "$SHELLCRASH_DIR/configs" /tmp/ShellCrash
|
||
shellcrash_set_cfg db_port "$(shellcrash_web_port)"
|
||
shellcrash_set_cfg external_ui_url "https://github.com/Zephyruso/zashboard/releases/latest/download/dist-cdn-fonts.zip"
|
||
shellcrash_set_cfg hostdir ":$(shellcrash_web_port)/ui"
|
||
|
||
if shellcrash_web_panel_installed; then
|
||
return 0
|
||
fi
|
||
|
||
tmp="/tmp/ShellCrash/zashboard.tar.gz"
|
||
panel_tmp="/tmp/ShellCrash/ui.zashboard.$$"
|
||
rm -f "$tmp"
|
||
rm -rf "$panel_tmp"
|
||
mkdir -p "$panel_tmp"
|
||
|
||
if ! shellcrash_fetch_dashboard "$tmp" \
|
||
"https://testingcf.jsdelivr.net/gh/juewuy/ShellCrash@master/bin/dashboard/zashboard.tar.gz" \
|
||
"https://testingcf.jsdelivr.net/gh/juewuy/ShellCrash@update/bin/dashboard/zashboard.tar.gz" \
|
||
"https://raw.githubusercontent.com/juewuy/ShellCrash/update/bin/dashboard/zashboard.tar.gz"; then
|
||
rm -rf "$panel_tmp"
|
||
kv message "Failed to download ShellCrash zashboard"
|
||
return 1
|
||
fi
|
||
|
||
if ! tar -tzf "$tmp" >/dev/null 2>&1; then
|
||
rm -rf "$panel_tmp"
|
||
kv message "Downloaded ShellCrash dashboard archive is invalid"
|
||
return 1
|
||
fi
|
||
tar -zxf "$tmp" -C "$panel_tmp"
|
||
|
||
host="$(shellcrash_web_host)"
|
||
port="$(shellcrash_web_port)"
|
||
if [ -d "$panel_tmp/assets" ]; then
|
||
sed -i "s/127\\.0\\.0\\.1/${host}/g; s/9090/${port}/g" "$panel_tmp"/assets/*.js 2>/dev/null || true
|
||
fi
|
||
if [ -d "$panel_tmp/_nuxt" ]; then
|
||
sed -i "s/127\\.0\\.0\\.1:9090/${host}:${port}/g" "$panel_tmp"/_nuxt/*.js 2>/dev/null || true
|
||
fi
|
||
sed -i "s/127\\.0\\.0\\.1:9090/${host}:${port}/g" "$panel_tmp"/*.html 2>/dev/null || true
|
||
|
||
base_real="$(realpath -m "$SHELLCRASH_DIR")"
|
||
ui_real="$(realpath -m "$SHELLCRASH_DIR/ui")"
|
||
case "$ui_real" in
|
||
"$base_real"/ui) ;;
|
||
*)
|
||
rm -rf "$panel_tmp"
|
||
kv message "Refusing to replace unexpected ShellCrash UI path: $ui_real"
|
||
return 1
|
||
;;
|
||
esac
|
||
rm -rf "$SHELLCRASH_DIR/ui"
|
||
mv "$panel_tmp" "$SHELLCRASH_DIR/ui"
|
||
chgrp -R shellcrash "$SHELLCRASH_DIR/ui" 2>/dev/null || true
|
||
chmod -R a+rX "$SHELLCRASH_DIR/ui" 2>/dev/null || true
|
||
rm -f "$tmp"
|
||
|
||
shellcrash_web_panel_installed
|
||
}
|
||
|
||
service_shellcrash_status() {
|
||
local running detail enabled
|
||
if [ ! -x "$SHELLCRASH_DIR/start.sh" ] && ! unit_exists shellcrash.service; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "ShellCrash is not installed"
|
||
return 0
|
||
fi
|
||
running=0
|
||
detail="$(filtered_pgrep '(^|[ /])CrashCore([ ]|$)' | tr '\n' ';' || true)"
|
||
if unit_active shellcrash.service || [ -n "$detail" ]; then
|
||
running=1
|
||
fi
|
||
if [ "$running" != 1 ] && shellcrash_should_autostart_on_direct_fail; then
|
||
shellcrash_autostart_if_needed >/dev/null 2>&1 || true
|
||
detail="$(filtered_pgrep '(^|[ /])CrashCore([ ]|$)' | tr '\n' ';' || true)"
|
||
if unit_active shellcrash.service || [ -n "$detail" ]; then
|
||
running=1
|
||
fi
|
||
fi
|
||
enabled="$(systemctl is-enabled shellcrash.service 2>/dev/null || true)"
|
||
[ -z "$detail" ] && detail="dir=$SHELLCRASH_DIR"
|
||
kv available 1
|
||
kv running "$running"
|
||
kv status "$([ "$running" = 1 ] && echo online || echo stopped)"
|
||
kv url "$(shellcrash_web_url)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$(shellcrash_web_port)"
|
||
kv autostart "$([ "$enabled" = enabled ] && echo 1 || echo 0)"
|
||
kv detail "$detail; direct_google=$(shellcrash_status_direct_probe); config=$(shellcrash_config_available && echo 1 || echo 0); autostart=${enabled:-disabled}; docker_firewall=$(shellcrash_docker_firewall_detail)"
|
||
}
|
||
|
||
service_shellcrash_start() {
|
||
if [ ! -x "$SHELLCRASH_DIR/start.sh" ] && ! unit_exists shellcrash.service; then
|
||
kv message "ShellCrash is not installed"
|
||
exit 1
|
||
fi
|
||
if ! shellcrash_sync_runtime_config; then
|
||
kv message "ShellCrash config.yaml is missing; save/apply a YAML profile first"
|
||
exit 1
|
||
fi
|
||
if ! shellcrash_prepare_runtime; then
|
||
kv message "ShellCrash CrashCore is missing; check DNS/network and rerun apply"
|
||
exit 1
|
||
fi
|
||
if unit_exists shellcrash.service; then
|
||
shellcrash_systemd_start
|
||
elif [ -x "$SHELLCRASH_DIR/start.sh" ]; then
|
||
"$SHELLCRASH_DIR/start.sh" start
|
||
else
|
||
kv message "ShellCrash is not installed"
|
||
exit 1
|
||
fi
|
||
ensure_shellcrash_proxy_ports_for_docker >/dev/null 2>&1 || true
|
||
service_neko_reconfigure_for_shellcrash_change
|
||
kv message "ShellCrash started"
|
||
}
|
||
|
||
service_shellcrash_restart() {
|
||
if [ ! -x "$SHELLCRASH_DIR/start.sh" ] && ! unit_exists shellcrash.service; then
|
||
kv message "ShellCrash is not installed"
|
||
exit 1
|
||
fi
|
||
if ! shellcrash_sync_runtime_config; then
|
||
kv message "ShellCrash config.yaml is missing; save/apply a YAML profile first"
|
||
exit 1
|
||
fi
|
||
if ! shellcrash_prepare_runtime; then
|
||
kv message "ShellCrash CrashCore is missing; check DNS/network and rerun apply"
|
||
exit 1
|
||
fi
|
||
if unit_exists shellcrash.service; then
|
||
shellcrash_systemd_restart
|
||
elif [ -x "$SHELLCRASH_DIR/start.sh" ]; then
|
||
"$SHELLCRASH_DIR/start.sh" restart
|
||
else
|
||
kv message "ShellCrash is not installed"
|
||
exit 1
|
||
fi
|
||
ensure_shellcrash_proxy_ports_for_docker >/dev/null 2>&1 || true
|
||
service_neko_reconfigure_for_shellcrash_change
|
||
kv message "ShellCrash restarted"
|
||
}
|
||
|
||
service_shellcrash_stop() {
|
||
if unit_exists shellcrash.service; then
|
||
systemctl disable --now shellcrash.service >/dev/null 2>&1 || systemctl stop shellcrash.service
|
||
elif [ -x "$SHELLCRASH_DIR/start.sh" ]; then
|
||
"$SHELLCRASH_DIR/start.sh" stop
|
||
fi
|
||
service_neko_reconfigure_for_shellcrash_change
|
||
kv message "ShellCrash stopped"
|
||
}
|
||
|
||
service_shellcrash_web_panel() {
|
||
if ! ensure_shellcrash_web_panel; then
|
||
exit 1
|
||
fi
|
||
shellcrash_sync_runtime_config >/dev/null 2>&1 || true
|
||
if { flag_enabled "$SHELLCRASH_AUTO_START_ON_WEB_PANEL" || shellcrash_should_autostart_on_direct_fail; } && ! unit_active shellcrash.service && [ -z "$(filtered_pgrep '(^|[ /])CrashCore([ ]|$)' || true)" ]; then
|
||
service_shellcrash_start >/dev/null
|
||
fi
|
||
kv message "ShellCrash web panel ready"
|
||
kv url "$(shellcrash_web_url)"
|
||
kv port "$(shellcrash_web_port)"
|
||
kv manual_only "$SHELLCRASH_MANUAL_ONLY"
|
||
}
|
||
|
||
grant_shellcrash_web_write_access() {
|
||
local path
|
||
for path in "$SHELLCRASH_DIR/yamls" "$SHELLCRASH_DIR/configs" "$SHELLCRASH_DIR/jsons"; do
|
||
[ -d "$path" ] || continue
|
||
chgrp -R live "$path" 2>/dev/null || true
|
||
chmod -R g+rwX "$path" 2>/dev/null || true
|
||
find "$path" -type d -exec chmod g+s {} \; 2>/dev/null || true
|
||
done
|
||
}
|
||
|
||
service_shellcrash_apply_profile() {
|
||
local src="${ACTION_ARG1:-$SHELLCRASH_DIR/yamls/LsRzcR3pHoA9.yaml}"
|
||
local target_node="${ACTION_ARG2:-5d723acc-singbox_tuic3}"
|
||
local src_real stage_root shell_yaml_root profile_name cfg unit_path selected
|
||
|
||
if [ -z "$target_node" ] || printf '%s' "$target_node" | grep -q '[[:cntrl:]]'; then
|
||
kv message "Invalid ShellCrash target node"
|
||
exit 1
|
||
fi
|
||
if ! have realpath; then
|
||
kv message "realpath is required to apply ShellCrash profile"
|
||
exit 1
|
||
fi
|
||
|
||
src_real="$(realpath -m "$src")"
|
||
stage_root="$(realpath -m "$ROOT_DIR/.process_runner")"
|
||
shell_yaml_root="$(realpath -m "$SHELLCRASH_DIR/yamls")"
|
||
case "$src_real" in
|
||
"$stage_root"/*|"$shell_yaml_root"/*) ;;
|
||
*)
|
||
kv message "ShellCrash profile path must be staged under $stage_root or $shell_yaml_root"
|
||
exit 1
|
||
;;
|
||
esac
|
||
if [ ! -s "$src_real" ]; then
|
||
kv message "ShellCrash profile is missing or empty: $src_real"
|
||
exit 1
|
||
fi
|
||
|
||
mkdir -p "$SHELLCRASH_DIR/yamls" "$SHELLCRASH_DIR/configs" /tmp/ShellCrash
|
||
profile_name="$(basename "$src_real")"
|
||
case "$profile_name" in
|
||
*.yaml|*.yml) ;;
|
||
*) profile_name="LsRzcR3pHoA9.yaml" ;;
|
||
esac
|
||
install -m 0644 "$src_real" "$SHELLCRASH_DIR/yamls/$profile_name"
|
||
install -m 0644 "$src_real" "$SHELLCRASH_DIR/yamls/config.yaml"
|
||
rm -f "$SHELLCRASH_DIR/configs/web_save" "$SHELLCRASH_DIR/.start_error"
|
||
|
||
cfg="$SHELLCRASH_DIR/configs/ShellCrash.cfg"
|
||
[ -f "$cfg" ] || printf '%s\n' "#ShellCrash配置文件,不明勿动!" >"$cfg"
|
||
set_shellcrash_cfg() {
|
||
local key="$1" value="$2"
|
||
sed -i "/^${key}=.*/d" "$cfg"
|
||
printf '%s=%s\n' "$key" "$value" >>"$cfg"
|
||
}
|
||
set_shellcrash_cfg crashcore meta
|
||
set_shellcrash_cfg cpucore "$(shellcrash_cpu_core)"
|
||
set_shellcrash_cfg redir_mod "Redir模式"
|
||
set_shellcrash_cfg firewall_area 1
|
||
set_shellcrash_cfg dns_mod redir_host
|
||
set_shellcrash_cfg mix_port 7890
|
||
set_shellcrash_cfg redir_port 7892
|
||
set_shellcrash_cfg tproxy_port 7893
|
||
set_shellcrash_cfg db_port 9999
|
||
set_shellcrash_cfg dns_port 1053
|
||
set_shellcrash_cfg proxies_bypass ON
|
||
set_shellcrash_cfg sniffer ON
|
||
set_shellcrash_cfg skip_cert ON
|
||
set_shellcrash_cfg common_ports ON
|
||
set_shellcrash_cfg multiport "22,53,80,123,443,8080,8443"
|
||
set_shellcrash_cfg network_check OFF
|
||
|
||
cat >"$SHELLCRASH_DIR/configs/command.env" <<EOF
|
||
TMPDIR=/tmp/ShellCrash
|
||
BINDIR=$SHELLCRASH_DIR
|
||
COMMAND="\$TMPDIR/CrashCore -d \$BINDIR -f \$TMPDIR/config.yaml"
|
||
EOF
|
||
if ! shellcrash_sync_runtime_config "$SHELLCRASH_DIR/yamls/config.yaml"; then
|
||
kv message "Failed to sync ShellCrash runtime config.yaml"
|
||
exit 1
|
||
fi
|
||
if ! shellcrash_prepare_runtime; then
|
||
kv message "ShellCrash CrashCore is missing; check DNS/network and rerun apply"
|
||
exit 1
|
||
fi
|
||
grant_shellcrash_web_write_access
|
||
|
||
if unit_exists shellcrash.service; then
|
||
unit_path="$(systemctl show -p FragmentPath shellcrash.service | sed 's/^FragmentPath=//')"
|
||
if [ -n "$unit_path" ] && [ -f "$unit_path" ]; then
|
||
sed -i "s#^ExecStart=.*#ExecStart=/tmp/ShellCrash/CrashCore -d $SHELLCRASH_DIR -f /tmp/ShellCrash/config.yaml#" "$unit_path"
|
||
systemctl daemon-reload
|
||
fi
|
||
shellcrash_systemd_restart
|
||
elif [ -x "$SHELLCRASH_DIR/start.sh" ]; then
|
||
"$SHELLCRASH_DIR/start.sh" restart
|
||
else
|
||
kv message "ShellCrash is not installed"
|
||
exit 1
|
||
fi
|
||
ensure_shellcrash_web_panel >/dev/null 2>&1 || true
|
||
ensure_shellcrash_proxy_ports_for_docker >/dev/null 2>&1 || true
|
||
service_neko_reconfigure_for_shellcrash_change
|
||
|
||
selected=""
|
||
if have python3; then
|
||
selected="$(python3 - "$target_node" "$SHELLCRASH_DIR/configs/ShellCrash.cfg" <<'PY' || true
|
||
import json
|
||
import sys
|
||
import time
|
||
import urllib.parse
|
||
import urllib.request
|
||
|
||
target = sys.argv[1]
|
||
cfg_path = sys.argv[2]
|
||
base = "http://127.0.0.1:9999"
|
||
secret = ""
|
||
try:
|
||
with open(cfg_path, encoding="utf-8", errors="replace") as fh:
|
||
for line in fh:
|
||
if line.startswith("secret="):
|
||
secret = line.split("=", 1)[1].strip()
|
||
break
|
||
except OSError:
|
||
pass
|
||
|
||
headers = {"Content-Type": "application/json"}
|
||
if secret:
|
||
headers["Authorization"] = f"Bearer {secret}"
|
||
|
||
def request(path, *, data=None, method="GET"):
|
||
req = urllib.request.Request(base + path, data=data, headers=headers, method=method)
|
||
return urllib.request.urlopen(req, timeout=3).read()
|
||
|
||
last = None
|
||
for _ in range(30):
|
||
try:
|
||
payload = json.loads(request("/proxies").decode("utf-8"))
|
||
break
|
||
except Exception as exc: # noqa: BLE001
|
||
last = exc
|
||
time.sleep(1)
|
||
else:
|
||
raise SystemExit(f"api unavailable: {last}")
|
||
|
||
changed = []
|
||
for name, item in payload.get("proxies", {}).items():
|
||
all_nodes = item.get("all") or []
|
||
if target in all_nodes and item.get("type") in ("Selector", "URLTest", "Fallback", "LoadBalance"):
|
||
body = json.dumps({"name": target}).encode("utf-8")
|
||
request("/proxies/" + urllib.parse.quote(name, safe=""), data=body, method="PUT")
|
||
changed.append(name)
|
||
print("|".join(changed))
|
||
PY
|
||
)"
|
||
fi
|
||
|
||
kv message "ShellCrash profile applied"
|
||
kv profile "$SHELLCRASH_DIR/yamls/config.yaml"
|
||
kv target_node "$target_node"
|
||
kv selected "$selected"
|
||
}
|
||
|
||
service_srs_status() {
|
||
if ! have docker || [ ! -f "$SRS_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or SRS compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-srs || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$SRS_HTTP_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${SRS_HTTP_PORT}"
|
||
kv detail "${state:-container=missing};rtmp=${SRS_RTMP_PORT};api=${SRS_API_PORT};srt=${SRS_SRT_PORT};rtc=${SRS_WEBRTC_PORT}"
|
||
}
|
||
|
||
service_srs_start() {
|
||
compose_in_service_dir "$SRS_COMPOSE" up -d
|
||
kv message "SRS started"
|
||
}
|
||
|
||
service_srs_stop() {
|
||
compose_in_service_dir "$SRS_COMPOSE" down
|
||
kv message "SRS stopped"
|
||
}
|
||
|
||
service_webtty_status() {
|
||
if ! unit_exists live-webtty.service && ! have ttyd; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "ttyd is not installed"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
kv running "$(unit_active live-webtty.service && echo 1 || echo 0)"
|
||
kv status "$(unit_active live-webtty.service && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$WEBTTY_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${WEBTTY_PORT}"
|
||
kv detail "web user=${WEBTTY_USER}"
|
||
}
|
||
|
||
service_webtty_start() {
|
||
systemctl enable --now live-webtty.service
|
||
kv message "WebTTY started"
|
||
}
|
||
|
||
service_webtty_stop() {
|
||
systemctl stop live-webtty.service
|
||
kv message "WebTTY stopped"
|
||
}
|
||
|
||
service_cockpit_status() {
|
||
if ! unit_exists cockpit.socket; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Cockpit is not installed"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
kv running "$(unit_active cockpit.socket && echo 1 || echo 0)"
|
||
kv status "$(unit_active cockpit.socket && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port 9090
|
||
kv url "https://${HOSTNAME_ALIAS}.local:9090"
|
||
kv detail "login with live or any enabled system account"
|
||
}
|
||
|
||
service_cockpit_start() {
|
||
systemctl enable --now cockpit.socket
|
||
kv message "Cockpit started"
|
||
}
|
||
|
||
service_cockpit_stop() {
|
||
systemctl stop cockpit.socket
|
||
kv message "Cockpit stopped"
|
||
}
|
||
|
||
service_filebrowser_status() {
|
||
if ! have docker || [ ! -f "$FILEBROWSER_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or File Browser compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-filebrowser || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$FILEBROWSER_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${FILEBROWSER_PORT}"
|
||
kv detail "${state:-container=missing};user=${FILEBROWSER_USER:-live}"
|
||
}
|
||
|
||
service_filebrowser_start() {
|
||
compose_in_service_dir "$FILEBROWSER_COMPOSE" up -d
|
||
kv message "File Browser started"
|
||
}
|
||
|
||
service_filebrowser_stop() {
|
||
compose_in_service_dir "$FILEBROWSER_COMPOSE" down
|
||
kv message "File Browser stopped"
|
||
}
|
||
|
||
service_homepage_status() {
|
||
if ! have docker || [ ! -f "$HOMEPAGE_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or Homepage compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-homepage || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$HOMEPAGE_PORT"
|
||
kv url "$([ "$HOMEPAGE_PORT" = 80 ] && echo "http://${HOSTNAME_ALIAS}.local" || echo "http://${HOSTNAME_ALIAS}.local:${HOMEPAGE_PORT}")"
|
||
kv detail "${state:-container=missing}"
|
||
}
|
||
|
||
service_homepage_start() {
|
||
compose_in_service_dir "$HOMEPAGE_COMPOSE" up -d
|
||
kv message "Homepage started"
|
||
}
|
||
|
||
service_homepage_stop() {
|
||
compose_in_service_dir "$HOMEPAGE_COMPOSE" down
|
||
kv message "Homepage stopped"
|
||
}
|
||
|
||
service_netdata_status() {
|
||
if ! unit_exists netdata.service; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Netdata is not installed"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
kv running "$(unit_active netdata.service && echo 1 || echo 0)"
|
||
kv status "$(unit_active netdata.service && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$NETDATA_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${NETDATA_PORT}"
|
||
kv detail "metrics and alarms API available at /api/v1"
|
||
}
|
||
|
||
service_netdata_start() {
|
||
systemctl enable --now netdata
|
||
kv message "Netdata started"
|
||
}
|
||
|
||
service_netdata_stop() {
|
||
systemctl stop netdata
|
||
kv message "Netdata stopped"
|
||
}
|
||
|
||
service_adb_status() {
|
||
if ! have adb; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "adb is not installed"
|
||
return 0
|
||
fi
|
||
local details
|
||
details="$(adb devices -l 2>/dev/null | tr '\n' ';' || true)"
|
||
kv available 1
|
||
kv running "$(filtered_pgrep 'adb -L tcp:5037|[ /]adb([ ]|$)' >/dev/null 2>&1 && echo 1 || echo 0)"
|
||
kv status "$(filtered_pgrep 'adb -L tcp:5037|[ /]adb([ ]|$)' >/dev/null 2>&1 && echo online || echo idle)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port ""
|
||
kv url ""
|
||
kv detail "${details:-adb server ready}"
|
||
}
|
||
|
||
service_adb_start() {
|
||
adb start-server >/dev/null
|
||
kv message "ADB server started"
|
||
}
|
||
|
||
service_adb_stop() {
|
||
adb kill-server >/dev/null 2>&1 || true
|
||
kv message "ADB server stopped"
|
||
}
|
||
|
||
service_android_panel_status() {
|
||
if ! unit_exists live-android-panel.service; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Android web panel is not installed"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
kv running "$(unit_active live-android-panel.service && echo 1 || echo 0)"
|
||
kv status "$(unit_active live-android-panel.service && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$ANDROID_PANEL_PORT"
|
||
kv url "${CONSOLE_BASE_URL}/android"
|
||
kv detail "android-center=${CONSOLE_BASE_URL}/android;ws-scrcpy=${CONSOLE_BASE_URL}/android;git=ANDROID_PANEL_GIT_URL"
|
||
}
|
||
|
||
service_android_panel_start() {
|
||
systemctl enable --now live-android-panel.service
|
||
kv message "Android web panel started"
|
||
}
|
||
|
||
service_android_panel_stop() {
|
||
systemctl stop live-android-panel.service
|
||
kv message "Android web panel stopped"
|
||
}
|
||
|
||
service_chromium_status() {
|
||
if ! have chromium && ! have chromium-browser && ! have google-chrome; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Chromium or Google Chrome is not installed"
|
||
return 0
|
||
fi
|
||
local details count
|
||
count="$(filtered_pgrep 'chromium|chrome' | wc -l | tr -d ' ' || true)"
|
||
case "$count" in ''|*[!0-9]*) count=0 ;; esac
|
||
details="$(filtered_pgrep 'chromium|chrome' | head -3 | sed 's/[[:space:]][[:space:]]*/ /g' | cut -c1-220 | tr '\n' ';' || true)"
|
||
kv available 1
|
||
kv running "$([ "$count" -gt 0 ] && echo 1 || echo 0)"
|
||
kv status "$([ "$count" -gt 0 ] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "${BROWSER_REMOTE_DEBUG_PORT:-9222}"
|
||
kv url ""
|
||
if [ "$count" -gt 0 ]; then
|
||
kv detail "processes=$count; sample=${details}"
|
||
else
|
||
kv detail "profile=${BROWSER_PROFILE_DIR:-$HOME/.local/share/live-browser/profile}"
|
||
fi
|
||
}
|
||
|
||
service_chromium_start() {
|
||
if [ -z "${DISPLAY:-}" ] && command -v xvfb-run >/dev/null 2>&1; then
|
||
nohup xvfb-run -a "$BROWSER_LAUNCHER" >/tmp/live-browser.log 2>&1 &
|
||
else
|
||
[ -z "${DISPLAY:-}" ] && export DISPLAY=:0
|
||
nohup "$BROWSER_LAUNCHER" >/tmp/live-browser.log 2>&1 &
|
||
fi
|
||
disown || true
|
||
kv message "Chromium launched"
|
||
}
|
||
|
||
service_chromium_stop() {
|
||
pkill -f 'chromium|chrome' || true
|
||
kv message "Chromium stopped"
|
||
}
|
||
|
||
service_neko_status() {
|
||
if [ "${ENABLE_NEKO:-1}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "在 config/system-stack.env 设置 ENABLE_NEKO=1 后执行安装脚本或在此 Start 拉取容器"
|
||
return 0
|
||
fi
|
||
if [ ! -f "$NEKO_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "services/neko/docker-compose.yml missing"
|
||
return 0
|
||
fi
|
||
if ! have docker; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "docker not installed"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
local state browser_proxy status_value target_nat1to1 runtime_nat1to1
|
||
local running_count=0 total_count=0 states="" name instance_count idx ports="" profiles=""
|
||
local reconfigure_needed=0 nat_mismatches="" reconfigure_detail=""
|
||
local port network_ok port_ok http_code runtime_ok
|
||
instance_count="$(resolve_neko_instance_count)"
|
||
total_count="$instance_count"
|
||
target_nat1to1="$(resolve_neko_nat1to1)"
|
||
if [ -n "$target_nat1to1" ] && [ "${NEKO_WEBRTC_NAT1TO1:-}" != "$target_nat1to1" ]; then
|
||
NEKO_WEBRTC_NAT1TO1="$target_nat1to1"
|
||
fi
|
||
for idx in $(seq 1 "$instance_count"); do
|
||
name="live-neko-$idx"
|
||
port="$(resolve_neko_http_port_for_instance "$idx")"
|
||
state="$(docker_container_status "$name" || true)"
|
||
network_ok=0
|
||
port_ok=0
|
||
runtime_ok=0
|
||
http_code=skip
|
||
if echo "$state" | grep -qi running; then
|
||
if docker_container_network_attached "$name"; then
|
||
network_ok=1
|
||
fi
|
||
if docker_container_http_port_published "$name" "$port"; then
|
||
port_ok=1
|
||
http_code="$(neko_http_probe_code "$port")"
|
||
else
|
||
http_code=unpublished
|
||
fi
|
||
if [ "$network_ok" = "1" ] && [ "$port_ok" = "1" ] && neko_http_code_ok "$http_code"; then
|
||
runtime_ok=1
|
||
running_count=$((running_count + 1))
|
||
else
|
||
reconfigure_needed=1
|
||
nat_mismatches="${nat_mismatches}${name}:runtime_unreachable(port=${port},network=${network_ok},published=${port_ok},http=${http_code}),"
|
||
fi
|
||
runtime_nat1to1="$(docker_container_env_value "$name" NEKO_WEBRTC_NAT1TO1 || true)"
|
||
if [ -n "$target_nat1to1" ] && [ "$runtime_nat1to1" != "$target_nat1to1" ]; then
|
||
reconfigure_needed=1
|
||
nat_mismatches="${nat_mismatches}${name}:${runtime_nat1to1:-unset}->${target_nat1to1},"
|
||
fi
|
||
fi
|
||
states="${states}${name}:${state:-missing},port=${port},network=${network_ok},published=${port_ok},http=${http_code},ready=${runtime_ok};"
|
||
ports="${ports}${port},"
|
||
profiles="${profiles}$(resolve_neko_profile_dir_for_instance "$idx"),"
|
||
done
|
||
if [ "$running_count" -eq 0 ]; then
|
||
state="$(docker_container_status live-neko-chromium || true)"
|
||
if echo "$state" | grep -qi running; then
|
||
running_count=1
|
||
total_count=1
|
||
states="live-neko-chromium:${state};"
|
||
fi
|
||
fi
|
||
if [ "$running_count" -ge "$total_count" ]; then
|
||
status_value=online
|
||
elif [ "$running_count" -gt 0 ]; then
|
||
status_value=degraded
|
||
else
|
||
status_value=stopped
|
||
fi
|
||
if [ "$reconfigure_needed" = "1" ] && [ "$running_count" -gt 0 ]; then
|
||
status_value=degraded
|
||
reconfigure_detail="; needs_reconfigure=1; runtime_nat1to1_mismatch=${nat_mismatches%,}"
|
||
fi
|
||
kv running "$([ "$running_count" -ge 1 ] && echo 1 || echo 0)"
|
||
kv status "$status_value"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$NEKO_HTTP_PORT_1"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${NEKO_HTTP_PORT_1}"
|
||
browser_proxy="$(resolve_neko_browser_proxy)"
|
||
[ -n "$browser_proxy" ] || browser_proxy=direct
|
||
kv detail "image=$(resolve_neko_image); supervisor_conf=$(resolve_neko_supervisor_conf); start_url=${NEKO_START_URL}; nat1to1=${target_nat1to1:-unset}; instances=${running_count}/${total_count}; ports=${ports%,}; profiles=${profiles%,}; screen=${NEKO_DESKTOP_SCREEN}; shm=${NEKO_SHM_SIZE}; fps=${NEKO_MAX_FPS}; bitrate=${NEKO_VIDEO_BITRATE}; render=${NEKO_BROWSER_RENDER_MODE}; proxy=${browser_proxy}${reconfigure_detail}; containers=${states}"
|
||
}
|
||
|
||
service_neko_start() {
|
||
if [ "${ENABLE_NEKO:-1}" != "1" ]; then
|
||
kv message "请先在 config/system-stack.env 设置 ENABLE_NEKO=1"
|
||
exit 1
|
||
fi
|
||
if [ ! -f "$ROOT_DIR/tools/render_neko_compose.py" ]; then
|
||
kv message "Neko compose renderer missing"
|
||
return 0
|
||
fi
|
||
local prof idx port mux udp range compose_file service_names=""
|
||
local instance_count
|
||
instance_count="$(resolve_neko_instance_count)"
|
||
export_neko_env
|
||
sync_neko_host_assets
|
||
ensure_neko_browser_launcher_executable
|
||
prepare_neko_profile_dirs_for_compose
|
||
ensure_shellcrash_proxy_ports_for_docker >/dev/null 2>&1 || true
|
||
for idx in $(seq 1 "$instance_count"); do
|
||
prof="$(resolve_neko_profile_dir_for_instance "$idx")"
|
||
port="$(resolve_neko_http_port_for_instance "$idx")"
|
||
mux="$(resolve_neko_tcpmux_for_instance "$idx")"
|
||
udp="$(resolve_neko_udpmux_for_instance "$idx")"
|
||
range="$(resolve_neko_udp_range_for_instance "$idx")"
|
||
allow_tcp_port_if_firewall_active "$port"
|
||
allow_tcp_port_if_firewall_active "$mux"
|
||
allow_udp_port_if_firewall_active "$udp"
|
||
allow_udp_range_if_firewall_active "$range"
|
||
service_names="${service_names} neko${idx}"
|
||
done
|
||
remove_legacy_neko_container
|
||
remove_unreachable_neko_containers "$instance_count"
|
||
compose_file="$(render_neko_compose_file)"
|
||
# shellcheck disable=SC2086
|
||
compose_in_service_dir "$compose_file" up -d $service_names
|
||
if ! verify_neko_instances_running "$instance_count"; then
|
||
kv message "Neko start incomplete; UDP port conflict autofix may need reconfigure. Check CrashCore/ss -ulnp"
|
||
exit 1
|
||
fi
|
||
kv message "Neko stack started (${instance_count} browser instances)"
|
||
}
|
||
|
||
service_neko_reconfigure() {
|
||
if [ "${ENABLE_NEKO:-1}" != "1" ]; then
|
||
kv message "Neko disabled"
|
||
exit 1
|
||
fi
|
||
if [ ! -f "$ROOT_DIR/tools/render_neko_compose.py" ]; then
|
||
kv message "Neko compose renderer missing"
|
||
return 0
|
||
fi
|
||
local idx instance_count compose_file service_names=""
|
||
instance_count="$(resolve_neko_instance_count)"
|
||
export_neko_env
|
||
sync_neko_host_assets
|
||
prepare_neko_profile_dirs_for_compose
|
||
ensure_shellcrash_proxy_ports_for_docker >/dev/null 2>&1 || true
|
||
for idx in $(seq 1 "$instance_count"); do
|
||
service_names="${service_names} neko${idx}"
|
||
done
|
||
compose_file="$(render_neko_compose_file)"
|
||
remove_neko_recreate_conflicts "$instance_count"
|
||
# Recreate containers so WebRTC env changes take effect; profile volumes are left untouched.
|
||
# shellcheck disable=SC2086
|
||
compose_in_service_dir "$compose_file" up -d --force-recreate $service_names
|
||
if ! verify_neko_instances_running "$instance_count"; then
|
||
kv message "Neko reconfigure incomplete; inspect docker logs / ss -ulnp for UDP conflicts"
|
||
exit 1
|
||
fi
|
||
kv message "Neko WebRTC config reapplied (${instance_count} browser instances)"
|
||
}
|
||
|
||
service_neko_restart() {
|
||
service_neko_reconfigure
|
||
}
|
||
|
||
export_neko_env() {
|
||
export NEKO_HTTP_PORT="${NEKO_HTTP_PORT:-9200}"
|
||
export NEKO_HTTP_PORT_1="${NEKO_HTTP_PORT_1:-$NEKO_HTTP_PORT}"
|
||
export NEKO_HTTP_PORT_2="${NEKO_HTTP_PORT_2:-9201}"
|
||
export NEKO_HTTP_PORT_3="${NEKO_HTTP_PORT_3:-9202}"
|
||
export NEKO_INSTANCE_COUNT="${NEKO_INSTANCE_COUNT:-2}"
|
||
export NEKO_USER_PASS="${NEKO_USER_PASS:-12345678}"
|
||
export NEKO_ADMIN_PASS="${NEKO_ADMIN_PASS:-12345678}"
|
||
export NEKO_CONTAINER_USER="${NEKO_CONTAINER_USER:-neko}"
|
||
export NEKO_CONTAINER_HOME="${NEKO_CONTAINER_HOME:-/home/$NEKO_CONTAINER_USER}"
|
||
export NEKO_CONTAINER_UID="${NEKO_CONTAINER_UID:-1000}"
|
||
export NEKO_CONTAINER_GID="${NEKO_CONTAINER_GID:-1000}"
|
||
export NEKO_DISPLAY="${NEKO_DISPLAY:-:99.0}"
|
||
export ENV_USER="${ENV_USER:-$NEKO_CONTAINER_USER}"
|
||
export ENV_DISPLAY="${ENV_DISPLAY:-$NEKO_DISPLAY}"
|
||
export NEKO_HOST_ASSET_DIR="${NEKO_HOST_ASSET_DIR:-}"
|
||
export NEKO_PROFILE_HOST_DIR="${NEKO_PROFILE_HOST_DIR:-$(resolve_neko_profile_dir)}"
|
||
export NEKO_PROFILE_HOST_DIR_1="${NEKO_PROFILE_HOST_DIR_1:-$(resolve_neko_profile_dir_for_instance 1)}"
|
||
export NEKO_PROFILE_HOST_DIR_2="${NEKO_PROFILE_HOST_DIR_2:-$(resolve_neko_profile_dir_for_instance 2)}"
|
||
export NEKO_PROFILE_HOST_DIR_3="${NEKO_PROFILE_HOST_DIR_3:-$(resolve_neko_profile_dir_for_instance 3)}"
|
||
export NEKO_START_URL="${NEKO_START_URL:-https://accounts.google.com/ServiceLogin?continue=https://www.youtube.com/}"
|
||
export NEKO_IMAGE="$(resolve_neko_image)"
|
||
export NEKO_BROWSER_SUPERVISOR_CONF="$(resolve_neko_supervisor_conf)"
|
||
export NEKO_SHM_SIZE="${NEKO_SHM_SIZE:-512mb}"
|
||
export NEKO_DESKTOP_SCREEN="${NEKO_DESKTOP_SCREEN:-1280x720@15}"
|
||
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)"
|
||
neko_browser_proxy_auto_enable_if_needed
|
||
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}"
|
||
export NEKO_TAMPERMONKEY_REPAIR="${NEKO_TAMPERMONKEY_REPAIR:-1}"
|
||
export NEKO_WEBRTC_UDP_RANGE="${NEKO_WEBRTC_UDP_RANGE:-52000-52098}"
|
||
export NEKO_WEBRTC_UDP_RANGE_1="${NEKO_WEBRTC_UDP_RANGE_1:-$NEKO_WEBRTC_UDP_RANGE}"
|
||
export NEKO_WEBRTC_UDP_RANGE_2="${NEKO_WEBRTC_UDP_RANGE_2:-52101-52199}"
|
||
export NEKO_WEBRTC_UDP_RANGE_3="${NEKO_WEBRTC_UDP_RANGE_3:-52201-52299}"
|
||
export NEKO_WEBRTC_EPR="${NEKO_WEBRTC_EPR:-$NEKO_WEBRTC_UDP_RANGE_1}"
|
||
export NEKO_WEBRTC_EPR_1="${NEKO_WEBRTC_EPR_1:-$NEKO_WEBRTC_EPR}"
|
||
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}"
|
||
# 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
|
||
export NEKO_WEBRTC_ICELITE="${NEKO_WEBRTC_ICELITE:-1}"
|
||
else
|
||
export NEKO_WEBRTC_ICELITE=0
|
||
fi
|
||
export NEKO_WEBRTC_TCPMUX="${NEKO_WEBRTC_TCPMUX:-52300}"
|
||
export NEKO_WEBRTC_UDPMUX="${NEKO_WEBRTC_UDPMUX:-52300}"
|
||
export NEKO_WEBRTC_TCPMUX_1="${NEKO_WEBRTC_TCPMUX_1:-$NEKO_WEBRTC_TCPMUX}"
|
||
export NEKO_WEBRTC_TCPMUX_2="${NEKO_WEBRTC_TCPMUX_2:-52301}"
|
||
export NEKO_WEBRTC_TCPMUX_3="${NEKO_WEBRTC_TCPMUX_3:-52302}"
|
||
export NEKO_WEBRTC_UDPMUX_1="${NEKO_WEBRTC_UDPMUX_1:-$NEKO_WEBRTC_UDPMUX}"
|
||
export NEKO_WEBRTC_UDPMUX_2="${NEKO_WEBRTC_UDPMUX_2:-52301}"
|
||
export NEKO_WEBRTC_UDPMUX_3="${NEKO_WEBRTC_UDPMUX_3:-52302}"
|
||
target_nat1to1="$(resolve_neko_nat1to1)"
|
||
if [ -n "$target_nat1to1" ]; then
|
||
if [ "${NEKO_WEBRTC_NAT1TO1:-}" != "$target_nat1to1" ]; then
|
||
stack_env_upsert NEKO_WEBRTC_NAT1TO1 "$target_nat1to1"
|
||
fi
|
||
export NEKO_WEBRTC_NAT1TO1="$target_nat1to1"
|
||
fi
|
||
export NEKO_PLUGINS_ENABLED="${NEKO_PLUGINS_ENABLED:-true}"
|
||
}
|
||
|
||
verify_neko_instances_running() {
|
||
local count="${1:-}" idx name port state code failed=0
|
||
[ -n "$count" ] || count="$(resolve_neko_instance_count)"
|
||
sleep 2
|
||
for idx in $(seq 1 "$count"); do
|
||
name="live-neko-$idx"
|
||
port="$(resolve_neko_http_port_for_instance "$idx")"
|
||
state="$(docker_container_status "$name" || true)"
|
||
code="$(neko_http_probe_code "$port")"
|
||
if [[ "$state" != running* ]] || [ "$code" = "000" ]; then
|
||
failed=1
|
||
kv message "Neko instance ${idx} unhealthy state=${state:-missing} http=${code} port=${port}"
|
||
if have docker; then
|
||
docker inspect "$name" --format 'err={{.State.Error}} exit={{.State.ExitCode}}' 2>/dev/null || true
|
||
docker logs --tail 30 "$name" 2>&1 | tail -20 || true
|
||
fi
|
||
fi
|
||
done
|
||
return "$failed"
|
||
}
|
||
|
||
service_neko_stop() {
|
||
if [ ! -f "$ROOT_DIR/tools/render_neko_compose.py" ]; then
|
||
kv message "Neko compose missing"
|
||
return 0
|
||
fi
|
||
local compose_file
|
||
export_neko_env
|
||
compose_file="$(render_neko_compose_file)"
|
||
compose_in_service_dir "$compose_file" down --remove-orphans
|
||
kv message "Neko stack stopped"
|
||
}
|
||
|
||
service_mitmproxy_status() {
|
||
if [ "${ENABLE_MITMPROXY:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_MITMPROXY=1 in config/system-stack.env to enable mitmproxy"
|
||
return 0
|
||
fi
|
||
if ! have docker || [ ! -f "$MITMPROXY_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or mitmproxy compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-mitmproxy || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$MITMPROXY_WEB_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${MITMPROXY_WEB_PORT}"
|
||
kv detail "${state:-container=missing};proxy=${MITMPROXY_PROXY_PORT}"
|
||
}
|
||
|
||
service_mitmproxy_start() {
|
||
compose_in_service_dir "$MITMPROXY_COMPOSE" up -d
|
||
kv message "mitmproxy started"
|
||
}
|
||
|
||
service_mitmproxy_stop() {
|
||
compose_in_service_dir "$MITMPROXY_COMPOSE" down
|
||
kv message "mitmproxy stopped"
|
||
}
|
||
|
||
service_vscode_server_status() {
|
||
if [ "${ENABLE_VSCODE_SERVER:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_VSCODE_SERVER=1 in config/system-stack.env to enable code-server"
|
||
return 0
|
||
fi
|
||
if ! have docker || [ ! -f "$VSCODE_SERVER_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or code-server compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-code-server || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$VSCODE_SERVER_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${VSCODE_SERVER_PORT}"
|
||
kv detail "${state:-container=missing};password=${VSCODE_SERVER_PASSWORD}"
|
||
}
|
||
|
||
service_vscode_server_start() {
|
||
compose_in_service_dir "$VSCODE_SERVER_COMPOSE" up -d
|
||
kv message "code-server started"
|
||
}
|
||
|
||
service_vscode_server_stop() {
|
||
compose_in_service_dir "$VSCODE_SERVER_COMPOSE" down
|
||
kv message "code-server stopped"
|
||
}
|
||
|
||
service_uptime_kuma_status() {
|
||
if [ "${ENABLE_UPTIME_KUMA:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_UPTIME_KUMA=1 in config/system-stack.env to enable Uptime Kuma"
|
||
return 0
|
||
fi
|
||
if ! have docker || [ ! -f "$UPTIME_KUMA_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or Uptime Kuma compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-uptime-kuma || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$UPTIME_KUMA_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${UPTIME_KUMA_PORT}"
|
||
kv detail "${state:-container=missing}"
|
||
}
|
||
|
||
service_uptime_kuma_start() {
|
||
compose_in_service_dir "$UPTIME_KUMA_COMPOSE" up -d
|
||
kv message "Uptime Kuma started"
|
||
}
|
||
|
||
service_uptime_kuma_stop() {
|
||
compose_in_service_dir "$UPTIME_KUMA_COMPOSE" down
|
||
kv message "Uptime Kuma stopped"
|
||
}
|
||
|
||
service_pulseaudio_status() {
|
||
if [ "${ENABLE_PULSEAUDIO:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_PULSEAUDIO=1 in config/system-stack.env to enable PulseAudio"
|
||
return 0
|
||
fi
|
||
if ! unit_exists live-pulseaudio.service && ! have pulseaudio; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "PulseAudio is not installed"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
kv running "$(unit_active live-pulseaudio.service && echo 1 || echo 0)"
|
||
kv status "$(unit_active live-pulseaudio.service && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$PULSEAUDIO_TCP_PORT"
|
||
kv url "${CONSOLE_BASE_URL}/?view=services"
|
||
kv detail "tcp=${PULSEAUDIO_TCP_PORT}; control=${CONSOLE_BASE_URL}"
|
||
}
|
||
|
||
service_pulseaudio_start() {
|
||
systemctl enable --now live-pulseaudio.service
|
||
kv message "PulseAudio started"
|
||
}
|
||
|
||
service_pulseaudio_stop() {
|
||
systemctl stop live-pulseaudio.service
|
||
kv message "PulseAudio stopped"
|
||
}
|
||
|
||
service_kmscube_status() {
|
||
if [ "${ENABLE_KMSCUBE:-1}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_KMSCUBE=1 in config/system-stack.env to enable kmscube diagnostics"
|
||
return 0
|
||
fi
|
||
if ! unit_exists live-kmscube.service && ! have kmscube; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "kmscube is not installed"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
kv running "$(unit_active live-kmscube.service && echo 1 || echo 0)"
|
||
kv status "$(unit_active live-kmscube.service && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port ""
|
||
kv url "${CONSOLE_BASE_URL}/?view=services"
|
||
kv detail "launcher=${KMSCUBE_LAUNCHER}; drm=${KMSCUBE_DRM_DEVICE}"
|
||
}
|
||
|
||
service_kmscube_start() {
|
||
systemctl start live-kmscube.service
|
||
kv message "kmscube started"
|
||
}
|
||
|
||
service_kmscube_stop() {
|
||
systemctl stop live-kmscube.service
|
||
kv message "kmscube stopped"
|
||
}
|
||
|
||
service_openclaw_status() {
|
||
if [ "${ENABLE_OPENCLAW:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_OPENCLAW=1 in config/system-stack.env to enable OpenClaw"
|
||
return 0
|
||
fi
|
||
if ! have docker || [ ! -f "$OPENCLAW_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or OpenClaw compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-openclaw || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$OPENCLAW_GATEWAY_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${OPENCLAW_GATEWAY_PORT}"
|
||
kv detail "${state:-container=missing};bridge=${OPENCLAW_BRIDGE_PORT}"
|
||
}
|
||
|
||
service_openclaw_start() {
|
||
mkdir -p "$OPENCLAW_CONFIG_DIR" "$OPENCLAW_WORKSPACE_DIR"
|
||
compose_in_service_dir "$OPENCLAW_COMPOSE" up -d
|
||
kv message "OpenClaw started"
|
||
}
|
||
|
||
service_openclaw_stop() {
|
||
compose_in_service_dir "$OPENCLAW_COMPOSE" down
|
||
kv message "OpenClaw stopped"
|
||
}
|
||
|
||
service_homeassistant_status() {
|
||
if [ "${ENABLE_HOMEASSISTANT:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_HOMEASSISTANT=1 in config/system-stack.env to enable Home Assistant"
|
||
return 0
|
||
fi
|
||
if ! have docker || [ ! -f "$HOMEASSISTANT_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or Home Assistant compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-homeassistant || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$HOMEASSISTANT_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${HOMEASSISTANT_PORT}"
|
||
kv detail "${state:-container=missing};config=${HOMEASSISTANT_CONFIG_DIR};network=host"
|
||
}
|
||
|
||
service_homeassistant_start() {
|
||
mkdir -p "$HOMEASSISTANT_CONFIG_DIR"
|
||
chmod -R a+rwX "$HOMEASSISTANT_CONFIG_DIR" 2>/dev/null || true
|
||
compose_in_service_dir "$HOMEASSISTANT_COMPOSE" up -d
|
||
kv message "Home Assistant started"
|
||
}
|
||
|
||
service_homeassistant_stop() {
|
||
compose_in_service_dir "$HOMEASSISTANT_COMPOSE" down
|
||
kv message "Home Assistant stopped"
|
||
}
|
||
|
||
frigate_video_stable_path() {
|
||
local dev="$1" dir link target
|
||
for dir in /dev/v4l/by-id /dev/v4l/by-path; do
|
||
[ -d "$dir" ] || continue
|
||
for link in "$dir"/*; do
|
||
[ -e "$link" ] || continue
|
||
target="$(readlink -f "$link" 2>/dev/null || true)"
|
||
if [ "$target" = "$dev" ]; then
|
||
printf '%s' "$link"
|
||
return 0
|
||
fi
|
||
done
|
||
done
|
||
printf '%s' "$dev"
|
||
}
|
||
|
||
frigate_video_node_is_capture() {
|
||
local dev="$1" stable="${2:-}" info formats
|
||
case "$stable" in
|
||
*video-index0*) return 0 ;;
|
||
*video-index[1-9]*) return 1 ;;
|
||
esac
|
||
if have v4l2-ctl; then
|
||
info="$(v4l2-ctl -D -d "$dev" 2>/dev/null || true)"
|
||
if printf '%s\n' "$info" | grep -Eiq 'Video Capture|Video Capture Multiplanar'; then
|
||
return 0
|
||
fi
|
||
if printf '%s\n' "$info" | grep -Eiq 'Metadata Capture|Video Output|VBI'; then
|
||
return 1
|
||
fi
|
||
formats="$(v4l2-ctl -d "$dev" --list-formats-ext 2>/dev/null || true)"
|
||
[ -n "$formats" ] && return 0
|
||
fi
|
||
return 0
|
||
}
|
||
|
||
frigate_detect_usb_cameras() {
|
||
local dev base props name bus stable
|
||
for dev in $(find /dev -maxdepth 1 -type c -name 'video*' 2>/dev/null | sort -V); do
|
||
[ -e "$dev" ] || continue
|
||
base="$(basename "$dev")"
|
||
props="$(udevadm info -q property -n "$dev" 2>/dev/null || true)"
|
||
name="$(cat "/sys/class/video4linux/${base}/name" 2>/dev/null || true)"
|
||
bus="$(printf '%s\n' "$props" | awk -F= '$1=="ID_BUS"{print $2; exit}')"
|
||
if [ "$bus" = "usb" ] || printf '%s\n%s\n' "$props" "$name" | grep -Eiq 'uvc|usb|webcam|camera'; then
|
||
stable="$(frigate_video_stable_path "$dev")"
|
||
frigate_video_node_is_capture "$dev" "$stable" || continue
|
||
printf '%s|%s\n' "$stable" "${name:-USB camera}"
|
||
fi
|
||
done
|
||
}
|
||
|
||
frigate_config_has_camera_entries() {
|
||
local cfg="$1"
|
||
awk '
|
||
/^[[:space:]]*cameras:[[:space:]]*$/ { in_cameras=1; next }
|
||
in_cameras && /^[^[:space:]]/ { in_cameras=0 }
|
||
in_cameras && /^[[:space:]]{2}[A-Za-z0-9_-]+:[[:space:]]*$/ { found=1 }
|
||
END { exit found ? 0 : 1 }
|
||
' "$cfg"
|
||
}
|
||
|
||
frigate_config_can_be_autogenerated() {
|
||
local cfg="$1"
|
||
[ ! -s "$cfg" ] && return 0
|
||
grep -q 'Managed by live stack USB camera autodetect' "$cfg" 2>/dev/null && return 0
|
||
grep -Eq '^[[:space:]]*cameras:[[:space:]]*\{\}[[:space:]]*$' "$cfg" 2>/dev/null && return 0
|
||
if grep -Eq '^[[:space:]]*cameras:[[:space:]]*$' "$cfg" 2>/dev/null && ! frigate_config_has_camera_entries "$cfg"; then
|
||
return 0
|
||
fi
|
||
return 1
|
||
}
|
||
|
||
render_frigate_usb_compose_override() {
|
||
local devices="$1" tmp dev host
|
||
if [ -z "$devices" ]; then
|
||
rm -f "$FRIGATE_USB_COMPOSE"
|
||
return 0
|
||
fi
|
||
tmp="$(mktemp)"
|
||
printf '%s\n' "$devices" | while IFS='|' read -r dev _name; do
|
||
[ -n "$dev" ] || continue
|
||
host="$(readlink -f "$dev" 2>/dev/null || true)"
|
||
[ -n "$host" ] || host="$dev"
|
||
case "$host" in
|
||
/dev/video*) [ -e "$host" ] && printf ' - "%s:%s"\n' "$host" "$host" ;;
|
||
esac
|
||
done | awk '!seen[$0]++' >"$tmp"
|
||
if [ ! -s "$tmp" ]; then
|
||
rm -f "$tmp" "$FRIGATE_USB_COMPOSE"
|
||
return 0
|
||
fi
|
||
{
|
||
printf '%s\n' '# Generated by service_ctl.sh - do not hand-edit'
|
||
printf '%s\n' 'services:'
|
||
printf '%s\n' ' frigate:'
|
||
printf '%s\n' ' devices:'
|
||
cat "$tmp"
|
||
if [ -d /dev/v4l ]; then
|
||
printf '%s\n' ' volumes:'
|
||
printf '%s\n' ' - "/dev/v4l:/dev/v4l:ro"'
|
||
fi
|
||
} >"$FRIGATE_USB_COMPOSE"
|
||
rm -f "$tmp"
|
||
}
|
||
|
||
render_frigate_usb_config() {
|
||
local devices="$1" cfg="$FRIGATE_CONFIG_DIR/config.yml" dev name cam
|
||
mkdir -p "$FRIGATE_CONFIG_DIR"
|
||
if ! frigate_config_can_be_autogenerated "$cfg"; then
|
||
return 0
|
||
fi
|
||
{
|
||
printf '%s\n' '# Managed by live stack USB camera autodetect'
|
||
printf '%s\n' 'mqtt:'
|
||
printf '%s\n' ' enabled: false'
|
||
printf '%s\n' ''
|
||
printf '%s\n' 'detectors:'
|
||
printf '%s\n' ' cpu1:'
|
||
printf '%s\n' ' type: cpu'
|
||
printf '%s\n' ''
|
||
if [ -z "$devices" ]; then
|
||
printf '%s\n' 'cameras: {}'
|
||
else
|
||
printf '%s\n' 'cameras:'
|
||
printf '%s\n' "$devices" | while IFS='|' read -r dev name; do
|
||
[ -n "$dev" ] || continue
|
||
cam="$(basename "$dev" | tr -c 'A-Za-z0-9_' '_')"
|
||
[ -n "$cam" ] || cam="video"
|
||
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'
|
||
printf ' - -f\n'
|
||
printf ' - v4l2\n'
|
||
printf ' - -input_format\n'
|
||
printf ' - mjpeg\n'
|
||
printf ' - -video_size\n'
|
||
printf ' - 640x480\n'
|
||
printf ' - -framerate\n'
|
||
printf ' - "5"\n'
|
||
printf ' roles:\n'
|
||
printf ' - detect\n'
|
||
printf ' detect:\n'
|
||
printf ' enabled: true\n'
|
||
printf ' width: 640\n'
|
||
printf ' height: 480\n'
|
||
printf ' fps: 5\n'
|
||
done
|
||
fi
|
||
} >"$cfg"
|
||
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
|
||
install -m 0666 "$ROOT_DIR/services/frigate/config/config.yml" "$FRIGATE_CONFIG_DIR/config.yml"
|
||
fi
|
||
chmod -R a+rwX "$FRIGATE_CONFIG_DIR" "$FRIGATE_MEDIA_DIR" 2>/dev/null || true
|
||
}
|
||
|
||
service_frigate_status() {
|
||
if [ "${ENABLE_FRIGATE:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_FRIGATE=1 in config/system-stack.env to enable Frigate"
|
||
return 0
|
||
fi
|
||
if ! have docker || [ ! -f "$FRIGATE_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or Frigate compose file is missing"
|
||
return 0
|
||
fi
|
||
local state status_value
|
||
state="$(docker_container_status live-frigate || true)"
|
||
local usb_count
|
||
usb_count="$(frigate_detect_usb_cameras | awk 'NF{n++} END{print n+0}')"
|
||
if [[ "$state" == running* ]]; then
|
||
status_value=online
|
||
elif [[ "$state" == error=* ]]; then
|
||
status_value=error
|
||
else
|
||
status_value=stopped
|
||
fi
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$status_value"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$FRIGATE_WEB_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${FRIGATE_WEB_PORT}"
|
||
kv detail "${state:-container=missing};usb_cameras=${usb_count};rtsp=${FRIGATE_RTSP_PORT};webrtc=${FRIGATE_WEBRTC_PORT};config=${FRIGATE_CONFIG_DIR};media=${FRIGATE_MEDIA_DIR};usb_compose=${FRIGATE_USB_COMPOSE}"
|
||
}
|
||
|
||
service_frigate_start() {
|
||
local devices
|
||
ensure_frigate_default_config
|
||
devices="$(frigate_detect_usb_cameras || true)"
|
||
render_frigate_usb_config "$devices"
|
||
render_frigate_usb_compose_override "$devices"
|
||
compose_frigate up -d
|
||
kv message "Frigate started"
|
||
}
|
||
|
||
service_frigate_stop() {
|
||
compose_frigate down
|
||
kv message "Frigate stopped"
|
||
}
|
||
|
||
service_frigate_auto() {
|
||
if [ "${ENABLE_FRIGATE:-0}" != "1" ] || [ "${FRIGATE_USB_AUTOSTART:-0}" != "1" ]; then
|
||
kv message "Frigate USB autostart disabled"
|
||
return 0
|
||
fi
|
||
local devices count state
|
||
devices="$(frigate_detect_usb_cameras || true)"
|
||
count="$(printf '%s\n' "$devices" | awk 'NF{n++} END{print n+0}')"
|
||
if [ "$count" -le 0 ]; then
|
||
render_frigate_usb_compose_override ""
|
||
kv message "No USB/UVC camera detected"
|
||
return 0
|
||
fi
|
||
render_frigate_usb_config "$devices"
|
||
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
|
||
fi
|
||
compose_frigate up -d
|
||
kv message "Frigate auto-started; usb_cameras=${count}"
|
||
}
|
||
|
||
normalize_wol_mac() {
|
||
printf '%s' "$1" | tr '[:lower:]' '[:upper:]' | sed 's/[^0-9A-F]//g'
|
||
}
|
||
|
||
service_wol_status() {
|
||
if [ "${ENABLE_WOL:-1}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_WOL=1 in config/system-stack.env to enable Wake-on-LAN"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
kv running 0
|
||
kv status ready
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$WOL_PORT"
|
||
kv url "${CONSOLE_BASE_URL}/?view=studio"
|
||
kv detail "broadcast=${WOL_BROADCAST};port=${WOL_PORT};send_magic_packet"
|
||
}
|
||
|
||
service_wol_start() {
|
||
kv message "Wake-on-LAN is ready; use the Studio WOL form or service_ctl wol wake <mac>"
|
||
}
|
||
|
||
service_wol_stop() {
|
||
kv message "Wake-on-LAN has no background daemon to stop"
|
||
}
|
||
|
||
service_wol_wake() {
|
||
local mac="${ACTION_ARG1:-}" broadcast="${ACTION_ARG2:-$WOL_BROADCAST}" port="${WOL_PORT:-9}" clean
|
||
clean="$(normalize_wol_mac "$mac")"
|
||
if [ "${#clean}" -ne 12 ]; then
|
||
kv message "Invalid MAC address"
|
||
exit 1
|
||
fi
|
||
python3 - "$clean" "$broadcast" "$port" <<'PY'
|
||
import socket
|
||
import sys
|
||
|
||
mac, broadcast, port = sys.argv[1], sys.argv[2], int(sys.argv[3])
|
||
payload = bytes.fromhex("FF" * 6 + mac * 16)
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||
try:
|
||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||
sock.sendto(payload, (broadcast, port))
|
||
finally:
|
||
sock.close()
|
||
PY
|
||
kv message "Wake-on-LAN packet sent to $mac via $broadcast:$port"
|
||
}
|
||
|
||
ensure_ntfy_default_config() {
|
||
mkdir -p "$NTFY_CONFIG_DIR" "$NTFY_CACHE_DIR"
|
||
if [ ! -s "$NTFY_CONFIG_DIR/server.yml" ] && [ -s "$ROOT_DIR/services/ntfy/config/server.yml" ]; then
|
||
install -m 0666 "$ROOT_DIR/services/ntfy/config/server.yml" "$NTFY_CONFIG_DIR/server.yml"
|
||
sed -i "s#http://live.local:8089#http://${HOSTNAME_ALIAS}.local:${NTFY_PORT}#g" "$NTFY_CONFIG_DIR/server.yml" 2>/dev/null || true
|
||
fi
|
||
chmod -R a+rwX "$NTFY_CONFIG_DIR" "$NTFY_CACHE_DIR" 2>/dev/null || true
|
||
}
|
||
|
||
service_ntfy_status() {
|
||
if [ "${ENABLE_NTFY:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_NTFY=1 in config/system-stack.env to enable ntfy"
|
||
return 0
|
||
fi
|
||
if ! have docker || [ ! -f "$NTFY_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or ntfy compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-ntfy || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$NTFY_PORT"
|
||
kv url "http://${HOSTNAME_ALIAS}.local:${NTFY_PORT}"
|
||
kv detail "${state:-container=missing};config=${NTFY_CONFIG_DIR};cache=${NTFY_CACHE_DIR}"
|
||
}
|
||
|
||
service_ntfy_start() {
|
||
ensure_ntfy_default_config
|
||
compose_in_service_dir "$NTFY_COMPOSE" up -d
|
||
kv message "ntfy started"
|
||
}
|
||
|
||
service_ntfy_stop() {
|
||
compose_in_service_dir "$NTFY_COMPOSE" down
|
||
kv message "ntfy stopped"
|
||
}
|
||
|
||
coturn_secret_ready() {
|
||
[ -n "${COTURN_STATIC_AUTH_SECRET:-}" ] && [ "${COTURN_STATIC_AUTH_SECRET:-}" != "change-me" ]
|
||
}
|
||
|
||
service_coturn_status() {
|
||
if [ "${ENABLE_COTURN:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_COTURN=1 and COTURN_STATIC_AUTH_SECRET in config/system-stack.env to enable TURN relay"
|
||
return 0
|
||
fi
|
||
if ! coturn_secret_ready; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unsafe
|
||
kv detail "COTURN_STATIC_AUTH_SECRET is empty or unsafe; refusing to run an open TURN relay"
|
||
return 0
|
||
fi
|
||
if ! have docker || [ ! -f "$COTURN_COMPOSE" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Docker or coturn compose file is missing"
|
||
return 0
|
||
fi
|
||
local state
|
||
state="$(docker_container_status live-coturn || true)"
|
||
kv available 1
|
||
kv running "$([[ "$state" == running* ]] && echo 1 || echo 0)"
|
||
kv status "$([[ "$state" == running* ]] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$COTURN_PORT"
|
||
kv url "turn:${HOSTNAME_ALIAS}.local:${COTURN_PORT}?transport=udp"
|
||
kv detail "${state:-container=missing};turn=${COTURN_PORT};turns=${COTURN_TLS_PORT};relay=${COTURN_MIN_PORT}-${COTURN_MAX_PORT};realm=${COTURN_REALM}"
|
||
}
|
||
|
||
service_coturn_start() {
|
||
if [ "${ENABLE_COTURN:-0}" != "1" ]; then
|
||
kv message "Set ENABLE_COTURN=1 first"
|
||
exit 1
|
||
fi
|
||
if ! coturn_secret_ready; then
|
||
kv message "Set a non-empty COTURN_STATIC_AUTH_SECRET before starting coturn"
|
||
exit 1
|
||
fi
|
||
allow_udp_port_if_firewall_active "$COTURN_PORT"
|
||
allow_tcp_port_if_firewall_active "$COTURN_PORT"
|
||
allow_tcp_port_if_firewall_active "$COTURN_TLS_PORT"
|
||
allow_udp_range_if_firewall_active "${COTURN_MIN_PORT}:${COTURN_MAX_PORT}"
|
||
compose_in_service_dir "$COTURN_COMPOSE" up -d
|
||
kv message "coturn started"
|
||
}
|
||
|
||
service_coturn_stop() {
|
||
compose_in_service_dir "$COTURN_COMPOSE" down
|
||
kv message "coturn stopped"
|
||
}
|
||
|
||
redroid_default_name() {
|
||
printf '%s' "${REDROID_INSTANCE_PREFIX}-1"
|
||
}
|
||
|
||
redroid_resolved_image() {
|
||
if [ -n "${REDROID_IMAGE:-}" ]; then
|
||
printf '%s' "$REDROID_IMAGE"
|
||
return 0
|
||
fi
|
||
case "$(uname -m)" in
|
||
x86_64|amd64)
|
||
printf '%s' "${REDROID_IMAGE_X86_ALT:-$REDROID_IMAGE_X86}"
|
||
;;
|
||
aarch64|arm64)
|
||
printf '%s' "${REDROID_IMAGE_ARM64_ALT:-$REDROID_IMAGE_ARM64}"
|
||
;;
|
||
*)
|
||
printf '%s' "$REDROID_IMAGE_X86"
|
||
;;
|
||
esac
|
||
}
|
||
|
||
redroid_binderfs_ready() {
|
||
[ -e /dev/binderfs/binder-control ]
|
||
}
|
||
|
||
redroid_legacy_binder_devices_ready() {
|
||
[ -e /dev/binder ] && [ -e /dev/hwbinder ] && [ -e /dev/vndbinder ]
|
||
}
|
||
|
||
redroid_binder_mode() {
|
||
if redroid_binderfs_ready; then
|
||
printf '%s\n' binderfs
|
||
return 0
|
||
fi
|
||
if redroid_legacy_binder_devices_ready; then
|
||
printf '%s\n' legacy
|
||
return 0
|
||
fi
|
||
return 1
|
||
}
|
||
|
||
redroid_binder_is_provisionable() {
|
||
redroid_binder_mode >/dev/null 2>&1 && return 0
|
||
grep -qw binder /proc/filesystems 2>/dev/null
|
||
}
|
||
|
||
redroid_support_detail() {
|
||
if redroid_binderfs_ready; then
|
||
printf '%s' "binderfs mounted"
|
||
return 0
|
||
fi
|
||
if redroid_legacy_binder_devices_ready; then
|
||
printf '%s' "legacy binder devices available"
|
||
return 0
|
||
fi
|
||
if grep -qw binder /proc/filesystems 2>/dev/null; then
|
||
printf '%s' "binder filesystem supported but not mounted"
|
||
return 0
|
||
fi
|
||
printf '%s' "binder kernel support unavailable"
|
||
}
|
||
|
||
ensure_redroid_host_support() {
|
||
if redroid_binder_mode >/dev/null 2>&1; then
|
||
return 0
|
||
fi
|
||
if have modprobe; then
|
||
modprobe binder_linux devices=binder,hwbinder,vndbinder >/dev/null 2>&1 || \
|
||
modprobe binder_linux >/dev/null 2>&1 || true
|
||
fi
|
||
if redroid_binder_mode >/dev/null 2>&1; then
|
||
return 0
|
||
fi
|
||
if grep -qw binder /proc/filesystems 2>/dev/null; then
|
||
mkdir -p /dev/binderfs
|
||
if have mountpoint; then
|
||
if ! mountpoint -q /dev/binderfs 2>/dev/null; then
|
||
mount -t binder binder /dev/binderfs >/dev/null 2>&1 || \
|
||
mount -t binderfs binderfs /dev/binderfs >/dev/null 2>&1 || true
|
||
fi
|
||
elif ! grep -qE '[[:space:]]/dev/binderfs[[:space:]]' /proc/mounts 2>/dev/null; then
|
||
mount -t binder binder /dev/binderfs >/dev/null 2>&1 || \
|
||
mount -t binderfs binderfs /dev/binderfs >/dev/null 2>&1 || true
|
||
fi
|
||
fi
|
||
redroid_binder_mode >/dev/null 2>&1
|
||
}
|
||
|
||
redroid_container_matches_host_support() {
|
||
local name="$1"
|
||
case "$(redroid_binder_mode 2>/dev/null || true)" in
|
||
binderfs)
|
||
docker inspect -f '{{range .Mounts}}{{println .Destination}}{{end}}' "$name" 2>/dev/null | grep -qx '/dev/binderfs'
|
||
;;
|
||
legacy)
|
||
docker inspect -f '{{range .HostConfig.Devices}}{{println .PathOnHost}}{{end}}' "$name" 2>/dev/null | grep -qx '/dev/binder' && \
|
||
docker inspect -f '{{range .HostConfig.Devices}}{{println .PathOnHost}}{{end}}' "$name" 2>/dev/null | grep -qx '/dev/hwbinder' && \
|
||
docker inspect -f '{{range .HostConfig.Devices}}{{println .PathOnHost}}{{end}}' "$name" 2>/dev/null | grep -qx '/dev/vndbinder'
|
||
;;
|
||
*)
|
||
return 1
|
||
;;
|
||
esac
|
||
}
|
||
|
||
service_redroid_status() {
|
||
if [ "${ENABLE_REDROID:-0}" != "1" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Set ENABLE_REDROID=1 in config/system-stack.env to enable Redroid"
|
||
return 0
|
||
fi
|
||
if ! have docker; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "docker not installed"
|
||
return 0
|
||
fi
|
||
local default_name running_line all_lines binder_detail
|
||
default_name="$(redroid_default_name)"
|
||
binder_detail="$(redroid_support_detail)"
|
||
running_line="$(docker ps --format '{{.Names}} {{.Status}} {{.Ports}}' 2>/dev/null | grep -E "^${REDROID_INSTANCE_PREFIX}-|^${default_name} " | head -n1 || true)"
|
||
all_lines="$(docker ps -a --format '{{.Names}} {{.Image}} {{.Status}} {{.Ports}}' 2>/dev/null | grep -i redroid || true)"
|
||
if ! redroid_binder_mode >/dev/null 2>&1 && ! redroid_binder_is_provisionable; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$REDROID_DEFAULT_PORT"
|
||
kv url ""
|
||
kv detail "${binder_detail}; image=$(redroid_resolved_image); default=${default_name}; adb=${HOSTNAME_ALIAS}.local:${REDROID_DEFAULT_PORT}"
|
||
return 0
|
||
fi
|
||
kv available 1
|
||
kv running "$([ -n "$running_line" ] && echo 1 || echo 0)"
|
||
kv status "$([ -n "$running_line" ] && echo online || echo stopped)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port "$REDROID_DEFAULT_PORT"
|
||
kv url ""
|
||
kv detail "${all_lines:-binder=${binder_detail}; image=$(redroid_resolved_image); default=${default_name}; adb=${HOSTNAME_ALIAS}.local:${REDROID_DEFAULT_PORT}; boot=${REDROID_BOOT_ARGS}; expect_gms=${REDROID_EXPECT_GMS}; expect_magisk=${REDROID_EXPECT_MAGISK}}"
|
||
}
|
||
|
||
service_redroid_start() {
|
||
if [ "${ENABLE_REDROID:-0}" != "1" ]; then
|
||
kv message "Set ENABLE_REDROID=1 first"
|
||
exit 1
|
||
fi
|
||
if ! have docker; then
|
||
kv message "docker not installed"
|
||
exit 1
|
||
fi
|
||
local name image data_dir
|
||
name="$(redroid_default_name)"
|
||
image="$(redroid_resolved_image)"
|
||
data_dir="${REDROID_DATA_PREFIX}-${name}"
|
||
if ! ensure_redroid_host_support; then
|
||
kv message "Redroid host prerequisites unavailable: $(redroid_support_detail)"
|
||
exit 1
|
||
fi
|
||
mkdir -p "$data_dir"
|
||
if docker ps --format '{{.Names}}' 2>/dev/null | grep -qx "$name"; then
|
||
if redroid_container_matches_host_support "$name"; then
|
||
kv message "Redroid already running"
|
||
exit 0
|
||
fi
|
||
docker rm -f "$name" >/dev/null
|
||
fi
|
||
if docker ps -a --format '{{.Names}}' 2>/dev/null | grep -qx "$name"; then
|
||
if ! redroid_container_matches_host_support "$name"; then
|
||
docker rm -f "$name" >/dev/null
|
||
else
|
||
docker start "$name" >/dev/null
|
||
kv message "Redroid container started"
|
||
exit 0
|
||
fi
|
||
fi
|
||
docker run -d \
|
||
--name "$name" \
|
||
--privileged \
|
||
--pull always \
|
||
--restart unless-stopped \
|
||
-p "${REDROID_DEFAULT_PORT}:5555" \
|
||
-v "${data_dir}:/data" \
|
||
-e "REDROID_GPU_MODE=${REDROID_GPU_MODE:-guest}" \
|
||
-e "REDROID_DEVICE_PROFILE=${REDROID_DEVICE_PROFILE:-pixel-7-pro}" \
|
||
$( [ "${REDROID_ADB_INSECURE:-1}" = "1" ] && printf '%s ' '-e' 'REDROID_ADB_INSECURE=1' ) \
|
||
$( [ -e /dev/kvm ] && printf '%s ' '--device' '/dev/kvm' ) \
|
||
$( [ -d /dev/dri ] && printf '%s ' '-v /dev/dri:/dev/dri' ) \
|
||
$( [ -d /dev/dma_heap ] && printf '%s ' '-v /dev/dma_heap:/dev/dma_heap' ) \
|
||
$( redroid_binderfs_ready && printf '%s ' '-v' '/dev/binderfs:/dev/binderfs' ) \
|
||
$( redroid_legacy_binder_devices_ready && printf '%s ' '--device' '/dev/binder' '--device' '/dev/hwbinder' '--device' '/dev/vndbinder' ) \
|
||
$( [ -e /dev/rga ] && printf '%s ' '--device /dev/rga' ) \
|
||
$( [ -e /dev/mpp_service ] && printf '%s ' '--device /dev/mpp_service' ) \
|
||
"$image" ${REDROID_BOOT_ARGS:-} >/dev/null
|
||
kv message "Redroid container created"
|
||
}
|
||
|
||
service_redroid_stop() {
|
||
local name
|
||
name="$(redroid_default_name)"
|
||
if docker ps --format '{{.Names}}' 2>/dev/null | grep -qx "$name"; then
|
||
docker stop "$name" >/dev/null
|
||
kv message "Redroid container stopped"
|
||
return 0
|
||
fi
|
||
kv message "Redroid container not running"
|
||
}
|
||
|
||
service_android_gateway_status() {
|
||
if ! have nmcli; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "需要 NetworkManager(nmcli) 才能创建共享连接"
|
||
return 0
|
||
fi
|
||
if [ -z "${ANDROID_DOWNLINK_IF:-}" ]; then
|
||
kv available 1
|
||
kv running 0
|
||
kv status skipped
|
||
kv detail "单网口或未配置下行口:无需透明网关。安卓可经 USB 网络共享 / Wi‑Fi / 同 LAN 访问板子。仅当板载第二以太网直连安卓板时,在 system-stack.env 设置 ANDROID_DOWNLINK_IF=eth1(或实际接口名)"
|
||
kv url ""
|
||
return 0
|
||
fi
|
||
if [ ! -d "/sys/class/net/$ANDROID_DOWNLINK_IF" ]; then
|
||
kv available 0
|
||
kv running 0
|
||
kv status unavailable
|
||
kv detail "Downlink interface $ANDROID_DOWNLINK_IF is missing"
|
||
return 0
|
||
fi
|
||
local active
|
||
active="$(nmcli -t -f NAME connection show --active 2>/dev/null | grep "^${ANDROID_GATEWAY_NAME}$" || true)"
|
||
kv available 1
|
||
kv running "$([ -n "$active" ] && echo 1 || echo 0)"
|
||
kv status "$([ -n "$active" ] && echo online || echo configured)"
|
||
kv host "${HOSTNAME_ALIAS}.local"
|
||
kv port ""
|
||
kv url ""
|
||
kv detail "uplink=${ANDROID_UPLINK_IF};downlink=${ANDROID_DOWNLINK_IF};gateway=${ANDROID_GATEWAY_CIDR}"
|
||
}
|
||
|
||
service_android_gateway_start() {
|
||
if [ -z "${ANDROID_DOWNLINK_IF:-}" ]; then
|
||
kv message "未配置 ANDROID_DOWNLINK_IF:单网口场景无需启动安卓网关"
|
||
exit 0
|
||
fi
|
||
if ! nmcli connection show "$ANDROID_GATEWAY_NAME" >/dev/null 2>&1; then
|
||
nmcli connection add type ethernet ifname "$ANDROID_DOWNLINK_IF" con-name "$ANDROID_GATEWAY_NAME" \
|
||
ipv4.method shared ipv4.addresses "$ANDROID_GATEWAY_CIDR" connection.autoconnect yes
|
||
fi
|
||
nmcli connection up "$ANDROID_GATEWAY_NAME"
|
||
kv message "Android gateway started"
|
||
}
|
||
|
||
service_android_gateway_stop() {
|
||
nmcli connection down "$ANDROID_GATEWAY_NAME" >/dev/null 2>&1 || true
|
||
kv message "Android gateway stopped"
|
||
}
|
||
|
||
dispatch() {
|
||
local fn="service_${SERVICE_ID}_${ACTION}"
|
||
if ! declare -F "$fn" >/dev/null 2>&1; then
|
||
if [ "$ACTION" = "restart" ] && declare -F "service_${SERVICE_ID}_stop" >/dev/null 2>&1 && declare -F "service_${SERVICE_ID}_start" >/dev/null 2>&1; then
|
||
"service_${SERVICE_ID}_stop" >/dev/null 2>&1 || true
|
||
"service_${SERVICE_ID}_start"
|
||
return 0
|
||
fi
|
||
if [ "$ACTION" = "install" ] && declare -F "service_${SERVICE_ID}_start" >/dev/null 2>&1; then
|
||
"service_${SERVICE_ID}_start"
|
||
return 0
|
||
fi
|
||
if [ "$ACTION" = "uninstall" ] && declare -F "service_${SERVICE_ID}_stop" >/dev/null 2>&1; then
|
||
"service_${SERVICE_ID}_stop" >/dev/null 2>&1 || true
|
||
kv message "Service uninstalled via stop fallback"
|
||
return 0
|
||
fi
|
||
if [ "$ACTION" = "upgrade" ] && declare -F "service_${SERVICE_ID}_stop" >/dev/null 2>&1 && declare -F "service_${SERVICE_ID}_start" >/dev/null 2>&1; then
|
||
"service_${SERVICE_ID}_stop" >/dev/null 2>&1 || true
|
||
"service_${SERVICE_ID}_start" >/dev/null 2>&1 || true
|
||
kv message "Service upgraded via restart fallback"
|
||
return 0
|
||
fi
|
||
echo "MESSAGE=Unsupported service/action: ${SERVICE_ID}/${ACTION}"
|
||
exit 1
|
||
fi
|
||
"$fn"
|
||
}
|
||
|
||
if [ -z "$SERVICE_ID" ]; then
|
||
echo "MESSAGE=Missing service id"
|
||
exit 1
|
||
fi
|
||
|
||
dispatch
|