rust-mc-status 2.0.0

High-performance asynchronous Rust library for querying Minecraft server status (Java & Bedrock)
Documentation
//! SRV DNS lookup example for Minecraft servers.
//!
//! This example demonstrates the automatic port determination through SRV records.
//! When pinging Java servers without an explicit port, the library automatically
//! queries for SRV records (_minecraft._tcp.{hostname}) to find the correct host and port.

use rust_mc_status::{McClient, ServerEdition};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = McClient::new()
        .with_timeout(Duration::from_secs(5))
        .with_max_parallel(10);

    println!("=== SRV DNS Lookup Demonstration ===\n");

    // Example 1: Address WITHOUT port - SRV record lookup will be performed
    println!("1. Query server WITHOUT port specified:");
    println!("   Address: mc.hypixel.net");
    println!("   Expected behavior: library will check SRV record _minecraft._tcp.mc.hypixel.net");
    println!("   If SRV record found - uses host and port from it");
    println!("   If SRV record not found - uses default port (25565)\n");

    match client.ping("mc.hypixel.net", ServerEdition::Java).await {
        Ok(status) => {
            println!("   ✅ Result:");
            println!("      IP: {}", status.ip);
            println!("      Port: {} (port used)", status.port);
            println!("      Hostname: {} (original hostname)", status.hostname);
            println!("      Latency: {:.2} ms", status.latency);
        }
        Err(e) => {
            println!("   ❌ Error: {}", e);
        }
    }

    println!("\n{}", "=".repeat(60));
    println!();

    // Example 2: Address WITH port - SRV lookup is NOT performed
    println!("2. Query server WITH port specified:");
    println!("   Address: mc.hypixel.net:25565");
    println!("   Expected behavior: SRV lookup is skipped, specified port is used\n");

    match client.ping("mc.hypixel.net:25565", ServerEdition::Java).await {
        Ok(status) => {
            println!("   ✅ Result:");
            println!("      IP: {}", status.ip);
            println!("      Port: {} (explicitly specified port)", status.port);
            println!("      Hostname: {}", status.hostname);
            println!("      Latency: {:.2} ms", status.latency);
        }
        Err(e) => {
            println!("   ❌ Error: {}", e);
        }
    }

    println!("\n{}", "=".repeat(60));
    println!();

    // Example 3: Explanation of how SRV lookup works
    println!("3. How SRV DNS Lookup works:\n");
    println!("   When you specify an address WITHOUT port (e.g., 'example.com'):");
    println!("   1. Library parses address and uses default port (25565)");
    println!("   2. Checks SRV record cache (TTL 5 minutes)");
    println!("   3. If not in cache, makes DNS query for:");
    println!("      _minecraft._tcp.example.com");
    println!("   4. If SRV record found:");
    println!("      - Extracts target host and port from record");
    println!("      - Caches the result");
    println!("      - Connects to the found host and port");
    println!("   5. If SRV record NOT found:");
    println!("      - Uses original host and default port (25565)");
    println!("      - Caches the result (to avoid checking again)");
    println!("\n   When you specify an address WITH port (e.g., 'example.com:25565'):");
    println!("   - SRV lookup is completely skipped");
    println!("   - Uses specified host and port directly");
    println!("\n   In handshake packet, original hostname is always used");
    println!("   (as required by Minecraft protocol), but actual connection");
    println!("   happens to host from SRV record (if found).");

    println!("\n{}", "=".repeat(60));
    println!();

    // Example 4: Benefits of SRV lookup
    println!("4. Benefits of SRV DNS Lookup:\n");
    println!("   ✅ Automatic determination of correct port");
    println!("   ✅ Support for servers on non-standard ports");
    println!("   ✅ Matches standard Minecraft client behavior");
    println!("   ✅ Caching results for performance");
    println!("   ✅ Backward compatible (works without SRV records)");

    Ok(())
}