Add Dangerous Pi MVP implementation - complete backend and system integration

This commit adds the complete Dangerous Pi web management interface with all MVP features implemented and tested locally.

## New Features

### Backend (Python + FastAPI)
- Complete FastAPI backend with async support
- 40+ API endpoints (Health, PM3, WiFi, Updates, UPS, BLE, Plugins)
- 6 managers: Session, WiFi, Update, UPS, BLE, Plugin
- SQLite database with sessions, config, history, crash reports
- Server-Sent Events (SSE) for real-time notifications
- Mock PM3 worker for development without hardware

### WiFi Manager
- Interface detection (USB vs built-in)
- Network scanning with signal strength
- Mode switching (AP/Client/Dual/Auto/Off)
- Network connection with password support
- Hidden SSID and saved networks support
- Static IP and DHCP configuration
- 10 WiFi API endpoints

### Update Manager
- GitHub releases API integration
- Automatic periodic update checks
- Semantic version comparison
- Update download with progress tracking
- SHA256 checksum verification
- Automatic installation with backup and rollback
- PM3 client rebuild after updates
- 6 Update API endpoints

### UPS Manager
- I2C battery monitoring (MAX17040-compatible)
- Battery percentage, voltage, current tracking
- Power source detection (AC/Battery)
- Safe shutdown triggers at configurable thresholds
- Event callbacks for battery warnings
- SSE and BLE notification integration
- 3 UPS API endpoints

### BLE Manager
- Bluetooth Low Energy notification support
- Auto-detects BLE capability
- Multiple notification types (updates, battery, shutdown, etc.)
- BLE advertising management
- Device connection tracking
- 4 BLE API endpoints

### Plugin Framework
- Dynamic plugin loading/unloading
- Plugin lifecycle management (load, enable, disable, unload)
- Hook system for extensibility
- JSON-based metadata
- Example "Hello World" plugin included
- 7 Plugin API endpoints

### Frontend (Remix.js + React)
- Cyberpunk-themed responsive UI
- Dashboard with system status
- PM3 command interface with history
- Settings page with WiFi and Update management
- Command logs viewer
- Theme toggle (Dark/Light/Auto)
- Server-side rendering (SSR)
- Mobile-first responsive design

### System Integration
- Systemd service with security hardening
- Automated install/uninstall scripts
- Environment configuration template
- Hardware access groups (i2c, bluetooth, gpio, dialout)
- Pi-gen stage 04 integration for OS image building
- Port conflict resolution with ttyd-bash
- I2C interface auto-enable for UPS HAT

### Testing
- test_backend.py - Backend API tests
- test_ups.py - UPS manager tests
- test_ble.py - BLE manager tests
- test_plugins.py - Plugin manager tests
- All tests passing locally

### Documentation
- 12 comprehensive documentation files
- claude.md - AI development guide
- WIFI_MANAGER.md - WiFi management guide
- UPDATE_MANAGER.md - Update system guide
- PORT_CONFLICT.md - Port conflict resolution guide
- MVP_COMPLETE.md - MVP implementation summary
- PROJECT_STATUS.md - Project status and roadmap
- systemd/README.md - Service management docs
- pi-gen integration documentation

## Technical Details
- ~5,000+ lines of backend code
- 11 Python dependencies (smbus2 added for UPS)
- FastAPI with async/await throughout
- Type hints and docstrings on all functions
- RESTful API design with SSE for notifications
- Security hardening (non-root, protected dirs, resource limits)

## Next Steps
- Deploy to Raspberry Pi Zero 2 W hardware
- Test with real Proxmark3 device
- Test UPS HAT integration
- Test BLE on Pi hardware
- Build custom OS image with pi-gen
- Performance optimization for Pi Zero 2 W

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
michael
2025-11-26 08:11:36 -08:00
parent 0a586c5360
commit 1da6730735
68 changed files with 12673 additions and 5 deletions

View File

@@ -0,0 +1,56 @@
#!/bin/bash -e
# Install Dangerous Pi backend and dependencies
echo "Installing Dangerous Pi..."
# Install Python packages
pip3 install --break-system-packages \
fastapi==0.115.0 \
uvicorn[standard]==0.32.0 \
python-multipart==0.0.12 \
aiosqlite==0.20.0 \
aiohttp==3.10.5 \
httpx==0.27.2 \
sse-starlette==2.1.3 \
pydantic==2.9.0 \
pydantic-settings==2.5.0 \
psutil==6.1.0 \
smbus2==0.4.3
# Create installation directory
mkdir -p /opt/dangerous-pi
cd /opt/dangerous-pi
# Copy application files (from files directory in this stage)
cp -r "${ROOTFS_DIR}/tmp/dangerous-pi-files/"* /opt/dangerous-pi/
# Create data and logs directories
mkdir -p /opt/dangerous-pi/data
mkdir -p /opt/dangerous-pi/logs
# Set ownership and permissions
chown -R pi:pi /opt/dangerous-pi
chmod +x /opt/dangerous-pi/scripts/*.sh
chmod +x /opt/dangerous-pi/systemd/*.sh
# Create environment file from template
cp /opt/dangerous-pi/systemd/dangerous-pi.env.example /opt/dangerous-pi/.env
chown pi:pi /opt/dangerous-pi/.env
chmod 600 /opt/dangerous-pi/.env
# Install systemd service
cp /opt/dangerous-pi/systemd/dangerous-pi.service /etc/systemd/system/
chmod 644 /etc/systemd/system/dangerous-pi.service
# Add pi user to required hardware groups
usermod -a -G i2c,bluetooth,gpio,dialout pi
# Enable I2C interface (for UPS HAT)
if ! grep -q "^dtparam=i2c_arm=on" /boot/config.txt; then
echo "dtparam=i2c_arm=on" >> /boot/config.txt
fi
# Enable service to start on boot
systemctl enable dangerous-pi.service
echo "Dangerous Pi installation complete!"

View File

@@ -0,0 +1,32 @@
#!/bin/bash -e
# Prepare Dangerous Pi files for installation
# This script runs OUTSIDE the chroot environment
# It prepares files that will be copied into the image
echo "Preparing Dangerous Pi files..."
# Create temporary directory for files
mkdir -p "${ROOTFS_DIR}/tmp/dangerous-pi-files"
# Copy application files (relative to pi-gen directory)
DANGEROUS_PI_SRC="$(dirname "$(dirname "$(dirname "$(dirname "$0")")")")"
# Copy application structure
cp -r "${DANGEROUS_PI_SRC}/app" "${ROOTFS_DIR}/tmp/dangerous-pi-files/"
cp -r "${DANGEROUS_PI_SRC}/systemd" "${ROOTFS_DIR}/tmp/dangerous-pi-files/"
cp -r "${DANGEROUS_PI_SRC}/scripts" "${ROOTFS_DIR}/tmp/dangerous-pi-files/"
cp "${DANGEROUS_PI_SRC}/requirements.txt" "${ROOTFS_DIR}/tmp/dangerous-pi-files/"
# Copy VERSION file if it exists
if [ -f "${DANGEROUS_PI_SRC}/VERSION" ]; then
cp "${DANGEROUS_PI_SRC}/VERSION" "${ROOTFS_DIR}/tmp/dangerous-pi-files/"
else
echo "0.1.0" > "${ROOTFS_DIR}/tmp/dangerous-pi-files/VERSION"
fi
# Create empty directories
mkdir -p "${ROOTFS_DIR}/tmp/dangerous-pi-files/data"
mkdir -p "${ROOTFS_DIR}/tmp/dangerous-pi-files/logs"
echo "Files prepared successfully"

View File

@@ -0,0 +1,30 @@
#!/bin/bash -e
# Handle port conflict with ttyd-bash
echo "Handling port conflicts..."
# Option 1: Disable ttyd-bash (default)
# Uncomment to use this option:
# if systemctl is-enabled ttyd-bash 2>/dev/null; then
# systemctl disable ttyd-bash
# echo "Disabled ttyd-bash to avoid port conflict"
# fi
# Option 2: Change Dangerous Pi port to 8001
# Uncomment to use this option:
# if [ -f /opt/dangerous-pi/.env ]; then
# sed -i 's/^PORT=.*/PORT=8001/' /opt/dangerous-pi/.env
# echo "Changed Dangerous Pi port to 8001"
# fi
# Option 3: Change ttyd-bash port to 8002
# Uncomment to use this option:
# if [ -f /etc/systemd/system/ttyd-bash.service ]; then
# sed -i 's/--port 8000/--port 8002/' /etc/systemd/system/ttyd-bash.service
# echo "Changed ttyd-bash port to 8002"
# fi
# By default, we do nothing and let the user resolve it post-install
# This prevents accidental breakage of existing setups
echo "Port conflict resolution deferred to post-install"
echo "Run /opt/dangerous-pi/scripts/resolve-port-conflict.sh after boot"

View File

@@ -0,0 +1,185 @@
# Dangerous Pi - pi-gen Stage Integration
This directory contains the pi-gen stage scripts for installing Dangerous Pi into the custom Raspberry Pi OS image.
## Files
- `00-run.sh` - Pre-chroot script that prepares application files
- `00-run-chroot.sh` - Chroot script that installs Dangerous Pi and dependencies
## What Gets Installed
The stage performs the following actions:
### 1. Python Dependencies
Installs all required Python packages via pip:
- FastAPI and Uvicorn (web framework and ASGI server)
- AsyncIO libraries (aiosqlite, aiohttp)
- Pydantic (data validation)
- SSE-Starlette (Server-Sent Events)
- psutil (system monitoring)
- smbus2 (I2C communication for UPS)
### 2. Application Installation
- Creates `/opt/dangerous-pi` directory
- Copies application files (backend, frontend, plugins)
- Creates data and logs directories
- Sets proper ownership (pi:pi)
### 3. Configuration
- Creates environment file from template at `/opt/dangerous-pi/.env`
- Configures default settings (can be customized post-install)
### 4. Systemd Service
- Installs systemd service unit
- Enables service to start on boot
- Configures service with security hardening
### 5. Hardware Access
- Enables I2C interface in `/boot/config.txt` (for UPS HAT)
- Adds `pi` user to hardware groups:
- `i2c` - I2C bus access
- `bluetooth` - Bluetooth access
- `gpio` - GPIO pin access
- `dialout` - Serial device access (for Proxmark3)
## Build Process
During the pi-gen build:
1. `00-run.sh` executes outside the chroot:
- Copies application files to temporary location
- Prepares directory structure
2. `00-run-chroot.sh` executes inside the chroot:
- Installs Python dependencies
- Copies files to `/opt/dangerous-pi`
- Configures system
- Installs and enables systemd service
## Integration with Existing pi-pm3 Stages
Dangerous Pi (stage 04) runs after:
- Stage 01: Proxmark3 installation
- Stage 02: RaspAP installation
- Stage 03: ttyd installation
This order ensures all dependencies are available.
## Port Conflict Handling
The ttyd-bash service (from stage 03) uses port 8000, which conflicts with Dangerous Pi's default port. See the port conflict handling task for resolution options.
## Post-Build Configuration
After the image is built and booted, you can customize:
1. Edit `/opt/dangerous-pi/.env` to configure settings
2. Restart service: `sudo systemctl restart dangerous-pi`
3. View status: `sudo systemctl status dangerous-pi`
4. View logs: `sudo journalctl -u dangerous-pi -f`
## Testing the Stage
To test this stage during development:
1. Make changes to Dangerous Pi code
2. Run pi-gen build (this stage will copy the latest code)
3. Flash image to SD card
4. Boot Raspberry Pi
5. Check service status
## Customization
### Change Installation Directory
Edit `00-run-chroot.sh`:
```bash
mkdir -p /your/custom/path
cd /your/custom/path
```
Also update the systemd service file paths.
### Add Additional Dependencies
Edit `00-run-chroot.sh` and add to the pip install command:
```bash
pip3 install --break-system-packages \
<existing packages> \
your-new-package==1.0.0
```
### Skip Service Auto-Start
Comment out or remove from `00-run-chroot.sh`:
```bash
# systemctl enable dangerous-pi.service
```
## Troubleshooting
### Build Fails - Python Package Error
Check that all package versions in the pip install command match `requirements.txt`.
### Build Fails - Permission Error
Ensure scripts are executable:
```bash
chmod +x 00-run.sh 00-run-chroot.sh
```
### Service Doesn't Start After Boot
Check systemd logs:
```bash
sudo journalctl -u dangerous-pi -b
```
Common issues:
- Missing dependencies
- Incorrect file permissions
- Port conflict with ttyd
- Missing environment variables
### I2C Not Working
Verify I2C is enabled:
```bash
grep i2c_arm /boot/config.txt
ls -l /dev/i2c*
```
### Can't Access Proxmark3
Verify user is in dialout group:
```bash
groups pi | grep dialout
ls -l /dev/ttyACM*
```
## Development Workflow
1. Make changes to Dangerous Pi code in main repository
2. Test locally with `python3 -m app.backend.main`
3. When ready to test in image:
- Run pi-gen build (or incremental build for this stage)
- Flash to SD card and test
4. Iterate as needed
## Version Management
The VERSION file is copied from the repository root. If it doesn't exist, version defaults to "0.1.0".
To set a specific version:
```bash
echo "1.0.0" > /path/to/dangerous-pi/VERSION
```
Then rebuild the image.