aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-05-28 00:07:37 -0400
committerbnewbold <bnewbold@robocracy.org>2016-05-28 00:07:37 -0400
commitf01a8c7160a676153a0e1ffcdc1654d2a0c6a2dd (patch)
treeada112cd55c2625a20681bbb96558d1e318b667c
parent0814e7f2a1dd1ed6c6b9a6133f6920194630bb75 (diff)
downloaducp-f01a8c7160a676153a0e1ffcdc1654d2a0c6a2dd.tar.gz
ucp-f01a8c7160a676153a0e1ffcdc1654d2a0c6a2dd.zip
more rapid development
-rw-r--r--Cargo.lock9
-rw-r--r--Cargo.toml1
-rw-r--r--src/client.rs63
-rw-r--r--src/common.rs2
-rw-r--r--src/main.rs20
-rw-r--r--src/server.rs69
6 files changed, 119 insertions, 45 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 01b7d6b..65a7d65 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2,6 +2,7 @@
name = "ucp"
version = "0.1.0"
dependencies = [
+ "daemonize 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"utp 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -20,6 +21,14 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "daemonize"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "env_logger"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 8f3fdb9..183e38d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,4 +5,5 @@ authors = ["bnewbold <bnewbold@robocracy.org>"]
[dependencies]
getopts = "0.2"
+daemonize = "0.2"
utp = "*"
diff --git a/src/client.rs b/src/client.rs
index 75329f3..9687488 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -1,16 +1,57 @@
extern crate utp;
-use std::str;
+use std::string::String;
use std::env;
-use std::process::exit;
-use getopts::Options;
-use utp::{UtpSocket, UtpListener};
-
-pub fn run_client(host: &str, local_file: &str, remote_file: &str, is_recv: bool) {
- println!("host: {}", host);
- println!("local_file: {}", local_file);
- println!("remote_file: {}", remote_file);
- println!("is_recv: {}", is_recv);
-}
+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 buf = [0; 2000];
+ let mut socket = UtpSocket::connect((remote_host, remote_port)).unwrap();;
+ socket.send_to("PING".as_bytes());
+ socket.flush();
+ let (amt, _src) = socket.recv_from(&mut buf).ok().unwrap();
+ let reply = String::from_utf8_lossy(&buf[..amt]);
+ println!("Got uTP reply: {}", reply);
+ socket.close();
+}
diff --git a/src/common.rs b/src/common.rs
index 5830ab6..063b166 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -4,7 +4,7 @@ extern crate utp;
use std::str;
use std::env;
use std::process::exit;
-use utp::{UtpSocket, UtpListener};
+use utp::{UtpSocket};
fn send_files(socket: UtpSocket, file_path: &str, recursive: bool) {
diff --git a/src/main.rs b/src/main.rs
index 683c2bf..8a78e67 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,15 +11,14 @@ use std::process::exit;
use getopts::Options;
use utp::{UtpSocket, UtpListener};
-fn usage(prog_name: &str, opts: Options) {
- let brief = format!("usage:\t{} [-h] [-v] [[user@]host1:]srcfile [[user@]host2:]destfile", prog_name);
+fn usage(opts: Options) {
+ let brief = "usage:\tucp [-h] [-v] [[user@]host1:]srcfile [[user@]host2:]destfile";
print!("{}", opts.usage(&brief));
}
fn main() {
let args: Vec<String> = env::args().collect();
- let prog_name = args[0].clone();
// First check for "hidden" server mode
if args.len() > 1 && args[1] == "server" {
@@ -29,25 +28,26 @@ fn main() {
let mut opts = Options::new();
opts.optflag("h", "help", "print this help menu");
- opts.optflag("v", "verbose", "more debugging messages");
+ //opts.optflag("v", "verbose", "more debugging messages");
opts.optflag("r", "recursive", "whether to recursively transfer files (directory)");
let matches = match opts.parse(&args[1..]) {
Ok(m) => { m }
- Err(f) => { println!("Error parsing args!"); usage(&prog_name, opts); exit(-1); }
+ Err(f) => { println!("Error parsing args: {}", f); usage(opts); exit(-1); }
};
- let verbose: bool = matches.opt_present("v");
+ //let verbose: bool = matches.opt_present("v");
+ let recursive: bool = matches.opt_present("r");
if matches.opt_present("h") {
- usage(&prog_name, opts);
+ usage(opts);
return;
}
if matches.free.len() != 2 {
println!("Expected a single source and single destination");
println!("");
- usage(&prog_name, opts);
+ usage(opts);
return;
}
@@ -71,7 +71,7 @@ fn main() {
let spl: Vec<&str> = srcfile.split(":").collect();
let host = spl[0];
let remote_file = spl[1];
- client::run_client(host, local_file, remote_file, is_recv);
+ client::run_client(host, local_file, remote_file, recursive, is_recv);
},
(false, true) => {
let is_recv = false;
@@ -79,7 +79,7 @@ fn main() {
let spl: Vec<&str> = destfile.split(":").collect();
let host = spl[0];
let local_file = spl[1];
- client::run_client(host, local_file, remote_file, is_recv);
+ client::run_client(host, local_file, remote_file, recursive, is_recv);
},
}
}
diff --git a/src/server.rs b/src/server.rs
index 1cdba76..e1b414e 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -1,55 +1,79 @@
extern crate getopts;
extern crate utp;
+extern crate daemonize;
use std::str;
use std::env;
+use std::env::home_dir;
use std::process::exit;
use getopts::Options;
use utp::{UtpSocket, UtpListener};
-
fn run_server(path: &str, is_receive: bool) {
- // Connect to an hypothetical local server running on port 3540
- let addr = "127.0.0.1:3540";
+ // TODO: try to detect the address the SSH connection came in on via the SSH_CONNECTION env
+ // variable.
+
+ // Connect to an hypothetical local server running on port 61000
+ let addr = "127.0.0.1:61000";
// Accept connection from anybody
let listener = UtpListener::bind(addr).expect("Error binding to local port");
- for connection in listener.incoming() {
+ let listen_port = listener.local_addr().unwrap().port();
+ let listen_addr = listener.local_addr().unwrap().ip();
+
+ // Send back details so client can connect
+ println!("UDP CONNECT {} {} {}", listen_addr, listen_port, "<SECRET>");
+
+ // TODO: maybe wait for an ACK of some sort here before daemonizing?
+
+ // At this point we partially daemonize (fork and redirect terminal), so that SSH will drop us.
+ // But, don't clobber working dir.
+ let working_dir = match env::home_dir() {
+ Some(path) => path,
+ None => env::current_dir().unwrap(),
+ };
+ // XXX: should maybe use log/syslog from here on?
+ let daemonizer = daemonize::Daemonize::new().working_directory(working_dir);
- let (mut socket, _src) = connection.unwrap();
- println!("Got connection from {}", socket.peer_addr().unwrap());
+ match daemonizer.start() {
+ Ok(_) => println!("Success, daemonized"),
+ Err(e) => println!("{}", e),
+ }
- loop {
+ let (mut socket, _src) = listener.accept().unwrap();
+ println!("Got connection from {}", socket.peer_addr().unwrap());
- let mut buf = [0; 1000];
- let (amt, _src) = socket.recv_from(&mut buf).ok().unwrap();
- if amt <= 0 {
- break;
- }
- let buf = &buf[..amt];
- let s = str::from_utf8(buf).unwrap();
- println!("\tgot: {}", s);
+ loop {
+ let mut buf = [0; 1000];
+ let (amt, _src) = socket.recv_from(&mut buf).ok().unwrap();
+ if amt <= 0 {
+ break;
}
+ let buf = &buf[..amt];
+ let s = str::from_utf8(buf).unwrap();
+ println!("\tgot: {}", s);
+ socket.send_to(buf);
}
}
-fn usage(prog_name: &str, opts: Options) {
- let brief = format!("usage:\t{} server ...", prog_name); // XXX:
+fn usage_server(opts: Options) {
+ let brief = "usage:\tucp server ..."; // XXX:
+ println!("");
+ println!("IMPORTANT: this is the server mode of ucp. Unless you are developing/debugging, you probably want the 'regular' one (from the 'server' from you command)");
print!("{}", opts.usage(&brief));
}
pub fn main_server() {
let args: Vec<String> = env::args().collect();
- let prog_name = args[0].clone();
let mut opts = Options::new();
opts.optflag("h", "help", "print this help menu");
- opts.optflag("v", "verbose", "more debugging messages");
+ //opts.optflag("v", "verbose", "more debugging messages");
opts.optflag("d", "dir-mode", "read/write a dir instead of file (server side)");
opts.optopt("f", "from", "file or dir to read from (server side)", "FILE");
opts.optopt("t", "to", "file or dir to write to (server side)", "FILE");
@@ -57,15 +81,15 @@ pub fn main_server() {
assert!(args.len() >= 2 && args[1] == "server");
let matches = match opts.parse(&args[2..]) {
Ok(m) => { m }
- Err(f) => { println!("Error parsing args!"); usage(&prog_name, opts); exit(-1); }
+ Err(f) => { println!("Error parsing args!"); usage_server(opts); exit(-1); }
};
if matches.opt_present("h") {
- usage(&prog_name, opts);
+ usage_server(opts);
return;
}
- let verbose: bool = matches.opt_present("v");
+ //let verbose: bool = matches.opt_present("v");
let dir_mode: bool = matches.opt_present("d");
match (matches.opt_present("f"), matches.opt_present("t")) {
@@ -80,7 +104,6 @@ pub fn main_server() {
run_server(&matches.opt_str("f").unwrap(), false);
}
if matches.opt_present("t") {
- unimplemented!();
run_server(&matches.opt_str("t").unwrap(), true);
}
}