├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md └── src └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target/ -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "atty" 7 | version = "0.2.14" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 10 | dependencies = [ 11 | "hermit-abi", 12 | "libc", 13 | "winapi", 14 | ] 15 | 16 | [[package]] 17 | name = "autocfg" 18 | version = "1.1.0" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 21 | 22 | [[package]] 23 | name = "bitflags" 24 | version = "1.3.2" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 27 | 28 | [[package]] 29 | name = "bpaf" 30 | version = "0.7.4" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "c1d82edc4ffa19253b3a40a94d051613f18b6a5ee48f4e930599ee4c35ee0b74" 33 | dependencies = [ 34 | "bpaf_derive", 35 | "owo-colors", 36 | ] 37 | 38 | [[package]] 39 | name = "bpaf_derive" 40 | version = "0.3.3" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "5ed63113bfb3a9bb25dd131acdf0044c7404000ea57dd9eec1f221e3547b4748" 43 | dependencies = [ 44 | "proc-macro2", 45 | "quote", 46 | "syn", 47 | ] 48 | 49 | [[package]] 50 | name = "bytes" 51 | version = "1.2.1" 52 | source = "registry+https://github.com/rust-lang/crates.io-index" 53 | checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" 54 | 55 | [[package]] 56 | name = "cfg-if" 57 | version = "1.0.0" 58 | source = "registry+https://github.com/rust-lang/crates.io-index" 59 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 60 | 61 | [[package]] 62 | name = "hermit-abi" 63 | version = "0.1.19" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" 66 | dependencies = [ 67 | "libc", 68 | ] 69 | 70 | [[package]] 71 | name = "ip_sniffer" 72 | version = "0.2.0" 73 | dependencies = [ 74 | "bpaf", 75 | "tokio", 76 | ] 77 | 78 | [[package]] 79 | name = "is_ci" 80 | version = "1.1.1" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb" 83 | 84 | [[package]] 85 | name = "libc" 86 | version = "0.2.137" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" 89 | 90 | [[package]] 91 | name = "lock_api" 92 | version = "0.4.9" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" 95 | dependencies = [ 96 | "autocfg", 97 | "scopeguard", 98 | ] 99 | 100 | [[package]] 101 | name = "log" 102 | version = "0.4.17" 103 | source = "registry+https://github.com/rust-lang/crates.io-index" 104 | checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" 105 | dependencies = [ 106 | "cfg-if", 107 | ] 108 | 109 | [[package]] 110 | name = "memchr" 111 | version = "2.5.0" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" 114 | 115 | [[package]] 116 | name = "mio" 117 | version = "0.8.5" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" 120 | dependencies = [ 121 | "libc", 122 | "log", 123 | "wasi", 124 | "windows-sys", 125 | ] 126 | 127 | [[package]] 128 | name = "num_cpus" 129 | version = "1.14.0" 130 | source = "registry+https://github.com/rust-lang/crates.io-index" 131 | checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" 132 | dependencies = [ 133 | "hermit-abi", 134 | "libc", 135 | ] 136 | 137 | [[package]] 138 | name = "owo-colors" 139 | version = "3.5.0" 140 | source = "registry+https://github.com/rust-lang/crates.io-index" 141 | checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" 142 | dependencies = [ 143 | "supports-color", 144 | ] 145 | 146 | [[package]] 147 | name = "parking_lot" 148 | version = "0.12.1" 149 | source = "registry+https://github.com/rust-lang/crates.io-index" 150 | checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" 151 | dependencies = [ 152 | "lock_api", 153 | "parking_lot_core", 154 | ] 155 | 156 | [[package]] 157 | name = "parking_lot_core" 158 | version = "0.9.4" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | checksum = "4dc9e0dc2adc1c69d09143aff38d3d30c5c3f0df0dad82e6d25547af174ebec0" 161 | dependencies = [ 162 | "cfg-if", 163 | "libc", 164 | "redox_syscall", 165 | "smallvec", 166 | "windows-sys", 167 | ] 168 | 169 | [[package]] 170 | name = "pin-project-lite" 171 | version = "0.2.9" 172 | source = "registry+https://github.com/rust-lang/crates.io-index" 173 | checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" 174 | 175 | [[package]] 176 | name = "proc-macro2" 177 | version = "1.0.47" 178 | source = "registry+https://github.com/rust-lang/crates.io-index" 179 | checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" 180 | dependencies = [ 181 | "unicode-ident", 182 | ] 183 | 184 | [[package]] 185 | name = "quote" 186 | version = "1.0.21" 187 | source = "registry+https://github.com/rust-lang/crates.io-index" 188 | checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" 189 | dependencies = [ 190 | "proc-macro2", 191 | ] 192 | 193 | [[package]] 194 | name = "redox_syscall" 195 | version = "0.2.16" 196 | source = "registry+https://github.com/rust-lang/crates.io-index" 197 | checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" 198 | dependencies = [ 199 | "bitflags", 200 | ] 201 | 202 | [[package]] 203 | name = "scopeguard" 204 | version = "1.1.0" 205 | source = "registry+https://github.com/rust-lang/crates.io-index" 206 | checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" 207 | 208 | [[package]] 209 | name = "signal-hook-registry" 210 | version = "1.4.0" 211 | source = "registry+https://github.com/rust-lang/crates.io-index" 212 | checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" 213 | dependencies = [ 214 | "libc", 215 | ] 216 | 217 | [[package]] 218 | name = "smallvec" 219 | version = "1.10.0" 220 | source = "registry+https://github.com/rust-lang/crates.io-index" 221 | checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" 222 | 223 | [[package]] 224 | name = "socket2" 225 | version = "0.4.7" 226 | source = "registry+https://github.com/rust-lang/crates.io-index" 227 | checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" 228 | dependencies = [ 229 | "libc", 230 | "winapi", 231 | ] 232 | 233 | [[package]] 234 | name = "supports-color" 235 | version = "1.3.1" 236 | source = "registry+https://github.com/rust-lang/crates.io-index" 237 | checksum = "8ba6faf2ca7ee42fdd458f4347ae0a9bd6bcc445ad7cb57ad82b383f18870d6f" 238 | dependencies = [ 239 | "atty", 240 | "is_ci", 241 | ] 242 | 243 | [[package]] 244 | name = "syn" 245 | version = "1.0.103" 246 | source = "registry+https://github.com/rust-lang/crates.io-index" 247 | checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" 248 | dependencies = [ 249 | "proc-macro2", 250 | "quote", 251 | "unicode-ident", 252 | ] 253 | 254 | [[package]] 255 | name = "tokio" 256 | version = "1.22.0" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | checksum = "d76ce4a75fb488c605c54bf610f221cea8b0dafb53333c1a67e8ee199dcd2ae3" 259 | dependencies = [ 260 | "autocfg", 261 | "bytes", 262 | "libc", 263 | "memchr", 264 | "mio", 265 | "num_cpus", 266 | "parking_lot", 267 | "pin-project-lite", 268 | "signal-hook-registry", 269 | "socket2", 270 | "tokio-macros", 271 | "winapi", 272 | ] 273 | 274 | [[package]] 275 | name = "tokio-macros" 276 | version = "1.8.0" 277 | source = "registry+https://github.com/rust-lang/crates.io-index" 278 | checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" 279 | dependencies = [ 280 | "proc-macro2", 281 | "quote", 282 | "syn", 283 | ] 284 | 285 | [[package]] 286 | name = "unicode-ident" 287 | version = "1.0.5" 288 | source = "registry+https://github.com/rust-lang/crates.io-index" 289 | checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" 290 | 291 | [[package]] 292 | name = "wasi" 293 | version = "0.11.0+wasi-snapshot-preview1" 294 | source = "registry+https://github.com/rust-lang/crates.io-index" 295 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 296 | 297 | [[package]] 298 | name = "winapi" 299 | version = "0.3.9" 300 | source = "registry+https://github.com/rust-lang/crates.io-index" 301 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 302 | dependencies = [ 303 | "winapi-i686-pc-windows-gnu", 304 | "winapi-x86_64-pc-windows-gnu", 305 | ] 306 | 307 | [[package]] 308 | name = "winapi-i686-pc-windows-gnu" 309 | version = "0.4.0" 310 | source = "registry+https://github.com/rust-lang/crates.io-index" 311 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 312 | 313 | [[package]] 314 | name = "winapi-x86_64-pc-windows-gnu" 315 | version = "0.4.0" 316 | source = "registry+https://github.com/rust-lang/crates.io-index" 317 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 318 | 319 | [[package]] 320 | name = "windows-sys" 321 | version = "0.42.0" 322 | source = "registry+https://github.com/rust-lang/crates.io-index" 323 | checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" 324 | dependencies = [ 325 | "windows_aarch64_gnullvm", 326 | "windows_aarch64_msvc", 327 | "windows_i686_gnu", 328 | "windows_i686_msvc", 329 | "windows_x86_64_gnu", 330 | "windows_x86_64_gnullvm", 331 | "windows_x86_64_msvc", 332 | ] 333 | 334 | [[package]] 335 | name = "windows_aarch64_gnullvm" 336 | version = "0.42.0" 337 | source = "registry+https://github.com/rust-lang/crates.io-index" 338 | checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" 339 | 340 | [[package]] 341 | name = "windows_aarch64_msvc" 342 | version = "0.42.0" 343 | source = "registry+https://github.com/rust-lang/crates.io-index" 344 | checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" 345 | 346 | [[package]] 347 | name = "windows_i686_gnu" 348 | version = "0.42.0" 349 | source = "registry+https://github.com/rust-lang/crates.io-index" 350 | checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" 351 | 352 | [[package]] 353 | name = "windows_i686_msvc" 354 | version = "0.42.0" 355 | source = "registry+https://github.com/rust-lang/crates.io-index" 356 | checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" 357 | 358 | [[package]] 359 | name = "windows_x86_64_gnu" 360 | version = "0.42.0" 361 | source = "registry+https://github.com/rust-lang/crates.io-index" 362 | checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" 363 | 364 | [[package]] 365 | name = "windows_x86_64_gnullvm" 366 | version = "0.42.0" 367 | source = "registry+https://github.com/rust-lang/crates.io-index" 368 | checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" 369 | 370 | [[package]] 371 | name = "windows_x86_64_msvc" 372 | version = "0.42.0" 373 | source = "registry+https://github.com/rust-lang/crates.io-index" 374 | checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" 375 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ip_sniffer" 3 | version = "0.2.0" 4 | authors = ["tensor-programming "] 5 | edition = "2021" 6 | 7 | [dependencies] 8 | tokio = { version = "1", features = ["full"] } 9 | bpaf = {version = "0.7", features = ["derive", "bright-color"]} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust Port Sniffer 2 | 3 | # A port sniffer CLI made with pure STD rust. 4 | 5 | ## Run `cargo run` to run the app, run `cargo build` to build an executable file. 6 | 7 | ### Check out the Youtube Tutorial for this [Rust Tutorial](https://youtu.be/-Jp7sabBCp4). Here is our [Youtube Channel](https://www.youtube.com/channel/UCYqCZOwHbnPwyjawKfE21wg) Subscribe for more content. 8 | 9 | ### Check out our blog at [tensor-programming.com](http://tensor-programming.com/). 10 | 11 | ### Our [Twitter](https://twitter.com/TensorProgram), our [facebook](https://www.facebook.com/Tensor-Programming-1197847143611799/) and our [Steemit](https://steemit.com/@tensor). 12 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use bpaf::Bpaf; 2 | use std::io::{self, Write}; 3 | use std::net::{IpAddr, Ipv4Addr}; 4 | use std::sync::mpsc::{channel, Sender}; 5 | use tokio::net::TcpStream; 6 | use tokio::task; 7 | 8 | // Max IP Port. 9 | const MAX: u16 = 65535; 10 | 11 | // Address fallback. 12 | const IPFALLBACK: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); 13 | 14 | // CLI Arguments. 15 | #[derive(Debug, Clone, Bpaf)] 16 | #[bpaf(options)] 17 | pub struct Arguments { 18 | // Address argument. Accepts -a and --address and an IpAddr type. Falls back to the above constant. 19 | #[bpaf(long, short, argument("Address"), fallback(IPFALLBACK))] 20 | /// The address that you want to sniff. Must be a valid ipv4 address. Falls back to 127.0.0.1 21 | pub address: IpAddr, 22 | #[bpaf( 23 | long("start"), 24 | short('s'), 25 | guard(start_port_guard, "Must be greater than 0"), 26 | fallback(1u16) 27 | )] 28 | /// The start port for the sniffer. (must be greater than 0) 29 | pub start_port: u16, 30 | #[bpaf( 31 | long("end"), 32 | short('e'), 33 | guard(end_port_guard, "Must be less than or equal to 65535"), 34 | fallback(MAX) 35 | )] 36 | /// The end port for the sniffer. (must be less than or equal to 65535) 37 | pub end_port: u16, 38 | } 39 | 40 | fn start_port_guard(input: &u16) -> bool { 41 | *input > 0 42 | } 43 | 44 | fn end_port_guard(input: &u16) -> bool { 45 | *input <= MAX 46 | } 47 | 48 | 49 | // Scan the port. 50 | async fn scan(tx: Sender, start_port: u16, addr: IpAddr) { 51 | // Attempts Connects to the address and the given port. 52 | match TcpStream::connect(format!("{}:{}", addr, start_port)).await { 53 | // If the connection is successful, print out a . and then pass the port through the channel. 54 | Ok(_) => { 55 | print!("."); 56 | io::stdout().flush().unwrap(); 57 | tx.send(start_port).unwrap(); 58 | } 59 | // If the connection is unsuccessful, do nothing. Means port is not open. 60 | Err(_) => {} 61 | } 62 | } 63 | 64 | #[tokio::main] 65 | async fn main() { 66 | // collect the arguments. 67 | let opts = arguments().run(); 68 | // Initialize the channel. 69 | let (tx, rx) = channel(); 70 | // Iterate through all of the ports (based on user input) so that we can spawn a single task for each. 71 | // (Much faster than before because it uses green threads instead of OS threads.) 72 | for i in opts.start_port..opts.end_port { 73 | let tx = tx.clone(); 74 | 75 | task::spawn(async move { scan(tx, i, opts.address).await }); 76 | } 77 | // Create the vector for all of the outputs. 78 | let mut out = vec![]; 79 | // Drop the tx clones. 80 | drop(tx); 81 | // Wait for all of the outputs to finish and push them into the vector. 82 | 83 | for p in rx { 84 | out.push(p); 85 | } 86 | 87 | println!(""); 88 | out.sort(); 89 | for v in out { 90 | // Iterate through the outputs and print them out as being open. 91 | println!("{} is open", v); 92 | } 93 | } 94 | --------------------------------------------------------------------------------