Some fixes, resolve warnings
This commit is contained in:
parent
f3a28fedcc
commit
645ea892cb
34
src/main.rs
34
src/main.rs
@ -1,26 +1,18 @@
|
|||||||
#![allow(unused)]
|
|
||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::future::poll_fn;
|
|
||||||
use futures::FutureExt;
|
use futures::FutureExt;
|
||||||
use futures::TryFutureExt;
|
|
||||||
use minecraft_protocol::data::chat::{Message, Payload};
|
use minecraft_protocol::data::chat::{Message, Payload};
|
||||||
use minecraft_protocol::data::server_status::*;
|
use minecraft_protocol::data::server_status::*;
|
||||||
use minecraft_protocol::decoder::Decoder;
|
use minecraft_protocol::decoder::Decoder;
|
||||||
use minecraft_protocol::encoder::Encoder;
|
use minecraft_protocol::encoder::Encoder;
|
||||||
use minecraft_protocol::version::v1_14_4::handshake::Handshake;
|
use minecraft_protocol::version::v1_14_4::handshake::Handshake;
|
||||||
use minecraft_protocol::version::v1_14_4::status::{PingRequest, PingResponse, StatusResponse};
|
use minecraft_protocol::version::v1_14_4::status::StatusResponse;
|
||||||
use tokio::io;
|
use tokio::io;
|
||||||
use tokio::io::AsyncRead;
|
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
use tokio::io::ReadBuf;
|
|
||||||
use tokio::net::tcp::ReadHalf;
|
use tokio::net::tcp::ReadHalf;
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
use tokio::sync::mpsc::unbounded_channel;
|
use tokio::sync::mpsc::unbounded_channel;
|
||||||
@ -71,7 +63,7 @@ async fn read_packet<'a>(
|
|||||||
while buf.len() < 2 {
|
while buf.len() < 2 {
|
||||||
// Read packet from socket
|
// Read packet from socket
|
||||||
let mut tmp = Vec::with_capacity(64);
|
let mut tmp = Vec::with_capacity(64);
|
||||||
stream.read_buf(&mut tmp).await;
|
stream.read_buf(&mut tmp).await.map_err(|_| ())?;
|
||||||
if tmp.is_empty() {
|
if tmp.is_empty() {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
@ -92,7 +84,7 @@ async fn read_packet<'a>(
|
|||||||
while buf.len() < consumed + len as usize {
|
while buf.len() < consumed + len as usize {
|
||||||
// Read packet from socket
|
// Read packet from socket
|
||||||
let mut tmp = Vec::with_capacity(64);
|
let mut tmp = Vec::with_capacity(64);
|
||||||
stream.read_buf(&mut tmp).await;
|
stream.read_buf(&mut tmp).await.map_err(|_| ())?;
|
||||||
if tmp.is_empty() {
|
if tmp.is_empty() {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
@ -109,7 +101,7 @@ async fn read_packet<'a>(
|
|||||||
|
|
||||||
/// Proxy the given inbound stream to a target address.
|
/// Proxy the given inbound stream to a target address.
|
||||||
// TODO: do not drop error here, return Box<dyn Error>
|
// TODO: do not drop error here, return Box<dyn Error>
|
||||||
async fn proxy(mut client: Client, mut inbound: TcpStream, addr_target: String) -> Result<(), ()> {
|
async fn proxy(client: Client, mut inbound: TcpStream, addr_target: String) -> Result<(), ()> {
|
||||||
let mut outbound = TcpStream::connect(addr_target).await.map_err(|_| ())?;
|
let mut outbound = TcpStream::connect(addr_target).await.map_err(|_| ())?;
|
||||||
|
|
||||||
let (mut ri, mut wi) = inbound.split();
|
let (mut ri, mut wi) = inbound.split();
|
||||||
@ -126,7 +118,7 @@ async fn proxy(mut client: Client, mut inbound: TcpStream, addr_target: String)
|
|||||||
if client.state() == ClientState::Login {
|
if client.state() == ClientState::Login {
|
||||||
eprintln!("STARTED FULL PROXY");
|
eprintln!("STARTED FULL PROXY");
|
||||||
|
|
||||||
wo.writable().await;
|
wo.writable().await.map_err(|_| ())?;
|
||||||
|
|
||||||
// Forward remaining buffer
|
// Forward remaining buffer
|
||||||
wo.write_all(&buf).await.map_err(|_| ())?;
|
wo.write_all(&buf).await.map_err(|_| ())?;
|
||||||
@ -194,10 +186,10 @@ async fn proxy(mut client: Client, mut inbound: TcpStream, addr_target: String)
|
|||||||
sample: vec![],
|
sample: vec![],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let server_status = StatusResponse { server_status };
|
let packet = StatusResponse { server_status };
|
||||||
|
|
||||||
let mut data = Vec::new();
|
let mut data = Vec::new();
|
||||||
server_status.encode(&mut data).map_err(|_| ())?;
|
packet.encode(&mut data).map_err(|_| ())?;
|
||||||
|
|
||||||
let response = RawPacket::new(0, data).encode()?;
|
let response = RawPacket::new(0, data).encode()?;
|
||||||
client_send_queue
|
client_send_queue
|
||||||
@ -243,18 +235,18 @@ async fn proxy(mut client: Client, mut inbound: TcpStream, addr_target: String)
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// Forward remaining data
|
// Forward remaining data
|
||||||
client_send_queue.send(buf.to_vec());
|
client_send_queue.send(buf.to_vec()).map_err(|_| ())?;
|
||||||
buf.clear();
|
buf.clear();
|
||||||
|
|
||||||
// Keep reading until we have at least 2 bytes
|
// Keep reading until we have at least 2 bytes
|
||||||
loop {
|
loop {
|
||||||
// Read packet from socket
|
// Read packet from socket
|
||||||
let mut tmp = Vec::new();
|
let mut tmp = Vec::new();
|
||||||
ro.read_buf(&mut tmp).await;
|
ro.read_buf(&mut tmp).await.map_err(|_| ())?;
|
||||||
if tmp.is_empty() {
|
if tmp.is_empty() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
client_send_queue.send(tmp);
|
client_send_queue.send(tmp).map_err(|_| ())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forward raw packet to server
|
// Forward raw packet to server
|
||||||
@ -264,7 +256,7 @@ async fn proxy(mut client: Client, mut inbound: TcpStream, addr_target: String)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read packet from stream
|
// Read packet from stream
|
||||||
let (packet, raw) = match read_packet(&mut buf, &mut ro).await {
|
let (_packet, raw) = match read_packet(&mut buf, &mut ro).await {
|
||||||
Ok(Some(packet)) => packet,
|
Ok(Some(packet)) => packet,
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
eprintln!("Closing connection, could not read more");
|
eprintln!("Closing connection, could not read more");
|
||||||
@ -272,12 +264,12 @@ async fn proxy(mut client: Client, mut inbound: TcpStream, addr_target: String)
|
|||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
// Forward raw packet to server
|
// Forward raw packet to server
|
||||||
client_send_queue.send(buf.to_vec());
|
client_send_queue.send(buf.to_vec()).map_err(|_| ())?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
client_send_queue.send(raw);
|
client_send_queue.send(raw).map_err(|_| ())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -92,11 +92,6 @@ impl RawPacket {
|
|||||||
let (read, len) = types::read_var_int(buf)?;
|
let (read, len) = types::read_var_int(buf)?;
|
||||||
buf = &buf[read..][..len as usize];
|
buf = &buf[read..][..len as usize];
|
||||||
|
|
||||||
Self::decode_data(len, buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Decode packet from raw buffer without the length header.
|
|
||||||
pub fn decode_data(len: i32, mut buf: &[u8]) -> Result<Self, ()> {
|
|
||||||
// Read packet ID, select buf
|
// Read packet ID, select buf
|
||||||
let (read, packet_id) = types::read_var_int(buf)?;
|
let (read, packet_id) = types::read_var_int(buf)?;
|
||||||
buf = &buf[read..];
|
buf = &buf[read..];
|
||||||
|
@ -1,7 +1,3 @@
|
|||||||
use std::io::Read;
|
|
||||||
|
|
||||||
use bytes::BytesMut;
|
|
||||||
|
|
||||||
/// Try to read var-int from data buffer.
|
/// Try to read var-int from data buffer.
|
||||||
pub fn read_var_int(buf: &[u8]) -> Result<(usize, i32), ()> {
|
pub fn read_var_int(buf: &[u8]) -> Result<(usize, i32), ()> {
|
||||||
for len in 1..=5.min(buf.len()) {
|
for len in 1..=5.min(buf.len()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user