5.6 KiB
CLI to API Integration
This document describes how TrafegoDNS integrates its Command-Line Interface (CLI) with the API server, allowing for secure local operations.
Architecture Overview
TrafegoDNS follows a multi-tier architecture:
- API Server: The core backend that provides REST endpoints for all operations
- CLI Client: A command-line interface that can use the API or direct service access
- State Management: Central store and action broker for managing application state
This architecture brings several benefits:
- Consistent behavior between CLI and web UI interactions
- Centralized security and validation at the API layer
- Flexible access patterns (API, direct, or hybrid)
- Multi-mode operation with graceful fallbacks
Communication Patterns
The CLI can communicate with the backend using several patterns:
- API Communication: Standard HTTP requests to the API server
- Direct Service Access: In-memory access to application services when running inside the container
- Action Broker: Dispatch events to the central state management system
- Fallback Chain: Cascading attempts from API → Action Broker → Direct Access
This flexible approach ensures the CLI works in various contexts:
- Inside Docker container with API access
- Inside Docker container without API (direct mode)
- External to the container with API access
- In development environment with live reloading
Authentication Methods
API Authentication
For API communication, the CLI uses these authentication mechanisms:
- Bearer Token: Standard JWT authentication with Bearer token
- Special Headers: Custom X-Trafego-CLI header with the CLI token
- Local Authentication Bypass: Special middleware that grants admin privileges to local CLI requests
Local Auth Bypass
For local operations (CLI commands), TrafegoDNS implements a secure local authentication bypass:
- When the CLI runs commands, it sends requests to the API server with special authorization headers
- The
localAuthBypassMiddlewaredetects these requests and grants them administrative privileges - This allows CLI operations to bypass authentication while still using the API endpoints
The local auth bypass is:
- Only active for localhost connections and CLI clients with the proper security token
- Configurable via environment variables
- Secured with a token that's shared between the CLI and API server
Container Integration
Inside the Docker container, the CLI is:
- Automatically installed in standard PATH locations (
/usr/local/bin/trafego) - Pre-configured with the correct environment variables
- Provided with specialized wrappers that load the application context
- Set up with appropriate permissions to access config files
This enables seamless usage with:
docker exec -it trafegodns trafego dns process
Without requiring any additional setup or configuration.
Operation Modes
TrafegoDNS can operate in several modes:
-
Full Mode (default): Both API server and CLI are active
npm start -
API-Only Mode: Only the API server runs (good for headless servers)
npm run start:api -
CLI-Only Mode: Uses direct service calls without the API server (legacy mode)
npm run start:cli -
Development Mode: Includes additional logging and debugging
npm run dev
Environment Variables
The following environment variables control the API/CLI integration:
| Variable | Default | Description |
|---|---|---|
API_URL |
http://localhost:3000 |
URL for API communication |
CLI_TOKEN |
trafegodns-cli |
Token for CLI authentication |
USE_API_MODE |
true |
Enable/disable API mode |
API_PORT |
3000 |
Port for the API server |
LOCAL_AUTH_BYPASS |
true |
Enable/disable local auth bypass |
TRAFEGO_INTERNAL_TOKEN |
random |
Security token for internal requests |
API_ONLY |
false |
Run in API-only mode without CLI |
DISABLE_CLI |
false |
Disable interactive CLI when running in API mode |
CONTAINER |
true |
Indicates running in a container environment |
TRAFEGO_CLI |
true |
Identifies CLI-specific environment |
Command Structure
The CLI uses a hierarchical command structure:
trafego <command-group> <command> [options]
Command Groups
dns: DNS record management commandsdb: Database management commandssystem: System status and management commands
Each group contains multiple commands with their own options and parameters.
Implementation Notes
API Client
The CLI's API client (src/cli/apiClient.js) includes:
- Environment detection to determine if running in a container
- Direct service access when available inside the container
- Authentication header handling for different scenarios
- Graceful fallback between API and direct access methods
- Automatic error recovery and retry logic
Command Handlers
Command handlers (src/cli/commands/) follow these patterns:
- Attempt API access first (most reliable)
- Fall back to action broker if available
- Use direct service access as a last resort
- Provide detailed error messages when all methods fail
- Format output consistently regardless of data source
Security Considerations
- The local auth bypass only works for localhost connections and authenticated CLI clients
- The bypass token is randomly generated at startup or can be set via environment variables
- External API requests still require proper authentication via JWT
- The API server implements rate limiting, CORS protection, and other security measures
- Direct service access is only available inside the container