encrypted msg + online reporting + refactor

This commit is contained in:
JMARyA 2025-04-30 09:35:21 +02:00
parent 125d50530d
commit a567214f58
19 changed files with 318 additions and 304 deletions

View file

@ -1,19 +1,16 @@
use api::Identity;
use sage::Identity;
use sheepd_core::{
args::{SheepdArgs, SheepdCommand},
config::AgentConfig,
};
mod api;
mod sheepd_core;
use rumqttc::{AsyncClient, Event, MqttOptions, QoS, Transport};
use std::{error::Error, path::PathBuf, time::Duration};
use tokio::{
sync::OnceCell,
task,
time::{self, sleep},
};
use rumqttc::AsyncClient;
use tokio::sync::OnceCell;
pub static MQTT: OnceCell<AsyncClient> = OnceCell::const_new();
pub static IDENTITY: OnceCell<Identity> = OnceCell::const_new();
pub static AGENT: OnceCell<AgentConfig> = OnceCell::const_new();
#[tokio::main]
async fn main() {
@ -25,42 +22,6 @@ async fn main() {
SheepdCommand::Join(join_command) => sheepd_core::cmd::join(join_command),
}
} else {
log::info!("Starting sheepd");
let conf = AgentConfig::try_load();
if conf.is_none() {
log::error!("No config file at /etc/sheepd/config.toml");
std::process::exit(1);
}
let i = if let Some(i) = Identity::try_load(&PathBuf::from("/etc/sheepd")) {
i
} else {
let i = Identity::new();
i.save(&PathBuf::from("/etc/sheepd"));
i
};
let conf = conf.unwrap();
let machine_id = std::fs::read_to_string("/etc/machine-id").unwrap();
let machine_id = machine_id.trim();
log::info!("Connecting to MQTT as {machine_id}");
let (client, mut eventloop) = api::mqtt_connect(machine_id, &conf.mqtt);
crate::MQTT.set(client).unwrap();
log::info!("Connection done");
log::info!("Start sub for {}", format!("{machine_id}/cmd"));
crate::MQTT
.get()
.unwrap()
.subscribe(format!("{machine_id}/cmd"), QoS::AtMostOnce)
.await
.unwrap();
api::run_event_loop(eventloop).await;
sheepd_core::daemon::start_daemon().await;
}
}