forked from navos/sheepd
✨ encrypted msg + online reporting + refactor
This commit is contained in:
parent
125d50530d
commit
a567214f58
19 changed files with 318 additions and 304 deletions
|
@ -1,7 +1,9 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use sage::Identity;
|
||||
|
||||
use crate::{
|
||||
api::{self, Identity, JoinResponse},
|
||||
api::{self, JoinResponse},
|
||||
sheepd_core::config::AgentConfig,
|
||||
};
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ use owl::{Deserialize, Serialize};
|
|||
|
||||
use crate::api::JoinResponse;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct AgentConfig {
|
||||
pub home: String,
|
||||
pub token: String,
|
||||
|
|
60
src/sheepd_core/daemon.rs
Normal file
60
src/sheepd_core/daemon.rs
Normal file
|
@ -0,0 +1,60 @@
|
|||
use std::{path::PathBuf, time::Duration};
|
||||
|
||||
use rumqttc::{AsyncClient, QoS};
|
||||
use sage::Identity;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{api, sheepd_core::config::AgentConfig};
|
||||
|
||||
use super::mqtt::send_back;
|
||||
|
||||
pub async fn report_online(client: AsyncClient) {
|
||||
loop {
|
||||
send_back(&client, "online", json!(crate::api::Result::Ok())).await;
|
||||
tokio::time::sleep(Duration::from_secs(60)).await;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_daemon() {
|
||||
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 _ = crate::IDENTITY.set(i);
|
||||
|
||||
let conf = conf.unwrap();
|
||||
crate::AGENT.set(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, eventloop) = api::mqtt_connect(machine_id, &crate::AGENT.get().unwrap().mqtt);
|
||||
|
||||
crate::MQTT.set(client.clone()).unwrap();
|
||||
|
||||
log::info!("Connection done");
|
||||
|
||||
tokio::task::spawn(report_online(client.clone()));
|
||||
|
||||
log::info!("Listen on {}", format!("{machine_id}/cmd"));
|
||||
crate::MQTT
|
||||
.get()
|
||||
.unwrap()
|
||||
.subscribe(format!("{machine_id}/cmd"), QoS::AtMostOnce)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
api::run_event_loop(eventloop, crate::sheepd_core::mqtt::handle_mqtt).await;
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
pub mod args;
|
||||
pub mod cmd;
|
||||
pub mod config;
|
||||
pub mod daemon;
|
||||
pub mod mqtt;
|
||||
|
|
42
src/sheepd_core/mqtt.rs
Normal file
42
src/sheepd_core/mqtt.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use owl::Serialize;
|
||||
use rumqttc::AsyncClient;
|
||||
use sage::PersonaIdentity;
|
||||
|
||||
// Client MQTT
|
||||
pub async fn handle_mqtt(topic: String, data: Vec<u8>) {
|
||||
//println!("got real raw: {}", String::from_utf8_lossy(&data));
|
||||
let pk = (
|
||||
String::new(),
|
||||
crate::AGENT.get().unwrap().server_sign.clone(),
|
||||
);
|
||||
let pk = pk.sign_key().unwrap();
|
||||
let payload = crate::IDENTITY.get().unwrap().decrypt(&data, &pk).unwrap();
|
||||
println!(
|
||||
"got payload {}",
|
||||
String::from_utf8(payload.payload).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
pub async fn send_back<T: Serialize>(client: &AsyncClient, topic: &str, request: T) {
|
||||
let data = serde_json::to_string(&request).unwrap();
|
||||
|
||||
let pk = crate::AGENT.get().unwrap();
|
||||
let pk = (pk.server_age.clone(), String::new());
|
||||
let rec = pk.enc_key().unwrap();
|
||||
let machine_id = std::fs::read_to_string("/etc/machine-id")
|
||||
.unwrap()
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
let payload = crate::IDENTITY
|
||||
.get()
|
||||
.unwrap()
|
||||
.encrypt(data.as_bytes(), &rec);
|
||||
let topic = format!("{machine_id}/{topic}");
|
||||
|
||||
log::info!("Publish to {machine_id}{topic}");
|
||||
client
|
||||
.publish(topic, rumqttc::QoS::AtMostOnce, true, payload)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue