From fbc478222c29014c9eae6eb9f450e679e9798b3b Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 17 Oct 2022 10:05:32 +0200 Subject: stuff --- src/config.rs | 1 + src/main.rs | 182 ++++++++++++++++++++++++++++++---------------------------- 2 files changed, 94 insertions(+), 89 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 45ed6f2..b3d4e31 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { + pub protocol: u32, pub backend: SocketAddr, pub bind: SocketAddr, pub whitelist: Vec, diff --git a/src/main.rs b/src/main.rs index 8111ebd..d1459e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,9 @@ async fn handle_client(config: Arc, sock: TcpStream) -> Result<(), anyho "new client (version={}, intent={:?})", p.protocol_version, p.intention ); + if p.protocol_version == config.protocol { + bail!("protocol version unsupported") + } match p.intention { ConnectionProtocol::Status => { handle_status_intent(config, downstream_writer, downstream_reader).await?; @@ -147,47 +150,6 @@ async fn handle_client(config: Arc, sock: TcpStream) -> Result<(), anyho connect(upstream_writer, downstream_reader).await?; Ok(()) - // for _ in 0..3 { - // let a = read_packet::( - // &mut upstream_reader, - // &mut buf, - // None, - // &mut None, - // ) - // .await?; - // debug!("login {a:?}"); - // write_packet(&a, &mut downstream_writer, None, &mut None).await?; - // } - - // tokio::spawn(async move { - // let mut buf = BytesMut::new(); - // loop { - // let a = read_packet::( - // &mut upstream_reader, - // &mut buf, - // None, - // &mut None, - // ) - // .await - // .unwrap(); - // debug!("downstream {a:?}"); - // write_packet(&a, &mut downstream_writer, None, &mut None) - // .await - // .unwrap(); - // } - // }); - - // loop { - // let a = read_packet::( - // &mut downstream_reader, - // &mut buf, - // None, - // &mut None, - // ) - // .await?; - // debug!("upstream {a:?}"); - // write_packet(&a, &mut upstream_writer, None, &mut None).await?; - // } } async fn connect(mut writer: OwnedWriteHalf, mut reader: OwnedReadHalf) -> anyhow::Result<()> { @@ -213,9 +175,9 @@ async fn handle_status_intent( write_packet( &ServerboundHandshakePacket::ClientIntention(ClientIntentionPacket { - protocol_version: 760, - hostname: "127.0.0.1".to_string(), - port: 25567, + protocol_version: config.protocol, + hostname: config.backend.ip().to_string(), + port: config.backend.port(), intention: ConnectionProtocol::Status, }), &mut upstream_writer, @@ -231,49 +193,91 @@ async fn handle_status_intent( connect(upstream_writer, reader).await?; return Ok(()); - - // loop { - // let req = read_packet::(&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::(&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(()) + +// for _ in 0..3 { +// let a = read_packet::( +// &mut upstream_reader, +// &mut buf, +// None, +// &mut None, +// ) +// .await?; +// debug!("login {a:?}"); +// write_packet(&a, &mut downstream_writer, None, &mut None).await?; +// } + +// tokio::spawn(async move { +// let mut buf = BytesMut::new(); +// loop { +// let a = read_packet::( +// &mut upstream_reader, +// &mut buf, +// None, +// &mut None, +// ) +// .await +// .unwrap(); +// debug!("downstream {a:?}"); +// write_packet(&a, &mut downstream_writer, None, &mut None) +// .await +// .unwrap(); +// } +// }); + +// loop { +// let a = read_packet::( +// &mut downstream_reader, +// &mut buf, +// None, +// &mut None, +// ) +// .await?; +// debug!("upstream {a:?}"); +// write_packet(&a, &mut upstream_writer, None, &mut None).await?; +// } -- cgit v1.2.3-70-g09d2