104 lines
4.5 KiB
Bash
104 lines
4.5 KiB
Bash
#!/bin/bash
|
|
# Operator integration: upstream initializer plus the approved, pinned LAN TTS patch.
|
|
set -Eeuo pipefail
|
|
web_pid=''
|
|
gateway_pid=''
|
|
stopping=0
|
|
|
|
stop_children() {
|
|
(( stopping == 0 )) || return 0
|
|
stopping=1
|
|
trap '' TERM INT
|
|
# Upstream su creates another session. Include descendant process groups,
|
|
# rather than assuming every child stays in the initial setsid group.
|
|
# The official image has no ps/awk. Read Linux process metadata with Bash
|
|
# builtins, handling the parenthesized command field before parsing stat.
|
|
local -a service_groups=()
|
|
local -A parents=() groups=() selected=()
|
|
local stat line state parent group node seen
|
|
for stat in /proc/[0-9]*/stat; do
|
|
[[ -r "$stat" ]] || continue
|
|
line=$(<"$stat") || continue
|
|
pid=${stat#/proc/}; pid=${pid%/stat}
|
|
read -r state parent group _ <<< "${line##*) }"
|
|
parents[$pid]=$parent
|
|
groups[$pid]=$group
|
|
done
|
|
for pid in "${!parents[@]}"; do
|
|
node=$pid; seen=0
|
|
while [[ "$node" != "$$" && -n "${parents[$node]:-}" ]] && (( seen++ < 1000 )); do
|
|
node=${parents[$node]}
|
|
done
|
|
group=${groups[$pid]}
|
|
if [[ "$node" == "$$" ]] && (( group > 1 && group != $$ )); then selected[$group]=1; fi
|
|
done
|
|
service_groups=("${!selected[@]}")
|
|
for pid in "${service_groups[@]}"; do
|
|
kill -TERM -- "-$pid" 2>/dev/null || true
|
|
done
|
|
for (( i=0; i<25; i++ )); do
|
|
local alive=0
|
|
for pid in "${service_groups[@]}"; do
|
|
if kill -0 -- "-$pid" 2>/dev/null; then alive=1; fi
|
|
done
|
|
(( alive == 1 )) || break
|
|
sleep 1
|
|
done
|
|
for pid in "${service_groups[@]}"; do
|
|
kill -KILL -- "-$pid" 2>/dev/null || true
|
|
done
|
|
wait 2>/dev/null || true
|
|
}
|
|
trap 'stop_children; exit 0' TERM INT
|
|
trap stop_children EXIT
|
|
|
|
# Operator-approved exception for this exact private TTS endpoint only.
|
|
# Unknown upstream versions keep their original policy; never patch them blindly.
|
|
export HERMES_WEBUI_TRUSTED_TTS_BASE_URL=http://192.168.1.212:8120/v1
|
|
/usr/local/bin/python3 - <<'TTS_PATCH'
|
|
import hashlib
|
|
from pathlib import Path
|
|
# Stock initialization copies /apptoo into /app on every start.
|
|
p = Path('/apptoo/api/routes.py')
|
|
source = p.read_text()
|
|
original_sha = '9f32e7ca096a491a36c1e774752af18c2d02ca402af4dad1886a5048deae10dc'
|
|
patched_sha = '5e51d1e2791804b825bdd3c8f000faaea3989d34cdbcd6c406800573eb8e5710'
|
|
insertion = ' # Explicit operator trust for one literal RFC1918 OpenAI TTS base URL.\n # Chat input/config cannot broaden this process environment allowlist.\n trusted = os.getenv("HERMES_WEBUI_TRUSTED_TTS_BASE_URL", "").strip().rstrip("/")\n if trusted and raw.rstrip("/") == trusted and parsed.scheme == "http":\n import ipaddress\n try:\n address = ipaddress.ip_address(hostname)\n allowed_networks = ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16")\n trusted_lan = any(address in ipaddress.ip_network(n) for n in allowed_networks)\n if trusted_lan and parsed.port and parsed.path.rstrip("/") == "/v1":\n return urlunsplit((parsed.scheme, parsed.netloc, "/v1", "", ""))\n except ValueError:\n pass\n raise ValueError("invalid trusted OpenAI TTS base_url")\n'
|
|
anchor = ' if parsed.scheme == "https":\n # Public https hosts are allowed (a user\'s own OpenAI-compatible server),'
|
|
sha = hashlib.sha256(source.encode()).hexdigest()
|
|
if sha == patched_sha:
|
|
print('LAN TTS adaptation already present', flush=True)
|
|
elif sha == original_sha and source.count(anchor) == 1:
|
|
updated = source.replace(anchor, insertion + anchor, 1)
|
|
assert hashlib.sha256(updated.encode()).hexdigest() == patched_sha
|
|
compile(updated, str(p), 'exec')
|
|
p.write_text(updated)
|
|
print('Approved LAN TTS adaptation applied', flush=True)
|
|
else:
|
|
print('WARNING: unknown WebUI source; LAN TTS adaptation NOT applied; revalidation required', flush=True)
|
|
TTS_PATCH
|
|
|
|
# setsid gives each service its own process group, including init/su children.
|
|
setsid /hermeswebui_init.bash &
|
|
web_pid=$!
|
|
ready=0
|
|
for (( i=0; i<1200; i++ )); do
|
|
kill -0 "$web_pid" 2>/dev/null || exit 1
|
|
if [[ -x /app/venv/bin/hermes ]] && curl -fsS --max-time 2 http://127.0.0.1:8787/health >/dev/null 2>&1; then
|
|
ready=1
|
|
break
|
|
fi
|
|
sleep 1
|
|
done
|
|
(( ready == 1 )) || exit 1
|
|
|
|
# The upstream initializer has now prepared the venv and unprivileged user.
|
|
setsid su -s /bin/bash hermeswebui -c 'cd /app/hermes-agent-src; exec /app/venv/bin/hermes gateway run --external-supervisor' &
|
|
gateway_pid=$!
|
|
|
|
# Any service exit, even a deliberate gateway restart, restarts the whole unit.
|
|
set +e
|
|
wait -n "$web_pid" "$gateway_pid"
|
|
set -e
|
|
exit 1
|