use std::io;
use azalea_protocol::{
connect::{Connection, ConnectionError, Proxy},
packets::{
handshaking::{
client_intention_packet::ClientIntentionPacket, ClientboundHandshakePacket,
ServerboundHandshakePacket,
},
status::{
clientbound_status_response_packet::ClientboundStatusResponsePacket,
serverbound_status_request_packet::ServerboundStatusRequestPacket,
ClientboundStatusPacket,
},
ClientIntention, PROTOCOL_VERSION,
},
resolver, ServerAddress,
};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum PingError {
#[error("{0}")]
Resolver(#[from] resolver::ResolverError),
#[error("{0}")]
Connection(#[from] ConnectionError),
#[error("{0}")]
ReadPacket(#[from] Box<azalea_protocol::read::ReadPacketError>),
#[error("{0}")]
WritePacket(#[from] io::Error),
#[error("The given address could not be parsed into a ServerAddress")]
InvalidAddress,
}
pub async fn ping_server(
address: impl TryInto<ServerAddress>,
) -> Result<ClientboundStatusResponsePacket, PingError> {
let address: ServerAddress = address.try_into().map_err(|_| PingError::InvalidAddress)?;
let resolved_address = resolver::resolve_address(&address).await?;
let conn = Connection::new(&resolved_address).await?;
ping_server_with_connection(address, conn).await
}
pub async fn ping_server_with_proxy(
address: impl TryInto<ServerAddress>,
proxy: Proxy,
) -> Result<ClientboundStatusResponsePacket, PingError> {
let address: ServerAddress = address.try_into().map_err(|_| PingError::InvalidAddress)?;
let resolved_address = resolver::resolve_address(&address).await?;
let conn = Connection::new_with_proxy(&resolved_address, proxy).await?;
ping_server_with_connection(address, conn).await
}
pub async fn ping_server_with_connection(
address: ServerAddress,
mut conn: Connection<ClientboundHandshakePacket, ServerboundHandshakePacket>,
) -> Result<ClientboundStatusResponsePacket, PingError> {
conn.write(
ClientIntentionPacket {
protocol_version: PROTOCOL_VERSION,
hostname: address.host.clone(),
port: address.port,
intention: ClientIntention::Status,
}
.get(),
)
.await?;
let mut conn = conn.status();
conn.write(ServerboundStatusRequestPacket {}.get()).await?;
let packet = conn.read().await?;
loop {
match packet {
ClientboundStatusPacket::StatusResponse(p) => return Ok(p),
ClientboundStatusPacket::PongResponse(_) => {
}
}
}
}