Support connecting to offline mode servers

This commit is contained in:
Thinkofname 2016-04-03 11:20:31 +01:00
parent d2c5d0641c
commit 58ffe250e2
2 changed files with 40 additions and 15 deletions

View File

@ -739,6 +739,9 @@ impl Conn {
0
};
if self.compression_threshold >= 0 && buf.len() as i32 > self.compression_threshold {
if self.compression_write.is_none() {
self.compression_write = Some(ZlibEncoder::new(io::Cursor::new(Vec::new()), flate2::Compression::Default));
}
extra = 0;
let uncompressed_size = buf.len();
let mut new = Vec::new();
@ -766,6 +769,9 @@ impl Conn {
let mut buf = io::Cursor::new(ibuf);
if self.compression_threshold >= 0 {
if self.compression_read.is_none() {
self.compression_read = Some(ZlibDecoder::new(io::Cursor::new(Vec::new())));
}
let uncompressed_size = try!(VarInt::read_from(&mut buf)).0;
if uncompressed_size != 0 {
let mut new = Vec::with_capacity(uncompressed_size as usize);
@ -806,13 +812,8 @@ impl Conn {
self.cipher = Option::Some(openssl::EVPCipher::new(key, key, decrypt));
}
pub fn set_compresssion(&mut self, threshold: i32, read: bool) {
pub fn set_compresssion(&mut self, threshold: i32) {
self.compression_threshold = threshold;
if read {
self.compression_read = Some(ZlibDecoder::new(io::Cursor::new(Vec::new())));
} else {
self.compression_write = Some(ZlibEncoder::new(io::Cursor::new(Vec::new()), flate2::Compression::Default));
}
}
pub fn do_status(mut self) -> Result<(Status, time::Duration), Error> {

View File

@ -110,11 +110,30 @@ impl Server {
username: profile.username.clone(),
}));
let packet = match try!(conn.read_packet()) {
protocol::packet::Packet::EncryptionRequest(val) => val,
protocol::packet::Packet::LoginDisconnect(val) => return Err(protocol::Error::Disconnect(val.reason)),
val => return Err(protocol::Error::Err(format!("Wrong packet: {:?}", val))),
};
let packet;
loop {
match try!(conn.read_packet()) {
protocol::packet::Packet::SetInitialCompression(val) => {
conn.set_compresssion(val.threshold.0);
},
protocol::packet::Packet::EncryptionRequest(val) => {
packet = val;
break;
},
protocol::packet::Packet::LoginSuccess(val) => {
warn!("Server is running in offline mode");
debug!("Login: {} {}", val.username, val.uuid);
let mut read = conn.clone();
let mut write = conn.clone();
read.state = protocol::State::Play;
write.state = protocol::State::Play;
let rx = Self::spawn_reader(read);
return Ok(Server::new(resources, console, Some(write), Some(rx)));
}
protocol::packet::Packet::LoginDisconnect(val) => return Err(protocol::Error::Disconnect(val.reason)),
val => return Err(protocol::Error::Err(format!("Wrong packet: {:?}", val))),
};
}
let mut key = openssl::PublicKey::new(&packet.public_key.data);
let shared = openssl::gen_random(16);
@ -138,8 +157,8 @@ impl Server {
loop {
match try!(read.read_packet()) {
protocol::packet::Packet::SetInitialCompression(val) => {
read.set_compresssion(val.threshold.0, true);
write.set_compresssion(val.threshold.0, false);
read.set_compresssion(val.threshold.0);
write.set_compresssion(val.threshold.0);
}
protocol::packet::Packet::LoginSuccess(val) => {
debug!("Login: {} {}", val.username, val.uuid);
@ -152,6 +171,12 @@ impl Server {
}
}
let rx = Self::spawn_reader(read);
Ok(Server::new(resources, console, Some(write), Some(rx)))
}
fn spawn_reader(mut read: protocol::Conn) -> mpsc::Receiver<Result<packet::Packet, protocol::Error>> {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
loop {
@ -165,8 +190,7 @@ impl Server {
}
}
});
Ok(Server::new(resources, console, Some(write), Some(rx)))
rx
}
pub fn dummy_server(resources: Arc<RwLock<resources::Manager>>, console: Arc<Mutex<console::Console>>) -> Server {