[cli] work on cli

This commit is contained in:
core 2022-12-30 19:20:43 -05:00
parent b642112b4d
commit 1f5a812262
Signed by: core
GPG Key ID: FDBF740DADDCEECF
2 changed files with 35 additions and 37 deletions

View File

@ -1,6 +1,15 @@
use std::error::Error;
use std::os::unix::net::UnixListener;
pub enum PacketThreadCommand { pub enum PacketThreadCommand {
UpdateConfig(UpdateConfigCommand), UpdateConfig(UpdateConfigCommand),
Stop Stop
} }
pub struct UpdateConfigCommand {} pub struct UpdateConfigCommand {}
pub fn command_handler(listener: UnixListener) -> Result<(), Box<dyn Error>> {
listener.set_nonblocking(true)?;
Ok(())
}

View File

@ -1,24 +1,20 @@
use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io;
use std::io::Read; use std::os::unix::net::{UnixListener};
use std::net::{Ipv4Addr, SocketAddr};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::Path;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::thread::{sleep, spawn}; use std::thread::{spawn};
use std::time::Duration;
use clap::Parser; use clap::Parser;
use daemonize::Daemonize; use daemonize::Daemonize;
use log::{error, info}; use log::{error, info};
use quicktap::cidr::{IpInet, Ipv4Inet};
use quicktap::drivers::tun::TunDevice; use quicktap::drivers::tun::TunDevice;
use quicktap::drivers::tungeneric::{GenericDriver, GenericInterface}; use quicktap::drivers::tungeneric::{GenericDriver, GenericInterface};
use crate::cli::Cli; use crate::cli::Cli;
use crate::command::PacketThreadCommand; use crate::command::{command_handler, PacketThreadCommand};
use crate::pproc::packet_thread; use crate::pproc::packet_thread;
pub mod cli; pub mod cli;
@ -65,7 +61,7 @@ fn main() {
info!("Creating unconfigured device"); info!("Creating unconfigured device");
let mut device = match TunDevice::new(&GenericInterface { let device = match TunDevice::new(&GenericInterface {
addresses: vec![], addresses: vec![],
mtu: None, mtu: None,
name: args.interface_name.clone(), name: args.interface_name.clone(),
@ -77,23 +73,6 @@ fn main() {
} }
}; };
info!("Device created; opening listening socket");
let mut config_listener = match UnixListener::bind(format!("/var/run/{}.sock", args.interface_name)) {
Ok(s) => s,
Err(e) => {
error!("Failed to open listen socket /var/run/{}.sock: {}", args.interface_name, e);
std::process::exit(3);
}
};
match config_listener.set_nonblocking(true) {
Ok(_) => (),
Err(e) => {
error!("Failed to set stream as nonblocking: {}", e);
std::process::exit(4);
}
};
info!("Spawning packet processor thread"); info!("Spawning packet processor thread");
let (tx, rx) = channel::<PacketThreadCommand>(); let (tx, rx) = channel::<PacketThreadCommand>();
@ -107,7 +86,19 @@ fn main() {
} }
}); });
sleep(Duration::from_secs(10)); info!("Creating command listener");
match UnixListener::bind(format!("/var/run/{}.sock", args.interface_name)) {
Ok(l) => {
// handle l
match command_handler(l) {
Ok(_) => (),
Err(e) => error!("Error in command handler: {}", e)
}
},
Err(e) => {
error!("Error creating command listener {}: {}", format!("/var/run/{}.sock", args.interface_name), e);
}
};
match tx.send(PacketThreadCommand::Stop) { match tx.send(PacketThreadCommand::Stop) {
Ok(_) => (), Ok(_) => (),
@ -128,13 +119,11 @@ fn main() {
info!("Cleaning up"); info!("Cleaning up");
if Path::new(&format!("/var/run/{}.sock", args.interface_name)).exists() { match std::fs::remove_file(format!("/var/run/{}.sock", args.interface_name)) {
match std::fs::remove_file(format!("/var/run/{}.sock", args.interface_name)) { Ok(_) => (),
Ok(_) => (), Err(e) => {
Err(e) => { error!("Failed to remove existing socket: {}", e);
error!("Failed to remove existing socket: {}", e); std::process::exit(7);
std::process::exit(7);
}
} }
} }