#!/bin/bash

set -e

echo "=== Dante Auto Installer By @nimazerobit ==="

install_dante() {
    # Generate random credentials
    RANDOM_USER="user$(openssl rand -hex 2)"
    RANDOM_PASS="$(openssl rand -base64 12)"

    echo ""
    echo "[+] Suggested random credentials:"
    echo "[+] Username: $RANDOM_USER"
    echo "[+] Password: $RANDOM_PASS"
    echo ""

    read -p "Use these credentials? (Y/n): " USE_RANDOM

    if [[ "$USE_RANDOM" == "n" || "$USE_RANDOM" == "N" ]]; then
        read -p "[?] Enter username: " USERNAME
        read -p "[?] Enter password: " PASSWORD
    else
        USERNAME=$RANDOM_USER
        PASSWORD=$RANDOM_PASS
    fi

    echo ""
    read -p "[?] Enter SOCKS5 port (default 1080): " PORT
    PORT=${PORT:-1080}

    echo ""
    echo "[+] Installing Dante..."

    apt update -y
    apt install -y dante-server

    IFACE=$(ip route | grep default | awk '{print $5}')
    echo "[+] Using network interface: $IFACE"

    useradd -m -s /usr/sbin/nologin $USERNAME || true
    echo "$USERNAME:$PASSWORD" | chpasswd

    mv /etc/danted.conf /etc/danted.conf.bak 2>/dev/null || true

    cat <<EOF > /etc/danted.conf
logoutput: syslog

internal: 0.0.0.0 port = $PORT
external: $IFACE

method: username

user.privileged: root
user.notprivileged: nobody

client pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: error
}

client block {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: connect error
}

pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    protocol: tcp udp
    log: error
}

block {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: connect error
}
EOF

    echo "[+] Configuring firewall..."

    ufw allow $PORT/tcp || true
    ufw allow $PORT/udp || true

    echo "[+] Restarting Dante..."

    systemctl restart danted
    systemctl enable danted

    echo ""
    echo "########### DONE! ###########"
    echo "-----------------------------"
    echo "IP: $(curl -s ifconfig.me)"
    echo "Port: $PORT"
    echo "Username: $USERNAME"
    echo "Password: $PASSWORD"
    echo "-----------------------------"
}

uninstall_dante() {
    echo "[!] Uninstalling Dante..."

    systemctl stop danted || true
    systemctl disable danted || true

    apt purge -y dante-server
    apt autoremove -y

    rm -f /etc/danted.conf

    echo "[?] Do you want to remove created user? (y/N): "
    read REMOVE_USER

    if [[ "$REMOVE_USER" == "y" || "$REMOVE_USER" == "Y" ]]; then
        read -p "[?] Enter username to remove: " DEL_USER
        userdel -r $DEL_USER 2>/dev/null || true
        echo "[+] User removed (if existed)."
    fi

    echo "[+] Dante removed successfully."
}

dante_status() {
    echo "[+] Service status:"
    systemctl status danted --no-pager

    echo ""
    echo "[+] Listening ports:"
    ss -tulnp | grep danted || echo "No active listening port found"
}

while true; do
    echo ""
    echo "========= MENU ========="
    echo "1. Install Dante"
    echo "2. Uninstall Dante"
    echo "3. Dante Status"
    echo "4. Exit"
    echo "========================"
    read -p "Select an option [1-4]: " CHOICE

    case $CHOICE in
        1)
            install_dante
            ;;
        2)
            uninstall_dante
            ;;
        3)
            dante_status
            ;;
        4)
            echo "Bye!"
            exit 0
            ;;
        *)
            echo "[!] Invalid option"
            ;;
    esac
done