mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-10-19 21:22:50 +03:00
CI / AMD64 Build (push) Waiting to run
CI / AMD64 Smoke Test (push) Blocked by required conditions
CI / ARM Build (push) Waiting to run
CI / Jetson Jetpack 6 (push) Waiting to run
CI / AMD64 Extra Build (push) Blocked by required conditions
CI / ARM Extra Build (push) Blocked by required conditions
CI / Synaptics Build (push) Blocked by required conditions
CI / Assemble and push default build (push) Blocked by required conditions
* keep /config out of the root environment in the frigate run script
The script exported HOME=/config and prepended /config/.local/lib to LD_LIBRARY_PATH while still root, then ran python3, ffmpeg, grep, and opt_in_out as root. The runtime user owns /config, so it could plant a .pth file in the user site-packages or a library on the loader path and get code run as root on the next start. Root helpers now run with root's environment (and python3 -I), and HOME, LD_LIBRARY_PATH, and PATH reach only the dropped process through env on the s6-setuidgid line. opt_in_out runs as the runtime user, so the openvino marker no longer gets written into /config by root.
* don't follow a planted symlink when writing /dev/shm/go2rtc.yaml
After the first start the go2rtc user owned /dev/shm/go2rtc.yaml and could swap it for a dangling symlink. The run script's -f test was false for a dangling link, so the link survived, and root's create_config.py then created the link target and the chown handed it to go2rtc. The run script now removes any existing entry, create_config.py creates the file with O_EXCL and O_NOFOLLOW at mode 0640, and the chown uses -h.
* run root python helpers in isolated mode and keep HOME=/config for non-root containers
The previous commit only set HOME=/config when dropping from root, so a container started with --user lost it. The other root-run helpers in the go2rtc, nginx, and certsync scripts now use python3 -I too, so they never load user site-packages or PYTHON* variables.
* validate networking.listen values before rendering them into nginx config
ListenConfig and get_nginx_settings.py only checked that the text after the last colon parsed as an int, and tempio rendered the raw string into the listen directive, so a value like "5000; location /x/ { alias /; } #:5000" injected nginx directives. Both now go through parse_listen_address, which accepts only a port, IPv4:port, or [IPv6]:port with a 1 to 65535 port and rebuilds the value from the parsed parts. get_nginx_settings.py falls back to the default port on an invalid value so nginx still serves the safe-mode UI.
* tighten comments
* accept hostnames, wildcards, and padded values in networking.listen
The stricter parser broke localhost:5000, *:5000, and values with surrounding whitespace that the old int() parsing accepted. parse_listen_address now strips the value and accepts RFC 1123 hostnames and *, still rebuilding the rendered value from the parsed parts. The ListenConfig validator runs in before mode, because pydantic coerced true to port 1 while get_nginx_settings.py rejected it and fell back to 5000, so the auth internal port and nginx disagreed.
* always create go2rtc.yaml after removing it and don't let opt_in_out stop frigate
The go2rtc else-branch was only reachable when a regular file was planted between rm and the test, and then loaded that file as go2rtc's config. create_config.py now runs unconditionally and its exclusive create fails closed. opt_in_out runs after s6-svc -O, so a failure there was terminal under errexit.
* fall back to default listen ports when either is invalid or they collide
* keep valid listen values and refuse to start nginx when the ports collide
116 lines
4.3 KiB
Plaintext
Executable File
116 lines
4.3 KiB
Plaintext
Executable File
#!/command/with-contenv bash
|
|
# shellcheck shell=bash
|
|
# Start the go2rtc service
|
|
|
|
set -o errexit -o nounset -o pipefail
|
|
|
|
runs_as_root=0
|
|
if [[ "$(id -u)" -eq 0 ]]; then
|
|
if [[ "${FRIGATE_RUN_AS_ROOT:-false}" == "true" ]] || /usr/local/bin/service-runs-as-root go2rtc; then
|
|
runs_as_root=1
|
|
fi
|
|
fi
|
|
|
|
# Root via FRIGATE_ROOT_SERVICES only; the escape hatch sweeps nothing and
|
|
# leaves no unprivileged service, so /config/go2rtc stays as safe as pre-drop.
|
|
granular_root=0
|
|
if [[ "$runs_as_root" -eq 1 && "${FRIGATE_RUN_AS_ROOT:-false}" != "true" ]]; then
|
|
granular_root=1
|
|
fi
|
|
|
|
# Logs should be sent to stdout so that s6 can collect them
|
|
|
|
function get_ip_and_port_from_supervisor() {
|
|
local ip_address
|
|
# Example: 192.168.1.10/24
|
|
local ip_regex='^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/[0-9]{1,2}$'
|
|
if ip_address=$(
|
|
curl -fsSL \
|
|
-H "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
http://supervisor/network/interface/default/info |
|
|
jq --exit-status --raw-output '.data.ipv4.address[0]'
|
|
) && [[ "${ip_address}" =~ ${ip_regex} ]]; then
|
|
ip_address="${BASH_REMATCH[1]}"
|
|
echo "[INFO] Got IP address from supervisor: ${ip_address}"
|
|
else
|
|
echo "[WARN] Failed to get IP address from supervisor"
|
|
return 0
|
|
fi
|
|
|
|
local webrtc_port
|
|
local port_regex='^([0-9]{1,5})$'
|
|
if webrtc_port=$(
|
|
curl -fsSL \
|
|
-H "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
http://supervisor/addons/self/info |
|
|
jq --exit-status --raw-output '.data.network["8555/tcp"]'
|
|
) && [[ "${webrtc_port}" =~ ${port_regex} ]]; then
|
|
webrtc_port="${BASH_REMATCH[1]}"
|
|
echo "[INFO] Got WebRTC port from supervisor: ${webrtc_port}"
|
|
else
|
|
echo "[WARN] Failed to get WebRTC port from supervisor"
|
|
return 0
|
|
fi
|
|
|
|
export FRIGATE_GO2RTC_WEBRTC_CANDIDATE_INTERNAL="${ip_address}:${webrtc_port}"
|
|
}
|
|
|
|
function set_libva_version() {
|
|
local ffmpeg_path
|
|
ffmpeg_path=$(python3 -I /usr/local/ffmpeg/get_ffmpeg_path.py)
|
|
LIBAVFORMAT_VERSION_MAJOR=$("$ffmpeg_path" -version | grep -Po "libavformat\W+\K\d+")
|
|
export LIBAVFORMAT_VERSION_MAJOR
|
|
}
|
|
|
|
set_libva_version
|
|
|
|
# create_config.py creates the file exclusively and fails if anything
|
|
# reappears at this path
|
|
rm -f /dev/shm/go2rtc.yaml
|
|
|
|
echo "[INFO] Preparing new go2rtc config..."
|
|
|
|
if [[ -n "${SUPERVISOR_TOKEN:-}" ]]; then
|
|
# Running as a Home Assistant Add-on, infer the IP address and port
|
|
get_ip_and_port_from_supervisor
|
|
fi
|
|
|
|
python3 -I /usr/local/go2rtc/create_config.py
|
|
|
|
# HomeKit persistence. The helper is symlink-safe; hand off to go2rtc only when dropping.
|
|
readonly homekit_config_path="/config/go2rtc_homekit.yml"
|
|
if [[ "$(id -u)" -eq 0 && "$runs_as_root" -eq 0 ]]; then
|
|
python3 -I /usr/local/go2rtc/prepare_homekit.py "${homekit_config_path}" --chown
|
|
chown -h go2rtc:go2rtc /dev/shm/go2rtc.yaml 2>/dev/null || true
|
|
else
|
|
python3 -I /usr/local/go2rtc/prepare_homekit.py "${homekit_config_path}"
|
|
fi
|
|
|
|
readonly config_path="/config"
|
|
|
|
# the sweep hands /config to uid 1000, so a root service must not exec from it
|
|
if [[ "$granular_root" -eq 1 && -x "${config_path}/go2rtc" ]]; then
|
|
echo "[WARN] Ignoring '${config_path}/go2rtc' because FRIGATE_ROOT_SERVICES runs this service as root and /config is owned by the runtime user; using the embedded binary"
|
|
echo "[WARN] Use FRIGATE_RUN_AS_ROOT=true instead if you need both a custom go2rtc build and root"
|
|
readonly binary_path="/usr/local/go2rtc/bin/go2rtc"
|
|
elif [[ -x "${config_path}/go2rtc" ]]; then
|
|
readonly binary_path="${config_path}/go2rtc"
|
|
echo "[WARN] Using go2rtc binary from '${binary_path}' instead of the embedded one"
|
|
else
|
|
readonly binary_path="/usr/local/go2rtc/bin/go2rtc"
|
|
fi
|
|
|
|
echo "[INFO] Starting go2rtc..."
|
|
|
|
# Replace the bash process with the go2rtc process, redirecting stderr to stdout
|
|
# Use HomeKit config as the primary config so writebacks go there
|
|
# The main config from Frigate will be loaded as a secondary config
|
|
exec 2>&1
|
|
if [[ "$(id -u)" -ne 0 || "$runs_as_root" -eq 1 ]]; then
|
|
exec "${binary_path}" -config="${homekit_config_path}" -config=/dev/shm/go2rtc.yaml
|
|
else
|
|
exec s6-setuidgid go2rtc "${binary_path}" -config="${homekit_config_path}" -config=/dev/shm/go2rtc.yaml
|
|
fi
|