veilid/veilid-server/src/main.rs

157 lines
4.9 KiB
Rust
Raw Normal View History

2021-11-22 16:28:30 +00:00
#![forbid(unsafe_code)]
2021-12-08 03:09:45 +00:00
#![deny(clippy::all)]
2021-12-09 21:11:52 +00:00
#![deny(unused_must_use)]
#![recursion_limit = "256"]
2021-11-22 16:28:30 +00:00
mod client_api;
2022-01-15 23:24:37 +00:00
mod cmdline;
mod server;
2021-11-22 16:28:30 +00:00
mod settings;
2022-06-28 03:46:29 +00:00
mod tools;
2022-01-15 23:24:37 +00:00
#[cfg(unix)]
mod unix;
mod veilid_logs;
#[cfg(windows)]
mod windows;
use server::*;
2023-06-03 22:33:27 +00:00
use std::collections::HashMap;
2023-03-03 15:55:31 +00:00
use std::str::FromStr;
2022-06-28 03:46:29 +00:00
use tools::*;
2022-01-15 23:24:37 +00:00
use veilid_logs::*;
2021-11-22 16:28:30 +00:00
2022-06-10 21:07:10 +00:00
#[instrument(err)]
2022-07-07 03:15:51 +00:00
fn main() -> EyreResult<()> {
2022-06-08 01:31:05 +00:00
#[cfg(windows)]
let _ = ansi_term::enable_ansi_support();
2022-07-07 03:15:51 +00:00
color_eyre::install()?;
2022-06-08 01:31:05 +00:00
2022-01-15 23:24:37 +00:00
let (settings, matches) = cmdline::process_command_line()?;
2021-11-22 16:28:30 +00:00
2022-01-15 23:24:37 +00:00
// --- Dump Config ---
if matches.occurrences_of("dump-config") != 0 {
return serde_yaml::to_writer(std::io::stdout(), &*settings.read())
2022-07-07 03:15:51 +00:00
.wrap_err("failed to write yaml");
2022-01-15 23:24:37 +00:00
}
2022-01-19 02:21:11 +00:00
// --- Generate DHT Key ---
2023-03-01 20:50:30 +00:00
if matches.occurrences_of("generate-key-pair") != 0 {
2023-03-03 15:55:31 +00:00
if let Some(ckstr) = matches.get_one::<String>("generate-key-pair") {
2023-05-29 19:24:57 +00:00
if ckstr == "" {
let mut tks = veilid_core::TypedKeySet::new();
let mut tss = veilid_core::TypedSecretSet::new();
for ck in veilid_core::VALID_CRYPTO_KINDS {
let tkp = veilid_core::Crypto::generate_keypair(ck)
.wrap_err("invalid crypto kind")?;
tks.add(veilid_core::TypedKey::new(tkp.kind, tkp.value.key));
tss.add(veilid_core::TypedSecret::new(tkp.kind, tkp.value.secret));
}
println!(
"Public Keys:\n{}\nSecret Keys:\n{}\n",
tks.to_string(),
tss.to_string()
);
} else {
let ck: veilid_core::CryptoKind =
veilid_core::FourCC::from_str(ckstr).wrap_err("couldn't parse crypto kind")?;
let tkp =
veilid_core::Crypto::generate_keypair(ck).wrap_err("invalid crypto kind")?;
println!("{}", tkp.to_string());
}
2023-03-03 15:55:31 +00:00
return Ok(());
} else {
bail!("missing crypto kind");
}
2021-11-22 16:28:30 +00:00
}
2023-06-03 22:33:27 +00:00
// -- Emit JSON-Schema --
if matches.occurrences_of("emit-schema") != 0 {
if let Some(esstr) = matches.value_of("emit-schema") {
let mut schemas = HashMap::<String, String>::new();
2023-06-06 23:09:29 +00:00
veilid_core::json_api::emit_schemas(&mut schemas);
2023-06-03 22:33:27 +00:00
if let Some(schema) = schemas.get(esstr) {
println!("{}", schema);
} else {
println!("Valid schemas:");
for s in schemas.keys() {
println!(" {}", s);
}
}
return Ok(());
}
}
2021-11-22 16:28:30 +00:00
2022-05-16 15:52:48 +00:00
// See if we're just running a quick command
let (server_mode, success, failure) = if matches.occurrences_of("set-node-id") != 0 {
(
ServerMode::ShutdownImmediate,
"Node Id and Secret set successfully",
"Failed to set Node Id and Secret",
)
} else if matches.occurrences_of("dump-txt-record") != 0 {
(ServerMode::DumpTXTRecord, "", "Failed to dump txt record")
} else {
(ServerMode::Normal, "", "")
};
// Handle non-normal server modes
if !matches!(server_mode, ServerMode::Normal) {
// run the server to set the node id and quit
2022-06-28 03:46:29 +00:00
return block_on(async {
2022-06-11 22:47:58 +00:00
// Init combined console/file logger
2022-07-01 16:13:52 +00:00
let veilid_logs = VeilidLogs::setup(settings.clone())?;
2022-06-11 22:47:58 +00:00
2022-07-01 16:13:52 +00:00
run_veilid_server(settings, server_mode, veilid_logs).await
2022-06-11 22:47:58 +00:00
})
.map(|v| {
println!("{}", success);
v
})
.map_err(|e| {
println!("{}", failure);
e
});
2022-05-16 15:52:48 +00:00
}
2022-01-15 23:24:37 +00:00
// --- Daemon Mode ----
2022-05-16 15:52:48 +00:00
if settings.read().daemon.enabled {
2022-01-15 23:24:37 +00:00
cfg_if! {
if #[cfg(windows)] {
2022-07-12 17:07:02 +00:00
return windows::run_service(settings, matches);
2022-01-15 23:24:37 +00:00
} else if #[cfg(unix)] {
return unix::run_daemon(settings, matches);
}
2021-11-22 16:28:30 +00:00
}
}
2022-01-15 23:24:37 +00:00
// --- Normal Startup ---
2022-11-03 15:28:29 +00:00
let panic_on_shutdown = matches.occurrences_of("panic") != 0;
2022-01-15 23:24:37 +00:00
ctrlc::set_handler(move || {
2022-11-03 15:28:29 +00:00
if panic_on_shutdown {
let orig_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
// invoke the default handler and exit the process
orig_hook(panic_info);
let backtrace = backtrace::Backtrace::new();
eprintln!("Backtrace:\n{:?}", backtrace);
std::process::exit(1);
}));
panic!("panic requested");
} else {
shutdown();
}
2022-01-15 23:24:37 +00:00
})
.expect("Error setting Ctrl-C handler");
// Run the server loop
2022-06-28 03:46:29 +00:00
block_on(async {
2022-06-11 22:47:58 +00:00
// Init combined console/file logger
2022-07-01 16:13:52 +00:00
let veilid_logs = VeilidLogs::setup(settings.clone())?;
2022-06-11 22:47:58 +00:00
2022-07-01 16:13:52 +00:00
run_veilid_server(settings, server_mode, veilid_logs).await
2022-06-11 22:47:58 +00:00
})
2021-11-22 16:28:30 +00:00
}