forked from navos/sheepd
✨ mqtt connections
almost working, need encryption
This commit is contained in:
parent
20ab0153d1
commit
125d50530d
15 changed files with 1259 additions and 42 deletions
|
@ -1,37 +1,53 @@
|
|||
use crate::{api, sheepd_core::config::AgentConfig};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{
|
||||
api::{self, Identity, JoinResponse},
|
||||
sheepd_core::config::AgentConfig,
|
||||
};
|
||||
|
||||
use super::args::JoinCommand;
|
||||
|
||||
fn domain(host: &str) -> String {
|
||||
if host.starts_with("http") {
|
||||
return host.to_string();
|
||||
} else {
|
||||
format!("https://{host}")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn join(conf: JoinCommand) {
|
||||
// TODO : check for root
|
||||
// TODO : check if joined somewhere already
|
||||
|
||||
log::info!("Joining to {}", conf.home);
|
||||
|
||||
let url = format!("http://{}/join", conf.home);
|
||||
let _ = std::fs::create_dir_all("/etc/sheepd");
|
||||
|
||||
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 url = format!("{}/join", domain(&conf.home));
|
||||
println!("{url}");
|
||||
let mut res = ureq::post(url)
|
||||
.send_json(&api::JoinParams {
|
||||
join_token: None,
|
||||
machine_id: std::fs::read_to_string("/etc/machine-id").unwrap(),
|
||||
hostname: std::fs::read_to_string("/etc/hostname").unwrap(),
|
||||
identity: i.public(),
|
||||
})
|
||||
.unwrap();
|
||||
let res: serde_json::Value = res.body_mut().read_json().unwrap();
|
||||
|
||||
let token = res
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.get("ok")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap();
|
||||
let res: JoinResponse = res.body_mut().read_json().unwrap();
|
||||
|
||||
log::info!("Joined {} successfully", conf.home);
|
||||
|
||||
std::fs::write(
|
||||
"/etc/sheepd.toml",
|
||||
toml::to_string(&AgentConfig::new(&conf.home, token)).unwrap(),
|
||||
"/etc/sheepd/config.toml",
|
||||
toml::to_string(&AgentConfig::new(&conf.home, res)).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
|
|
@ -1,16 +1,28 @@
|
|||
use owl::{Deserialize, Serialize};
|
||||
|
||||
use crate::api::JoinResponse;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AgentConfig {
|
||||
pub home: String,
|
||||
pub token: String,
|
||||
pub mqtt: String,
|
||||
pub server_age: String,
|
||||
pub server_sign: String,
|
||||
}
|
||||
|
||||
impl AgentConfig {
|
||||
pub fn new(home_server: &str, token: &str) -> Self {
|
||||
pub fn try_load() -> Option<Self> {
|
||||
toml::from_str(&std::fs::read_to_string("/etc/sheepd/config.toml").ok()?).ok()
|
||||
}
|
||||
|
||||
pub fn new(home: &str, join: JoinResponse) -> Self {
|
||||
Self {
|
||||
home: home_server.to_string(),
|
||||
token: token.to_string(),
|
||||
token: join.token,
|
||||
mqtt: join.mqtt,
|
||||
home: home.to_string(),
|
||||
server_age: join.identity.0,
|
||||
server_sign: join.identity.1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue