From fc43109f3a84b6b63d54f21188224cec95066d41 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Mon, 16 Jan 2023 23:21:34 -0300 Subject: [PATCH] Integrate with newest changes --- .../rootfs/etc/cont-init.d/prepare-go2rtc.sh | 73 +++++++++---------- docker/rootfs/etc/services.d/go2rtc/run | 6 +- .../rootfs/usr/local/go2rtc/create_config.py | 10 ++- 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/docker/rootfs/etc/cont-init.d/prepare-go2rtc.sh b/docker/rootfs/etc/cont-init.d/prepare-go2rtc.sh index 2c3d21d38..ab415ead5 100755 --- a/docker/rootfs/etc/cont-init.d/prepare-go2rtc.sh +++ b/docker/rootfs/etc/cont-init.d/prepare-go2rtc.sh @@ -4,44 +4,39 @@ set -o errexit -o nounset -o pipefail -if [[ -z "${SUPERVISOR_TOKEN:-}" ]]; then - # Not running as a Home Assistant add-on, don't do anything - exit 0 +if [[ -n "${SUPERVISOR_TOKEN:-}" ]]; then + # Running as a Home Assistant add-on, infer the IP address and port + + # Example: 192.168.1.10/24 + ip_regex='^([0-9]{1,3}\.{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 "Got IP address from supervisor: ${ip_address}" >&2 + else + echo "Failed to get IP address from supervisor" >&2 + fi + + 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["22/tcp"]' + ) && [[ "${webrtc_port}" =~ ${port_regex} ]]; then + webrtc_port="${BASH_REMATCH[1]}" + echo "Got WebRTC port from supervisor: ${ip_address}" >&2 + else + echo "Failed to get WebRTC port from supervisor" >&2 + fi + + export FRIGATE_GO2RTC_WEBRTC_CANDIDATE_INTERNAL="${ip_address}:${webrtc_port}" fi -# Example: 192.168.1.10/24 -ip_regex='^([0-9]{1,3}\.{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 "Got IP address from supervisor: ${ip_address}" >&2 -else - echo "Failed to get IP address from supervisor" >&2 - # Exit with success so that the container can still start - exit 0 -fi - -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 "Got WebRTC port from supervisor: ${ip_address}" >&2 -else - echo "Failed to get WebRTC port from supervisor" >&2 - # Exit with success so that the container can still start - exit 0 -fi - -# Replace the IP address in the config -candidate="${ip_address}:${webrtc_port}" -sed --in-place "s/# - %%CANDIDATE%%/- ${candidate}/" /usr/local/go2rtc/go2rtc.yaml +python3 /usr/local/go2rtc/create_config.py > /dev/shm/go2rtc.yaml diff --git a/docker/rootfs/etc/services.d/go2rtc/run b/docker/rootfs/etc/services.d/go2rtc/run index db5c2a869..a67615630 100755 --- a/docker/rootfs/etc/services.d/go2rtc/run +++ b/docker/rootfs/etc/services.d/go2rtc/run @@ -2,10 +2,6 @@ # shellcheck shell=bash # Start the go2rtc service -set -o errexit -o nounset -o pipefail - -raw_config=$(python3 /usr/local/go2rtc/create_config.py) - # Replace the bash process with the go2rtc process, redirecting stderr to stdout exec 2>&1 -exec go2rtc -config="${raw_config}" +exec go2rtc -config=/dev/shm/go2rtc.yaml diff --git a/docker/rootfs/usr/local/go2rtc/create_config.py b/docker/rootfs/usr/local/go2rtc/create_config.py index 20e8de494..a79b822ed 100644 --- a/docker/rootfs/usr/local/go2rtc/create_config.py +++ b/docker/rootfs/usr/local/go2rtc/create_config.py @@ -25,7 +25,15 @@ go2rtc_config: dict[str, any] = config["go2rtc"] if not go2rtc_config.get("log", {}).get("format"): go2rtc_config["log"] = {"format": "text"} +default_candidates = [] +# Use FRIGATE_GO2RTC_WEBRTC_CANDIDATE_INTERNAL as candidate if set +if os.environ.get("FRIGATE_GO2RTC_WEBRTC_CANDIDATE_INTERNAL"): + default_candidates.append( + os.environ['FRIGATE_GO2RTC_WEBRTC_CANDIDATE_INTERNAL'] + )55 +default_candidates.append("stun:85") + if not go2rtc_config.get("webrtc", {}).get("candidates", []): - go2rtc_config["webrtc"] = {"candidates": ["stun:8555"]} + go2rtc_config["webrtc"] = {"candidates": default_candidates} print(json.dumps(go2rtc_config)) \ No newline at end of file