#!/usr/bin/env bash set -euo pipefail # Load IP configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true # Reset Nginx Proxy Manager Admin Password # This script resets the admin password in NPM database set -e PROXMOX_HOST="${PROXMOX_HOST_R630_01}" CONTAINER_ID=105 # NEW_PASSWORD should come from argument or environment variable NEW_PASSWORD="${1:-${NPM_PASSWORD:-}}" if [ -z "$NEW_PASSWORD" ]; then echo "❌ Password is required. Provide as argument or set NPM_PASSWORD in ~/.env" echo " Usage: $0 [email]" exit 1 fi EMAIL="${2:-nsatoshi2007@hotmail.com}" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "🔐 Nginx Proxy Manager Password Reset" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" echo "Container: $CONTAINER_ID on $PROXMOX_HOST" echo "New Password: $NEW_PASSWORD" echo "" # Check if container is running if ! ssh root@"$PROXMOX_HOST" "pct status $CONTAINER_ID" | grep -q "running"; then echo "❌ Container $CONTAINER_ID is not running" exit 1 fi echo "📋 Resetting password..." # Reset password using NPM's built-in method # Try to use NPM's own password hashing echo "📋 Generating password hash using NPM's environment..." # Method 1: Try using available bcrypt modules (bcrypt is already installed) PASSWORD_HASH=$(ssh root@"$PROXMOX_HOST" "pct exec $CONTAINER_ID -- bash -c ' cd /app # Try bcrypt first (already available) node -e \" try { const bcrypt = require(\\\"bcrypt\\\"); console.log(bcrypt.hashSync(process.argv[1], 10)); } catch(e1) { // Try bcryptjs if bcrypt fails try { const bcryptjs = require(\\\"bcryptjs\\\"); console.log(bcryptjs.hashSync(process.argv[1], 10)); } catch(e2) { console.error(\\\"ERROR: Cannot find bcrypt or bcryptjs\\\"); process.exit(1); } } \" \"$NEW_PASSWORD\" ' 2>/dev/null) if [ -z "$PASSWORD_HASH" ] || echo "$PASSWORD_HASH" | grep -q "ERROR\|Cannot"; then echo "⚠️ bcrypt modules not available, trying to install bcryptjs..." echo "📦 Installing bcryptjs (this may take a minute)..." ssh root@"$PROXMOX_HOST" "pct exec $CONTAINER_ID -- bash -c 'cd /app && timeout 120 npm install bcryptjs --no-save 2>&1'" if [ $? -eq 0 ]; then echo "✅ bcryptjs installed successfully" PASSWORD_HASH=$(ssh root@"$PROXMOX_HOST" "pct exec $CONTAINER_ID -- bash -c ' cd /app node -e \"const bcrypt = require(\\\"bcryptjs\\\"); console.log(bcrypt.hashSync(process.argv[1], 10));\" \"$NEW_PASSWORD\" ' 2>/dev/null) else echo "❌ Failed to install bcryptjs" exit 1 fi fi if [ -z "$PASSWORD_HASH" ] || [ "$PASSWORD_HASH" = "null" ]; then echo "❌ Failed to generate password hash" echo "💡 Alternative: Access NPM web UI and use 'Forgot Password' feature" echo " Or manually reset via: http://${IP_NGINX_LEGACY:-192.168.11.26}:81" exit 1 fi echo "✅ Password hash generated" # Update database using Node.js and better-sqlite3 echo "📝 Updating database using Node.js..." ssh root@"$PROXMOX_HOST" "pct exec $CONTAINER_ID -- bash -c ' cd /app npm install better-sqlite3 --no-save --silent 2>&1 | tail -3 || true node -e \" const Database = require(\\\"better-sqlite3\\\"); const db = new Database(\\\"/data/database.sqlite\\\"); const hash = process.argv[1]; const email = process.argv[2]; const stmt = db.prepare(\\\"UPDATE user SET password = ?, modified_on = datetime(\\\\\\\"now\\\\\\\") WHERE email = ?\\\"); const info = stmt.run(hash, email); if (info.changes > 0) { console.log(\\\"Password updated for \\\" + email); } else { // If user doesn'\''t exist, create it const insertStmt = db.prepare(\\\"INSERT INTO user (email, name, password, is_admin, created_on, modified_on) VALUES (?, ?, ?, 1, datetime(\\\\\\\"now\\\\\\\"), datetime(\\\\\\\"now\\\\\\\"))\\\"); const insertInfo = insertStmt.run(email, email.split(\\\"@\\\")[0], hash); if (insertInfo.changes > 0) { console.log(\\\"User created and password set for \\\" + email); } else { console.error(\\\"Failed to update or create user for \\\" + email); process.exit(1); } } db.close(); \" \"$PASSWORD_HASH\" \"$EMAIL\" '" echo "" echo "✅ Password reset complete!" echo "" echo "New credentials:" echo " Email: $EMAIL" echo " Password: $NEW_PASSWORD" echo "" echo "Test login at: http://${IP_NGINX_LEGACY:-192.168.11.26}:81" rm -f /tmp/npm-password-hash.txt