Skip to content

Commit

Permalink
feat: add error handling lib
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyYe committed Mar 4, 2024
1 parent e21179a commit a79c5bd
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions knock-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.80"
clap = { version = "4.5.1", features = ["derive"] }
log = "0.4.21"
pretty_env_logger = "0.5.0"
Expand Down
3 changes: 2 additions & 1 deletion knock-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::Result;
use std::fs::File;
use std::io::Read;

pub use config::Config;
pub use config::Rule;
pub mod config;

pub fn load_config(path: &str) -> Result<Config, Box<dyn std::error::Error>> {
pub fn load_config(path: &str) -> Result<Config> {
let mut file = File::open(path)?;
let mut content = String::new();

Expand Down
3 changes: 2 additions & 1 deletion knock-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use clap::Parser;
use log::{error, LevelFilter};
use pretty_env_logger::env_logger::Builder;
Expand All @@ -16,7 +17,7 @@ struct Args {
rule: Option<String>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() -> Result<()> {
let args = Args::parse();

Builder::new()
Expand Down
3 changes: 2 additions & 1 deletion knock-cli/src/rule.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::config::Config;
use crate::config::Rule;
use anyhow::Result;
use log::{error, info};
use std::collections::HashMap;
use std::net::{SocketAddr, TcpStream, ToSocketAddrs};
Expand All @@ -20,7 +21,7 @@ impl RuleExecutor {
RuleExecutor { rules }
}

pub fn run(&self, name: &str) -> Result<(), Box<dyn std::error::Error>> {
pub fn run(&self, name: &str) -> Result<()> {
if let Some(rule) = self.rules.get(name) {
info!("Executing rule: {}", rule.name);
// Iterate over the ports and attempt to connect to each
Expand Down
1 change: 1 addition & 0 deletions knockd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.80"
clap = { version = "4.5.1", features = ["derive"] }
log = "0.4.21"
pnet = "0.34.0"
Expand Down
3 changes: 2 additions & 1 deletion knockd/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Result;
use std::fs::File;
use std::io::Read;

pub use config::Config;
pub mod config;

pub fn load_config(path: &str) -> Result<Config, Box<dyn std::error::Error>> {
pub fn load_config(path: &str) -> Result<Config> {
let mut file = File::open(path)?;
let mut content = String::new();

Expand Down
3 changes: 2 additions & 1 deletion knockd/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use std::process::Command;

pub fn execute_command(command: &str) -> Result<(), Box<dyn std::error::Error>> {
pub fn execute_command(command: &str) -> Result<()> {
let mut parts = command.split_whitespace();
let command = parts.next().unwrap();
let args = parts;
Expand Down
3 changes: 2 additions & 1 deletion knockd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use clap::Parser;
use log::LevelFilter;
use pretty_env_logger::env_logger::Builder;
Expand All @@ -17,7 +18,7 @@ struct Args {
config: String,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() -> Result<()> {
let args = Args::parse();

// Initialize the logger
Expand Down
3 changes: 1 addition & 2 deletions knockd/src/server/server.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
extern crate pnet;

use crate::sequence::SequenceDetector;
use pnet::datalink::Channel::Ethernet;
use pnet::datalink::{self, NetworkInterface};
use pnet::packet::ethernet::{EtherTypes, EthernetPacket};
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::packet::tcp::TcpPacket;
use pnet::packet::Packet;

use crate::sequence::SequenceDetector;

pub struct Server {
interface_name: String,
detector: Box<dyn SequenceDetector>,
Expand Down

0 comments on commit a79c5bd

Please sign in to comment.