1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::default::Default;
use std::net::{ TcpListener, Ipv4Addr };

#[derive(Debug)]
pub struct PortRange {
    pub min: u16,
    pub max: u16
}

impl Default for PortRange {
    fn default() -> Self {
        PortRange { min: 1024, max: 65535 }
    }
}

pub fn get_port_in_range(range: PortRange) -> Option<u16> {
    return (range.min..range.max).filter(|p| check_port_available(&p)).nth(0); 
}


pub fn get_port() -> Option<u16> {
    return get_port_in_range(PortRange::default());
}

pub fn get_port_prefer(ports: Vec<u16>) -> Option<u16> {
    let port = ports.into_iter().filter(|p| check_port_available(&p)).nth(0);

    if port.is_some() {
       return port;
    } else {
       return get_port();
    }
}

fn check_port_available(port: &u16) -> bool {
    match TcpListener::bind((Ipv4Addr::LOCALHOST, *port)) {
        Ok(_) => return true,
        Err(_) => return false
    };
}