#!/usr/bin/bash

echo "[I] Dowow Reverse SSH installer"

# home path
HOME=$(eval echo ~root)

# the path to the service
SERVICE_NAME="autossh.service"
SERVICE_PATH="/etc/systemd/system/$SERVICE_NAME"
KEYS_DIRECTORY="$HOME/.ssh"
PUBKEY_PATH="$HOME/.ssh/id_ed25519.pub"
PRIVKEY_PATH="$HOME/.ssh/id_ed25519"

# local autossh script path
SCRIPT_DIR="/etc/dowow"
SCRIPT_FILE="reverse_ssh.sh"
SCRIPT_PATH="$SCRIPT_DIR/$SCRIPT_FILE"

# proxy host
PROXY_HOST="dowow.su"
# proxy user
PROXY_SERVICE_USER="dowowRoot"

# the host to use as the proxy
PROXY_USER="portforward"

# check UID
if [ $UID -ne 0 ]; then
    echo "[!] The script must be run as root"
    exit 1
fi

# get the remote port to use
read -p "[U] Choose the port to use at $PROXY_HOST: " PROXY_PORT
# invalid port
if ( ! ( ( [ $PROXY_PORT -ge 10000 ] ) && [ $PROXY_PORT -le 20000 ] ) ) &> /dev/null; then
    echo "[!] Invalid port!"
    exit 1
fi
# calculate the maintain port
MAINTAIN_PORT=$(( ($PROXY_PORT - 10033) * 2 + 20022 ))
# log
echo "[I] Gonna use $PROXY_PORT for shell and $MAINTAIN_PORT for maintainment"

# try to run AutoSSH
autossh -V > /dev/null
# is AutoSSH installed?
if [ $? -ne 0 ]; then
    echo "[!] AutoSSH is not installed, installing..."
    echo "[I] Updating apt index..."
    # update
    if ! apt -y update; then
        echo "[!] Could not update APT index!"
        exit 2
    fi
    # install
    echo "[I] Installing autossh..."
    if ! apt -y install autossh; then
        echo "[!] Could not install AutoSSH!"
        exit 3
    fi
    # good, check if installed
    autossh -V > /dev/null
    if [ $? -ne 0 ]; then
        echo "[!] Could not install AutoSSH!"
        exit 4
    fi
fi

# installed
echo "[I] AutoSSH is installed"

# ensure the directory for keys exist
mkdir -p $KEYS_DIRECTORY &> /dev/null
# check if we have ed25519 key
if ! [ -e $PUBKEY_PATH ]; then
    echo "[!] ed25519 key does not exist! Creating..."
    if ! ssh-keygen -t ed25519 -f $PRIVKEY_PATH -N "" > /dev/null; then
        echo "[!] Could not create ed25519 key in $PRIVKEY_PATH"
        exit 5
    fi
    if ! [ -e $PUBKEY_PATH ]; then
        echo "[!] Could not create ed25519 public key in $PUBKEY_PATH"
    fi
fi
echo "[I] Using ed25519 pubkey from $PUBKEY_PATH"

# authorize and unlock the user
echo "[I] Unlocking $PROXY_USER@$PROXY_HOST..."
ssh $PROXY_SERVICE_USER@$PROXY_HOST "usermod -U $PROXY_USER"
echo "[I] User $PROXY_USER is allowed to login now, installing the key..."
ssh-copy-id -i $PUBKEY_PATH $PROXY_USER@$PROXY_HOST
echo "[I] Locking $PROXY_USER@$PROXY_HOST..."
ssh $PROXY_SERVICE_USER@$PROXY_HOST "usermod -L $PROXY_USER"

# check if script exists
if [ -e $SERVICE_PATH ]; then
    echo "[!] $SERVICE_PATH already exists, removing..."
    rm -f $SERVICE_PATH
fi
if [ -e $SCRIPT_PATH ]; then
    echo "[!] $SCRIPT_PATH already exists, removing..."
    rm -f $SCRIPT_PATH
fi
# create the directories
mkdir -p $SCRIPT_DIR &> /dev/null
# create the script
echo "[I] Creating the startup script..."
echo "#!/usr/bin/bash" > $SCRIPT_PATH
echo "MAINTAIN_PORT=$MAINTAIN_PORT" >> $SCRIPT_PATH
echo "PROXY_PORT=$PROXY_PORT" >> $SCRIPT_PATH
echo >> $SCRIPT_PATH
echo "autossh -M \$MAINTAIN_PORT -N \\" >> $SCRIPT_PATH
echo "    -o \"PubkeyAuthentication=yes\" \\" >> $SCRIPT_PATH
echo "    -o \"StrictHostKeyChecking=false\" \\" >> $SCRIPT_PATH
echo "    -o \"PasswordAuthentication=no\" \\" >> $SCRIPT_PATH
echo "    -o \"ServerAliveInterval 15\" \\" >> $SCRIPT_PATH
echo "    -o \"ServerAliveCountMax 2\" \\" >> $SCRIPT_PATH
echo "    -R $PROXY_HOST:\$PROXY_PORT:localhost:22 \\" >> $SCRIPT_PATH
echo "    $PROXY_USER@$PROXY_HOST" >> $SCRIPT_PATH

# create the service
echo "[I] Creating the systemd service..."
echo "[Unit]" >> $SERVICE_PATH
echo "Description=Reverse SSH (by script)" >> $SERVICE_PATH
echo >> $SERVICE_PATH
echo "[Service]" >> $SERVICE_PATH
echo "Type=simple" >> $SERVICE_PATH
echo "User=root" >> $SERVICE_PATH
echo "Group=root" >> $SERVICE_PATH
echo "ExecStart=/usr/bin/bash $SCRIPT_PATH" >> $SERVICE_PATH
echo "Restart=always" >> $SERVICE_PATH
echo "RestartSec=5" >> $SERVICE_PATH
echo >> $SERVICE_PATH
echo "[Install]" >> $SERVICE_PATH
echo "WantedBy=multi-user.target" >> $SERVICE_PATH

# reload systemd daemon
systemctl daemon-reload
# enable the service
systemctl enable $SERVICE_NAME
# start the service
systemctl start $SERVICE_NAME

echo "[I] Reverse SSH is installed. Use $PROXY_HOST:$PROXY_PORT to access it."
