fix: resolve Minecraft DNS SRV redirects for ping and login

Co-Authored-By: OpenAI Codex gpt-6.1-sol <noreply@openai.com>
This commit is contained in:
Ryan B
2026-10-24 14:04:46 -04:00
parent 7fca3c2f97
commit 83657f3bbf
6 changed files with 467 additions and 8 deletions
Generated
+276 -5
View File
@@ -155,7 +155,7 @@ dependencies = [
"objc2-foundation 0.3.2",
"parking_lot",
"percent-encoding",
"windows-sys 0.59.0",
"windows-sys 0.60.2",
"wl-clipboard-rs",
"x11rb",
]
@@ -187,6 +187,17 @@ dependencies = [
"libloading",
]
[[package]]
name = "async-trait"
version = "0.1.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.6",
]
[[package]]
name = "atomic-waker"
version = "1.1.2"
@@ -699,6 +710,30 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "critical-section"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
[[package]]
name = "crossbeam-channel"
version = "0.5.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.23"
@@ -718,7 +753,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
dependencies = [
"generic-array",
"rand_core",
"rand_core 0.6.4",
"typenum",
]
@@ -738,7 +773,7 @@ dependencies = [
"cpufeatures",
"curve25519-dalek-derive",
"fiat-crypto",
"rand_core",
"rand_core 0.6.4",
"rustc_version",
"subtle",
]
@@ -760,6 +795,12 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f"
[[package]]
name = "data-encoding"
version = "2.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06"
[[package]]
name = "derive_arbitrary"
version = "1.5.0"
@@ -871,6 +912,18 @@ dependencies = [
"simdutf8",
]
[[package]]
name = "enum-as-inner"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "equivalent"
version = "1.0.2"
@@ -991,12 +1044,27 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "futures-channel"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4"
dependencies = [
"futures-core",
]
[[package]]
name = "futures-core"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e"
[[package]]
name = "futures-io"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed"
[[package]]
name = "futures-task"
version = "0.3.34"
@@ -1188,6 +1256,52 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284"
[[package]]
name = "hickory-proto"
version = "0.25.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8a6fe56c0038198998a6f217ca4e7ef3a5e51f46163bd6dd60b5c71ca6c6502"
dependencies = [
"async-trait",
"cfg-if",
"data-encoding",
"enum-as-inner",
"futures-channel",
"futures-io",
"futures-util",
"idna",
"ipnet",
"once_cell",
"rand",
"ring",
"thiserror 2.0.21",
"tinyvec",
"tokio",
"tracing",
"url",
]
[[package]]
name = "hickory-resolver"
version = "0.25.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc62a9a99b0bfb44d2ab95a7208ac952d31060efc16241c87eaf36406fecf87a"
dependencies = [
"cfg-if",
"futures-util",
"hickory-proto",
"ipconfig",
"moka",
"once_cell",
"parking_lot",
"rand",
"resolv-conf",
"smallvec",
"thiserror 2.0.21",
"tokio",
"tracing",
]
[[package]]
name = "hidapi"
version = "2.6.7"
@@ -1314,7 +1428,7 @@ dependencies = [
"serde",
"serde_json",
"sha2",
"socket2",
"socket2 0.5.10",
"spake2",
]
@@ -1375,11 +1489,13 @@ version = "0.1.0"
dependencies = [
"flate2",
"getrandom 0.3.4",
"hickory-resolver",
"horizon-core",
"horizon-world",
"serde_json",
"sha1",
"sha2",
"tokio",
]
[[package]]
@@ -1653,6 +1769,25 @@ dependencies = [
"generic-array",
]
[[package]]
name = "ipconfig"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d40460c0ce33d6ce4b0630ad68ff63d6661961c48b6dba35e5a4d81cfb48222"
dependencies = [
"socket2 0.6.5",
"widestring",
"windows-registry",
"windows-result 0.4.1",
"windows-sys 0.61.2",
]
[[package]]
name = "ipnet"
version = "2.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0"
[[package]]
name = "itertools"
version = "0.14.0"
@@ -1917,6 +2052,34 @@ dependencies = [
"simd-adler32",
]
[[package]]
name = "mio"
version = "1.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee"
dependencies = [
"libc",
"wasi",
"windows-sys 0.61.2",
]
[[package]]
name = "moka"
version = "0.12.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4293f18e7567a1caf3c584855554377025c65e0aa445344d04171f5ad63d19b9"
dependencies = [
"crossbeam-channel",
"crossbeam-epoch",
"crossbeam-utils",
"equivalent",
"parking_lot",
"portable-atomic",
"smallvec",
"tagptr",
"uuid",
]
[[package]]
name = "multiversion_no_op"
version = "1.0.0"
@@ -2416,6 +2579,10 @@ name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
dependencies = [
"critical-section",
"portable-atomic",
]
[[package]]
name = "opaque-debug"
@@ -2619,6 +2786,15 @@ dependencies = [
"zerovec",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "presser"
version = "0.3.1"
@@ -2712,6 +2888,26 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rand"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41"
dependencies = [
"rand_chacha",
"rand_core 0.9.5",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core 0.9.5",
]
[[package]]
name = "rand_core"
version = "0.6.4"
@@ -2721,6 +2917,15 @@ dependencies = [
"getrandom 0.2.17",
]
[[package]]
name = "rand_core"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
dependencies = [
"getrandom 0.3.4",
]
[[package]]
name = "range-alloc"
version = "0.1.5"
@@ -2821,6 +3026,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832"
[[package]]
name = "resolv-conf"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e061d1b48cb8d38042de4ae0a7a6401009d6143dc80d2e2d6f31f0bdd6470c7"
[[package]]
name = "ring"
version = "0.17.14"
@@ -3137,6 +3348,16 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "socket2"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4"
dependencies = [
"libc",
"windows-sys 0.61.2",
]
[[package]]
name = "spake2"
version = "0.4.0"
@@ -3145,7 +3366,7 @@ checksum = "c5482afe85a0b6ce956c945401598dbc527593c77ba51d0a87a586938b1b893a"
dependencies = [
"curve25519-dalek",
"hkdf",
"rand_core",
"rand_core 0.6.4",
"sha2",
]
@@ -3286,6 +3507,12 @@ dependencies = [
"syn 3.0.6",
]
[[package]]
name = "tagptr"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417"
[[package]]
name = "target-lexicon"
version = "0.13.5"
@@ -3376,13 +3603,36 @@ dependencies = [
"zerovec",
]
[[package]]
name = "tinyvec"
version = "1.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee"
[[package]]
name = "tokio"
version = "1.53.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e95f91fcc7a621e8b030f6aa23c71fe9838ae2fb4d8118b75602a328f5144044"
dependencies = [
"bytes",
"libc",
"mio",
"pin-project-lite",
"socket2 0.6.5",
"tokio-macros",
"windows-sys 0.61.2",
]
[[package]]
name = "tokio-macros"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.6",
]
[[package]]
@@ -3452,6 +3702,9 @@ name = "tracing-core"
version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
dependencies = [
"once_cell",
]
[[package]]
name = "tree_magic_mini"
@@ -3561,6 +3814,7 @@ version = "1.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cc1186384beb7dd8eedea376413fd654937285ea6c9cfbb928dc3043ea4b606"
dependencies = [
"getrandom 0.4.3",
"js-sys",
"wasm-bindgen",
]
@@ -4297,6 +4551,12 @@ dependencies = [
"web-sys",
]
[[package]]
name = "widestring"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471"
[[package]]
name = "winapi-util"
version = "0.1.11"
@@ -4429,6 +4689,17 @@ dependencies = [
"windows-link",
]
[[package]]
name = "windows-registry"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720"
dependencies = [
"windows-link",
"windows-result 0.4.1",
"windows-strings",
]
[[package]]
name = "windows-result"
version = "0.1.2"
+8 -3
View File
@@ -268,8 +268,13 @@ fn run(
target
}
};
let c =
Connection::connect(&host, port, Duration::from_secs(10)).map_err(|e| e.to_string())?;
let c = if address.starts_with(FRIEND) {
// The friends relay supplies a concrete endpoint, not a Minecraft hostname.
Connection::connect(&host, port, Duration::from_secs(10))
} else {
Connection::connect_server(&host, port, Duration::from_secs(10))
}
.map_err(|e| e.to_string())?;
(host, port, c)
};
*shared_counters.lock().unwrap() = Some(c.reader.counters.clone());
@@ -559,7 +564,7 @@ pub fn load_language(resources: &std::path::Path) -> Language {
pub fn ping(address: &str, timeout: Duration) -> Result<(serde_json::Value, u128), String> {
use horizon_protocol::packets::status;
let (host, port) = parse_address(address).ok_or("Invalid address")?;
let mut c = Connection::connect(&host, port, timeout).map_err(|e| e.to_string())?;
let mut c = Connection::connect_server(&host, port, timeout).map_err(|e| e.to_string())?;
c.reader
.set_read_timeout(Some(timeout))
.map_err(|e| e.to_string())?;
+3
View File
@@ -16,6 +16,9 @@ getrandom = "0.3"
sha1 = "0.10"
# Secure chat: SHA256withRSA message signatures.
sha2 = "0.10"
# Minecraft SRV redirects, using the system DNS configuration.
hickory-resolver = "0.25.2"
tokio = { version = "1", features = ["rt", "time"] }
# Its own name, not `probe`: Cargo copies every crate's examples into one target/<profile>/examples.
[[example]]
+13
View File
@@ -191,6 +191,19 @@ impl Connection {
.to_socket_addrs()
.map_err(|e| Error::new(format!("Unknown host {host}: {e}")))?
.collect();
Self::connect_addresses(host, addrs, timeout)
}
/// Minecraft's SRV redirect applies to the socket destination only. Callers
/// keep the player's original host and port in the protocol handshake.
pub fn connect_server(host: &str, port: u16, timeout: Duration) -> Result<Self> {
let addrs = crate::resolver::resolve(host, port, timeout)?;
Self::connect_addresses(host, addrs, timeout)
}
fn connect_addresses(
host: &str,
addrs: Vec<std::net::SocketAddr>,
timeout: Duration,
) -> Result<Self> {
let mut last = Error::new(format!("Unknown host {host}"));
for addr in addrs {
match TcpStream::connect_timeout(&addr, timeout) {
+1
View File
@@ -12,6 +12,7 @@ pub mod ids;
pub mod item;
pub mod packets;
pub mod registry;
mod resolver;
pub mod rsa;
pub mod secure_chat;
pub mod servers;
+166
View File
@@ -0,0 +1,166 @@
//! Minecraft server DNS redirects (`ServerRedirectHandler` / `ServerNameResolver`).
//! Only the default port uses SRV; no record or a failed query leaves the
//! original address in use. The protocol handshake still uses that address.
use crate::{Error, Result};
use std::{
net::{IpAddr, SocketAddr, ToSocketAddrs},
time::Duration,
};
pub(crate) fn resolve(host: &str, port: u16, timeout: Duration) -> Result<Vec<SocketAddr>> {
resolve_with(
host,
port,
|name| srv_redirect(name, timeout),
|host, port| {
(host, port)
.to_socket_addrs()
.map(|addrs| addrs.collect())
.map_err(|e| Error::new(format!("Unknown host {host}: {e}")))
},
)
}
fn resolve_with(
host: &str,
port: u16,
mut srv: impl FnMut(&str) -> Option<(String, u16)>,
mut addresses: impl FnMut(&str, u16) -> Result<Vec<SocketAddr>>,
) -> Result<Vec<SocketAddr>> {
if port == 25565
&& host.parse::<IpAddr>().is_err()
&& host != "localhost"
&& let Some((target, target_port)) =
srv(&format!("_minecraft._tcp.{}.", host.trim_end_matches('.')))
{
// Do not follow another SRV record at the target. Vanilla resolves its
// addresses directly, and does not fall back if the target is unknown.
return addresses(&target, target_port);
}
addresses(host, port)
}
fn srv_redirect(name: &str, timeout: Duration) -> Option<(String, u16)> {
// Runs only on the blocking ping/login worker, never the render thread.
// Use the system's configured resolvers, not a public fallback.
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.ok()?;
runtime.block_on(async {
let mut builder = hickory_resolver::TokioResolver::builder_tokio().ok()?;
builder.options_mut().attempts = 1;
builder.options_mut().timeout = timeout;
let resolver = builder.build();
let records = tokio::time::timeout(timeout, resolver.srv_lookup(name))
.await
.ok()?
.ok()?;
// ServerRedirectHandler uses the first record, not weighted balancing.
let record = records.iter().next()?;
Some((record.target().to_utf8(), record.port()))
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn redirects_default_port_even_when_the_original_host_has_no_addresses() {
let result = resolve_with(
"play.example.org",
25565,
|name| {
assert_eq!(name, "_minecraft._tcp.play.example.org.");
Some(("backend.example.org.".into(), 25570))
},
|host, port| {
assert_eq!((host, port), ("backend.example.org.", 25570));
Ok(vec!["127.0.0.1:25570".parse().unwrap()])
},
)
.unwrap();
assert_eq!(
result,
vec!["127.0.0.1:25570".parse::<SocketAddr>().unwrap()]
);
}
#[test]
fn no_record_or_lookup_failure_keeps_the_original_address() {
resolve_with(
"play.example.org",
25565,
|_| None,
|host, port| {
assert_eq!((host, port), ("play.example.org", 25565));
Ok(vec![])
},
)
.unwrap();
}
#[test]
fn explicit_nondefault_ports_and_ip_literals_do_not_query_srv() {
for (host, port) in [
("play.example.org", 25570),
("127.0.0.1", 25565),
("::1", 25565),
("localhost", 25565),
] {
resolve_with(
host,
port,
|_| panic!("unexpected SRV query"),
|actual, actual_port| {
assert_eq!((actual, actual_port), (host, port));
Ok(vec![])
},
)
.unwrap();
}
}
#[test]
fn fully_qualified_host_does_not_get_a_second_trailing_dot() {
resolve_with(
"play.example.org.",
25565,
|name| {
assert_eq!(name, "_minecraft._tcp.play.example.org.");
None
},
|_, _| Ok(vec![]),
)
.unwrap();
}
#[test]
fn unknown_redirect_target_does_not_fall_back_to_the_original() {
let result = resolve_with(
"play.example.org",
25565,
|_| Some(("missing.example.org.".into(), 25570)),
|host, _| {
assert_eq!(host, "missing.example.org.");
Err(Error::new("unknown redirected host"))
},
);
assert_eq!(result.unwrap_err().to_string(), "unknown redirected host");
}
#[test]
fn literal_address_can_connect_without_dns() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let connection = crate::connection::Connection::connect_server(
"127.0.0.1",
address.port(),
Duration::from_secs(1),
)
.unwrap();
let (accepted, _) = listener.accept().unwrap();
drop((connection, accepted));
}
}