-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch rama-tls to use a unique handle per sslkeylogfile
Closes #341
- Loading branch information
Showing
12 changed files
with
213 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[formatting] | ||
array_auto_collapse = false | ||
array_auto_expand = false |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//! Keylog facility used by any tls implementation | ||
//! supported by rama, and which can be used for your owns as well. | ||
//! | ||
//! Center to thsi module is the `KeyLogger` which is a wrapper around | ||
//! a FS file | ||
|
||
use parking_lot::RwLock; | ||
use rama_core::error::{ErrorContext, OpaqueError}; | ||
use std::{ | ||
collections::{hash_map::Entry, HashMap}, | ||
fs::OpenOptions, | ||
io::Write, | ||
sync::OnceLock, | ||
}; | ||
|
||
/// Get a key log file handle for the given path | ||
/// only one file handle will be opened per unique path String. | ||
/// | ||
/// # To be unique or ditto | ||
/// | ||
/// Paths are case-sensitive by default for rama, as utf-8 compatible. | ||
/// Normalize yourself prior to passing a path to this function if you're concerned. | ||
pub fn new_key_log_file_handle(path: String) -> Result<KeyLogFileHandle, OpaqueError> { | ||
let mapping = GLOBAL_KEY_LOG_FILE_MAPPING.get_or_init(Default::default); | ||
if let Some(handle) = mapping.read().get(&path).cloned() { | ||
return Ok(handle); | ||
} | ||
let mut mut_mapping = mapping.write(); | ||
match mut_mapping.entry(path.clone()) { | ||
Entry::Occupied(entry) => Ok(entry.get().clone()), | ||
Entry::Vacant(entry) => { | ||
let handle = try_init_key_log_file_handle(path)?; | ||
entry.insert(handle.clone()); | ||
Ok(handle) | ||
} | ||
} | ||
} | ||
|
||
fn try_init_key_log_file_handle(path: String) -> Result<KeyLogFileHandle, OpaqueError> { | ||
tracing::trace!( | ||
file = ?path, | ||
"KeyLogFileHandle: try to create a new handle", | ||
); | ||
|
||
let mut file = OpenOptions::new() | ||
.append(true) | ||
.create(true) | ||
.open(&path) | ||
.with_context(|| format!("create key log file {path:?}"))?; | ||
|
||
let (tx, rx) = flume::unbounded::<String>(); | ||
|
||
let path_name = path.clone(); | ||
std::thread::spawn(move || { | ||
tracing::trace!( | ||
file = ?path_name, | ||
"KeyLogFileHandle[rx]: receiver task up and running", | ||
); | ||
while let Ok(line) = rx.recv() { | ||
if let Err(err) = file.write_all(line.as_bytes()) { | ||
tracing::error!( | ||
file = path_name, | ||
error = %err, | ||
"KeyLogFileHandle[rx]: failed to write file", | ||
); | ||
} | ||
} | ||
}); | ||
|
||
Ok(KeyLogFileHandle { path, sender: tx }) | ||
} | ||
|
||
static GLOBAL_KEY_LOG_FILE_MAPPING: OnceLock<RwLock<HashMap<String, KeyLogFileHandle>>> = | ||
OnceLock::new(); | ||
|
||
#[derive(Debug, Clone)] | ||
/// Handle to a (tls) keylog file. | ||
/// | ||
/// See [`new_key_log_file_handle`] for more info, | ||
/// as that is the one creating it. | ||
pub struct KeyLogFileHandle { | ||
path: String, | ||
sender: flume::Sender<String>, | ||
} | ||
|
||
impl KeyLogFileHandle { | ||
/// Write a line to the keylogger. | ||
pub fn write_log_line(&self, line: String) { | ||
if let Err(err) = self.sender.send(line) { | ||
tracing::error!( | ||
file = %self.path, | ||
error = %err, | ||
"KeyLogFileHandle[tx]: failed to send log line for writing", | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.