Added some logic to connect to the tournament manager TLS socket, not handling the hello or anything though.

master
noah metz 2024-01-20 16:04:22 -07:00
parent 47d243ff84
commit 7b4fafa3bd
3 changed files with 231 additions and 21 deletions

156
Cargo.lock generated

@ -26,6 +26,54 @@ dependencies = [
"memchr", "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]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.79" version = "1.0.79"
@ -92,6 +140,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.4" version = "0.9.4"
@ -114,6 +168,29 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" 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]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.1" version = "1.0.1"
@ -153,6 +230,21 @@ dependencies = [
"spin", "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]] [[package]]
name = "futures-core" name = "futures-core"
version = "0.3.30" version = "0.3.30"
@ -222,6 +314,12 @@ dependencies = [
"windows-sys 0.52.0", "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]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.1.0" version = "2.1.0"
@ -322,12 +420,50 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" 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]] [[package]]
name = "openssl-probe" name = "openssl-probe"
version = "0.1.5" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" 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]] [[package]]
name = "petgraph" name = "petgraph"
version = "0.6.4" version = "0.6.4"
@ -350,6 +486,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb"
[[package]] [[package]]
name = "prettyplease" name = "prettyplease"
version = "0.2.16" version = "0.2.16"
@ -780,11 +922,25 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" 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]] [[package]]
name = "vex_mqtt_rust" name = "vex_mqtt_rust"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bytes", "bytes",
"env_logger",
"openssl",
"prost", "prost",
"prost-build", "prost-build",
"prost-types", "prost-types",

@ -11,6 +11,8 @@ prost-build = "0.12.3"
[dependencies] [dependencies]
bytes = "1.5.0" bytes = "1.5.0"
env_logger = "0.11.0"
openssl = "0.10.63"
prost = "0.12.3" prost = "0.12.3"
prost-types = "0.12.3" prost-types = "0.12.3"
rumqttc = "0.23.0" rumqttc = "0.23.0"

@ -8,6 +8,14 @@ use prost::Message;
use std::io::Cursor; use std::io::Cursor;
use serde::{Serialize, Deserialize}; 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: // MQTT Topics:
// - division/{division_id} // - division/{division_id}
// - division/{division_id}/ranking // - division/{division_id}/ranking
@ -156,55 +164,97 @@ struct Match {
} }
struct TMClient { struct TMClient {
stream: Arc<Mutex<openssl::ssl::SslStream<TcpStream>>>,
notices: mpsc::Sender<tm::Notice>, notices: mpsc::Sender<tm::Notice>,
requests: mpsc::Receiver<tm::Notice>, responses: mpsc::Sender<BackendMessage>,
responses: mpsc::Sender<tm::Notice>,
} }
struct TMConnection { struct TMConnection {
stream: Arc<Mutex<openssl::ssl::SslStream<TcpStream>>>,
notices: mpsc::Receiver<tm::Notice>, notices: mpsc::Receiver<tm::Notice>,
requests: mpsc::Sender<tm::Notice>, responses: mpsc::Receiver<BackendMessage>,
responses: mpsc::Receiver<tm::Notice>,
} }
struct BackendMessage { struct BackendMessage {
status: u8,
request_id: u32,
data: Vec<u8>,
}
impl BackendMessage {
fn from_bytes(bytes: Vec<u8>) -> Option<BackendMessage> {
if bytes.len() < 5 {
return None;
}
None
}
fn as_bytes(self: BackendMessage) -> Vec<u8> {
return Vec::new();
}
} }
impl TMClient { impl TMClient {
fn new() -> (TMClient, TMConnection) { fn new() -> (TMClient, TMConnection) {
let (notice_tx, notice_rx) = mpsc::channel(); let (notice_tx, notice_rx) = mpsc::channel();
let (request_tx, request_rx) = mpsc::channel();
let (response_tx, response_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, notices: notice_tx,
requests: request_rx,
responses: response_tx responses: response_tx
}, },
TMConnection{ TMConnection{
stream: stream,
notices: notice_rx, notices: notice_rx,
requests: request_tx,
responses: response_rx responses: response_rx
},) },);
} }
fn process(self: &TMClient) { fn process(self: &TMClient) {
// Check if there's any incoming requests to process(aka send) let mut incoming = [0; 2048];
match self.requests.try_recv() { let mut stream = self.stream.lock().unwrap();
Ok(request) => println!("Got incoming request"), match stream.read(&mut incoming) {
Err(error) => {}, 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 impl TMConnection {
fn send(request: BackendMessage) -> BackendMessage { fn send(self: &mut TMConnection, request: BackendMessage) -> Result<BackendMessage, std::io::Error> {
request 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<MQTTMessage>,
} }
fn main() { fn main() {
env_logger::init();
let mut event = Event{ let mut event = Event{
name: String::from(""), name: String::from(""),
divisions: Vec::new(), divisions: Vec::new(),