Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ExitOnForwardFailure and allow config of ServerAlive* #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autossh/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions autossh/config.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -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}"
}
34 changes: 18 additions & 16 deletions autossh/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -19,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=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
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[@]}"