A lightweight Rust-based server for managing and retrieving IP addresses of devices using an authenticated API.
- IPv6 Support: The server listens on IPv6 addresses by default
- Authentication: All API endpoints require authentication with an auth code
- Device Tracking: Store and retrieve device IP addresses with automatic expiration
- Command Line Interface: Configure auth code and timeout via CLI options
- Comprehensive Logging: Detailed logging of all client operations
- Robust Error Handling: Comprehensive error handling with thiserror
- Modular Architecture: Well-organized code modules following industry standards
- RESTful API: Clean API with GET/POST endpoints
- Thread-safe: Uses DashMap for concurrent access
- Self-contained Configuration: Configuration file located with executable
- Rust 2024 Edition
- Cargo
- Clone the repository
- Navigate to the project directory
- Build the project:
cargo build --releaseThe server supports both command-line arguments and configuration file for settings. The configuration file is config.toml located in the same directory as the executable. If the file doesn't exist, it will be created with default values:
auth_code = "basic_auth_code"
expire_time = 86400 # 24 hours in secondsThe server accepts command line arguments that override configuration file settings:
./ip_server [OPTIONS]Options:
-a, --auth-code <AUTH_CODE>: Set authorization code-e, --expire-time <EXPIRE_TIME>: Set timeout in seconds--save-config: Save parameters to config file-h, --help: Show help information
The command-line arguments take precedence over values in the config file. If --save-config flag is provided, the current parameters will be saved to the config file.
The server logs all operations to a .log file in the same directory as the executable. Each log entry contains:
- Timestamp of the operation
- Client IP address
- Operation type (GET_IP or UPDATE_IP)
- Detailed operation information
Example log entry:
[2026-02-01 01:45:24] IP: ::1, Operation: UPDATE_IP, Details: Successfully updated IP for device: test_device, IP: ::1
- GET / - Check if the server is running
Response: "The server is running now."
- POST /update - Register or update a device's IP address
Request body:
{
"auth_code": "your_auth_code",
"device_name": "device_identifier"
}Response: "ok" on success
Status codes:
- 200: Success
- 401: Invalid auth code
- 500: Internal server error
- POST /get - Retrieve a device's IP address
Request body:
{
"auth_code": "your_auth_code",
"device_name": "device_identifier"
}Response: Device's IP address as string on success
Status codes:
- 200: Success, returns IP address
- 401: Invalid auth code
- 404: Device not found
- 410: Device record expired
- 500: Internal server error
Start the server with default configuration:
cargo runOr with custom parameters:
# Set auth code and timeout via CLI
cargo run -- --auth-code "my_secret_code" --expire-time 7200
# Set parameters and save to config file
cargo run -- --auth-code "my_secret_code" --expire-time 7200 --save-configThe server will listen on [::1]:3000 (IPv6 localhost).
Example usage:
# Update device IP
curl -X POST http://[::1]:3000/update \
-H "Content-Type: application/json" \
-d '{"auth_code": "basic_auth_code", "device_name": "my_device"}'
# Get device IP
curl -X POST http://[::1]:3000/get \
-H "Content-Type: application/json" \
-d '{"auth_code": "basic_auth_code", "device_name": "my_device"}'The server uses Axum as the web framework with Tokio for asynchronous runtime. The code is organized into modular components:
models: Data structures and state managementhandlers: API endpoint handlersconfig: Configuration managementlogging: Logging functionalityerrors: Custom error types
Device information is stored in a thread-safe DashMap with automatic expiration checking on retrieval. Configuration and log files are stored in the same directory as the executable.
MIT License