aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs177
1 files changed, 96 insertions, 81 deletions
diff --git a/src/main.rs b/src/main.rs
index 9a16629..8111ebd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,31 +1,33 @@
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::{
+ handshake::{client_intention_packet::ClientIntentionPacket, ServerboundHandshakePacket},
+ login::ServerboundLoginPacket,
+ ConnectionProtocol,
+ },
+ read::read_packet,
+ write::write_packet,
};
-use azalea_protocol::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket};
-use azalea_protocol::packets::ConnectionProtocol;
-use azalea_protocol::read::read_packet;
-use azalea_protocol::write::write_packet;
use bytes::BytesMut;
use config::Config;
use log::{error, info, warn};
-use std::fs::read_to_string;
-use std::net::SocketAddr;
-use std::sync::Arc;
-use tokio::io::{AsyncReadExt, AsyncWriteExt};
-use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
-use tokio::net::{TcpListener, TcpStream};
+use std::{fs::read_to_string, sync::Arc};
+use tokio::{
+ io::{AsyncReadExt, AsyncWriteExt},
+ net::{
+ tcp::{OwnedReadHalf, OwnedWriteHalf},
+ TcpListener, TcpStream,
+ },
+};
#[tokio::main]
async fn main() {
- env_logger::init_from_env("LOG");
+ env_logger::builder()
+ .filter_level(log::LevelFilter::Info)
+ .parse_env("LOG")
+ .init();
let config =
Arc::new(serde_yaml::from_str::<Config>(&read_to_string("proxy.yaml").unwrap()).unwrap());
@@ -35,11 +37,26 @@ async fn main() {
loop {
match listener.accept().await {
Ok((sock, addr)) => {
+ info!("connected: {addr}");
let config = config.clone();
tokio::spawn(async move {
- match handle_client(config, sock, addr).await {
- Ok(()) => (),
- Err(e) => warn!("{e}"),
+ match handle_client(config, sock).await {
+ Ok(()) => info!("disconnected: {addr}"),
+ Err(err) => {
+ // write_packet::<ClientboundLoginPacket, _>(
+ // &ClientboundLoginPacket::LoginDisconnect(
+ // ClientboundLoginDisconnectPacket {
+ // reason: azalea_chat::component::Component::Text(
+ // legacy_color_code_to_text_component(&format!("{err}")),
+ // ),
+ // },
+ // ),
+ // &mut sock,
+ // None,
+ // &mut None,
+ // );
+ warn!("error: ({addr}) {err}")
+ }
}
});
}
@@ -48,13 +65,9 @@ async fn main() {
}
}
-async fn handle_client(
- config: Arc<Config>,
- sock: TcpStream,
- addr: SocketAddr,
-) -> anyhow::Result<()> {
- info!("connection {addr}");
+async fn handle_client(config: Arc<Config>, sock: TcpStream) -> Result<(), anyhow::Error> {
let mut buf = BytesMut::new();
+ sock.set_nodelay(true)?;
let (mut downstream_reader, downstream_writer) = sock.into_split();
let handshake = read_packet::<ServerboundHandshakePacket, _>(
@@ -64,6 +77,7 @@ async fn handle_client(
&mut None,
)
.await?;
+
let upstream_handshake = match handshake {
ServerboundHandshakePacket::ClientIntention(p) => {
info!(
@@ -73,10 +87,10 @@ async fn handle_client(
match p.intention {
ConnectionProtocol::Status => {
handle_status_intent(config, downstream_writer, downstream_reader).await?;
- bail!("we dont support ping yet")
+ return Ok(());
}
ConnectionProtocol::Login => {}
- _ => bail!("invalid intent"),
+ _ => bail!("unsupported intent"),
}
p
}
@@ -96,13 +110,10 @@ async fn handle_client(
match profile {
Some(profile) => {
- warn!("auth as {}", profile.username);
+ info!("auth as {}", profile.username);
p.username = profile.username.clone();
}
- None => {
- warn!("no profile found, disconnecting client");
- return Ok(());
- }
+ None => bail!("no profile found, disconnecting client"),
}
p
}
@@ -183,17 +194,21 @@ async fn connect(mut writer: OwnedWriteHalf, mut reader: OwnedReadHalf) -> anyho
let mut buf = [0; 1024];
loop {
let size = reader.read(&mut buf).await?;
+ if size == 0 {
+ break Ok(());
+ }
writer.write_all(&buf[..size]).await?;
}
}
async fn handle_status_intent(
config: Arc<Config>,
- mut writer: OwnedWriteHalf,
- mut reader: OwnedReadHalf,
+ writer: OwnedWriteHalf,
+ reader: OwnedReadHalf,
) -> anyhow::Result<()> {
- let mut buf = BytesMut::new();
+ // let mut buf = BytesMut::new();
let upstream = TcpStream::connect(config.backend).await?;
+ upstream.set_nodelay(true)?;
let (upstream_reader, mut upstream_writer) = upstream.into_split();
write_packet(
@@ -217,48 +232,48 @@ async fn handle_status_intent(
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(())
+ // 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(())
}