use chrono::SecondsFormat;
use serde::Serialize;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
pub const DEFAULT_TAIL: usize = 500;
pub const MAX_TAIL: usize = 5000;
const WORKERS_DIR: &str = "workers";
const MAX_SCAN: u64 = 8 * 1024 * 1024;
const CHUNK: u64 = 64 * 1024;
const REDACTED: &str = "…redacted";
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct LogFile {
pub name: String,
pub size: u64,
pub modified: String,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Tail {
pub name: String,
pub lines: Vec<String>,
pub truncated: bool,
pub size: u64,
pub modified: String,
}
pub fn clamp_tail(n: Option<usize>) -> usize {
n.unwrap_or(DEFAULT_TAIL).clamp(1, MAX_TAIL)
}
fn modified_of(md: &std::fs::Metadata) -> String {
md.modified()
.map(|t| {
chrono::DateTime::<chrono::Utc>::from(t).to_rfc3339_opts(SecondsFormat::Secs, true)
})
.unwrap_or_default()
}
fn rank(name: &str) -> (u8, u64, String) {
if name == "agent.log" {
return (0, 0, String::new());
}
if let Some(w) = name.strip_prefix("workers/") {
let num = w
.strip_prefix('w')
.and_then(|r| r.strip_suffix(".log"))
.and_then(|r| r.parse::<u64>().ok());
return match num {
Some(n) => (3, n, String::new()),
None => (4, 0, w.to_string()),
};
}
if name.starts_with("broker") {
return (1, 0, name.to_string());
}
(2, 0, name.to_string())
}
fn collect(dir: &Path, prefix: &str, out: &mut Vec<LogFile>) {
let Ok(rd) = std::fs::read_dir(dir) else {
return;
};
for e in rd.flatten() {
let Ok(fname) = e.file_name().into_string() else {
continue;
};
let Ok(md) = std::fs::symlink_metadata(e.path()) else {
continue;
};
if !md.is_file() || !fname.ends_with(".log") {
continue;
}
out.push(LogFile {
name: format!("{prefix}{fname}"),
size: md.len(),
modified: modified_of(&md),
});
}
}
pub fn list(dir: &Path) -> Vec<LogFile> {
let mut out = vec![];
collect(dir, "", &mut out);
collect(&dir.join(WORKERS_DIR), "workers/", &mut out);
out.sort_by_key(|f| rank(&f.name));
out
}
pub fn tail(dir: &Path, name: &str, n: usize) -> Option<Tail> {
let f = list(dir).into_iter().find(|f| f.name == name)?;
let path: PathBuf = dir.join(&f.name);
let (lines, truncated, size) = read_tail(&path, n).ok()?;
Some(Tail {
name: f.name,
lines,
truncated,
size,
modified: f.modified,
})
}
fn read_tail(path: &Path, n: usize) -> std::io::Result<(Vec<String>, bool, u64)> {
let mut file = std::fs::File::open(path)?;
let size = file.metadata()?.len();
let mut buf: Vec<u8> = vec![];
let mut pos = size;
let floor = size.saturating_sub(MAX_SCAN);
while pos > floor && count_newlines(&buf) <= n {
let step = CHUNK.min(pos - floor);
pos -= step;
file.seek(SeekFrom::Start(pos))?;
let mut chunk = vec![0u8; step as usize];
file.read_exact(&mut chunk)?;
chunk.extend_from_slice(&buf);
buf = chunk;
}
let mut truncated = pos > 0;
let mut text: &[u8] = &buf;
if text.last() == Some(&b'\n') {
text = &text[..text.len() - 1];
}
let mut parts: Vec<&[u8]> = text.split(|b| *b == b'\n').collect();
if truncated && !parts.is_empty() {
parts.remove(0);
}
if size == 0 {
parts.clear();
}
if parts.len() > n {
parts.drain(..parts.len() - n);
truncated = true;
}
let lines = parts
.into_iter()
.map(|l| {
let s = String::from_utf8_lossy(l);
redact(&strip_ansi(s.trim_end_matches('\r')))
})
.collect();
Ok((lines, truncated, size))
}
fn count_newlines(b: &[u8]) -> usize {
b.iter().filter(|c| **c == b'\n').count()
}
pub fn strip_ansi(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut it = s.chars().peekable();
while let Some(c) = it.next() {
if c != '\u{1b}' {
out.push(c);
continue;
}
match it.next() {
Some('[') => {
for x in it.by_ref() {
if ('\u{40}'..='\u{7e}').contains(&x) {
break;
}
}
}
Some(']') => {
while let Some(x) = it.next() {
if x == '\u{7}' {
break;
}
if x == '\u{1b}' {
it.next(); break;
}
}
}
_ => {}
}
}
out
}
const SECRET_KEYS: [&str; 11] = [
"authorization",
"client_secret",
"refresh_token",
"access_token",
"password",
"api_key",
"api-key",
"apikey",
"passwd",
"secret",
"token",
];
pub fn redact(line: &str) -> String {
let mut out = String::with_capacity(line.len());
let mut rest = line;
let mut after_word = false;
while !rest.is_empty() {
match secret_len(rest, after_word) {
Some(n) => {
out.push_str(REDACTED);
rest = &rest[n..];
after_word = false;
}
None => {
let c = rest.chars().next().unwrap();
out.push(c);
rest = &rest[c.len_utf8()..];
after_word = c.is_alphanumeric() || c == '_' || c == '-';
}
}
}
out
}
fn starts_with_ci(s: &str, p: &str) -> bool {
s.len() >= p.len() && s.as_bytes()[..p.len()].eq_ignore_ascii_case(p.as_bytes())
}
fn secret_len(s: &str, after_word: bool) -> Option<usize> {
let word = |t: &str| {
t.bytes()
.take_while(|b| b.is_ascii_alphanumeric() || *b == b'_')
.count()
};
let value = |t: &str| {
t.find(|c: char| c.is_whitespace() || matches!(c, '"' | '\'' | ',' | '}'))
.unwrap_or(t.len())
};
let spaces = |t: &str| t.len() - t.trim_start_matches([' ', '\t']).len();
if after_word {
return None;
}
if starts_with_ci(s, "zk_") {
let w = word(&s[3..]);
return (w > 0).then_some(3 + w);
}
if starts_with_ci(s, "bearer ") {
let w = value(&s[7..]);
return (w > 0).then_some(7 + w);
}
let key = SECRET_KEYS.iter().find(|k| starts_with_ci(s, k))?;
let mut i = key.len();
if s[i..].starts_with(|c: char| c.is_alphanumeric() || c == '_' || c == '-') {
return None;
}
i += usize::from(s[i..].starts_with('"'));
i += spaces(&s[i..]);
let colon = s[i..].starts_with(':');
if !colon && !s[i..].starts_with('=') {
return None;
}
i += 1;
if colon {
i += spaces(&s[i..]);
}
i += usize::from(s[i..].starts_with('"'));
for scheme in ["bearer ", "basic "] {
if starts_with_ci(&s[i..], scheme) {
i += scheme.len();
}
}
let w = value(&s[i..]);
(w > 0).then_some(i + w)
}
#[cfg(test)]
mod tests {
use super::*;
fn dir(tag: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("zc-logs-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(d.join("workers")).unwrap();
d
}
#[test]
fn list_orders_agent_broker_then_workers_by_number() {
let d = dir("order");
for f in [
"workers/w10.log",
"workers/w2.log",
"broker.log",
"agent.log",
"zzz.log",
"notes.txt",
"workers/x.txt",
] {
std::fs::write(d.join(f), "x\n").unwrap();
}
let names: Vec<_> = list(&d).into_iter().map(|f| f.name).collect();
assert_eq!(
names,
[
"agent.log",
"broker.log",
"zzz.log",
"workers/w2.log",
"workers/w10.log"
]
);
}
#[test]
fn tail_returns_last_n_and_flags_truncation() {
let d = dir("tail");
let body: String = (1..=10).map(|i| format!("line {i}\n")).collect();
std::fs::write(d.join("agent.log"), body).unwrap();
let t = tail(&d, "agent.log", 3).unwrap();
assert_eq!(t.lines, ["line 8", "line 9", "line 10"]);
assert!(t.truncated);
let t = tail(&d, "agent.log", 50).unwrap();
assert_eq!(t.lines.len(), 10);
assert!(!t.truncated);
assert_eq!(t.lines[0], "line 1");
assert_eq!(t.size, 71);
}
#[test]
fn tail_of_a_big_file_reads_only_the_end() {
let d = dir("big");
let body: String = (0..200_000).map(|i| format!("row {i}\n")).collect();
std::fs::write(d.join("agent.log"), &body).unwrap();
let t = tail(&d, "agent.log", 2).unwrap();
assert_eq!(t.lines, ["row 199998", "row 199999"]);
assert!(t.truncated);
}
#[test]
fn empty_file_and_no_trailing_newline() {
let d = dir("edge");
std::fs::write(d.join("agent.log"), "").unwrap();
std::fs::write(d.join("broker.log"), "a\nb").unwrap();
let t = tail(&d, "agent.log", 5).unwrap();
assert!(t.lines.is_empty() && !t.truncated);
assert_eq!(tail(&d, "broker.log", 5).unwrap().lines, ["a", "b"]);
}
#[test]
fn clamp() {
assert_eq!(clamp_tail(None), 500);
assert_eq!(clamp_tail(Some(0)), 1);
assert_eq!(clamp_tail(Some(99_999)), 5000);
}
#[test]
fn unlisted_names_are_not_found() {
let d = dir("trav");
std::fs::write(d.join("agent.log"), "x\n").unwrap();
std::fs::write(d.join("credentials"), "api_key=zk_1\n").unwrap();
for n in [
"../credentials",
"/etc/passwd",
"workers/../../x",
"..",
"credentials",
"workers/../agent.log",
"",
] {
assert!(tail(&d, n, 5).is_none(), "{n}");
}
}
#[test]
fn redacts_secrets() {
assert_eq!(
redact("key zk_12_abcDEF9 and Bearer abc.def token=xyz&x api_key=Q end"),
"key …redacted and …redacted …redacted …redacted end"
);
assert_eq!(redact("zk_ alone, a token= b"), "zk_ alone, a token= b");
assert_eq!(redact("héllo zk_a"), "héllo …redacted");
}
#[test]
fn redacts_any_case_and_the_key_value_and_json_forms() {
for (line, want) in [
("bearer abc.def", "…redacted"),
("Authorization: Basic dXNlcjpwYXNz", "…redacted"),
("authorization: Bearer abc", "…redacted"),
("token: x1", "…redacted"),
(r#"{"token":"x1","n":2}"#, r#"{"…redacted","n":2}"#),
(r#"{"Password": "p w"}"#, r#"{"…redacted w"}"#),
("password=hunter2 next", "…redacted next"),
("SECRET=s3", "…redacted"),
("api-key: k", "…redacted"),
("client_secret=c refresh_token=r", "…redacted …redacted"),
] {
assert_eq!(redact(line), want, "{line}");
}
for line in [
"tokens=3",
"mytoken=3",
"the password is set",
"secretary: x",
] {
assert_eq!(redact(line), line);
}
}
#[test]
fn strips_ansi() {
assert_eq!(
strip_ansi("\u{1b}[31mERROR\u{1b}[0m x \u{1b}]0;title\u{7}y"),
"ERROR x y"
);
}
}