Skip to content

Instantly share code, notes, and snippets.

@eternaleclipse
Last active March 3, 2019 20:41
Show Gist options
  • Save eternaleclipse/37057486dc2c05fdba551df20531646a to your computer and use it in GitHub Desktop.
Save eternaleclipse/37057486dc2c05fdba551df20531646a to your computer and use it in GitHub Desktop.
Simple multi-threaded Rust port scanner
#[macro_use]
extern crate clap;
use std::net::{TcpStream, SocketAddr};
use std::time::Duration;
use std::thread;
use std::sync::{Arc, Mutex, mpsc::channel};
use clap::{App, Arg};
enum Job {
Port(u16),
Finished
}
fn scan_port(port: u16) -> bool {
let mut addr: SocketAddr = SocketAddr::new("127.0.0.1".parse().unwrap(), port);
return TcpStream::connect_timeout(&addr, Duration::from_millis(100)).is_ok();
}
fn main() {
let matches = App::new("PortScanner")
.version("0.1")
.about("Simple port scanner")
.arg(Arg::with_name("START_PORT")
.short("s")
.help("Start port number")
.required(true)
.takes_value(true)
)
.arg(Arg::with_name("END_PORT")
.short("e")
.help("End port number")
.required(true)
.takes_value(true)
)
.arg(Arg::with_name("NUM_THREADS")
.short("t")
.help("Number of worker threads")
.required(true)
.takes_value(true)
)
.get_matches();
let start_port = value_t!(matches.value_of("START_PORT"), u16).unwrap();
let end_port = value_t!(matches.value_of("END_PORT"), u16).unwrap();
let num_threads = value_t!(matches.value_of("NUM_THREADS"), u32).unwrap();
let mut threads = Vec::new();
let (tx, rx) = channel();
let rx = Arc::new(Mutex::new(rx));
for _ in 0..num_threads {
let rx = rx.clone();
threads.push(thread::spawn(move || {
loop {
let job = (|| {
let rx = rx.lock().unwrap();
return rx.recv().unwrap();
})();
match job {
Job::Port(port) => {
if scan_port(port) {
println!("{}", port);
}
},
Job::Finished => {
return
}
}
}
}));
}
for port in start_port..end_port {
tx.send(Job::Port(port)).unwrap();
}
for _ in threads.iter() {
tx.send(Job::Finished).unwrap();
}
for t in threads {
t.join().unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment