From 3f4a94ec505517a30f8660cdde83f057af11e6b2 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Thu, 2 Jun 2016 21:43:37 -0400 Subject: server: add logging via syslog --- Cargo.lock | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++ src/main.rs | 2 + src/server.rs | 42 ++++++++++++++++---- 4 files changed, 161 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9ccb3e4..ac506b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,12 +3,23 @@ name = "ucp" version = "0.1.0" dependencies = [ "daemonize 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "sodiumoxide 0.0.10 (registry+https://github.com/rust-lang/crates.io-index)", + "syslog 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "udt 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "aho-corasick" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bitflags" version = "0.3.3" @@ -22,6 +33,20 @@ dependencies = [ "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "debug-builders" +version = "0.1.0" +source = "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" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.71 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gcc" version = "0.3.28" @@ -32,6 +57,15 @@ name = "getopts" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "libc" version = "0.1.12" @@ -65,11 +99,36 @@ name = "log" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "memchr" +version = "0.1.11" +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 = "pkg-config" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "regex" +version = "0.1.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rustc-serialize" version = "0.3.19" @@ -85,6 +144,44 @@ dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "syslog" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "unix_socket 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread-id" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "time" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "udt" version = "0.1.4" @@ -96,3 +193,27 @@ dependencies = [ "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unix_socket" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "debug-builders 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "utf8-ranges" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/Cargo.toml b/Cargo.toml index d1df197..af23584 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,8 @@ authors = ["bnewbold "] getopts = "^0.2" daemonize = "^0.2" udt = "0.1" +log = "0.3" +env_logger = "0.3" +syslog = "3" sodiumoxide = "*" rustc-serialize = "0.3" diff --git a/src/main.rs b/src/main.rs index 95e2dd0..dd6f7e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ // XXX: re-enable these warnings #![allow(unused_imports, unused_variables, unused_mut)] +#[macro_use] extern crate log; +extern crate env_logger; extern crate getopts; extern crate udt; extern crate sodiumoxide; diff --git a/src/server.rs b/src/server.rs index f9dc599..cb57499 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,5 +1,6 @@ extern crate daemonize; +extern crate syslog; use super::common; @@ -11,6 +12,8 @@ use std::io; use std::error::Error; use std::env::home_dir; use std::process::exit; +use log; +use env_logger; use getopts::Options; use udt::{self, UdtSocket, UdtStatus}; use crypto::{SecretStream, key2string, string2key, nonce2string, string2nonce}; @@ -26,7 +29,7 @@ pub fn get_local_ip() -> Result { } }, Err(_) => { - println!("Can't find $SSH_CONNECTION; running locally? Falling back to 127.0.0.1"); + warn!("Can't find $SSH_CONNECTION; running locally? Falling back to 127.0.0.1"); "127.0.0.1".to_string() }, }; @@ -86,7 +89,9 @@ fn run_server(path: &str, is_recv: bool, recursive: bool, daemonize: bool, no_cr let write_nonce = secretbox::Nonce::from_slice(&[0; secretbox::NONCEBYTES]).unwrap(); */ - // Send back details so client can connect + info!("listening on {}:{}", listen_addr, listen_port); + + // Send back (via SSH stdout) details so client can connect println!("UCP CONNECT {} {} {} {} {}", listen_addr, listen_port, @@ -112,10 +117,10 @@ fn run_server(path: &str, is_recv: bool, recursive: bool, daemonize: bool, no_cr Err(e) => println!("{}", e), } } else { - println!("Not daemonizing (DEBUG MODE)"); + info!("Not daemonizing (DEBUG MODE)"); } let (mut socket, _src) = listener.accept().unwrap(); - println!("Got connection from {}", socket.getpeername().unwrap()); + info!("Got connection from {}", socket.getpeername().unwrap()); let mut stream: UdtStream = UdtStream::new(socket); if !no_crypto { @@ -150,7 +155,7 @@ pub fn main_server() { 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.optflag("", "no-daemonize", "don't daemonize (for debuggign)"); opts.optopt("f", "from", "file or dir to read from (server side)", "FILE"); @@ -168,7 +173,7 @@ pub fn main_server() { return; } - //let verbose: bool = matches.opt_present("v"); + let verbose: bool = matches.opt_present("v"); let dir_mode: bool = matches.opt_present("d"); let daemonize: bool = !matches.opt_present("no-daemonize"); let no_crypto: bool = matches.opt_present("no-crypto"); @@ -181,6 +186,29 @@ pub fn main_server() { _ => {}, } + // Set up logging; syslog doesn't have a thingy + if daemonize { + match syslog::unix(syslog::Facility::LOG_USER) { + Ok(logger) => { + log::set_logger(|max_log_level| { + max_log_level.set(log::LogLevelFilter::Info); + logger + }).unwrap(); + }, + Err(_) => { + println!("Couldn't connect to syslog (and in daemonize mode"); + exit(-1); + } + } + } else { + let mut builder = env_logger::LogBuilder::new(); + builder.parse("INFO"); + if env::var("RUST_LOG").is_ok() { + builder.parse(&env::var("RUST_LOG").unwrap()); + } + builder.init().unwrap(); + } + let (file_name, is_recv) = if matches.opt_present("f") { (matches.opt_str("f").unwrap(), false) } else { @@ -189,7 +217,7 @@ pub fn main_server() { match run_server(&file_name, is_recv, dir_mode, daemonize, no_crypto) { Ok(_) => { exit(0); }, Err(msg) => { - writeln!(&mut ::std::io::stderr(), "{}", msg).unwrap(); + error!("{}", msg); exit(-1); } } -- cgit v1.2.3