mirror of
https://github.com/RipleyBooya/ssh-tunnel.git
synced 2025-12-13 00:18:37 +01:00
Create entrypoint.sh
This commit is contained in:
parent
5e76e0a377
commit
bb43e9cdaa
1 changed files with 48 additions and 0 deletions
48
docker/entrypoint.sh
Normal file
48
docker/entrypoint.sh
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
# Check if required environment variables are set
|
||||
if [ -z "$SSH_HOST" ] || [ -z "$SSH_USER" ] || [ -z "$REMOTE_PORTS" ] || [ -z "$LOCAL_PORTS" ]; then
|
||||
echo "ERROR: Required variables (SSH_HOST, SSH_USER, REMOTE_PORTS, LOCAL_PORTS) are missing."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if the SSH key exists in /tmp/ and copy it to /root/.ssh/
|
||||
if [ -f "/tmp/id_rsa" ]; then
|
||||
echo "Copying SSH key to /root/.ssh/id_rsa..."
|
||||
cp /tmp/id_rsa /root/.ssh/id_rsa
|
||||
chmod 600 /root/.ssh/id_rsa
|
||||
else
|
||||
echo "ERROR: SSH key is missing. Please mount your private key to /tmp/id_rsa."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Clear logs on container startup
|
||||
echo "" > /var/log/ssh-tunnel/tunnel.log
|
||||
|
||||
# Generate logrotate config using environment variables
|
||||
envsubst < /logrotate.template > /etc/logrotate.d/ssh-tunnel
|
||||
|
||||
# Build the SSH tunnel command
|
||||
TUNNEL_CMD=""
|
||||
LOCAL_PORT_ARRAY=($LOCAL_PORTS)
|
||||
REMOTE_PORT_ARRAY=($REMOTE_PORTS)
|
||||
|
||||
for i in "${!LOCAL_PORT_ARRAY[@]}"; do
|
||||
TUNNEL_CMD="$TUNNEL_CMD -L ${LOCAL_PORT_ARRAY[$i]}:${REMOTE_PORT_ARRAY[$i]}"
|
||||
done
|
||||
|
||||
echo "Starting SSH tunnel to $SSH_USER@$SSH_HOST" | tee -a /var/log/ssh-tunnel/tunnel.log
|
||||
echo "Configured tunnels:" | tee -a /var/log/ssh-tunnel/tunnel.log
|
||||
for i in "${!LOCAL_PORT_ARRAY[@]}"; do
|
||||
echo "- ${LOCAL_PORT_ARRAY[$i]} -> ${REMOTE_PORT_ARRAY[$i]}" | tee -a /var/log/ssh-tunnel/tunnel.log
|
||||
done
|
||||
|
||||
# Start autossh to maintain the tunnel (disables host key checking)
|
||||
exec autossh -M 0 \
|
||||
-o "StrictHostKeyChecking=no" \
|
||||
-o "UserKnownHostsFile=/dev/null" \
|
||||
-o "ServerAliveInterval=60" \
|
||||
-o "ServerAliveCountMax=3" \
|
||||
-N $TUNNEL_CMD $SSH_USER@$SSH_HOST 2>&1 | tee -a /var/log/ssh-tunnel/tunnel.log
|
||||
Loading…
Add table
Reference in a new issue