From 5ada41f4546b9a1c1fac7e3f088ba40b49d66c57 Mon Sep 17 00:00:00 2001 From: Liam Conway <11491642+liconway@users.noreply.github.com> Date: Mon, 22 Jan 2024 19:36:47 -0700 Subject: [PATCH] Add basic config for TM/MQTT host --- .env.template | 3 +++ .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 22 +++++++++++++++++----- 5 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 .env.template diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..ccd91f9 --- /dev/null +++ b/.env.template @@ -0,0 +1,3 @@ +TM_HOST=127.0.0.1 +TM_PASSWORD= +MQTT_HOST=localhost diff --git a/.gitignore b/.gitignore index ea8c4bf..fedaa2b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.env diff --git a/Cargo.lock b/Cargo.lock index ffa5833..2ace3ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -211,6 +211,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "either" version = "1.9.0" @@ -1080,6 +1086,7 @@ version = "0.1.0" dependencies = [ "backtrace-on-stack-overflow", "bytes", + "dotenv", "env_logger", "log", "openssl", diff --git a/Cargo.toml b/Cargo.toml index 0990de7..0c49dca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ prost-build = "0.12.3" [dependencies] backtrace-on-stack-overflow = "0.3.0" bytes = "1.5.0" +dotenv = "0.15.0" env_logger = "0.11.0" log = "0.4.20" openssl = "0.10.63" diff --git a/src/main.rs b/src/main.rs index a33bfe5..24cf1b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +extern crate dotenv; + use rumqttc::{MqttOptions, Client, QoS, LastWill}; use bytes::Bytes; use std::time::Duration; @@ -16,6 +18,9 @@ use sha2::{Sha256, Digest}; use std::net::TcpStream; use std::sync::mpsc; +use dotenv::dotenv; +use std::env; + // MQTT Topics: // - division/{division_id}/{round}/{match}/score // - division/{division_id}/ranking @@ -551,7 +556,7 @@ struct TMClient { const TCP_BUFFER_SIZE: usize = 10000; impl TMClient { - fn new(uuid: [u8; 16], client_name: [u8; 32], password: String, username: [u8; 16]) -> (TMClient, TMConnection) { + fn new(uuid: [u8; 16], client_name: [u8; 32], host: String, password: String, username: [u8; 16]) -> (TMClient, TMConnection) { let (work_tx, work_rx) = mpsc::channel(); let (response_tx, response_rx) = mpsc::channel(); let (request_tx, request_rx) = mpsc::channel(); @@ -562,7 +567,8 @@ impl TMClient { let connector = builder.build(); - let stream = TcpStream::connect("127.0.0.1:5000").unwrap(); + log::debug!("Connecting to TM using address {host}:5000"); + let stream = TcpStream::connect(format!("{host}:5000")).unwrap(); let mut stream_config = connector.configure().unwrap(); stream_config.set_verify_hostname(false); @@ -570,7 +576,7 @@ impl TMClient { 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 = stream_config.connect(&host, stream).unwrap(); stream.get_ref().set_read_timeout(Some(Duration::from_millis(100))).expect("Failed to set read timeout on socket"); @@ -1269,6 +1275,8 @@ enum Work { } fn main() { + dotenv().ok(); + env_logger::init(); let mut callbacks: HashMap = HashMap::new(); @@ -1280,7 +1288,9 @@ fn main() { callbacks.insert(tm::NoticeId::NoticeFieldTimerStopped, on_timer_stop); callbacks.insert(tm::NoticeId::NoticeFieldResetTimer, on_timer_reset); - let mut mqttoptions = MqttOptions::new("vex-bridge", "localhost", 1883); + let mqtt_host = env::var("MQTT_HOST").unwrap_or(String::from("localhost")); + log::debug!("Connecting to MQTT using address {mqtt_host}:1883"); + let mut mqttoptions = MqttOptions::new("vex-bridge", mqtt_host, 1883); mqttoptions.set_keep_alive(Duration::from_secs(5)); mqttoptions.set_last_will(LastWill{ topic: String::from("bridge/status"), @@ -1303,8 +1313,10 @@ fn main() { rand::thread_rng().fill_bytes(&mut uuid); let mut client_name = [0u8;32]; rand::thread_rng().fill_bytes(&mut client_name); + let tm_host = env::var("TM_HOST").unwrap_or(String::from("127.0.0.1")); let username: [u8;16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - let (mut tm_client, mut tm_connection) = TMClient::new(uuid, client_name, String::from(""), username); + let password = env::var("TM_PASSWORD").unwrap_or(String::from("")); + let (mut tm_client, mut tm_connection) = TMClient::new(uuid, client_name, tm_host, password, username); let tm_thread = thread::spawn(move || while running { tm_client.process();