azalea_protocol/
connect.rs

1//! Connect to remote servers/clients.
2
3use std::fmt::Debug;
4use std::io::{self, 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::TcpStream;
14use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf, ReuniteError};
15use tracing::{error, info};
16use uuid::Uuid;
17
18use crate::packets::ProtocolPacket;
19use crate::packets::config::{ClientboundConfigPacket, ServerboundConfigPacket};
20use crate::packets::game::{ClientboundGamePacket, ServerboundGamePacket};
21use crate::packets::handshake::{ClientboundHandshakePacket, ServerboundHandshakePacket};
22use crate::packets::login::c_hello::ClientboundHello;
23use crate::packets::login::{ClientboundLoginPacket, ServerboundLoginPacket};
24use crate::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket};
25use crate::read::{ReadPacketError, deserialize_packet, read_raw_packet, try_read_raw_packet};
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]) -> 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() == 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) -> 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) -> io::Result<()> {
210        self.raw.write(&serialize_packet(&packet).unwrap()).await
211    }
212
213    /// End the connection.
214    pub async fn shutdown(&mut self) -> 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>) -> 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    /// Split the reader and writer into the state-agnostic
248    /// [`RawReadConnection`] and [`RawWriteConnection`] types.
249    ///
250    /// This is meant to help with some types of proxies.
251    #[must_use]
252    pub fn into_split_raw(self) -> (RawReadConnection, RawWriteConnection) {
253        (self.reader.raw, self.writer.raw)
254    }
255}
256
257#[derive(Error, Debug)]
258pub enum ConnectionError {
259    #[error("{0}")]
260    Io(#[from] io::Error),
261}
262
263use socks5_impl::protocol::UserKey;
264
265#[derive(Debug, Clone)]
266pub struct Proxy {
267    pub addr: SocketAddr,
268    pub auth: Option<UserKey>,
269}
270
271impl Proxy {
272    pub fn new(addr: SocketAddr, auth: Option<UserKey>) -> Self {
273        Self { addr, auth }
274    }
275}
276
277impl Connection<ClientboundHandshakePacket, ServerboundHandshakePacket> {
278    /// Create a new connection to the given address.
279    pub async fn new(address: &SocketAddr) -> Result<Self, ConnectionError> {
280        let stream = TcpStream::connect(address).await?;
281
282        // enable tcp_nodelay
283        stream.set_nodelay(true)?;
284
285        Self::new_from_stream(stream).await
286    }
287
288    /// Create a new connection to the given address and Socks5 proxy. If you're
289    /// not using a proxy, use [`Self::new`] instead.
290    pub async fn new_with_proxy(
291        address: &SocketAddr,
292        proxy: Proxy,
293    ) -> Result<Self, ConnectionError> {
294        let proxy_stream = TcpStream::connect(proxy.addr).await?;
295        let mut stream = BufStream::new(proxy_stream);
296
297        let _ = socks5_impl::client::connect(&mut stream, address, proxy.auth)
298            .await
299            .map_err(io::Error::other)?;
300
301        Self::new_from_stream(stream.into_inner()).await
302    }
303
304    /// Create a new connection from an existing stream. Useful if you want to
305    /// set custom options on the stream. Otherwise, just use [`Self::new`].
306    pub async fn new_from_stream(stream: TcpStream) -> Result<Self, ConnectionError> {
307        let (read_stream, write_stream) = stream.into_split();
308
309        Ok(Connection {
310            reader: ReadConnection {
311                raw: RawReadConnection {
312                    read_stream,
313                    buffer: Cursor::new(Vec::new()),
314                    compression_threshold: None,
315                    dec_cipher: None,
316                },
317                _reading: PhantomData,
318            },
319            writer: WriteConnection {
320                raw: RawWriteConnection {
321                    write_stream,
322                    compression_threshold: None,
323                    enc_cipher: None,
324                },
325                _writing: PhantomData,
326            },
327        })
328    }
329
330    /// Change our state from handshake to login. This is the state that is used
331    /// for logging in.
332    #[must_use]
333    pub fn login(self) -> Connection<ClientboundLoginPacket, ServerboundLoginPacket> {
334        Connection::from(self)
335    }
336
337    /// Change our state from handshake to status. This is the state that is
338    /// used for pinging the server.
339    #[must_use]
340    pub fn status(self) -> Connection<ClientboundStatusPacket, ServerboundStatusPacket> {
341        Connection::from(self)
342    }
343}
344
345impl Connection<ClientboundLoginPacket, ServerboundLoginPacket> {
346    /// Set our compression threshold, i.e. the maximum size that a packet is
347    /// allowed to be without getting compressed. Setting it to 0 means every
348    /// packet will be compressed. If you set it to less than 0,
349    /// then compression is disabled.
350    pub fn set_compression_threshold(&mut self, threshold: i32) {
351        // if you pass a threshold of less than 0, compression is disabled
352        if threshold >= 0 {
353            self.reader.raw.compression_threshold = Some(threshold as u32);
354            self.writer.raw.compression_threshold = Some(threshold as u32);
355        } else {
356            self.reader.raw.compression_threshold = None;
357            self.writer.raw.compression_threshold = None;
358        }
359    }
360
361    /// Set the encryption key that is used to encrypt and decrypt packets. It's
362    /// the same for both reading and writing.
363    pub fn set_encryption_key(&mut self, key: [u8; 16]) {
364        let (enc_cipher, dec_cipher) = azalea_crypto::create_cipher(&key);
365        self.reader.raw.dec_cipher = Some(dec_cipher);
366        self.writer.raw.enc_cipher = Some(enc_cipher);
367    }
368
369    /// Change our state from login to configuration. This is the state where
370    /// the server sends us the registries and resource pack and stuff.
371    #[must_use]
372    pub fn config(self) -> Connection<ClientboundConfigPacket, ServerboundConfigPacket> {
373        Connection::from(self)
374    }
375
376    /// Authenticate with Minecraft's servers, which is required to join
377    /// online-mode servers. This must happen when you get a
378    /// `ClientboundLoginPacket::Hello` packet.
379    ///
380    /// # Examples
381    ///
382    /// ```rust,no_run
383    /// use azalea_auth::AuthResult;
384    /// use azalea_protocol::connect::Connection;
385    /// use azalea_protocol::packets::login::{
386    ///     ClientboundLoginPacket,
387    ///     ServerboundKey
388    /// };
389    /// use uuid::Uuid;
390    /// # use azalea_protocol::ServerAddress;
391    ///
392    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
393    /// let AuthResult { access_token, profile } = azalea_auth::auth(
394    ///     "[email protected]",
395    ///     azalea_auth::AuthOpts::default()
396    /// ).await.expect("Couldn't authenticate");
397    /// #
398    /// # let address = ServerAddress::try_from("[email protected]").unwrap();
399    /// # let resolved_address = azalea_protocol::resolver::resolve_address(&address).await?;
400    ///
401    /// let mut conn = Connection::new(&resolved_address).await?;
402    ///
403    /// // transition to the login state, in a real program we would have done a handshake first
404    /// let mut conn = conn.login();
405    ///
406    /// match conn.read().await? {
407    ///     ClientboundLoginPacket::Hello(p) => {
408    ///         // tell Mojang we're joining the server & enable encryption
409    ///         let e = azalea_crypto::encrypt(&p.public_key, &p.challenge).unwrap();
410    ///         conn.authenticate(
411    ///             &access_token,
412    ///             &profile.id,
413    ///             e.secret_key,
414    ///             &p
415    ///         ).await?;
416    ///         conn.write(ServerboundKey {
417    ///             key_bytes: e.encrypted_public_key,
418    ///             encrypted_challenge: e.encrypted_challenge,
419    ///         }).await?;
420    ///         conn.set_encryption_key(e.secret_key);
421    ///     }
422    ///     _ => {}
423    /// }
424    /// # Ok(())
425    /// # }
426    /// ```
427    pub async fn authenticate(
428        &self,
429        access_token: &str,
430        uuid: &Uuid,
431        private_key: [u8; 16],
432        packet: &ClientboundHello,
433    ) -> Result<(), ClientSessionServerError> {
434        azalea_auth::sessionserver::join(
435            access_token,
436            &packet.public_key,
437            &private_key,
438            uuid,
439            &packet.server_id,
440        )
441        .await
442    }
443}
444
445impl Connection<ServerboundHandshakePacket, ClientboundHandshakePacket> {
446    /// Change our state from handshake to login. This is the state that is used
447    /// for logging in.
448    #[must_use]
449    pub fn login(self) -> Connection<ServerboundLoginPacket, ClientboundLoginPacket> {
450        Connection::from(self)
451    }
452
453    /// Change our state from handshake to status. This is the state that is
454    /// used for pinging the server.
455    #[must_use]
456    pub fn status(self) -> Connection<ServerboundStatusPacket, ClientboundStatusPacket> {
457        Connection::from(self)
458    }
459}
460
461impl Connection<ServerboundLoginPacket, ClientboundLoginPacket> {
462    /// Set our compression threshold, i.e. the maximum size that a packet is
463    /// allowed to be without getting compressed. If you set it to less than 0
464    /// then compression gets disabled.
465    pub fn set_compression_threshold(&mut self, threshold: i32) {
466        // if you pass a threshold of less than 0, compression is disabled
467        if threshold >= 0 {
468            self.reader.raw.compression_threshold = Some(threshold as u32);
469            self.writer.raw.compression_threshold = Some(threshold as u32);
470        } else {
471            self.reader.raw.compression_threshold = None;
472            self.writer.raw.compression_threshold = None;
473        }
474    }
475
476    /// Set the encryption key that is used to encrypt and decrypt packets. It's
477    /// the same for both reading and writing.
478    pub fn set_encryption_key(&mut self, key: [u8; 16]) {
479        let (enc_cipher, dec_cipher) = azalea_crypto::create_cipher(&key);
480        self.reader.raw.dec_cipher = Some(dec_cipher);
481        self.writer.raw.enc_cipher = Some(enc_cipher);
482    }
483
484    /// Change our state from login to game. This is the state that's used when
485    /// the client is actually in the game.
486    #[must_use]
487    pub fn game(self) -> Connection<ServerboundGamePacket, ClientboundGamePacket> {
488        Connection::from(self)
489    }
490
491    /// Verify connecting clients have authenticated with Minecraft's servers.
492    /// This must happen after the client sends a `ServerboundLoginPacket::Key`
493    /// packet.
494    pub async fn authenticate(
495        &self,
496        username: &str,
497        public_key: &[u8],
498        private_key: &[u8; 16],
499        ip: Option<&str>,
500    ) -> Result<GameProfile, ServerSessionServerError> {
501        azalea_auth::sessionserver::serverside_auth(username, public_key, private_key, ip).await
502    }
503
504    /// Change our state back to configuration.
505    #[must_use]
506    pub fn config(self) -> Connection<ServerboundConfigPacket, ClientboundConfigPacket> {
507        Connection::from(self)
508    }
509}
510
511impl Connection<ServerboundConfigPacket, ClientboundConfigPacket> {
512    /// Change our state from configuration to game. This is the state that's
513    /// used when the client is actually in the world.
514    #[must_use]
515    pub fn game(self) -> Connection<ServerboundGamePacket, ClientboundGamePacket> {
516        Connection::from(self)
517    }
518}
519
520impl Connection<ClientboundConfigPacket, ServerboundConfigPacket> {
521    /// Change our state from configuration to game. This is the state that's
522    /// used when the client is actually in the world.
523    #[must_use]
524    pub fn game(self) -> Connection<ClientboundGamePacket, ServerboundGamePacket> {
525        Connection::from(self)
526    }
527}
528
529impl Connection<ClientboundGamePacket, ServerboundGamePacket> {
530    /// Change our state back to configuration.
531    #[must_use]
532    pub fn config(self) -> Connection<ClientboundConfigPacket, ServerboundConfigPacket> {
533        Connection::from(self)
534    }
535}
536impl Connection<ServerboundGamePacket, ClientboundGamePacket> {
537    /// Change our state back to configuration.
538    #[must_use]
539    pub fn config(self) -> Connection<ServerboundConfigPacket, ClientboundConfigPacket> {
540        Connection::from(self)
541    }
542}
543
544// rust doesn't let us implement From because allegedly it conflicts with
545// `core`'s "impl<T> From<T> for T" so we do this instead
546impl<R1, W1> Connection<R1, W1>
547where
548    R1: ProtocolPacket + Debug,
549    W1: ProtocolPacket + Debug,
550{
551    /// Creates a `Connection` of a type from a `Connection` of another type.
552    /// Useful for servers or custom packets.
553    #[must_use]
554    pub fn from<R2, W2>(connection: Connection<R1, W1>) -> Connection<R2, W2>
555    where
556        R2: ProtocolPacket + Debug,
557        W2: ProtocolPacket + Debug,
558    {
559        Connection {
560            reader: ReadConnection {
561                raw: connection.reader.raw,
562                _reading: PhantomData,
563            },
564            writer: WriteConnection {
565                raw: connection.writer.raw,
566                _writing: PhantomData,
567            },
568        }
569    }
570
571    /// Convert an existing `TcpStream` into a `Connection`. Useful for servers.
572    pub fn wrap(stream: TcpStream) -> Connection<R1, W1> {
573        let (read_stream, write_stream) = stream.into_split();
574
575        Connection {
576            reader: ReadConnection {
577                raw: RawReadConnection {
578                    read_stream,
579                    buffer: Cursor::new(Vec::new()),
580                    compression_threshold: None,
581                    dec_cipher: None,
582                },
583                _reading: PhantomData,
584            },
585            writer: WriteConnection {
586                raw: RawWriteConnection {
587                    write_stream,
588                    compression_threshold: None,
589                    enc_cipher: None,
590                },
591                _writing: PhantomData,
592            },
593        }
594    }
595
596    /// Convert from a `Connection` into a `TcpStream`. Useful for servers.
597    pub fn unwrap(self) -> Result<TcpStream, ReuniteError> {
598        self.reader
599            .raw
600            .read_stream
601            .reunite(self.writer.raw.write_stream)
602    }
603}