aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs88
1 files changed, 86 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index d79e98f..9a16629 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,8 +1,15 @@
pub mod config;
use anyhow::bail;
+use azalea_chat::text_component::legacy_color_code_to_text_component;
+use azalea_protocol::packets::handshake::client_intention_packet::ClientIntentionPacket;
use azalea_protocol::packets::handshake::ServerboundHandshakePacket;
use azalea_protocol::packets::login::ServerboundLoginPacket;
+use azalea_protocol::packets::status::clientbound_pong_response_packet::ClientboundPongResponsePacket;
+use azalea_protocol::packets::status::clientbound_status_response_packet::{
+ ClientboundStatusResponsePacket, Players, Version,
+};
+use azalea_protocol::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket};
use azalea_protocol::packets::ConnectionProtocol;
use azalea_protocol::read::read_packet;
use azalea_protocol::write::write_packet;
@@ -23,7 +30,7 @@ async fn main() {
let config =
Arc::new(serde_yaml::from_str::<Config>(&read_to_string("proxy.yaml").unwrap()).unwrap());
- let listener = TcpListener::bind("127.0.0.1:25565").await.unwrap();
+ let listener = TcpListener::bind(config.bind).await.unwrap();
info!("listening");
loop {
match listener.accept().await {
@@ -65,6 +72,7 @@ async fn handle_client(
);
match p.intention {
ConnectionProtocol::Status => {
+ handle_status_intent(config, downstream_writer, downstream_reader).await?;
bail!("we dont support ping yet")
}
ConnectionProtocol::Login => {}
@@ -102,7 +110,7 @@ async fn handle_client(
ServerboundLoginPacket::CustomQuery(_) => bail!("custom query not supported"),
};
- let upstream = TcpStream::connect("127.0.0.1:25567").await?;
+ let upstream = TcpStream::connect(config.backend).await?;
let (upstream_reader, mut upstream_writer) = upstream.into_split();
write_packet(
@@ -178,3 +186,79 @@ async fn connect(mut writer: OwnedWriteHalf, mut reader: OwnedReadHalf) -> anyho
writer.write_all(&buf[..size]).await?;
}
}
+
+async fn handle_status_intent(
+ config: Arc<Config>,
+ mut writer: OwnedWriteHalf,
+ mut reader: OwnedReadHalf,
+) -> anyhow::Result<()> {
+ let mut buf = BytesMut::new();
+ let upstream = TcpStream::connect(config.backend).await?;
+ let (upstream_reader, mut upstream_writer) = upstream.into_split();
+
+ write_packet(
+ &ServerboundHandshakePacket::ClientIntention(ClientIntentionPacket {
+ protocol_version: 760,
+ hostname: "127.0.0.1".to_string(),
+ port: 25567,
+ intention: ConnectionProtocol::Status,
+ }),
+ &mut upstream_writer,
+ None,
+ &mut None,
+ )
+ .await?;
+
+ tokio::spawn(async move {
+ connect(writer, upstream_reader).await.unwrap();
+ });
+
+ connect(upstream_writer, reader).await?;
+
+ return Ok(());
+
+ loop {
+ let req = read_packet::<ServerboundStatusPacket, _>(&mut reader, &mut buf, None, &mut None)
+ .await?;
+ info!("{req:?}");
+ match req {
+ ServerboundStatusPacket::StatusRequest(..) => {
+ write_packet(
+ &ClientboundStatusPacket::StatusResponse(ClientboundStatusResponsePacket {
+ description: azalea_chat::component::Component::Text(
+ legacy_color_code_to_text_component("blub"),
+ ),
+ favicon: None,
+ players: Players {
+ max: 10,
+ online: 0,
+ sample: vec![],
+ },
+ version: Version {
+ name: azalea_chat::component::Component::Text(
+ legacy_color_code_to_text_component("blub"),
+ ),
+ protocol: 760,
+ },
+ }),
+ &mut writer,
+ None,
+ &mut None,
+ )
+ .await?;
+ }
+ ServerboundStatusPacket::PingRequest(p) => {
+ write_packet(
+ &ClientboundStatusPacket::PongResponse(ClientboundPongResponsePacket {
+ time: p.time,
+ }),
+ &mut writer,
+ None,
+ &mut None,
+ )
+ .await?;
+ }
+ }
+ }
+ Ok(())
+}