Implement comprehensive rate limiting system and item spawn configuration

Major Features Added:
- Complete token bucket rate limiting for IRC commands and web interface
- Per-user rate tracking with category-based limits (Basic, Gameplay, Management, Admin, Web)
- Admin commands for rate limit management (\!rate_stats, \!rate_user, \!rate_unban, \!rate_reset)
- Automatic violation tracking and temporary bans with cleanup
- Global item spawn multiplier system with 75% spawn rate reduction
- Central admin configuration system (config.py)
- One-command bot startup script (start_petbot.sh)

Rate Limiting:
- Token bucket algorithm with burst capacity and refill rates
- Category limits: Basic (20/min), Gameplay (10/min), Management (5/min), Web (60/min)
- Graceful violation handling with user-friendly error messages
- Admin exemption and override capabilities
- Background cleanup of old violations and expired bans

Item Spawn System:
- Added global_spawn_multiplier to config/items.json for easy adjustment
- Reduced all individual spawn rates by 75% (multiplied by 0.25)
- Admins can fine-tune both global multiplier and individual item rates
- Game engine integration applies multiplier to all spawn calculations

Infrastructure:
- Single admin user configuration in config.py
- Enhanced startup script with dependency management and verification
- Updated documentation and help system with rate limiting guide
- Comprehensive test suite for rate limiting functionality

Security:
- Rate limiting protects against command spam and abuse
- IP-based tracking for web interface requests
- Proper error handling and status codes (429 for rate limits)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
megaproxy 2025-07-15 20:10:43 +00:00
parent f8ac661cd1
commit 915aa00bea
28 changed files with 5730 additions and 57 deletions

257
install_prerequisites.sh Executable file
View file

@ -0,0 +1,257 @@
#!/bin/bash
# PetBot Prerequisites Installation Script (Shell Version)
# This script installs all required packages and sets up the environment
set -e # Exit on any error
echo "============================================================"
echo "🐾 PetBot Prerequisites Installation Script (Shell Version)"
echo "============================================================"
echo
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}${NC} $1"
}
print_error() {
echo -e "${RED}${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠️${NC} $1"
}
print_info() {
echo -e "${BLUE}${NC} $1"
}
# Check if we're in the right directory
echo "🔍 Checking directory structure..."
if [ ! -f "requirements.txt" ] || [ ! -f "src/database.py" ] || [ ! -f "webserver.py" ]; then
print_error "Required files not found. Please run this script from the PetBot project directory."
exit 1
fi
print_status "Directory structure is correct"
# Check Python version
echo
echo "🔍 Checking Python version..."
python_version=$(python3 --version 2>&1)
if [ $? -ne 0 ]; then
print_error "Python 3 is not installed or not in PATH"
print_info "Please install Python 3.7+ and try again"
exit 1
fi
# Extract version numbers
version_string=$(echo "$python_version" | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+')
major=$(echo "$version_string" | cut -d'.' -f1)
minor=$(echo "$version_string" | cut -d'.' -f2)
if [ "$major" -lt 3 ] || ([ "$major" -eq 3 ] && [ "$minor" -lt 7 ]); then
print_error "Python 3.7+ required. Current version: $python_version"
exit 1
fi
print_status "Python version is compatible: $python_version"
# Check pip availability
echo
echo "🔍 Checking pip availability..."
if ! command -v pip3 &> /dev/null; then
if ! python3 -m pip --version &> /dev/null; then
print_error "pip is not available"
print_info "Install pip with:"
print_info " Ubuntu/Debian: sudo apt install python3-pip"
print_info " CentOS/RHEL: sudo yum install python3-pip"
print_info " macOS: brew install python3"
exit 1
fi
PIP_CMD="python3 -m pip"
else
PIP_CMD="pip3"
fi
pip_version=$($PIP_CMD --version)
print_status "pip is available: $pip_version"
# Create required directories
echo
echo "🔍 Creating required directories..."
if [ ! -d "data" ]; then
mkdir -p data
print_status "Created data directory"
else
print_status "Data directory already exists"
fi
if [ ! -d "backups" ]; then
mkdir -p backups
print_status "Created backups directory"
else
print_status "Backups directory already exists"
fi
if [ ! -d "logs" ]; then
mkdir -p logs
print_status "Created logs directory"
else
print_status "Logs directory already exists"
fi
# Install packages from requirements.txt
echo
echo "📦 Installing packages from requirements.txt..."
if $PIP_CMD install -r requirements.txt; then
print_status "Successfully installed packages from requirements.txt"
else
print_error "Failed to install from requirements.txt"
echo
echo "📦 Trying individual package installation..."
# Try individual packages
packages=(
"irc>=20.3.0"
"aiosqlite>=0.19.0"
"python-dotenv>=1.0.0"
)
failed_packages=()
for package in "${packages[@]}"; do
echo "Installing $package..."
if $PIP_CMD install "$package"; then
print_status "Successfully installed $package"
else
print_error "Failed to install $package"
failed_packages+=("$package")
fi
done
if [ ${#failed_packages[@]} -gt 0 ]; then
print_error "Failed to install the following packages:"
for package in "${failed_packages[@]}"; do
echo " - $package"
done
exit 1
fi
fi
# Verify installation
echo
echo "🔍 Verifying installation..."
# List of modules to test
modules=(
"irc:IRC client library"
"aiosqlite:Async SQLite database"
"dotenv:Environment variable loading"
"asyncio:Async programming (built-in)"
"sqlite3:SQLite database (built-in)"
"json:JSON handling (built-in)"
"socket:Network communication (built-in)"
"threading:Threading (built-in)"
)
failed_imports=()
for module_info in "${modules[@]}"; do
module=$(echo "$module_info" | cut -d':' -f1)
description=$(echo "$module_info" | cut -d':' -f2)
if python3 -c "import $module" 2>/dev/null; then
print_status "$module: $description"
else
print_error "$module: $description - Import failed"
failed_imports+=("$module")
fi
done
if [ ${#failed_imports[@]} -gt 0 ]; then
print_error "Some modules failed to import:"
for module in "${failed_imports[@]}"; do
echo " - $module"
done
exit 1
fi
# Test basic project imports
echo
echo "🧪 Testing project imports..."
if python3 -c "
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath('.')))
from src.database import Database
from src.irc_connection_manager import IRCConnectionManager
from src.backup_manager import BackupManager
print('All project imports successful')
" 2>/dev/null; then
print_status "Project imports successful"
else
print_error "Project imports failed"
print_info "This might be due to missing dependencies or path issues"
exit 1
fi
# Check for optional system dependencies
echo
echo "🔍 Checking optional system dependencies..."
if command -v git &> /dev/null; then
print_status "Git is available"
else
print_warning "Git is not available (optional for development)"
fi
if command -v curl &> /dev/null; then
print_status "curl is available"
else
print_warning "curl is not available (optional for testing)"
fi
# Make scripts executable
echo
echo "🔧 Making scripts executable..."
chmod +x install_prerequisites.py
chmod +x run_bot_debug.py
chmod +x run_bot_with_reconnect.py
chmod +x test_backup_simple.py
chmod +x test_reconnection.py
print_status "Made scripts executable"
# Show summary
echo
echo "🎉 Installation completed successfully!"
echo
echo "📋 Next steps:"
echo "1. Review and update configuration files in the config/ directory"
echo "2. Test the bot with: python3 run_bot_debug.py"
echo "3. Or use the new reconnection system: python3 run_bot_with_reconnect.py"
echo "4. Check the web interface at: http://localhost:8080"
echo "5. Review CLAUDE.md for development guidelines"
echo
echo "🔧 Available test commands:"
echo " python3 test_backup_simple.py - Test backup system"
echo " python3 test_reconnection.py - Test IRC reconnection"
echo
echo "📚 Documentation:"
echo " README.md - Project overview"
echo " CLAUDE.md - Development guide"
echo " TODO.md - Current status"
echo " issues.txt - Security audit findings"
echo " BACKUP_SYSTEM_INTEGRATION.md - Backup system guide"
echo
echo "🚀 You're ready to run PetBot!"
echo "✅ All prerequisites have been installed successfully!"