Skip to content

An simple tool which could be deployed on a server with a fixed public IP address to maintain IPv6 address records of other devices in real time.You can simply update the IPv6 address of one device and get it from the anthor device.

Notifications You must be signed in to change notification settings

Xhen-Starry-Night/ip_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IP Server

A lightweight Rust-based server for managing and retrieving IP addresses of devices using an authenticated API.

Features

  • 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

Prerequisites

  • Rust 2024 Edition
  • Cargo

Installation

  1. Clone the repository
  2. Navigate to the project directory
  3. Build the project:
cargo build --release

Configuration

The 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 seconds

Command Line Options

The 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.

Logging

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

API Endpoints

Health Check

  • GET / - Check if the server is running

Response: "The server is running now."

Update Device IP

  • 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

Get Device IP

  • 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

Running the Server

Start the server with default configuration:

cargo run

Or 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-config

The 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"}'

Architecture

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 management
  • handlers: API endpoint handlers
  • config: Configuration management
  • logging: Logging functionality
  • errors: 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.

License

MIT License

About

An simple tool which could be deployed on a server with a fixed public IP address to maintain IPv6 address records of other devices in real time.You can simply update the IPv6 address of one device and get it from the anthor device.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages