azalea_protocol/
connect.rs

1//! Connect to remote servers/clients.
2
3use std::fmt::Debug;
4use std::io::Cursor;
5use std::marker::PhantomData;
6use std::net::SocketAddr;
7
8use azalea_auth::game_profile::GameProfile;
9use azalea_auth::sessionserver::{ClientSessionServerError, ServerSessionServerError};
10use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc};
11use thiserror::Error;
12use tokio::io::{AsyncWriteExt, BufStream};
13use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf, ReuniteError};
14use tokio::net::TcpStream;
15use tracing::{error, info};
16use uuid::Uuid;
17
18use crate::packets::config::{ClientboundConfigPacket, ServerboundConfigPacket};
19use crate::packets::game::{ClientboundGamePacket, ServerboundGamePacket};
20use crate::packets::handshake::{ClientboundHandshakePacket, ServerboundHandshakePacket};
21use crate::packets::login::c_hello::ClientboundHello;
22use crate::packets::login::{ClientboundLoginPacket, ServerboundLoginPacket};
23use crate::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket};
24use crate::packets::ProtocolPacket;
25use crate::read::{deserialize_packet, read_raw_packet, try_read_raw_packet, ReadPacketError};
26use crate::write::{serialize_packet, write_raw_packet};
27
28pub struct RawReadConnection {
29    pub read_stream: OwnedReadHalf,
30    pub buffer: Cursor<Vec<u8>>,
31    pub compression_threshold: Option<u32>,
32    pub dec_cipher: Option<Aes128CfbDec>,
33}
34
35pub struct RawWriteConnection {
36    pub write_stream: OwnedWriteHalf,
37    pub compression_threshold: Option<u32>,
38    pub enc_cipher: Option<Aes128CfbEnc>,
39}
40
41/// The read half of a connection.
42pub struct ReadConnection<R: ProtocolPacket> {
43    pub raw: RawReadConnection,
44    _reading: PhantomData<R>,
45}
46
47/// The write half of a connection.
48pub struct WriteConnection<W: ProtocolPacket> {
49    pub raw: RawWriteConnection,
50    _writing: PhantomData<W>,
51}
52
53/// A connection that can read and write packets.
54///
55/// # Examples
56///
57/// Join an offline-mode server and go through the handshake.
58/// ```rust,no_run
59/// use azalea_protocol::{
60///     resolver,
61///     connect::Connection,
62///     packets::{
63///         self,
64///         ClientIntention, PROTOCOL_VERSION,
65///         login::{
66///             ClientboundLoginPacket,
67///             ServerboundHello,
68///             ServerboundKey
69///         },
70///         handshake::ServerboundIntention
71///     }
72/// };
73///
74/// #[tokio::main]
75/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
76///     let resolved_address = resolver::resolve_address(&"localhost".try_into().unwrap()).await?;
77///     let mut conn = Connection::new(&resolved_address).await?;
78///
79///     // handshake
80///     conn.write(ServerboundIntention {
81///         protocol_version: PROTOCOL_VERSION,
82///         hostname: resolved_address.ip().to_string(),
83///         port: resolved_address.port(),
84///         intention: ClientIntention::Login,
85///     }).await?;
86///
87///     let mut conn = conn.login();
88///
89///     // login
90///     conn.write(ServerboundHello {
91///         name: "bot".to_string(),
92///         profile_id: uuid::Uuid::nil(),
93///     }).await?;
94///
95///     let (conn, game_profile) = loop {
96///         let packet = conn.read().await?;
97///         match packet {
98///             ClientboundLoginPacket::Hello(p) => {
99///                 let e = azalea_crypto::encrypt(&p.public_key, &p.challenge).unwrap();
100///
101///                 conn.write(ServerboundKey {
102///                     key_bytes: e.encrypted_public_key,
103///                     encrypted_challenge: e.encrypted_challenge,
104///                 }).await?;
105///                 conn.set_encryption_key(e.secret_key);
106///             }
107///             ClientboundLoginPacket::LoginCompression(p) => {
108///                 conn.set_compression_threshold(p.compression_threshold);
109///             }
110///             ClientboundLoginPacket::LoginFinished(p) => {
111///                 break (conn.config(), p.game_profile);
112///             }
113///             ClientboundLoginPacket::LoginDisconnect(p) => {
114///                 eprintln!("login disconnect: {}", p.reason);
115///                 return Err("login disconnect".into());
116///             }
117///             ClientboundLoginPacket::CustomQuery(p) => {}
118///             ClientboundLoginPacket::CookieRequest(p) => {
119///                 conn.write(packets::login::ServerboundCookieResponse {
120///                     key: p.key,
121///                     payload: None,
122///                 })
123///                 .await?;
124///             }
125///         }
126///     };
127///
128///     Ok(())
129/// }
130/// ```
131pub struct Connection<R: ProtocolPacket, W: ProtocolPacket> {
132    pub reader: ReadConnection<R>,
133    pub writer: WriteConnection<W>,
134}
135
136impl RawReadConnection {
137    pub async fn read(&mut self) -> Result<Box<[u8]>, Box<ReadPacketError>> {
138        read_raw_packet::<_>(
139            &mut self.read_stream,
140            &mut self.buffer,
141            self.compression_threshold,
142            &mut self.dec_cipher,
143        )
144        .await
145    }
146
147    pub fn try_read(&mut self) -> Result<Option<Box<[u8]>>, Box<ReadPacketError>> {
148        try_read_raw_packet::<_>(
149            &mut self.read_stream,
150            &mut self.buffer,
151            self.compression_threshold,
152            &mut self.dec_cipher,
153        )
154    }
155}
156
157impl RawWriteConnection {
158    pub async fn write(&mut self, packet: &[u8]) -> std::io::Result<()> {
159        if let Err(e) = write_raw_packet(
160            packet,
161            &mut self.write_stream,
162            self.compression_threshold,
163            &mut self.enc_cipher,
164        )
165        .await
166        {
167            // detect broken pipe
168            if e.kind() == std::io::ErrorKind::BrokenPipe {
169                info!("Broken pipe, shutting down connection.");
170                if let Err(e) = self.shutdown().await {
171                    error!("Couldn't shut down: {}", e);
172                }
173            }
174            return Err(e);
175        }
176        Ok(())
177    }
178
179    /// End the connection.
180    pub async fn shutdown(&mut self) -> std::io::Result<()> {
181        self.write_stream.shutdown().await
182    }
183}
184
185impl<R> ReadConnection<R>
186where
187    R: ProtocolPacket + Debug,
188{
189    /// Read a packet from the stream.
190    pub async fn read(&mut self) -> Result<R, Box<ReadPacketError>> {
191        let raw_packet = self.raw.read().await?;
192        deserialize_packet(&mut Cursor::new(&raw_packet))
193    }
194
195    /// Try to read a packet from the stream, or return Ok(None) if there's no
196    /// packet.
197    pub fn try_read(&mut self) -> Result<Option<R>, Box<ReadPacketError>> {
198        let Some(raw_packet) = self.raw.try_read()? else {
199            return Ok(None);
200        };
201        Ok(Some(deserialize_packet(&mut Cursor::new(&raw_packet))?))
202    }
203}
204impl<W> WriteConnection<W>
205where
206    W: ProtocolPacket + Debug,
207{
208    /// Write a packet to the server.
209    pub async fn write(&mut self, packet: W) -> std::io::Result<()> {
210        self.raw.write(&serialize_packet(&packet).unwrap()).await
211    }
212
213    /// End the connection.
214    pub async fn shutdown(&mut self) -> std::io::Result<()> {
215        self.raw.shutdown().await
216    }
217}
218
219impl<R, W> Connection<R, W>
220where
221    R: ProtocolPacket + Debug,
222    W: ProtocolPacket + Debug,
223{
224    /// Read a packet from the other side of the connection.
225    pub async fn read(&mut self) -> Result<R, Box<ReadPacketError>> {
226        self.reader.read().await
227    }
228
229    /// Try to read a packet from the other side of the connection, or return
230    /// Ok(None) if there's no packet to read.
231    pub fn try_read(&mut self) -> Result<Option<R>, Box<ReadPacketError>> {
232        self.reader.try_read()
233    }
234
235    /// Write a packet to the other side of the connection.
236    pub async fn write(&mut self, packet: impl crate::packets::Packet<W>) -> std::io::Result<()> {
237        let packet = packet.into_variant();
238        self.writer.write(packet).await
239    }
240
241    /// Split the reader and writer into two objects. This doesn't allocate.
242    #[must_use]
243    pub fn into_split(self) -> (ReadConnection<R>, WriteConnection<W>) {
244        (self.reader, self.writer)
245    }
246}
247
248#[derive(Error, Debug)]
249pub enum ConnectionError {
250    #[error("{0}")]
251    Io(#[from] std::io::Error),
252}
253
254use socks5_impl::protocol::UserKey;
255
256#[derive(Debug, Clone)]
257pub struct Proxy {
258    pub addr: SocketAddr,
259    pub auth: Option<UserKey>,
260}
261
262impl Proxy {
263    pub fn new(addr: SocketAddr, auth: Option<UserKey>) -> Self {
264        Self { addr, auth }
265    }
266}
267
268impl Connection<ClientboundHandshakePacket, ServerboundHandshakePacket> {
269    /// Create a new connection to the given address.
270    pub async fn new(address: &SocketAddr) -> Result<Self, ConnectionError> {
271        let stream = TcpStream::connect(address).await?;
272
273        // enable tcp_nodelay
274        stream.set_nodelay(true)?;
275
276        Self::new_from_stream(stream).await
277    }
278
279    /// Create a new connection to the given address and Socks5 proxy. If you're
280    /// not using a proxy, use [`Self::new`] instead.
281    pub async fn new_with_proxy(
282        address: &SocketAddr,
283        proxy: Proxy,
284    ) -> Result<Self, ConnectionError> {
285        let proxy_stream = TcpStream::connect(proxy.addr).await?;
286        let mut stream = BufStream::new(proxy_stream);
287
288        let _ = socks5_impl::client::connect(&mut stream, address, proxy.auth)
289            .await
290            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
291
292        Self::new_from_stream(stream.into_inner()).await
293    }
294
295    /// Create a new connection from an existing stream. Useful if you want to
296    /// set custom options on the stream. Otherwise, just use [`Self::new`].
297    pub async fn new_from_stream(stream: TcpStream) -> Result<Self, ConnectionError> {
298        let (read_stream, write_stream) = stream.into_split();
299
300        Ok(Connection {
301            reader: ReadConnection {
302                raw: RawReadConnection {
303                    read_stream,
304                    buffer: Cursor::new(Vec::new()),
305                    compression_threshold: None,
306                    dec_cipher: None,
307                },
308                _reading: PhantomData,
309            },
310            writer: WriteConnection {
311                raw: RawWriteConnection {
312                    write_stream,
313                    compression_threshold: None,
314                    enc_cipher: None,
315                },
316                _writing: PhantomData,
317            },
318        })
319    }
320
321    /// Change our state from handshake to login. This is the state that is used
322    /// for logging in.
323    #[must_use]
324    pub fn login(self) -> Connection<ClientboundLoginPacket, ServerboundLoginPacket> {
325        Connection::from(self)
326    }
327
328    /// Change our state from handshake to status. This is the state that is
329    /// used for pinging the server.
330    #[must_use]
331    pub fn status(self) -> Connection<ClientboundStatusPacket, ServerboundStatusPacket> {
332        Connection::from(self)
333    }
334}
335
336impl Connection<ClientboundLoginPacket, ServerboundLoginPacket> {
337    /// Set our compression threshold, i.e. the maximum size that a packet is
338    /// allowed to be without getting compressed. If you set it to less than 0
339    /// then compression gets disabled.
340    pub fn set_compression_threshold(&mut self, threshold: i32) {
341        // if you pass a threshold of less than 0, compression is disabled
342        if threshold >= 0 {
343            self.reader.raw.compression_threshold = Some(threshold as u32);
344            self.writer.raw.compression_threshold = Some(threshold as u32);
345        } else {
346            self.reader.raw.compression_threshold = None;
347            self.writer.raw.compression_threshold = None;
348        }
349    }
350
351    /// Set the encryption key that is used to encrypt and decrypt packets. It's
352    /// the same for both reading and writing.
353    pub fn set_encryption_key(&mut self, key: [u8; 16]) {
354        let (enc_cipher, dec_cipher) = azalea_crypto::create_cipher(&key);
355        self.reader.raw.dec_cipher = Some(dec_cipher);
356        self.writer.raw.enc_cipher = Some(enc_cipher);
357    }
358
359    /// Change our state from login to configuration. This is the state where
360    /// the server sends us the registries and resource pack and stuff.
361    #[must_use]
362    pub fn config(self) -> Connection<ClientboundConfigPacket, ServerboundConfigPacket> {
363        Connection::from(self)
364    }
365
366    /// Authenticate with Minecraft's servers, which is required to join
367    /// online-mode servers. This must happen when you get a
368    /// `ClientboundLoginPacket::Hello` packet.
369    ///
370    /// # Examples
371    ///
372    /// ```rust,no_run
373    /// use azalea_auth::AuthResult;
374    /// use azalea_protocol::connect::Connection;
375    /// use azalea_protocol::packets::login::{
376    ///     ClientboundLoginPacket,
377    ///     ServerboundKey
378    /// };
379    /// use uuid::Uuid;
380    /// # use azalea_protocol::ServerAddress;
381    ///
382    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
383    /// let AuthResult { access_token, profile } = azalea_auth::auth(
384    ///     "[email protected]",
385    ///     azalea_auth::AuthOpts::default()
386    /// ).await.expect("Couldn't authenticate");
387    /// #
388    /// # let address = ServerAddress::try_from("[email protected]").unwrap();
389    /// # let resolved_address = azalea_protocol::resolver::resolve_address(&address).await?;
390    ///
391    /// let mut conn = Connection::new(&resolved_address).await?;
392    ///
393    /// // transition to the login state, in a real program we would have done a handshake first
394    /// let mut conn = conn.login();
395    ///
396    /// match conn.read().await? {
397    ///     ClientboundLoginPacket::Hello(p) => {
398    ///         // tell Mojang we're joining the server & enable encryption
399    ///         let e = azalea_crypto::encrypt(&p.public_key, &p.challenge).unwrap();
400    ///         conn.authenticate(
401    ///             &access_token,
402    ///             &profile.id,
403    ///             e.secret_key,
404    ///             &p
405    ///         ).await?;
406    ///         conn.write(ServerboundKey {
407    ///             key_bytes: e.encrypted_public_key,
408    ///             encrypted_challenge: e.encrypted_challenge,
409    ///         }).await?;
410    ///         conn.set_encryption_key(e.secret_key);
411    ///     }
412    ///     _ => {}
413    /// }
414    /// # Ok(())
415    /// # }
416    /// ```
417    pub async fn authenticate(
418        &self,
419        access_token: &str,
420        uuid: &Uuid,
421        private_key: [u8; 16],
422        packet: &ClientboundHello,
423    ) -> Result<(), ClientSessionServerError> {
424        azalea_auth::sessionserver::join(
425            access_token,
426            &packet.public_key,
427            &private_key,
428            uuid,
429            &packet.server_id,
430        )
431        .await
432    }
433}
434
435impl Connection<ServerboundHandshakePacket, ClientboundHandshakePacket> {
436    /// Change our state from handshake to login. This is the state that is used
437    /// for logging in.
438    #[must_use]
439    pub fn login(self) -> Connection<ServerboundLoginPacket, ClientboundLoginPacket> {
440        Connection::from(self)
441    }
442
443    /// Change our state from handshake to status. This is the state that is
444    /// used for pinging the server.
445    #[must_use]
446    pub fn status(self) -> Connection<ServerboundStatusPacket, ClientboundStatusPacket> {
447        Connection::from(self)
448    }
449}
450
451impl Connection<ServerboundLoginPacket, ClientboundLoginPacket> {
452    /// Set our compression threshold, i.e. the maximum size that a packet is
453    /// allowed to be without getting compressed. If you set it to less than 0
454    /// then compression gets disabled.
455    pub fn set_compression_threshold(&mut self, threshold: i32) {
456        // if you pass a threshold of less than 0, compression is disabled
457        if threshold >= 0 {
458            self.reader.raw.compression_threshold = Some(threshold as u32);
459            self.writer.raw.compression_threshold = Some(threshold as u32);
460        } else {
461            self.reader.raw.compression_threshold = None;
462            self.writer.raw.compression_threshold = None;
463        }
464    }
465
466    /// Set the encryption key that is used to encrypt and decrypt packets. It's
467    /// the same for both reading and writing.
468    pub fn set_encryption_key(&mut self, key: [u8; 16]) {
469        let (enc_cipher, dec_cipher) = azalea_crypto::create_cipher(&key);
470        self.reader.raw.dec_cipher = Some(dec_cipher);
471        self.writer.raw.enc_cipher = Some(enc_cipher);
472    }
473
474    /// Change our state from login to game. This is the state that's used when
475    /// the client is actually in the game.
476    #[must_use]
477    pub fn game(self) -> Connection<ServerboundGamePacket, ClientboundGamePacket> {
478        Connection::from(self)
479    }
480
481    /// Verify connecting clients have authenticated with Minecraft's servers.
482    /// This must happen after the client sends a `ServerboundLoginPacket::Key`
483    /// packet.
484    pub async fn authenticate(
485        &self,
486        username: &str,
487        public_key: &[u8],
488        private_key: &[u8; 16],
489        ip: Option<&str>,
490    ) -> Result<GameProfile, ServerSessionServerError> {
491        azalea_auth::sessionserver::serverside_auth(username, public_key, private_key, ip).await
492    }
493
494    /// Change our state back to configuration.
495    #[must_use]
496    pub fn config(self) -> Connection<ServerboundConfigPacket, ClientboundConfigPacket> {
497        Connection::from(self)
498    }
499}
500
501impl Connection<ServerboundConfigPacket, ClientboundConfigPacket> {
502    /// Change our state from configuration to game. This is the state that's
503    /// used when the client is actually in the world.
504    #[must_use]
505    pub fn game(self) -> Connection<ServerboundGamePacket, ClientboundGamePacket> {
506        Connection::from(self)
507    }
508}
509
510impl Connection<ClientboundConfigPacket, ServerboundConfigPacket> {
511    /// Change our state from configuration to game. This is the state that's
512    /// used when the client is actually in the world.
513    #[must_use]
514    pub fn game(self) -> Connection<ClientboundGamePacket, ServerboundGamePacket> {
515        Connection::from(self)
516    }
517}
518
519impl Connection<ClientboundGamePacket, ServerboundGamePacket> {
520    /// Change our state back to configuration.
521    #[must_use]
522    pub fn config(self) -> Connection<ClientboundConfigPacket, ServerboundConfigPacket> {
523        Connection::from(self)
524    }
525}
526impl Connection<ServerboundGamePacket, ClientboundGamePacket> {
527    /// Change our state back to configuration.
528    #[must_use]
529    pub fn config(self) -> Connection<ServerboundConfigPacket, ClientboundConfigPacket> {
530        Connection::from(self)
531    }
532}
533
534// rust doesn't let us implement From because allegedly it conflicts with
535// `core`'s "impl<T> From<T> for T" so we do this instead
536impl<R1, W1> Connection<R1, W1>
537where
538    R1: ProtocolPacket + Debug,
539    W1: ProtocolPacket + Debug,
540{
541    /// Creates a `Connection` of a type from a `Connection` of another type.
542    /// Useful for servers or custom packets.
543    #[must_use]
544    pub fn from<R2, W2>(connection: Connection<R1, W1>) -> Connection<R2, W2>
545    where
546        R2: ProtocolPacket + Debug,
547        W2: ProtocolPacket + Debug,
548    {
549        Connection {
550            reader: ReadConnection {
551                raw: connection.reader.raw,
552                _reading: PhantomData,
553            },
554            writer: WriteConnection {
555                raw: connection.writer.raw,
556                _writing: PhantomData,
557            },
558        }
559    }
560
561    /// Convert an existing `TcpStream` into a `Connection`. Useful for servers.
562    pub fn wrap(stream: TcpStream) -> Connection<R1, W1> {
563        let (read_stream, write_stream) = stream.into_split();
564
565        Connection {
566            reader: ReadConnection {
567                raw: RawReadConnection {
568                    read_stream,
569                    buffer: Cursor::new(Vec::new()),
570                    compression_threshold: None,
571                    dec_cipher: None,
572                },
573                _reading: PhantomData,
574            },
575            writer: WriteConnection {
576                raw: RawWriteConnection {
577                    write_stream,
578                    compression_threshold: None,
579                    enc_cipher: None,
580                },
581                _writing: PhantomData,
582            },
583        }
584    }
585
586    /// Convert from a `Connection` into a `TcpStream`. Useful for servers.
587    pub fn unwrap(self) -> Result<TcpStream, ReuniteError> {
588        self.reader
589            .raw
590            .read_stream
591            .reunite(self.writer.raw.write_stream)
592    }
593}