azalea_protocol/
resolver.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! Resolve IPs from hostnames.

use std::net::{IpAddr, SocketAddr};

use async_recursion::async_recursion;
use thiserror::Error;
use trust_dns_resolver::{
    config::{ResolverConfig, ResolverOpts},
    Name, TokioAsyncResolver,
};

use crate::ServerAddress;

#[derive(Error, Debug)]
pub enum ResolverError {
    #[error("No SRV record found")]
    NoSrvRecord,
    #[error("No IP found")]
    NoIp,
}

/// Resolve a Minecraft server address into an IP address and port.
/// If it's already an IP address, it's returned as-is.
#[must_use]
#[async_recursion]
pub async fn resolve_address(address: &ServerAddress) -> Result<SocketAddr, ResolverError> {
    // If the address.host is already in the format of an ip address, return it.
    if let Ok(ip) = address.host.parse::<IpAddr>() {
        return Ok(SocketAddr::new(ip, address.port));
    }

    // we specify Cloudflare instead of the default resolver because
    // trust_dns_resolver has an issue on Windows where it's really slow using the
    // default resolver
    let resolver = TokioAsyncResolver::tokio(ResolverConfig::cloudflare(), ResolverOpts::default());

    // first, we do a srv lookup for _minecraft._tcp.<host>
    let srv_redirect_result = resolver
        .srv_lookup(format!("_minecraft._tcp.{}", address.host).as_str())
        .await;

    // if it resolves that means it's a redirect so we call resolve_address again
    // with the new host
    if let Ok(redirect_result) = srv_redirect_result {
        let redirect_srv = redirect_result
            .iter()
            .next()
            .ok_or(ResolverError::NoSrvRecord)?;
        let redirect_address = ServerAddress {
            host: redirect_srv.target().to_ascii(),
            port: redirect_srv.port(),
        };

        if redirect_address.host == address.host {
            let lookup_ip_result = resolver.lookup_ip(redirect_address.host).await;
            let lookup_ip = lookup_ip_result.map_err(|_| ResolverError::NoIp)?;
            return Ok(SocketAddr::new(
                lookup_ip.iter().next().unwrap(),
                redirect_address.port,
            ));
        }

        return resolve_address(&redirect_address).await;
    }

    // there's no redirect, try to resolve this as an ip address
    let name = Name::from_ascii(&address.host).map_err(|_| ResolverError::NoIp)?;
    let lookup_ip_result = resolver.lookup_ip(name).await;
    let lookup_ip = lookup_ip_result.map_err(|_| ResolverError::NoIp)?;

    Ok(SocketAddr::new(
        lookup_ip.iter().next().unwrap(),
        address.port,
    ))
}