trifid/tfclient/src/util.rs

82 lines
2.1 KiB
Rust
Raw Normal View History

2023-06-26 02:00:36 +00:00
use crate::apiworker::APIWorkerMessage;
use crate::daemon::ThreadMessageSender;
use crate::nebulaworker::NebulaWorkerMessage;
use crate::socketworker::SocketWorkerMessage;
use crate::timerworker::TimerWorkerMessage;
2023-11-23 20:23:52 +00:00
use log::{error, warn};
use sha2::Digest;
use sha2::Sha256;
use url::Url;
pub fn sha256(bytes: &[u8]) -> String {
let mut hasher = Sha256::new();
hasher.update(bytes);
let digest = hasher.finalize();
hex::encode(digest)
2023-03-22 18:34:06 +00:00
}
pub fn check_server_url(server: &str) {
2023-05-14 17:47:49 +00:00
let api_base = match Url::parse(server) {
2023-03-22 18:34:06 +00:00
Ok(u) => u,
Err(e) => {
error!("Invalid server url `{}`: {}", server, e);
std::process::exit(1);
}
};
match api_base.scheme() {
2023-05-14 17:47:49 +00:00
"http" => {
warn!("HTTP api urls are not reccomended. Please switch to HTTPS if possible.")
}
2023-03-22 18:34:06 +00:00
"https" => (),
_ => {
2023-05-14 17:47:49 +00:00
error!(
"Unsupported protocol `{}` (expected one of http, https)",
api_base.scheme()
);
2023-03-22 18:34:06 +00:00
std::process::exit(1);
}
}
2023-05-14 17:47:49 +00:00
}
2023-06-26 02:00:36 +00:00
pub fn shutdown(transmitter: &ThreadMessageSender) {
match transmitter
.nebula_thread
.send(NebulaWorkerMessage::Shutdown)
{
Ok(_) => (),
Err(e) => {
error!(
2023-11-23 20:23:52 +00:00
"Error sending shutdown message to nebula worker thread: {}",
e
);
2023-06-26 02:00:36 +00:00
}
}
2023-11-23 20:23:52 +00:00
match transmitter.api_thread.send(APIWorkerMessage::Shutdown) {
2023-06-26 02:00:36 +00:00
Ok(_) => (),
Err(e) => {
error!("Error sending shutdown message to api worker thread: {}", e);
}
}
match transmitter
.socket_thread
.send(SocketWorkerMessage::Shutdown)
{
Ok(_) => (),
Err(e) => {
error!(
2023-11-23 20:23:52 +00:00
"Error sending shutdown message to socket worker thread: {}",
e
);
2023-06-26 02:00:36 +00:00
}
}
2023-11-23 20:23:52 +00:00
match transmitter.timer_thread.send(TimerWorkerMessage::Shutdown) {
2023-06-26 02:00:36 +00:00
Ok(_) => (),
Err(e) => {
error!(
2023-11-23 20:23:52 +00:00
"Error sending shutdown message to timer worker thread: {}",
e
);
2023-06-26 02:00:36 +00:00
}
}
2023-11-23 20:23:52 +00:00
}