#![cfg_attr(windows, windows_subsystem = "windows")]
use std::process::ExitCode;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use getrandom::SysRng;
use getrandom::rand_core::UnwrapErr;
use nibkeep_node::at_login::{self, ENGINE_VERSION, LoginEntry, SERVICE_PROGRAM, ServiceOptions};
use nibkeep_node::{BackgroundOptions, DEFAULT_PORT, NetworkOptions};
fn main() -> ExitCode {
let mut arguments = std::env::args_os().skip(1);
match arguments.next() {
None => {}
Some(argument) if argument == "--version" && arguments.next().is_none() => {
println!("{SERVICE_PROGRAM} {ENGINE_VERSION}");
return ExitCode::SUCCESS;
}
Some(_) => {
eprintln!("usage: {SERVICE_PROGRAM} [--version]");
return ExitCode::from(2);
}
}
nibkeep_node::name_program(SERVICE_PROGRAM);
let (Some(settings), Ok(program)) = (at_login::settings_folder(), std::env::current_exe())
else {
eprintln!("{SERVICE_PROGRAM}: there is no home folder here");
return ExitCode::FAILURE;
};
let stop = Arc::new(AtomicBool::new(false));
{
let stop = Arc::clone(&stop);
let _ = ctrlc::set_handler(move || stop.store(true, Ordering::SeqCst));
}
let network = NetworkOptions {
discovery: true,
port: Some(DEFAULT_PORT),
accept: true,
};
let mut options =
ServiceOptions::new(program, BackgroundOptions::new(SERVICE_PROGRAM, network));
options.entry = LoginEntry::platform();
if !at_login::app::kept_away() {
options.locked = keystore_locked();
}
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "ios", target_os = "android"))
))]
linux::end_with_session(Arc::clone(&stop));
match at_login::serve_at_login(&settings, &options, now, || UnwrapErr(SysRng), &stop) {
Ok(_) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("{SERVICE_PROGRAM}: {error}");
ExitCode::FAILURE
}
}
}
const KEYSTORE_LOOK: Duration = Duration::from_secs(5);
fn keystore_locked() -> Arc<at_login::KeyringLocked> {
let ready = Arc::new(AtomicBool::new(nibkeep_cli::platform_keystore()));
if !ready.load(Ordering::SeqCst) {
let ready = Arc::clone(&ready);
std::thread::spawn(move || {
while !nibkeep_cli::platform_keystore() {
std::thread::sleep(KEYSTORE_LOOK);
}
ready.store(true, Ordering::SeqCst);
});
}
Arc::new(move |service, account| {
if !ready.load(Ordering::SeqCst) {
return true;
}
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "ios", target_os = "android"))
))]
return linux::keyring_locked(service, account);
#[cfg(not(all(
unix,
not(any(target_os = "macos", target_os = "ios", target_os = "android"))
)))]
{
let _ = (service, account);
false
}
})
}
fn now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |since| {
u64::try_from(since.as_millis()).unwrap_or(u64::MAX)
})
}
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "ios", target_os = "android"))
))]
mod linux {
use std::collections::HashMap;
use std::ffi::OsString;
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use secret_service::EncryptionType;
use secret_service::blocking::SecretService;
const SESSION_LOOK: Duration = Duration::from_secs(10);
pub(super) fn keyring_locked(service: &str, account: &str) -> bool {
let Ok(secrets) = SecretService::connect(EncryptionType::Plain) else {
return false;
};
let attributes = HashMap::from([("service", service), ("username", account)]);
secrets
.search_items(attributes)
.is_ok_and(|found| found.unlocked.is_empty() && !found.locked.is_empty())
}
const GONE_AFTER: u32 = 3;
pub(super) fn end_with_session(stop: Arc<AtomicBool>) {
let Some(session) = std::env::var_os("XDG_SESSION_ID").filter(|id| !id.is_empty()) else {
return;
};
std::thread::spawn(move || {
let (mut known, mut unknown) = (false, 0);
while !stop.load(Ordering::SeqCst) {
match state(&session) {
Err(()) => return,
Ok(Some(state)) if state == "closing" => break,
Ok(Some(_)) => (known, unknown) = (true, 0),
Ok(None) => unknown += 1,
}
if known && unknown >= GONE_AFTER {
break;
}
std::thread::sleep(SESSION_LOOK);
}
stop.store(true, Ordering::SeqCst);
});
}
fn state(session: &OsString) -> Result<Option<String>, ()> {
let shown = Command::new("loginctl")
.arg("show-session")
.arg(session)
.args(["--property=State", "--value"])
.stdin(Stdio::null())
.stderr(Stdio::null())
.output()
.map_err(|_| ())?;
Ok(shown
.status
.success()
.then(|| String::from_utf8_lossy(&shown.stdout).trim().to_owned()))
}
}