✨ encrypted msg + online reporting + refactor
This commit is contained in:
parent
125d50530d
commit
a567214f58
19 changed files with 318 additions and 304 deletions
64
src/herd_core/mqtt.rs
Normal file
64
src/herd_core/mqtt.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use crate::Machine;
|
||||
use owl::prelude::*;
|
||||
use owl::{Serialize, get, query};
|
||||
use rumqttc::AsyncClient;
|
||||
use sage::PersonaIdentity;
|
||||
|
||||
pub async fn handle_mqtt(topic: String, data: Vec<u8>) {
|
||||
log::info!("Received client request from {topic}");
|
||||
let (client, cat) = topic.split_once('/').unwrap();
|
||||
let mac: Model<Machine> = get!(client).unwrap();
|
||||
|
||||
let dec = crate::IDENTITY
|
||||
.get()
|
||||
.unwrap()
|
||||
.decrypt(&data, &mac.read().identity.sign_key().unwrap())
|
||||
.unwrap();
|
||||
|
||||
// TODO : check for recency
|
||||
println!("got raw: {}", String::from_utf8(dec.payload).unwrap());
|
||||
|
||||
match cat {
|
||||
"online" => {
|
||||
log::info!("Device {client} reported ONLINE");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_msg<T: Serialize>(client: &AsyncClient, machine: &Model<Machine>, request: T) {
|
||||
let data = serde_json::to_string(&request).unwrap();
|
||||
let pk = &machine.read().identity;
|
||||
let rec = pk.enc_key().unwrap();
|
||||
let machine_id = machine.read().id.to_string().replace("-", "");
|
||||
|
||||
let payload = crate::IDENTITY
|
||||
.get()
|
||||
.unwrap()
|
||||
.encrypt(data.as_bytes(), &rec);
|
||||
let topic = format!("{machine_id}/cmd");
|
||||
|
||||
client
|
||||
.publish(topic, rumqttc::QoS::AtMostOnce, true, payload)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn listen_to_device(client: &AsyncClient, machine_id: &str) {
|
||||
// Online Presence
|
||||
client
|
||||
.subscribe(format!("{machine_id}/online"), rumqttc::QoS::AtMostOnce)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn listen_to_devices(client: &AsyncClient) {
|
||||
let machines: Vec<Model<Machine>> = query!(|_| true);
|
||||
for machine in machines {
|
||||
let machine_id = machine.read().id.to_string();
|
||||
let machine_id = machine_id.trim().replace("-", "");
|
||||
log::info!("Sub to {machine_id}");
|
||||
|
||||
listen_to_device(client, &machine_id).await;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue