batches all lan addresses together

removes dbg

fixes clap docs

use actual log

removes service level enabling and disabling of lan

adds reset endpoint

reset lan on install/uninstall
This commit is contained in:
Keagan McClelland
2021-02-25 16:52:16 -07:00
committed by Aiden McClelland
parent 6585d91816
commit 653961da64
10 changed files with 99 additions and 84 deletions

View File

@@ -7,20 +7,10 @@ pub struct AppId {
pub un_app_id: String,
}
pub async fn enable_lan(app_id: &AppId) -> Result<(), Error> {
let tor_address = crate::apps::info(&app_id.un_app_id).await?.tor_address;
let lan_address = tor_address
.as_ref()
.ok_or_else(|| {
failure::format_err!("Service {} does not have Tor Address", app_id.un_app_id)
})?
.strip_suffix(".onion")
.ok_or_else(|| failure::format_err!("Invalid Tor Address: {:?}", tor_address))?
.to_owned()
+ ".local";
let lan_address_ptr =
std::ffi::CString::new(lan_address).expect("Could not cast lan address to c string");
pub async fn enable_lan() -> Result<(), Error> {
unsafe {
let app_list = crate::apps::list_info().await?;
let simple_poll = avahi_sys::avahi_simple_poll_new();
let poll = avahi_sys::avahi_simple_poll_get(simple_poll);
let mut stack_err = 0;
@@ -35,7 +25,7 @@ pub async fn enable_lan(app_id: &AppId) -> Result<(), Error> {
let group =
avahi_sys::avahi_entry_group_new(avahi_client, Some(noop), std::ptr::null_mut());
let hostname_raw = avahi_sys::avahi_client_get_host_name_fqdn(avahi_client);
let hostname_bytes = dbg!(std::ffi::CStr::from_ptr(hostname_raw)).to_bytes_with_nul();
let hostname_bytes = std::ffi::CStr::from_ptr(hostname_raw).to_bytes_with_nul();
const HOSTNAME_LEN: usize = 1 + 15 + 1 + 5; // leading byte, main address, dot, "local"
debug_assert_eq!(hostname_bytes.len(), HOSTNAME_LEN);
let mut hostname_buf = [0; HOSTNAME_LEN + 1];
@@ -43,22 +33,36 @@ pub async fn enable_lan(app_id: &AppId) -> Result<(), Error> {
// assume fixed length prefix on hostname due to local address
hostname_buf[0] = 15; // set the prefix length to 15 for the main address
hostname_buf[16] = 5; // set the prefix length to 5 for "local"
dbg!(hostname_buf);
let _ = avahi_sys::avahi_entry_group_add_record(
group,
avahi_sys::AVAHI_IF_UNSPEC,
avahi_sys::AVAHI_PROTO_UNSPEC,
avahi_sys::AvahiPublishFlags_AVAHI_PUBLISH_USE_MULTICAST
| avahi_sys::AvahiPublishFlags_AVAHI_PUBLISH_ALLOW_MULTIPLE,
lan_address_ptr.as_ptr(),
avahi_sys::AVAHI_DNS_CLASS_IN as u16,
avahi_sys::AVAHI_DNS_TYPE_CNAME as u16,
avahi_sys::AVAHI_DEFAULT_TTL,
hostname_buf.as_ptr().cast(),
hostname_buf.len(),
);
for (app_id, app_info) in app_list {
let tor_address = app_info.tor_address;
let lan_address = tor_address
.as_ref()
.ok_or_else(|| {
failure::format_err!("Service {} does not have Tor Address", app_id)
})?
.strip_suffix(".onion")
.ok_or_else(|| failure::format_err!("Invalid Tor Address: {:?}", tor_address))?
.to_owned()
+ ".local";
let lan_address_ptr = std::ffi::CString::new(lan_address)
.expect("Could not cast lan address to c string");
let _ = avahi_sys::avahi_entry_group_add_record(
group,
avahi_sys::AVAHI_IF_UNSPEC,
avahi_sys::AVAHI_PROTO_UNSPEC,
avahi_sys::AvahiPublishFlags_AVAHI_PUBLISH_USE_MULTICAST
| avahi_sys::AvahiPublishFlags_AVAHI_PUBLISH_ALLOW_MULTIPLE,
lan_address_ptr.as_ptr(),
avahi_sys::AVAHI_DNS_CLASS_IN as u16,
avahi_sys::AVAHI_DNS_TYPE_CNAME as u16,
avahi_sys::AVAHI_DEFAULT_TTL,
hostname_buf.as_ptr().cast(),
hostname_buf.len(),
);
log::info!("Published {:?}", lan_address_ptr);
}
avahi_sys::avahi_entry_group_commit(group);
println!("{:?}", lan_address_ptr);
ctrlc::set_handler(move || {
// please the borrow checker with the below semantics
// avahi_sys::avahi_entry_group_free(group);

View File

@@ -463,6 +463,14 @@ async fn inner_main() -> Result<(), Error> {
)
.subcommand(SubCommand::with_name("reload").about("Reloads the tor configuration")),
)
.subcommand(
SubCommand::with_name("lan")
.about("Configures LAN services")
.subcommand(
SubCommand::with_name("enable")
.about("Publishes the LAN address for all services over avahi"),
),
)
.subcommand(
SubCommand::with_name("info")
.about("Prints information about an installed app")
@@ -1205,12 +1213,7 @@ async fn inner_main() -> Result<(), Error> {
#[cfg(feature = "avahi")]
#[cfg(not(feature = "portable"))]
("lan", Some(sub_m)) => match sub_m.subcommand() {
("enable", Some(sub_sub_m)) => {
crate::lan::enable_lan(&crate::lan::AppId {
un_app_id: sub_sub_m.value_of("ID").unwrap().to_owned(),
})
.await?
}
("enable", _) => crate::lan::enable_lan().await?,
_ => {
println!("{}", sub_m.usage());
std::process::exit(1);