From 7b4fafa3bd5052f9e831b4c31330f632dba34bfb Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sat, 20 Jan 2024 16:04:22 -0700 Subject: [PATCH] Added some logic to connect to the tournament manager TLS socket, not handling the hello or anything though. --- Cargo.lock | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/main.rs | 94 ++++++++++++++++++++++++------- 3 files changed, 231 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c227cc..ac96aef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,54 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.79" @@ -92,6 +140,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "core-foundation" version = "0.9.4" @@ -114,6 +168,29 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eeb342678d785662fd2514be38c459bb925f02b68dd2a3e0f21d7ef82d979dd" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -153,6 +230,21 @@ dependencies = [ "spin", ] +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "futures-core" version = "0.3.30" @@ -222,6 +314,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "indexmap" version = "2.1.0" @@ -322,12 +420,50 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "openssl" +version = "0.10.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15c9d69dd87a29568d4d017cfe8ec518706046a05184e5aea92d0af890b803c8" +dependencies = [ + "bitflags 2.4.1", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "openssl-probe" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-sys" +version = "0.9.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e1bf214306098e4832460f797824c05d25aacdf896f64a985fb0fd992454ae" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "petgraph" version = "0.6.4" @@ -350,6 +486,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb" + [[package]] name = "prettyplease" version = "0.2.16" @@ -780,11 +922,25 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "vex_mqtt_rust" version = "0.1.0" dependencies = [ "bytes", + "env_logger", + "openssl", "prost", "prost-build", "prost-types", diff --git a/Cargo.toml b/Cargo.toml index 4c9dede..1a54496 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ prost-build = "0.12.3" [dependencies] bytes = "1.5.0" +env_logger = "0.11.0" +openssl = "0.10.63" prost = "0.12.3" prost-types = "0.12.3" rumqttc = "0.23.0" diff --git a/src/main.rs b/src/main.rs index 334e0be..592e265 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,14 @@ use prost::Message; use std::io::Cursor; use serde::{Serialize, Deserialize}; +use std::io::prelude::*; +use std::io::BufReader; +use std::fs::File; + +use std::net::TcpStream; +use std::sync::Mutex; +use std::sync::Arc; + // MQTT Topics: // - division/{division_id} // - division/{division_id}/ranking @@ -156,55 +164,97 @@ struct Match { } struct TMClient { + stream: Arc>>, notices: mpsc::Sender, - requests: mpsc::Receiver, - responses: mpsc::Sender, + responses: mpsc::Sender, } struct TMConnection { + stream: Arc>>, notices: mpsc::Receiver, - requests: mpsc::Sender, - responses: mpsc::Receiver, + responses: mpsc::Receiver, } struct BackendMessage { + status: u8, + request_id: u32, + data: Vec, +} + +impl BackendMessage { + fn from_bytes(bytes: Vec) -> Option { + if bytes.len() < 5 { + return None; + } + None + } + + fn as_bytes(self: BackendMessage) -> Vec { + return Vec::new(); + } } impl TMClient { fn new() -> (TMClient, TMConnection) { let (notice_tx, notice_rx) = mpsc::channel(); - let (request_tx, request_rx) = mpsc::channel(); let (response_tx, response_rx) = mpsc::channel(); - (TMClient{ + + let mut builder = openssl::ssl::SslConnector::builder(openssl::ssl::SslMethod::tls()).unwrap(); + builder.set_ca_file("tm.crt").unwrap(); + builder.set_verify(openssl::ssl::SslVerifyMode::PEER); + + let connector = builder.build(); + + let stream = TcpStream::connect("127.0.0.1:5000").unwrap(); + + let mut stream_config = connector.configure().unwrap(); + stream_config.set_verify_hostname(false); + stream_config.set_certificate_chain_file("tm.crt").unwrap(); + stream_config.set_private_key_file("tm.crt", openssl::ssl::SslFiletype::PEM).unwrap(); + stream_config.set_use_server_name_indication(false); + + let stream = stream_config.connect("127.0.0.1", stream).unwrap(); + let stream = Arc::new(Mutex::new(stream)); + + return (TMClient{ + stream: stream.clone(), notices: notice_tx, - requests: request_rx, responses: response_tx }, TMConnection{ + stream: stream, notices: notice_rx, - requests: request_tx, responses: response_rx - },) + },); } fn process(self: &TMClient) { - // Check if there's any incoming requests to process(aka send) - match self.requests.try_recv() { - Ok(request) => println!("Got incoming request"), - Err(error) => {}, + let mut incoming = [0; 2048]; + let mut stream = self.stream.lock().unwrap(); + match stream.read(&mut incoming) { + Ok(read) => { + println!("Data({}): {}", read, String::from_utf8_lossy(&incoming)); + // If the data is a notice, parse it and send it to the notices mpsc + // If the data is a response, parse it and send it to the responses mpsc + }, + Err(error) => println!("Error: {}", error), } - // Do per-call processing(currently just pretending to receive an empty notice) - match self.notices.send(tm::Notice::default()) { - Ok(_) => println!("Received notice"), - Err(error) => println!("Recv error {}", error), - } } +} - // TODO: make send functionality - fn send(request: BackendMessage) -> BackendMessage { - request +impl TMConnection { + fn send(self: &mut TMConnection, request: BackendMessage) -> Result { + let mut stream = self.stream.lock().unwrap(); + match stream.write(&request.as_bytes()) { + Ok(_) => + match self.responses.recv() { + Ok(response) => Ok(response), + Err(_) => Err(std::io::Error::new(std::io::ErrorKind::ConnectionAborted, "channel closed")), + }, + Err(error) => Err(error), + } } } @@ -307,6 +357,8 @@ fn on_match_list_update(notice: tm::Notice, event: Event) -> (Vec, } fn main() { + env_logger::init(); + let mut event = Event{ name: String::from(""), divisions: Vec::new(),