trifid/tfclient/src/main.rs

119 lines
3.6 KiB
Rust
Raw Normal View History

2023-02-27 20:50:31 -05:00
// tfclient, an open source client for the Defined Networking nebula management protocol.
// Copyright (C) 2023 c0repwn3r
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-05-14 13:47:49 -04:00
pub mod apiworker;
pub mod config;
pub mod daemon;
pub mod dirs;
#[cfg(feature = "linked-nebula")]
pub mod nebulaworker;
#[cfg(not(feature = "linked-nebula"))]
#[path = "nebulaworker_inert.rs"]
pub mod nebulaworker;
2023-03-28 12:16:00 -04:00
pub mod socketclient;
2023-05-14 13:47:49 -04:00
pub mod socketworker;
2023-03-29 09:37:03 -04:00
pub mod timerworker;
2023-05-14 13:47:49 -04:00
pub mod util;
2023-03-28 12:16:00 -04:00
use crate::config::load_config;
use clap::{Parser, Subcommand};
2023-05-14 13:47:49 -04:00
use log::{error, info};
use simple_logger::SimpleLogger;
2023-03-20 11:20:39 -04:00
#[derive(Parser)]
#[command(author = "c0repwn3r", version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
2023-05-14 13:47:49 -04:00
subcommand: Commands,
}
2023-03-20 11:20:39 -04:00
#[derive(Subcommand)]
enum Commands {
/// Run the tfclient daemon in the foreground
Run {
#[clap(short, long, default_value = "tfclient")]
/// Service name specified on install
name: String,
#[clap(short, long)]
/// Server to use for API calls.
2023-05-14 13:47:49 -04:00
server: String,
},
2023-11-18 22:51:45 -05:00
/// Enroll this host using a trifid-api-old enrollment code
Enroll {
#[clap(short, long, default_value = "tfclient")]
/// Service name specified on install
name: String,
#[clap(short, long)]
/// Enrollment code used to enroll this node
code: String,
2023-03-30 17:34:00 -04:00
},
/// Manually trigger a config update. It's useful to use this in combination with the `disable_automatic_config_updates` option.
Update {
#[clap(short, long, default_value = "tfclient")]
/// Service name specified on install
name: String,
2023-05-14 13:47:49 -04:00
},
2023-03-20 11:20:39 -04:00
}
2023-02-02 18:38:39 -05:00
fn main() {
SimpleLogger::new().init().unwrap();
2023-03-20 11:20:39 -04:00
let args = Cli::parse();
match args.subcommand {
Commands::Run { name, server } => {
daemon::daemon_main(name, server);
}
2023-03-28 12:16:00 -04:00
Commands::Enroll { name, code } => {
info!("Loading config...");
let config = match load_config(&name) {
Ok(cfg) => cfg,
Err(e) => {
error!("Error loading configuration: {}", e);
std::process::exit(1);
}
};
match socketclient::enroll(&code, &config) {
Ok(_) => (),
Err(e) => {
error!("Error sending enrollment request: {}", e);
std::process::exit(1);
}
};
2023-05-14 13:47:49 -04:00
}
2023-03-30 17:34:00 -04:00
Commands::Update { name } => {
info!("Loading config...");
let config = match load_config(&name) {
Ok(cfg) => cfg,
Err(e) => {
error!("Error loading configuration: {}", e);
std::process::exit(1);
}
};
match socketclient::update(&config) {
Ok(_) => (),
Err(e) => {
error!("Error sending update request: {}", e);
std::process::exit(1);
}
};
2023-03-28 12:16:00 -04:00
}
}
2023-11-23 15:23:52 -05:00
}