aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock43
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs3
-rw-r--r--src/net.rs51
-rw-r--r--src/protocol/message/handshake/types.rs19
-rw-r--r--test.hex0
6 files changed, 95 insertions, 22 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1cc25f8..fc763da 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,6 +1,11 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
+name = "adler32"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "byteorder"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -11,10 +16,35 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "crc32fast"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "flate2"
+version = "1.0.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "miniz_oxide 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "libquassel"
version = "0.1.0"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -26,7 +56,20 @@ dependencies = [
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
+[[package]]
+name = "miniz_oxide"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
[metadata]
+"checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
+"checksum flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6bd6d6f4752952feb71363cffc9ebac9411b75b87c6ab6058c40c8900cf43c0f"
+"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
+"checksum miniz_oxide 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6f3f74f726ae935c3f514300cc6773a0c9492abc5e972d42ba0c0ebb88757625"
diff --git a/Cargo.toml b/Cargo.toml
index cab9236..0cae5bf 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,3 +9,4 @@ edition = "2018"
[dependencies]
log = "0.4"
byteorder = "1.3.2"
+flate2 = "1.0"
diff --git a/src/main.rs b/src/main.rs
index 28d175c..4a0de93 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,11 +14,12 @@ use protocol::primitive::{String, StringList};
use protocol::message::{ClientInit};
fn main() -> std::io::Result<()> {
+
let mut server = net::connect(
"localhost",
4242,
false,
- false,
+ true,
)?;
let mut features = StringList::new();
diff --git a/src/net.rs b/src/net.rs
index 145256a..dfcbb56 100644
--- a/src/net.rs
+++ b/src/net.rs
@@ -4,18 +4,26 @@ use std::io::{Error};
use std::result::Result;
use std::net::TcpStream;
use std::vec::Vec;
+use std::convert::TryInto;
+use std::io::Cursor;
+
+use flate2::Compress;
+use flate2::Decompress;
+use flate2::Compression;
+use flate2::FlushCompress;
+use flate2::FlushDecompress;
+use flate2::read::ZlibDecoder;
extern crate log;
// use log::{info, warn, debug};
-
use crate::protocol::message;
use crate::protocol::error::ErrorKind;
pub struct Client {
tcp_stream: TcpStream,
- pub address: &'static str,
- pub port: u32,
+ encoder: Compress,
+ decoder: Decompress,
pub tls: bool,
pub compression: bool,
}
@@ -25,24 +33,25 @@ impl Client {
use crate::protocol::message::handshake::{HandshakeDeserialize, HandshakeSerialize, HandshakeQRead, VariantMap};
use crate::protocol::message::handshake::{ClientInitAck, ClientLogin, ClientLoginAck, SessionInit};
- self.tcp_stream.write(&client.serialize().unwrap()).unwrap();
+ self.write(&client.serialize().unwrap()).unwrap();
let mut buf: Vec<u8> = [0; 2048].to_vec();
- let len = VariantMap::read(&mut self.tcp_stream, &mut buf).unwrap();
+ let len = VariantMap::read(self, &mut buf).unwrap();
buf.truncate(len);
let res = ClientInitAck::parse(&buf).unwrap();
println!("res: {:?}", res);
let login = ClientLogin {user: user.to_string(), password: pass.to_string()};
- self.tcp_stream.write(&login.serialize().unwrap()).unwrap();
+ self.write(&login.serialize().unwrap()).unwrap();
+ println!("res: {:?}", res);
let mut buf: Vec<u8> = [0; 2048].to_vec();
- let len = VariantMap::read(&mut self.tcp_stream, &mut buf).unwrap();
+ let len = VariantMap::read(self, &mut buf).unwrap();
buf.truncate(len);
let _res = ClientLoginAck::parse(&buf).unwrap();
let mut buf: Vec<u8> = [0; 2048].to_vec();
- let len = VariantMap::read(&mut self.tcp_stream, &mut buf).unwrap();
+ let len = VariantMap::read(self, &mut buf).unwrap();
buf.truncate(len);
let res = SessionInit::parse(&buf).unwrap();
@@ -50,10 +59,27 @@ impl Client {
}
}
+impl std::io::Read for Client {
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
+ return self.tcp_stream.read(buf);
+ }
+}
+
+impl std::io::Write for Client {
+ fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
+ let mut cbuf = Vec::with_capacity(buf.len());
+ self.encoder.compress_vec(buf, &mut cbuf, FlushCompress::Finish)?;
+ return self.tcp_stream.write(&buf);
+ }
+
+ fn flush(&mut self) -> Result<(), Error> {
+ return self.tcp_stream.flush();
+ }
+}
+
pub fn connect(address: &'static str, port: u32, tls: bool, compression: bool) -> Result<Client, Error> {
use crate::protocol::primitive::deserialize::Deserialize;
- //let mut s = BufWriter::new(TcpStream::connect(format!("{}:{}", address, port)).unwrap());
let mut s = TcpStream::connect(format!("{}:{}", address, port)).unwrap();
// Set Features
@@ -90,14 +116,15 @@ pub fn connect(address: &'static str, port: u32, tls: bool, compression: bool) -
}
let mut buf = [0; 4];
- s.read_exact(&mut buf)?;
+ s.read(&mut buf)?;
let (_, val) = ConnAck::parse(&buf).unwrap();
println!("Received: {:?}", val);
+// let sock = ZlibDecoder::new_with_buf(s, [0; 1].to_vec());
let server: Client = Client {
tcp_stream: s,
- address: address,
- port: port,
+ encoder: Compress::new(Compression::best(), true),
+ decoder: Decompress::new(true),
tls: tls,
compression: compression,
};
diff --git a/src/protocol/message/handshake/types.rs b/src/protocol/message/handshake/types.rs
index f099b0f..c809764 100644
--- a/src/protocol/message/handshake/types.rs
+++ b/src/protocol/message/handshake/types.rs
@@ -74,18 +74,19 @@ impl HandshakeQRead for VariantMap {
fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> {
s.read(&mut b[0..4])?;
let (_, len) = i32::parse(&b[0..4])?;
+ let ulen = len as usize;
// Read the 00 00 00 0a VariantType bytes and discard
- s.read(&mut b[4..8])?;
+ s.read(&mut b[4..ulen])?;
- let mut pos = 8;
- let len: usize = len as usize;
- loop {
- if pos >= len { break; }
- pos += Variant::read(s, &mut b[pos..])?;
- pos += Variant::read(s, &mut b[pos..])?;
- }
+// let mut pos = 8;
+// let len: usize = len as usize;
+// loop {
+// if pos >= len { break; }
+// pos += Variant::read(s, &mut b[pos..])?;
+// pos += Variant::read(s, &mut b[pos..])?;
+// }
- return Ok(pos);
+ return Ok(ulen + 4);
}
}
diff --git a/test.hex b/test.hex
deleted file mode 100644
index e69de29..0000000
--- a/test.hex
+++ /dev/null