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)]
|
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 cfg_if::*;
|
2022-07-07 03:15:51 +00:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
use color_eyre::eyre::{bail, ensure, eyre, Result as EyreResult, WrapErr};
|
2022-01-15 23:24:37 +00:00
|
|
|
use server::*;
|
2022-06-28 03:46:29 +00:00
|
|
|
use tools::*;
|
2022-06-10 21:07:10 +00:00
|
|
|
use tracing::*;
|
2022-01-15 23:24:37 +00:00
|
|
|
use veilid_logs::*;
|
2021-11-22 16:28:30 +00:00
|
|
|
|
2021-11-29 01:08:50 +00:00
|
|
|
#[allow(clippy::all)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub mod veilid_client_capnp {
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/proto/veilid_client_capnp.rs"));
|
|
|
|
}
|
|
|
|
|
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 ---
|
|
|
|
if matches.occurrences_of("generate-dht-key") != 0 {
|
2022-01-15 23:24:37 +00:00
|
|
|
let (key, secret) = veilid_core::generate_secret();
|
|
|
|
println!("Public: {}\nSecret: {}", key.encode(), secret.encode());
|
|
|
|
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 ---
|
|
|
|
ctrlc::set_handler(move || {
|
|
|
|
shutdown();
|
|
|
|
})
|
|
|
|
.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
|
|
|
}
|