Created
February 27, 2018 02:36
-
-
Save lukewilson2002/d2f66e60d2526c4b93c201e9b763e649 to your computer and use it in GitHub Desktop.
Over-engineered job-stealing multithreaded port scanner in Rust.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rayon; | |
use std::io::{stdout, Write}; | |
use std::net::{IpAddr, Ipv4Addr, TcpStream}; | |
use rayon::prelude::*; | |
static MAX_PORT: u16 = 65535; | |
fn main() { | |
let address = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); | |
println!("Testing ports from 0 to {}:", MAX_PORT); | |
let open_ports = get_open_ports(MAX_PORT, address); | |
eprintln!(""); | |
let stdout = stdout(); | |
let mut handle = stdout.lock(); | |
handle.write(b"Results:\n").unwrap(); | |
for port in &open_ports { | |
handle.write(format!("{number:>width$}\n", number=port, width=5).as_bytes()).unwrap(); | |
} | |
} | |
fn get_open_ports(ports: u16, address: IpAddr) -> Vec<u16> { | |
(0..ports).into_par_iter().filter(|&p| { | |
eprint!("."); | |
TcpStream::connect((address, p)).is_ok() | |
}).collect() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment