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::*;
|
|
|
|
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-01-15 23:24:37 +00:00
|
|
|
fn main() -> Result<(), String> {
|
2022-06-08 01:31:05 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
let _ = ansi_term::enable_ansi_support();
|
|
|
|
|
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())
|
|
|
|
.map_err(|e| e.to_string());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
let _logs = VeilidLogs::setup(settings.clone())?;
|
|
|
|
|
|
|
|
run_veilid_server(settings, server_mode).await
|
|
|
|
})
|
|
|
|
.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)] {
|
|
|
|
return windows::run_service(settings, matches).map_err(|e| format!("{}", e));
|
|
|
|
} 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
|
|
|
|
let _logs = VeilidLogs::setup(settings.clone())?;
|
|
|
|
|
|
|
|
run_veilid_server(settings, server_mode).await
|
|
|
|
})
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|