Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Stuff #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
chrono = "0.4"
rand = "0.8"
libc = "0.2"
whoami = "1.4"
hostname = { version = "0.3.1", features = ["set"] }
clap = { version = "4.4", features = ["derive"] }

Expand Down
81 changes: 55 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

// #[cfg(target_os = "windows")]
// use ::windows;
#[allow(unused_imports)]
#[cfg(target_os = "windows")]
use fiche_rs::windows::am_i_root_windows;
// use std::ffi::c_long;
Expand Down Expand Up @@ -54,13 +55,13 @@
impl Default for FicheSettings {
fn default() -> Self {
FicheSettings {
domain: "example.com".to_string(),
domain: "localhost".to_string(),
output_dir: "code".to_string(),
listen_addr: "0.0.0.0".to_string(),
port: 9999,
slug_len: 4,
https: false,
buffer_len: 32768,
buffer_len: 32768, // 2 << 14
user_name: None,
log_file_path: None,
banlist_path: None,
Expand Down Expand Up @@ -311,24 +312,30 @@
}

/// Check if IP is banned
fn is_banned(connection: &FicheConnection) -> bool {
fn is_banned(connection: &FicheConnection) -> Result<bool, FicheError> {
if !connection.address.is_some() {
return Err(FicheError::from("No IP Address".to_string()));
}
if let Some(banlist_path) = &connection.settings.banlist_path {
let banlist = fs::read_to_string(banlist_path).unwrap();
let ip = connection.address.expect("No IP.").ip().to_string();
banlist.contains(&ip)
let banlist = fs::read_to_string(banlist_path)?;
let ip = connection.address.unwrap().ip().to_string();
Ok(banlist.contains(&ip))

Check warning on line 322 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L320-L322

Added lines #L320 - L322 were not covered by tests
} else {
false
Ok(false)
}
}

/// Check if IP is whitelisted
fn is_whitelisted(connection: &FicheConnection) -> bool {
fn is_whitelisted(connection: &FicheConnection) -> Result<bool, FicheError> {
if !connection.address.is_some() {
return Err(FicheError::from("No IP Address".to_string()));

Check warning on line 331 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L331

Added line #L331 was not covered by tests
}
if let Some(whitelist_path) = &connection.settings.whitelist_path {
let whitelist = fs::read_to_string(whitelist_path).unwrap();
let ip = connection.address.expect("No IP").ip().to_string();
whitelist.contains(&ip)
let whitelist = fs::read_to_string(whitelist_path)?;
let ip = connection.address.unwrap().ip().to_string();
Ok(whitelist.contains(&ip))

Check warning on line 336 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L334-L336

Added lines #L334 - L336 were not covered by tests
} else {
false
Ok(false)
}
}

Expand All @@ -347,7 +354,7 @@
));

// check if IP is banned
if is_banned(&connection) && !is_whitelisted(&connection) {
if is_banned(&connection)? && !is_whitelisted(&connection)? {

Check warning on line 357 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L357

Added line #L357 was not covered by tests
print_error(&format!("{} is banned!", ip));
return Err(FicheError::from("IP is banned!".to_string()));
}
Expand Down Expand Up @@ -562,24 +569,22 @@

/// Check if we're running as root
fn am_i_root() -> bool {
#[cfg(target_os = "windows")]
return am_i_root_windows();
#[cfg(not(target_os = "windows"))]
unsafe {
libc::getuid() == 0
}
whoami::username() == "root" || whoami::username() == "Administrator"
}

#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::{
net::{Ipv4Addr, SocketAddr},
sync::Arc,
};

use crate::{am_i_root, FicheSettings};

#[test]
fn test_fiche_settings_defaults() {
let default_settings = FicheSettings::default();
assert_eq!(default_settings.domain, "example.com");
assert_eq!(default_settings.domain, "localhost");
assert_eq!(default_settings.output_dir, "code");
assert_eq!(default_settings.listen_addr, "0.0.0.0");
assert_eq!(default_settings.port, 9999);
Expand Down Expand Up @@ -677,31 +682,55 @@
let mut settings = FicheSettings::default();
let _ = crate::set_host_name(&settings.domain);
crate::set_domain_name(&mut settings);
assert_eq!(settings.domain, "http://example.com");
assert_eq!(settings.domain, "http://localhost");
}

#[test]
fn test_is_banned() {
fn test_is_banned_err() {
let settings = Arc::new(FicheSettings::default());
// Handing a connection with no actual socket or connection should
// raise and error in these functions.
let connection = crate::FicheConnection {
socket: None,
address: None,
settings: settings.clone(),
};
let result = crate::is_banned(&connection);
assert_eq!(result, false);
assert!(result.is_err());
}

#[test]
fn test_is_banned() {
let settings = Arc::new(FicheSettings::default());
// Handing a connection with no actual socket or connection should
// raise and error in these functions.
let connection = crate::FicheConnection {
socket: None,
address: Some(SocketAddr::new(
std::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1).into()),
8080,
)),
settings: settings.clone(),
};
let result = crate::is_banned(&connection);
assert!(result.is_ok());
assert_eq!(result.unwrap(), false);
}

#[test]
fn test_is_whitelisted() {
let settings = Arc::new(FicheSettings::default());
let connection = crate::FicheConnection {
socket: None,
address: None,
address: Some(SocketAddr::new(
std::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1).into()),
8080,
)),
settings: settings.clone(),
};
let result = crate::is_whitelisted(&connection);
assert_eq!(result, false);
assert!(result.is_ok());
assert_eq!(result.unwrap(), false);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub fn get_user_sid_by_name(user_name: &str) -> Option<PSID> {
}

/// Check if the current process has administrative rights.
#[allow(unused)]
pub fn am_i_root_windows() -> bool {
unsafe {
// Open a handle to the current process
Expand Down
Loading