aboutsummaryrefslogtreecommitdiffstats
path: root/src/client.rs
blob: a2ebf27db009b532427f76c9b94047df032d52c7 (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

extern crate utp;

use super::common;

use std::string::String;
use std::env;
use std::process;
use std::process::Command;
use utp::{UtpSocket};

pub fn run_client(host: &str, local_file: &str, remote_file: &str, remote_is_dir: bool, is_recv: bool) {
    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("--")
           .arg("ucp")
           .arg("server")
           .arg(if is_recv {"-f"} else {"-t"})
           .arg(remote_file);

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

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

    if !ssh_output.status.success() {
        panic!("SSH problem: {}", 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() != 5 || words[0] != "UDP" || words[1] != "CONNECT" {
        panic!("Unexpected data via SSH pipe (TCP)");
    }
    let remote_host = words[2];
    let remote_port = words[3].parse::<u16>().expect("failed to parse remote port number");
    let remote_secret = words[4];

    println!("Got remote details:");
    println!("\tport: {}", remote_port);
    println!("\thost: {}", remote_host);
    println!("\tsecret: {}", remote_secret);

    let mut socket = UtpSocket::connect((remote_host, remote_port)).unwrap();;
    if is_recv {
        common::receive_files(&mut socket, local_file, remote_is_dir);
    } else {
        common::send_files(&mut socket, local_file, remote_is_dir);
    }
    socket.close().unwrap();
}