Files
CyberGril/deploy/hel913-unified/bridge_ctl.template.sh
T

79 lines
3.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# HEL-913 运行包 · wss TLS 桥启停模板(8363 → 127.0.0.1:7880;脱敏待批件)
#
# 浏览器从 HTTPS(8361) 页面连 LiveKit 必须走 wss(混合内容策略),本桥在新
# 端口终结 TLS 并只向回环 7880 转发(字节级,协议无关;WebRTC 媒体面不经
# 本桥)。与 8361 同一张自签证书。改编自 HEL-910 已实测 bridge_ctl.sh;
# 本文件默认不运行,获批语音窗口才使用。子命令只认 start|stop|status。
#
# 用法: [ENV=..] bash bridge_ctl.template.sh start|stop|status
# 环境变量: HEL913_BASE / HEL913_BRIDGE_PORT(8363) / HEL913_CERT / HEL913_KEY
set -u
case "${1:-}" in start|stop|status) ;; *) echo "invalid subcommand" >&2; exit 2 ;; esac
BASE=${HEL913_BASE:-/opt/cybergril-hel913}
PORT=${HEL913_BRIDGE_PORT:-8363}
CERT=${HEL913_CERT:-/opt/cybergril-voice-certs/cybergril-voice.pem}
KEY=${HEL913_KEY:-/opt/cybergril-voice-certs/cybergril-voice.key}
LOG=$BASE/logs/bridge.log
PIDF=$BASE/logs/bridge.pid
MARKER="wss_bridge.example.py"
usage() { echo "usage: [ENV=..] bash $0 start|stop|status" >&2; exit 2; }
ours() {
local p=$1 cmd cw
case "$p" in ''|*[!0-9]*) return 1 ;; esac
[ "$p" != "$$" ] || return 1
kill -0 "$p" 2>/dev/null || return 1
cmd=$(tr '\0' ' ' < "/proc/$p/cmdline" 2>/dev/null) || return 1
cw=$(readlink "/proc/$p/cwd" 2>/dev/null) || return 1
case "$cmd" in *"$MARKER"*) ;; *) return 1 ;; esac
[ "$cw" = "$BASE/current/deploy/voice" ] || return 1
return 0
}
pidfile_pid() { [ -f "$PIDF" ] && cat "$PIDF" 2>/dev/null; }
stop() {
local p
p=$(pidfile_pid)
if [ -z "$p" ]; then echo "bridge not running (no pid file)"; return 0; fi
if kill -0 "$p" 2>/dev/null && ! ours "$p"; then
echo "REFUSED: pid $p alive but not this bridge (pid reuse?) — not killing; resolve $PIDF manually" >&2
return 1
fi
if ours "$p"; then kill "$p"; sleep 2; ours "$p" && kill -9 "$p"; fi
rm -f "$PIDF"
echo "bridge stopped"
}
case "${1:-}" in
stop) stop; exit $? ;;
status)
p=$(pidfile_pid)
if [ -n "$p" ]; then
ours "$p" && echo "running pid $p (identity verified)" \
|| { kill -0 "$p" 2>/dev/null && echo "pid $p alive but NOT ours" || echo "pid $p not alive (stale)"; }
else
echo "no pid file"
fi
exit 0 ;;
esac
p=$(pidfile_pid)
if [ -n "$p" ] && kill -0 "$p" 2>/dev/null; then
echo "REFUSED: bridge already running (pid $p)" >&2; exit 1
fi
rm -f "$PIDF"
[ -r "$CERT" ] && [ -r "$KEY" ] || { echo "REFUSED: cert/key not readable" >&2; exit 1; }
cd "$BASE/current/deploy/voice" || exit 1
nohup /usr/bin/python3 wss_bridge.example.py \
--listen "0.0.0.0:$PORT" --upstream 127.0.0.1:7880 \
--cert "$CERT" --key "$KEY" > "$LOG" 2>&1 &
echo $! > "$PIDF"
echo "bridge starting (pid $(cat "$PIDF")) on :$PORT"
sleep 3
timeout 8 openssl s_client -connect "127.0.0.1:$PORT" -servername 192.168.200.12 </dev/null 2>/dev/null \
| grep -q "TLSv1.3" && echo "OK: bridge TLS up" \
|| { echo "FAIL: TLS probe failed — check $LOG"; tail -5 "$LOG"; stop; exit 1; }