aboutsummaryrefslogtreecommitdiffstats
path: root/src/client.rs
blob: b7da71e24f15b8be1501dbcf80c43427410f99e5 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

use super::common;

use std::io::Write;
use std::string::String;
use std::str::{self, FromStr};
use std::env;
use std::net;
use std::io;
use std::process;
use std::error::Error;
use std::process::exit;
use std::process::Command;
use getopts::Options;
use udt::{self, UdtSocket};
use udt_extras::{UdtStream};
use crypto::{SecretStream, key2string, string2key, nonce2string, string2nonce};
use sodiumoxide::crypto::secretbox;

pub fn run_client(host: &str, local_file: &str, remote_file: &str, remote_is_dir: bool, is_recv: bool, no_crypto: bool, verbose: bool) -> Result<(), String> {

    if verbose {
        println!("local details:");
        println!("\thost: {}", host);
        println!("\tlocal_file: {}", local_file);
        println!("\tremote_file: {}", remote_file);
        println!("\tis_recv: {}", is_recv);
    };

    let mut ssh_cmd = Command::new("ssh");
    ssh_cmd.arg(host)
           .arg("-4")
           .arg("--")
           .arg("ucp")
           .arg("server")
           .arg(if is_recv {"-f"} else {"-t"})
           .arg(remote_file);

    if remote_is_dir {
        ssh_cmd.arg("-d");
    }
    if no_crypto {
        ssh_cmd.arg("--no-crypto");
    }

    let ssh_output = ssh_cmd.output().expect("couldn't get SSH sub-process output");

    if !ssh_output.status.success() {
        return Err(format!("Error on remote end: {}",
                           String::from_utf8_lossy(&ssh_output.stderr)));
    }

    let reply = String::from_utf8_lossy(&ssh_output.stdout);
    //println!("SSH reply: {}", reply);
    let words: Vec<&str> = reply.split_whitespace().collect();
    if words.len() != 7 || words[0] != "UCP" || words[1] != "CONNECT" {
        return Err("Unexpected data via SSH pipe (TCP)".to_string());
    }
    let remote_host = words[2];
    let remote_port = words[3].parse::<u16>().expect("failed to parse remote port number");
    let remote_secret = words[4];
    let remote_read_nonce = words[5];
    let remote_write_nonce = words[6];

    if verbose {
        println!("Got remote details:");
        println!("\tport: {}", remote_port);
        println!("\thost: {}", remote_host);
        println!("\tsecret key: {}", remote_secret);
        println!("\tsecret read nonce: {}", remote_read_nonce);
        println!("\tsecret write nonce: {}", remote_write_nonce);
    };

    let addr = net::IpAddr::from_str(remote_host).unwrap();
    let mut socket = UdtSocket::new(udt::SocketFamily::AFInet, udt::SocketType::Stream).unwrap();
    match socket.connect(net::SocketAddr::new(addr, remote_port)) {
        Ok(_) =>  { println!("Connected."); },
        Err(e) => { return Err(e.err_msg); },
    };
    let mut stream: UdtStream = UdtStream::new(socket);

    let io_result: io::Result<()>;
    if !no_crypto {
        let mut stream = SecretStream::new(stream);
        stream.key = string2key(remote_secret).unwrap();
        stream.read_nonce = string2nonce(remote_write_nonce).unwrap();
        stream.write_nonce = string2nonce(remote_read_nonce).unwrap();
        if is_recv {
            io_result = common::sink_files(&mut stream, local_file, remote_is_dir);
        } else {
            io_result = common::source_files(&mut stream, local_file, remote_is_dir);
        }
    } else {
        if is_recv {
            io_result = common::sink_files(&mut stream, local_file, remote_is_dir);
        } else {
            io_result = common::source_files(&mut stream, local_file, remote_is_dir);
        }
    }
    match io_result {
        Ok(_) => Ok(()),
        Err(e) => Err(e.description().to_string()),
    }
}

fn usage_client(opts: Options) {
    let brief = "usage:\tucp client ..."; // XXX:
    println!("");
    println!("IMPORTANT: this is the client mode of ucp. Unless you are developing/debugging, you probably want the 'regular' one (from the 'client' from you command)");
    print!("{}", opts.usage(&brief));
}

pub fn main_client() {

    let args: Vec<String> = env::args().collect();

    let mut opts = Options::new();
    opts.optflag("h", "help", "print this help menu");
    //opts.optflag("v", "verbose", "more debugging messages");
    opts.optflag("d", "dir-mode", "read/write a dir instead of file (client side)");
    opts.optopt("f", "from", "file or dir to read from (client side)", "FILE");
    opts.optopt("t", "to", "file or dir to write to (client side)", "FILE");
    opts.reqopt("", "host", "remote hostname to connect to", "HOSTNAME");
    opts.reqopt("", "port", "remote port to connect to", "PORT");
    opts.optopt("", "read-nonce", "secret read nonce", "NONCE");
    opts.optopt("", "write-nonce", "secret write nonce", "NONCE");
    opts.optopt("", "key", "secret key", "NONCE");
    opts.optflag("", "no-crypto", "sends data in the clear (no crypto or verification)");

    assert!(args.len() >= 2 && args[1] == "client");
    let matches = match opts.parse(&args[2..]) {
        Ok(m) => { m }
        Err(f) => { println!("{}", f.to_string()); usage_client(opts); exit(-1); }
    };

    if matches.opt_present("h") {
        usage_client(opts);
        return;
    }

    //let verbose: bool = matches.opt_present("v");
    let dir_mode: bool = matches.opt_present("d");
    let no_crypto: bool = matches.opt_present("no-crypto");

    if !no_crypto {
        if !matches.opt_present("key") ||
                !matches.opt_present("read-nonce") ||
                !matches.opt_present("write-nonce") {
            println!("If not --no-crypto, --key and --read-nonce and --write-nonce are required.");
            usage_client(opts);
            exit(-1);
        }
    }

    match (matches.opt_present("f"), matches.opt_present("t")) {
        (true, true) | (false, false) => {
            println!("Must be either 'from' or 'to', but not both");
            exit(-1);
            },
        _ => {},
    }

    let remote_host = matches.opt_str("host").unwrap();
    let remote_port = matches.opt_str("port").unwrap().parse::<u16>().unwrap();
    let addr = net::IpAddr::from_str(&remote_host).unwrap();
    let mut socket = UdtSocket::new(udt::SocketFamily::AFInet, udt::SocketType::Stream).unwrap();
    socket.connect(net::SocketAddr::new(addr, remote_port)).unwrap();;
    let mut stream: UdtStream = UdtStream::new(socket);
    println!("opened socket");

    let mut ret: io::Result<()>;
    if !no_crypto {
        let mut stream = SecretStream::new(stream);
        stream.key = string2key(&matches.opt_str("key").unwrap()).unwrap();
        stream.read_nonce = string2nonce(&matches.opt_str("read-nonce").unwrap()).unwrap();
        stream.write_nonce = string2nonce(&matches.opt_str("write-nonce").unwrap()).unwrap();
        if matches.opt_present("f") {
            ret = common::source_files(&mut stream, &matches.opt_str("f").unwrap(), dir_mode);
        } else if matches.opt_present("t") {
            ret = common::sink_files(&mut stream, &matches.opt_str("t").unwrap(), dir_mode);
        } else {
            unreachable!();
        }
    } else {
        if matches.opt_present("f") {
            ret = common::source_files(&mut stream, &matches.opt_str("f").unwrap(), dir_mode);
        } else if matches.opt_present("t") {
            ret = common::sink_files(&mut stream, &matches.opt_str("t").unwrap(), dir_mode);
        } else {
            unreachable!();
        }
    }

    match ret {
        Ok(_) => { exit(0); },
        Err(err) => {
            writeln!(&mut ::std::io::stderr(), "{}", err.description()).unwrap();
            exit(-1);
        }
    }
}