# LXD Proxy API (Slim Framework) This is a PHP-based API using the **Slim 4 Framework** that dynamically manages and proxies traffic to LXD containers based on subdomain mappings. --- ## ๐Ÿงฉ Features - Automatically creates and starts LXD containers per subdomain - CAPTCHA validation for provisioning - Proxies requests to containerized environments - Persists domain-to-container mapping - Waits for container service to be ready before forwarding - Logs last access per container --- ## ๐Ÿ“ Project Structure backend/ โ”œโ”€โ”€ app/ โ”‚ โ”œโ”€โ”€ src/ โ”‚ โ”‚ โ”œโ”€โ”€ Controllers/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ProxyController.php โ”‚ โ”‚ โ”œโ”€โ”€ Services/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ LxdService.php โ”‚ โ”‚ โ””โ”€โ”€ Utils/ โ”‚ โ”‚ โ””โ”€โ”€ SubdomainHelper.php โ”‚ โ”œโ”€โ”€ vendor/ # Composer dependencies โ”‚ โ”œโ”€โ”€ public/ โ”‚ โ”‚ โ””โ”€โ”€ last-access-logs/ โ”‚ โ”œโ”€โ”€ config.json # Domain-to-container mappings โ”‚ โ””โ”€โ”€ index.php # Slim entry point โ”œโ”€โ”€ composer.json โ””โ”€โ”€ README.md --- ## โš™๏ธ Requirements - PHP 8.1+ - LXD installed and configured - PHP-FPM + NGINX - Composer - `slim/slim` - `slim/psr7` - `guzzlehttp/guzzle` - `vlucas/phpdotenv` --- ## ๐Ÿš€ Setup Instructions 1. **put the folder in /var/www/html** 2. **Install dependencies** ```bash composer install 3. **Ensure PHP and NGINX are configured** NGINX config example: ```bash server { listen 80; server_name *.lxdapp.local; root /var/www/html/lxd-app/backend/public; index index.php; location / { try_files $uri /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/run/php/php8.4-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_read_timeout 300; } } 4. **Map domain in /etc/hosts** ```bash 127.0.0.1 test.lxdapp.local 5. **Make sure LXD is working** ```bash lxc list 6. **๐Ÿ” CAPTCHA Protection** The /api/v1/proxy POST endpoint expects a field panswer with the correct CAPTCHA. You can configure PCaptcha class for custom logic. 7. **๐Ÿงช API Usage** POST /api/v1/proxy **Request Body:** ```bash { "source": "login", "panswer": "abc123" } **Headers:** Origin: http://customer.lxdapp.local **Response:** ```bash { "status": "success", "ip": "10.210.189.24" } **๐Ÿง  Notes** Container names are auto-generated using the subdomain prefix. All containers are mapped in config.json. If a container does not yet exist, it's created, started, and software is installed. The system waits for the container to expose port 80 before proxying. **๐Ÿชต Logs** Last access logs for containers are saved in: public/last-access-logs/container-*.txt NGINX error logs (for debugging): ```bash tail -f /var/log/nginx/error.log **๐Ÿ‘จโ€๐Ÿ’ป Development** Codebase follows PSR-4 autoloading (App\ namespace). To add new functionality, work within app/src/Controllers or Services.