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
41
42
43
44
use std::io::{Cursor, Read};

use clap::{Arg, Command};

use ncat::errors::*;

fn main() -> Result<()> {
    let mut app = Command::new("ncat")
        .arg(
            Arg::new("listen-port")
                .short('l')
                .long("listen")
                .takes_value(true)
                .required(false)
                .conflicts_with("ip")
                .conflicts_with("port"),
        )
        .arg(Arg::new("ip").requires("port").required(false))
        .arg(Arg::new("port").requires("ip").required(false));

    let mut help = Cursor::new(Vec::new());
    app.write_help(&mut help).unwrap();
    let matches = app.get_matches();

    if !matches.args_present() {
        let mut help_str = String::new();
        help.set_position(0);
        help.read_to_string(&mut help_str).unwrap();
        print!("{}", help_str);
        return Ok(());
    }

    use ncat::{connect, listen};
    if matches.is_present("listen-port") {
        let listen_port: u16 = matches.value_of("listen-port").unwrap().parse()?;
        listen::listen(listen_port)
    } else if matches.is_present("ip") && matches.is_present("port") {
        let ip = matches.value_of("ip").unwrap();
        let port: u16 = matches.value_of("port").unwrap().parse()?;
        connect::connect(&format!("{}:{}", ip, port))
    } else {
        unreachable!()
    }
}