diff options
| author | mat <github@matdoes.dev> | 2021-12-12 13:52:32 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-12 13:52:32 -0600 |
| commit | 2c3bf3b79e133acd01580144771a7cf238ecc4ee (patch) | |
| tree | ea134df743df2c022d88ea41269e2f70ce84b0b1 /minecraft-client/src | |
| parent | 6ed7b6e38fb8a3f4517c300807ce85ffb1952055 (diff) | |
| download | azalea-drasl-2c3bf3b79e133acd01580144771a7cf238ecc4ee.tar.xz | |
move ping over to minecraft-client
Diffstat (limited to 'minecraft-client/src')
| -rw-r--r-- | minecraft-client/src/lib.rs | 2 | ||||
| -rw-r--r-- | minecraft-client/src/ping.rs | 46 |
2 files changed, 47 insertions, 1 deletions
diff --git a/minecraft-client/src/lib.rs b/minecraft-client/src/lib.rs index 3491f6ad..1fc97709 100644 --- a/minecraft-client/src/lib.rs +++ b/minecraft-client/src/lib.rs @@ -1,4 +1,4 @@ - +pub mod ping; #[cfg(test)] mod tests { diff --git a/minecraft-client/src/ping.rs b/minecraft-client/src/ping.rs new file mode 100644 index 00000000..05aa84be --- /dev/null +++ b/minecraft-client/src/ping.rs @@ -0,0 +1,46 @@ +///! Ping Minecraft servers. + +use minecraft_protocol::{ + connection::Connection, + packets::{ + handshake::client_intention_packet::ClientIntentionPacket, + status::{ + clientbound_status_response_packet::ClientboundStatusResponsePacket, + serverbound_status_request_packet::ServerboundStatusRequestPacket, + }, + ConnectionProtocol, Packet, PacketTrait, + }, + resolver, ServerAddress, +}; + +pub async fn ping_server( + address: &ServerAddress, +) -> Result<ClientboundStatusResponsePacket, String> { + let resolved_address = resolver::resolve_address(address).await?; + + let mut conn = Connection::new(&resolved_address).await?; + + // send the client intention packet and switch to the status state + conn.send_packet( + ClientIntentionPacket { + protocol_version: 757, + hostname: address.host.clone(), + port: address.port, + intention: ConnectionProtocol::Status, + } + .get(), + ) + .await; + conn.switch_state(ConnectionProtocol::Status); + + // send the empty status request packet + conn.send_packet(ServerboundStatusRequestPacket {}.get()) + .await; + + let packet = conn.read_packet().await.unwrap(); + + Ok(match packet { + Packet::ClientboundStatusResponsePacket(p) => p, + _ => Err("Invalid packet type".to_string())?, + }) +} |
