Skip to content

Commit

Permalink
copy bidirectional
Browse files Browse the repository at this point in the history
  • Loading branch information
hungl6844 committed Apr 5, 2024
1 parent bbe0961 commit 6a9dd70
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ edition = "2021"
[dependencies]
toml = "0.7.6"
serde = { version = "1.0.188", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
tokio = { version = "1", features = ["full", "net"] }
serde_json = "1.0"
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ mod structure;

use structure::{config::Config, protocol::parse};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::net::{TcpListener, TcpStream};
use tokio::spawn;

use crate::structure::protocol::{self, ServerboundPackets, StatusJson, Version, Players, Sample, Description, write_varint};
use crate::structure::protocol::{self, ServerboundPackets, StatusJson, Version, Players, Description, write_varint, State};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -38,7 +39,7 @@ async fn proxy(/*child: &Child,*/ config: &Config) -> Result<(), Box<dyn std::er
let (mut stream, address) = client.unwrap();
println!("new client connected from: {}", address);

let mut status = protocol::State::Listening;
let mut status = State::Listening;

loop {
stream.readable().await?;
Expand All @@ -62,7 +63,7 @@ async fn proxy(/*child: &Child,*/ config: &Config) -> Result<(), Box<dyn std::er
}

ServerboundPackets::PingRequest { .. } => {
if status != protocol::State::Status {
if status != State::Status {
eprintln!("wrong state! state was {status:?}, should have been Status");
stream.shutdown().await?;
continue 'conn;
Expand Down Expand Up @@ -108,8 +109,15 @@ async fn proxy(/*child: &Child,*/ config: &Config) -> Result<(), Box<dyn std::er
stream.write_all(buf.as_slice()).await?;
}

_ => {
return Err("uninplemented packet".into());
ServerboundPackets::Unimplemented => {
if status == State::Login {
let cport = config.server_port;
spawn(async move {
let mut server = TcpStream::connect(("127.0.0.1", cport)).await.unwrap();
tokio::io::copy_bidirectional(&mut server, &mut stream).await.unwrap();
});
continue 'conn;
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/structure/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub enum ServerboundPackets {
id: u8,
payload: i64,
},
Unimplemented
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -143,9 +144,7 @@ where
}

_ => {
// this is a scenario that I don't want to think about because I might have to
// redesign this entire project over it
Err("this packet is unimplemented".into())
Ok(ServerboundPackets::Unimplemented)
}
}
}
Expand Down Expand Up @@ -186,6 +185,10 @@ pub async fn to_bytes(value: ServerboundPackets) -> Vec<u8> {
byte_array.push(1);
byte_array.push(id);
}

ServerboundPackets::Unimplemented => {
eprintln!("cannot convert to bytes, returning empty vec (this will be fixed after thiserror setup)") ;
}
};

byte_array
Expand Down

0 comments on commit 6a9dd70

Please sign in to comment.