mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 20:14:49 +00:00
adds avahi base service to init function of mdns controller (#407)
* adds avahi base service to init function of mdns controller * Apply suggestions from code review * adds hostname set to startup sequence * Apply suggestions from code review Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com>
This commit is contained in:
committed by
Aiden McClelland
parent
46fc276822
commit
da5a0d622b
1
appmgr/Cargo.lock
generated
1
appmgr/Cargo.lock
generated
@@ -798,6 +798,7 @@ dependencies = [
|
|||||||
"fd-lock-rs",
|
"fd-lock-rs",
|
||||||
"futures",
|
"futures",
|
||||||
"git-version",
|
"git-version",
|
||||||
|
"hex",
|
||||||
"http",
|
"http",
|
||||||
"hyper-ws-listener",
|
"hyper-ws-listener",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ emver = { version = "0.1.2", features = ["serde"] }
|
|||||||
fd-lock-rs = "0.1.3"
|
fd-lock-rs = "0.1.3"
|
||||||
futures = "0.3.8"
|
futures = "0.3.8"
|
||||||
git-version = "0.3.4"
|
git-version = "0.3.4"
|
||||||
|
hex = "0.4.3"
|
||||||
http = "0.2.3"
|
http = "0.2.3"
|
||||||
hyper-ws-listener = { git = "https://github.com/Start9Labs/hyper-ws-listener.git", branch = "main" }
|
hyper-ws-listener = { git = "https://github.com/Start9Labs/hyper-ws-listener.git", branch = "main" }
|
||||||
indexmap = { version = "1.6.2", features = ["serde"] }
|
indexmap = { version = "1.6.2", features = ["serde"] }
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ async fn inner_main() -> Result<(), Error> {
|
|||||||
// os sync
|
// os sync
|
||||||
embassy::volume::disk::mount("/dev/sda", "/mnt/embassy-os-crypt").await?;
|
embassy::volume::disk::mount("/dev/sda", "/mnt/embassy-os-crypt").await?;
|
||||||
|
|
||||||
|
// hostname-set
|
||||||
|
embassy::hostname::sync_hostname().await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
appmgr/src/hostname.rs
Normal file
36
appmgr/src/hostname.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
use digest::Digest;
|
||||||
|
use tokio::process::Command;
|
||||||
|
|
||||||
|
use crate::util::Invoke;
|
||||||
|
use crate::{Error, ErrorKind};
|
||||||
|
|
||||||
|
pub async fn get_hostname() -> Result<String, Error> {
|
||||||
|
let out = Command::new("hostname")
|
||||||
|
.invoke(ErrorKind::ParseSysInfo)
|
||||||
|
.await?;
|
||||||
|
let out_string = String::from_utf8(out)?;
|
||||||
|
Ok(out_string.trim().to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_hostname(hostname: &str) -> Result<(), Error> {
|
||||||
|
let _out = Command::new("hostnamectl")
|
||||||
|
.arg("set-hostname")
|
||||||
|
.arg(hostname)
|
||||||
|
.invoke(ErrorKind::ParseSysInfo)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_product_key() -> Result<String, Error> {
|
||||||
|
let out = tokio::fs::read_to_string("/boot/product_key.txt").await?;
|
||||||
|
Ok(out.trim().to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn sync_hostname() -> Result<(), Error> {
|
||||||
|
let key = get_product_key().await?;
|
||||||
|
let mut hasher = sha2::Sha256::new();
|
||||||
|
hasher.update(key.as_bytes());
|
||||||
|
let res = hasher.finalize();
|
||||||
|
set_hostname(&format!("start9-{}", hex::encode(&res[0..4]))).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ pub mod db;
|
|||||||
pub mod dependencies;
|
pub mod dependencies;
|
||||||
pub mod developer;
|
pub mod developer;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod hostname;
|
||||||
pub mod id;
|
pub mod id;
|
||||||
pub mod inspect;
|
pub mod inspect;
|
||||||
pub mod install;
|
pub mod install;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use avahi_sys::{
|
use avahi_sys::{
|
||||||
self, avahi_entry_group_commit, avahi_entry_group_free, avahi_entry_group_reset, avahi_free,
|
self, avahi_entry_group_add_service, avahi_entry_group_commit, avahi_entry_group_free,
|
||||||
AvahiEntryGroup,
|
avahi_entry_group_reset, avahi_free, AvahiEntryGroup,
|
||||||
};
|
};
|
||||||
use libc::c_void;
|
use libc::c_void;
|
||||||
use patch_db::{DbHandle, OptionModel};
|
use patch_db::{DbHandle, OptionModel};
|
||||||
@@ -93,6 +93,19 @@ impl MdnsControllerInner {
|
|||||||
let hostname_raw = avahi_sys::avahi_client_get_host_name_fqdn(avahi_client);
|
let hostname_raw = avahi_sys::avahi_client_get_host_name_fqdn(avahi_client);
|
||||||
hostname_buf
|
hostname_buf
|
||||||
.extend_from_slice(std::ffi::CStr::from_ptr(hostname_raw).to_bytes_with_nul());
|
.extend_from_slice(std::ffi::CStr::from_ptr(hostname_raw).to_bytes_with_nul());
|
||||||
|
let http_tcp_cstr = std::ffi::CString::new("_http._tcp")
|
||||||
|
.expect("Could not cast _http._tcp to c string");
|
||||||
|
avahi_entry_group_add_service(
|
||||||
|
group,
|
||||||
|
avahi_sys::AVAHI_IF_UNSPEC,
|
||||||
|
avahi_sys::AVAHI_PROTO_UNSPEC,
|
||||||
|
avahi_sys::AvahiPublishFlags_AVAHI_PUBLISH_USE_MULTICAST,
|
||||||
|
hostname_raw,
|
||||||
|
http_tcp_cstr.as_ptr(),
|
||||||
|
std::ptr::null(),
|
||||||
|
std::ptr::null(),
|
||||||
|
443,
|
||||||
|
);
|
||||||
avahi_free(hostname_raw as *mut c_void);
|
avahi_free(hostname_raw as *mut c_void);
|
||||||
}
|
}
|
||||||
let buflen = hostname_buf.len();
|
let buflen = hostname_buf.len();
|
||||||
|
|||||||
Reference in New Issue
Block a user