Files
ha-addons/ssh-bridge/run.sh
T

55 lines
2.0 KiB
Bash
Executable File

#!/bin/ash
set -e
# Everything persistent lives under /data (survives restarts and updates).
mkdir -p /data/ssh /data/home/bridge /data/crontabs
chown bridge:bridge /data/home/bridge /data/crontabs
chmod 700 /data/home/bridge
# --- tailscaled (real TUN inside this container's netns; host never joins tailnet)
tailscaled \
--statedir=/data/tailscale \
--tun=tailscale0 &
TAILSD=$!
# Wait for the socket before trying to bring the node up.
# (tailscaled's default socket is /var/run/tailscale/tailscaled.sock; statedir
# only moves the state files, not the socket.)
i=0
until [ -e /var/run/tailscale/tailscaled.sock ]; do
i=$((i+1)); [ "$i" -gt 30 ] && { echo "tailscaled socket never appeared"; exit 1; }
sleep 1
done
# Auth key is a file in /share (never shown in the add-on options UI).
# --accept-dns=true: MagicDNS for tailnet names; non-tailnet names (internet
# and *.local.hass.io) are forwarded to the container's upstream resolver,
# which is HAOS DNS.
if [ -r /share/ssh-bridge/tailscale_authkey ]; then
AUTHKEY="$(cat /share/ssh-bridge/tailscale_authkey)"
# shellcheck disable=SC2086
tailscale up \
--authkey="$AUTHKEY" \
--hostname="${tailscale_hostname:-ssh-bridge}" \
--accept-dns=true \
${tailscale_extra_args} \
--reset
unset AUTHKEY
else
echo "WARN: /share/ssh-bridge/tailscale_authkey missing; tailscale node not registered"
fi
# --- busybox crond with a persistent crontab spool (user-owned crontabs,
# e.g. bridge's fleet supervise entry). -b daemonizes; logs to /data.
ln -sfn /data/crontabs /var/spool/cron/crontabs
crond -b -l 6 -L /data/cron.log
# --- sshd host key (generated once, kept in /data; never regenerate —
# client hosts pin this fingerprint)
if [ ! -f /data/ssh/ssh_host_ed25519_key ]; then
ssh-keygen -q -t ed25519 -N "" -f /data/ssh/ssh_host_ed25519_key
fi
# --- sshd in foreground; supervisor restarts the add-on if it dies
exec /usr/sbin/sshd -D -f /etc/ssh/sshd_config -e