#!/bin/bash
set -e

# Update packages
apt update

# Install Dante SOCKS5 server
apt install -y dante-server

# Get the VPS network interface
IFACE=$(ip route | awk '/default/ {print $5; exit}')

# Create a proxy user
read -rp "Proxy username: " PROXY_USER
read -rsp "Proxy password: " PROXY_PASS
echo

id "$PROXY_USER" >/dev/null 2>&1 || \
    useradd --no-create-home --shell /usr/sbin/nologin "$PROXY_USER"

echo "$PROXY_USER:$PROXY_PASS" | chpasswd

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

internal: $IFACE port = 1080
external: $IFACE

method: username
user.privileged: proxy
user.notprivileged: nobody

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

proxy pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    command: connect
    protocol: tcp
    method: username
    log: error
}
EOF

# Start proxy
systemctl enable danted
systemctl restart danted

# Show status and port
systemctl --no-pager status danted
echo
echo "SOCKS5 proxy is listening on port 1080"
echo "Username: $PROXY_USER"
echo "VPS IP:"
curl -4 -s https://ifconfig.me
echo
