From 5575ed26b08316e2652937771a1dc6d6a6ec4501 Mon Sep 17 00:00:00 2001 From: jcgoette Date: Sun, 19 Dec 2021 18:47:40 -0500 Subject: [PATCH 1/3] add ExitOnForwardFailure and allow config of ServerAlive* --- autossh/config.json | 12 ++++++++---- autossh/run.sh | 4 +++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/autossh/config.json b/autossh/config.json index 9525c06..8a4d74c 100644 --- a/autossh/config.json +++ b/autossh/config.json @@ -1,6 +1,6 @@ { "name": "Autossh", - "version": "0.4.0", + "version": "0.5.0", "slug": "autossh", "description": "Automatically connect to a ssh server for forwarding ports", "url": "https://github.com/odinuge/hassio-addons", @@ -15,9 +15,11 @@ "username": "autossh", "remote_forwarding": ["127.0.0.1:8123:172.17.0.1:8123"], "local_forwarding": [""], - "other_ssh_options": "-v", + "other_ssh_options": "-v -o ExitOnForwardFailure=yes", "monitor_port": "0", - "gatetime": "30" + "gatetime": "30", + "server_alive_interval": "30", + "server_alive_count_max": "3" }, "schema": { "hostname": "str", @@ -27,7 +29,9 @@ "local_forwarding": ["str"], "other_ssh_options": "str", "monitor_port": "int", - "gatetime": "int" + "gatetime": "int", + "server_alive_interval": "int", + "server_alive_count_max": "int" }, "image": "odinuge/hassio-addon-autossh-{arch}" } diff --git a/autossh/run.sh b/autossh/run.sh index 8343432..524b803 100644 --- a/autossh/run.sh +++ b/autossh/run.sh @@ -11,6 +11,8 @@ USERNAME=$(jq --raw-output ".username" $CONFIG_PATH) REMOTE_FORWARDING=$(jq --raw-output ".remote_forwarding[]" $CONFIG_PATH) LOCAL_FORWARDING=$(jq --raw-output ".local_forwarding[]" $CONFIG_PATH) +SERVER_ALIVE_INTERVAL=$(jq --raw-output ".server_alive_interval" $CONFIG_PATH) +SERVER_ALIVE_COUNT_MAX=$(jq --raw-output ".server_alive_count_max" $CONFIG_PATH) OTHER_SSH_OPTIONS=$(jq --raw-output ".other_ssh_options" $CONFIG_PATH) MONITOR_PORT=$(jq --raw-output ".monitor_port" $CONFIG_PATH) GATETIME=$(jq --raw-output ".gatetime" $CONFIG_PATH) @@ -29,7 +31,7 @@ fi echo "[INFO] public key is:" cat "${KEY_PATH}/autossh_rsa_key.pub" -command_args="-M ${MONITOR_PORT} -N -q -o ServerAliveInterval=20 -o ServerAliveCountMax=3 ${USERNAME}@${HOSTNAME} -p ${SSH_PORT} -i ${KEY_PATH}/autossh_rsa_key" +command_args="-M ${MONITOR_PORT} -N -q -o ServerAliveInterval=${SERVER_ALIVE_INTERVAL} -o ServerAliveCountMax=${SERVER_ALIVE_COUNT_MAX} ${USERNAME}@${HOSTNAME} -p ${SSH_PORT} -i ${KEY_PATH}/autossh_rsa_key" if [ ! -z "$REMOTE_FORWARDING" ]; then while read -r line; do From 0edbcfc3a52a13f10371ac3490eea8508966bc5d Mon Sep 17 00:00:00 2001 From: jcgoette Date: Sun, 19 Dec 2021 18:48:14 -0500 Subject: [PATCH 2/3] shellcheck and shfmt --- autossh/run.sh | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/autossh/run.sh b/autossh/run.sh index 524b803..86cdc62 100644 --- a/autossh/run.sh +++ b/autossh/run.sh @@ -21,40 +21,40 @@ export AUTOSSH_GATETIME=$GATETIME # Generate key if [ ! -d "$KEY_PATH" ]; then - echo "[INFO] Setup private key" - mkdir -p "$KEY_PATH" - ssh-keygen -b 4096 -t rsa -N "" -f "${KEY_PATH}/autossh_rsa_key" + echo "[INFO] Setup private key" + mkdir -p "$KEY_PATH" + ssh-keygen -b 4096 -t rsa -N "" -f "${KEY_PATH}/autossh_rsa_key" else - echo "[INFO] Restore private_keys" + echo "[INFO] Restore private_keys" fi echo "[INFO] public key is:" cat "${KEY_PATH}/autossh_rsa_key.pub" -command_args="-M ${MONITOR_PORT} -N -q -o ServerAliveInterval=${SERVER_ALIVE_INTERVAL} -o ServerAliveCountMax=${SERVER_ALIVE_COUNT_MAX} ${USERNAME}@${HOSTNAME} -p ${SSH_PORT} -i ${KEY_PATH}/autossh_rsa_key" +command_args=(-M "${MONITOR_PORT}" -N -q -o ServerAliveInterval="${SERVER_ALIVE_INTERVAL}" -o ServerAliveCountMax="${SERVER_ALIVE_COUNT_MAX}" "${USERNAME}"@"${HOSTNAME}" -p "${SSH_PORT}" -i "${KEY_PATH}"/autossh_rsa_key) -if [ ! -z "$REMOTE_FORWARDING" ]; then +if [ -n "$REMOTE_FORWARDING" ]; then while read -r line; do - command_args="${command_args} -R $line" - done <<< "$REMOTE_FORWARDING" + command_args=("${command_args[@]}" -R "$line") + done <<<"$REMOTE_FORWARDING" fi -if [ ! -z "$LOCAL_FORWARDING" ]; then +if [ -n "$LOCAL_FORWARDING" ]; then while read -r line; do - command_args="${command_args} -L $line" - done <<< "$LOCAL_FORWARDING" + command_args=("${command_args[@]}" -L "$line") + done <<<"$LOCAL_FORWARDING" fi echo "[INFO] testing ssh connection" -ssh -o StrictHostKeyChecking=no -p $SSH_PORT $HOSTNAME 2>/dev/null || true +ssh -o StrictHostKeyChecking=no -p "$SSH_PORT" "$HOSTNAME" 2>/dev/null || true echo "[INFO] listing host keys" -ssh-keyscan -p $SSH_PORT $HOSTNAME || true +ssh-keyscan -p "$SSH_PORT" "$HOSTNAME" || true -command_args="${command_args} ${OTHER_SSH_OPTIONS}" +command_args=("${command_args[@]}" ${OTHER_SSH_OPTIONS}) echo "[INFO] AUTOSSH_GATETIME=$AUTOSSH_GATETIME" -echo "[INFO] command args: ${command_args}" +echo "[INFO] command args:" "${command_args[@]}" # Start autossh -/usr/bin/autossh ${command_args} +/usr/bin/autossh "${command_args[@]}" From 048759480b20c766cddd1aa11599de17d5328481 Mon Sep 17 00:00:00 2001 From: jcgoette Date: Sun, 19 Dec 2021 21:50:25 -0500 Subject: [PATCH 3/3] remove unnecessary packages --- autossh/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autossh/Dockerfile b/autossh/Dockerfile index acee987..056a3df 100644 --- a/autossh/Dockerfile +++ b/autossh/Dockerfile @@ -3,7 +3,7 @@ FROM $BUILD_FROM ENV LANG C.UTF-8 -RUN apk add --no-cache tzdata jq openssh vim curl autossh +RUN apk add --no-cache autossh COPY run.sh / RUN chmod a+x /run.sh