From cfcd240f6f9d3446ba24647a52b259446c514842 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Sun, 19 May 2024 12:15:47 +0100 Subject: [PATCH] feat: basic port testing functionality using `nc` --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 22 ++++++++++++++++++---- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c1102a0..bbd6c28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,6 +93,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "owo-colors" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f" + [[package]] name = "pinger" version = "1.1.1" @@ -194,6 +200,7 @@ checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" name = "sage" version = "0.1.0" dependencies = [ + "owo-colors", "pinger", ] diff --git a/Cargo.toml b/Cargo.toml index 12bf030..6f6699b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +owo-colors = "4.0.0" pinger = "1.1.1" diff --git a/src/main.rs b/src/main.rs index bad942a..4d39bfb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,24 @@ use std::io::prelude::*; -use std::net::TcpStream; +use std::process::Command; + +use owo_colors::OwoColorize; fn main() -> std::io::Result<()> { - let mut stream = TcpStream::connect("100.64.0.2")?; + let ports = [80, 443, 8448]; + for port in ports { + let netcat = Command::new("nc") + .arg("-vz") + .arg("devraza.giize.com") + .arg(format!("{}", port)) + .output()?; - stream.write(&[1])?; - stream.read(&mut [0; 128])?; + let splitted = std::str::from_utf8(&netcat.stderr[..]).unwrap().split(" "); + let collection = &splitted.collect::>(); + if collection[7] == "succeeded!\n" { + println!("{} {} {}", "✓".green(), collection[2], collection[4]); + } else { + println!("{} {} {}", "✕".red(), collection[3], collection[6]); + } + } Ok(()) }