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");
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!();
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!();
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!();
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(())
}