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:
Keagan McClelland
2021-08-19 11:42:17 -06:00
committed by Aiden McClelland
parent 46fc276822
commit da5a0d622b
6 changed files with 57 additions and 2 deletions

1
appmgr/Cargo.lock generated
View File

@@ -798,6 +798,7 @@ dependencies = [
"fd-lock-rs",
"futures",
"git-version",
"hex",
"http",
"hyper-ws-listener",
"indexmap",

View File

@@ -61,6 +61,7 @@ emver = { version = "0.1.2", features = ["serde"] }
fd-lock-rs = "0.1.3"
futures = "0.3.8"
git-version = "0.3.4"
hex = "0.4.3"
http = "0.2.3"
hyper-ws-listener = { git = "https://github.com/Start9Labs/hyper-ws-listener.git", branch = "main" }
indexmap = { version = "1.6.2", features = ["serde"] }

View File

@@ -4,6 +4,9 @@ async fn inner_main() -> Result<(), Error> {
// os sync
embassy::volume::disk::mount("/dev/sda", "/mnt/embassy-os-crypt").await?;
// hostname-set
embassy::hostname::sync_hostname().await?;
Ok(())
}

36
appmgr/src/hostname.rs Normal file
View 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(())
}

View File

@@ -26,6 +26,7 @@ pub mod db;
pub mod dependencies;
pub mod developer;
pub mod error;
pub mod hostname;
pub mod id;
pub mod inspect;
pub mod install;

View File

@@ -1,8 +1,8 @@
use std::collections::HashMap;
use avahi_sys::{
self, avahi_entry_group_commit, avahi_entry_group_free, avahi_entry_group_reset, avahi_free,
AvahiEntryGroup,
self, avahi_entry_group_add_service, avahi_entry_group_commit, avahi_entry_group_free,
avahi_entry_group_reset, avahi_free, AvahiEntryGroup,
};
use libc::c_void;
use patch_db::{DbHandle, OptionModel};
@@ -93,6 +93,19 @@ impl MdnsControllerInner {
let hostname_raw = avahi_sys::avahi_client_get_host_name_fqdn(avahi_client);
hostname_buf
.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);
}
let buflen = hostname_buf.len();