From e0141d1c91db718e09d319c4a256b7125d0cea96 Mon Sep 17 00:00:00 2001 From: Ebrahim Shafiei Date: Sat, 6 Jul 2024 18:41:24 +0330 Subject: [PATCH] ver 1.3 --- Cargo.toml | 2 ++ compile-win.bat | 8 ++++++ compile.bat | 8 ++++++ src/main.rs | 74 +++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 compile-win.bat create mode 100644 compile.bat diff --git a/Cargo.toml b/Cargo.toml index 5b4675b..440e346 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] +netstat = "0.7" tokio = { version = "1", features = ["full"] } hyper = { version = "0.14", features = ["full"] } futures-util = "0.3.30" + diff --git a/compile-win.bat b/compile-win.bat new file mode 100644 index 0000000..0e2213e --- /dev/null +++ b/compile-win.bat @@ -0,0 +1,8 @@ +@echo off + +set CURRENT_DIR=%CD% + +cd /d %CURRENT_DIR% + + +cargo build --release \ No newline at end of file diff --git a/compile.bat b/compile.bat new file mode 100644 index 0000000..0e2213e --- /dev/null +++ b/compile.bat @@ -0,0 +1,8 @@ +@echo off + +set CURRENT_DIR=%CD% + +cd /d %CURRENT_DIR% + + +cargo build --release \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1303ff9..5527646 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,11 @@ ********************************************************************** * ------------------------------------------------------------------- * Project Name : Abdal Fake Web Server - * File Name : sds.rs + * File Name : main.rs * Author : Ebrahim Shafiei (EbraSha) * Email : Prof.Shafiei@Gmail.com * Created On : 2024-07-06 - * Description : [A brief description of what this file does] + * Description : A powerful and fast software for reverse engineering web requests and analyzing their content * ------------------------------------------------------------------- * * "Coding is an engaging and beloved hobby for me. I passionately and insatiably pursue knowledge in cybersecurity and programming." @@ -18,37 +18,87 @@ use futures_util::TryFutureExt; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Request, Response, Server}; +use hyper::header::HeaderValue; use std::convert::Infallible; +use netstat::{get_sockets_info, ProtocolSocketInfo, AddressFamilyFlags, ProtocolFlags}; +use std::io::{self}; + +// const WHITE: &str = "\x1b[37m"; +// const BLACK: &str = "\x1b[30m"; +// const RED: &str = "\x1b[31m"; +// const GREEN: &str = "\x1b[32m"; +// const YELLOW: &str = "\x1b[33m"; +// const BLUE: &str = "\x1b[34m"; +// const MAGENTA: &str = "\x1b[35m"; +const CYAN: &str = "\x1b[36m"; +const RESET: &str = "\x1b[0m"; async fn handle_request(req: Request) -> Result, Infallible> { - println!("Received request: {:?}", req); - println!(); - println!(); - Ok(Response::new(Body::from("Request received"))) + let request_str = format!("{:?}", req); + let colored_request_str = request_str.replace("uri:", &format!("{}uri:{}{}", CYAN, RESET, "")); + let colored_request_str = colored_request_str.replace("\"host\":", &format!("{}\"host\":{}{}", CYAN, RESET, "")); + + println!("{}", colored_request_str); + let mut response = Response::new(Body::from("Request received\nProgrammer: Ebrahim Shafiei (EbraSha)")); + response.headers_mut().insert("Server", HeaderValue::from_static("Abdal Fake Web Server 1.3")); + response.headers_mut().insert("x-programmer", HeaderValue::from_static("Ebrahim Shafiei (EbraSha)")); + response.headers_mut().insert("x-programmer-mail", HeaderValue::from_static("Prof.Shafiei@Gmail.com")); + response.headers_mut().insert("x-powered-by", HeaderValue::from_static("Abdal Security Group")); + Ok(response) } #[tokio::main] async fn main() { println!("==========================================="); - println!("Welcome to Abdal Fake Web Server ver 1.0"); + println!("Welcome to Abdal Fake Web Server ver 1.3"); println!("Programmer : Ebrahim Shafiei (EbraSha)"); println!("Email : Prof.Shafiei@Gmail.com"); println!("==========================================="); + + // Check if ports 80 or 443 are already in use + if is_port_in_use(80) { + eprintln!("Port 80 is already in use.Please stop the process and try again."); + wait_for_user(); + return; + } + + if is_port_in_use(443) { + eprintln!("Port 443 is already in use.Please stop the process and try again."); + wait_for_user(); + return; + } + if let Err(e) = run().await { eprintln!("Server error: {}", e); } } +fn is_port_in_use(port: u16) -> bool { + let sockets_info = get_sockets_info(AddressFamilyFlags::IPV4, ProtocolFlags::TCP).unwrap(); + for socket in sockets_info { + if let ProtocolSocketInfo::Tcp(tcp_info) = socket.protocol_socket_info { + if tcp_info.local_port == port { + return true; + } + } + } + false +} + +fn wait_for_user() { + println!("Press Enter to exit..."); + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed to read line"); +} + async fn run() -> Result<(), Box> { let addr = ([0, 0, 0, 0], 80).into(); - let make_svc = - make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) }); + let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) }); let server = Server::bind(&addr).serve(make_svc); let addr_ssl = ([0, 0, 0, 0], 443).into(); - let make_svc_ssl = - make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) }); + let make_svc_ssl = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) }); let server_ssl = Server::bind(&addr_ssl).serve(make_svc_ssl); println!("Listening on http://{} and https://{}", addr, addr_ssl); @@ -59,4 +109,4 @@ async fn run() -> Result<(), Box> { )?; Ok(()) -} \ No newline at end of file +}