aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-15 23:10:55 -0600
committermat <github@matdoes.dev>2021-12-15 23:10:55 -0600
commit9642558f8f8d983a7087f15d68be8cf07a85f0c2 (patch)
tree5f0a967f005cd5db510a13ab290c8ad6669b25aa /azalea-client/src
parent72aefe871ca4983431b1a0b707b472e73ffea836 (diff)
downloadazalea-drasl-9642558f8f8d983a7087f15d68be8cf07a85f0c2.tar.xz
azalea
Diffstat (limited to 'azalea-client/src')
-rw-r--r--azalea-client/src/connect.rs53
-rw-r--r--azalea-client/src/crypt.rs0
-rw-r--r--azalea-client/src/lib.rs13
-rw-r--r--azalea-client/src/ping.rs44
4 files changed, 110 insertions, 0 deletions
diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs
new file mode 100644
index 00000000..fad5fa92
--- /dev/null
+++ b/azalea-client/src/connect.rs
@@ -0,0 +1,53 @@
+///! Connect to Minecraft servers.
+use azalea_protocol::{
+ connect::HandshakeConnection,
+ packets::{
+ handshake::client_intention_packet::ClientIntentionPacket,
+ login::{serverbound_hello_packet::ServerboundHelloPacket, LoginPacket},
+ ConnectionProtocol, PROTOCOL_VERSION,
+ },
+ resolver, ServerAddress,
+};
+
+pub async fn join_server(address: &ServerAddress) -> Result<(), String> {
+ let username = "bot".to_string();
+
+ let resolved_address = resolver::resolve_address(address).await?;
+
+ let mut conn = HandshakeConnection::new(&resolved_address).await?;
+
+ // handshake
+ conn.write(
+ ClientIntentionPacket {
+ protocol_version: PROTOCOL_VERSION,
+ hostname: address.host.clone(),
+ port: address.port,
+ intention: ConnectionProtocol::Login,
+ }
+ .get(),
+ )
+ .await;
+ let mut conn = conn.login();
+
+ // login start
+ conn.write(ServerboundHelloPacket { username }.get()).await;
+
+ // encryption request
+ loop {
+ match conn.read().await.unwrap() {
+ LoginPacket::ClientboundHelloPacket(encryption_request_packet) => {
+ println!(
+ "Got encryption request {:?} {:?}",
+ encryption_request_packet.nonce, encryption_request_packet.public_key
+ );
+ }
+ _ => (),
+ }
+ }
+
+ // TODO: client auth
+
+ // TODO: encryption response
+
+ Ok(())
+}
diff --git a/azalea-client/src/crypt.rs b/azalea-client/src/crypt.rs
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/azalea-client/src/crypt.rs
diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs
new file mode 100644
index 00000000..8c1bcfe9
--- /dev/null
+++ b/azalea-client/src/lib.rs
@@ -0,0 +1,13 @@
+//! Significantly abstract azalea-protocol so it's actually useable for bots.
+
+pub mod connect;
+pub mod ping;
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn it_works() {
+ let result = 2 + 2;
+ assert_eq!(result, 4);
+ }
+}
diff --git a/azalea-client/src/ping.rs b/azalea-client/src/ping.rs
new file mode 100644
index 00000000..87ccdf66
--- /dev/null
+++ b/azalea-client/src/ping.rs
@@ -0,0 +1,44 @@
+///! Ping Minecraft servers.
+use azalea_protocol::{
+ connect::HandshakeConnection,
+ packets::{
+ handshake::client_intention_packet::ClientIntentionPacket,
+ status::{
+ clientbound_status_response_packet::ClientboundStatusResponsePacket,
+ serverbound_status_request_packet::ServerboundStatusRequestPacket, StatusPacket,
+ },
+ ConnectionProtocol, PROTOCOL_VERSION,
+ },
+ resolver, ServerAddress,
+};
+
+pub async fn ping_server(
+ address: &ServerAddress,
+) -> Result<ClientboundStatusResponsePacket, String> {
+ let resolved_address = resolver::resolve_address(address).await?;
+
+ let mut conn = HandshakeConnection::new(&resolved_address).await?;
+
+ // send the client intention packet and switch to the status state
+ conn.write(
+ ClientIntentionPacket {
+ protocol_version: PROTOCOL_VERSION,
+ hostname: address.host.clone(),
+ port: address.port,
+ intention: ConnectionProtocol::Status,
+ }
+ .get(),
+ )
+ .await;
+ let mut conn = conn.status();
+
+ // send the empty status request packet
+ conn.write(ServerboundStatusRequestPacket {}.get()).await;
+
+ let packet = conn.read().await.unwrap();
+
+ match packet {
+ StatusPacket::ClientboundStatusResponsePacket(p) => Ok(*p),
+ _ => Err("Invalid packet type".to_string()),
+ }
+}