| 1 | use std::fs; |
| 2 | use std::io; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | |
| 5 | use app_base::support::{self, ClientInfo}; |
| 6 | use app_call_model::report::report_directory; |
| 7 | use app_call_model::report::scrub::Scrubber; |
| 8 | use chrono::Utc; |
| 9 | |
| 10 | const KEEP_CALL_REPORTS: usize = 3; |
| 11 | const DIAGNOSTICS_NAME: &str = "diagnostics.log"; |
| 12 | const CRASH_NAME: &str = "crash.log"; |
| 13 | const TIMELINE_NAME: &str = "last-call-timeline.log"; |
| 14 | const DIAGNOSTICS_MERGE_CAP: u64 = 1024 * 1024; |
| 15 | const CRASH_MERGE_CAP: u64 = 128 * 1024; |
| 16 | |
| 17 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | pub struct SupportBundle { |
| 19 | pub directory: PathBuf, |
| 20 | pub folder_name: String, |
| 21 | } |
| 22 | |
| 23 | pub fn write_support_bundle(data_dir: &Path, client: &ClientInfo) -> io::Result<SupportBundle> { |
| 24 | write_support_bundle_from(data_dir, client, &report_directory()) |
| 25 | } |
| 26 | |
| 27 | fn write_support_bundle_from( |
| 28 | data_dir: &Path, |
| 29 | client: &ClientInfo, |
| 30 | call_reports: &Path, |
| 31 | ) -> io::Result<SupportBundle> { |
| 32 | let folder_name = support::bundle_folder_name(Utc::now()); |
| 33 | let directory = support::support_bundles_directory(data_dir).join(&folder_name); |
| 34 | fs::create_dir_all(&directory)?; |
| 35 | fs::write(directory.join("client.md"), client.markdown())?; |
| 36 | fs::write(directory.join("README.md"), support::readme_text())?; |
| 37 | write_scrubbed_text( |
| 38 | &app_base::diagnostics::read_merged(DIAGNOSTICS_MERGE_CAP), |
| 39 | &directory.join(DIAGNOSTICS_NAME), |
| 40 | )?; |
| 41 | write_scrubbed_text( |
| 42 | &app_base::crash_log::read_merged(CRASH_MERGE_CAP), |
| 43 | &directory.join(CRASH_NAME), |
| 44 | )?; |
| 45 | copy_recent_call_reports(&directory, call_reports)?; |
| 46 | copy_latest_call_timeline(&directory)?; |
| 47 | Ok(SupportBundle { |
| 48 | directory, |
| 49 | folder_name, |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | fn write_scrubbed_text(raw: &str, destination: &Path) -> io::Result<()> { |
| 54 | if raw.trim().is_empty() { |
| 55 | return Ok(()); |
| 56 | } |
| 57 | let mut scrubber = Scrubber::default(); |
| 58 | let scrubbed = crate::caldav_credentials::redact_secret_text(&scrubber.scrub(raw)); |
| 59 | fs::write(destination, scrubbed) |
| 60 | } |
| 61 | |
| 62 | fn copy_recent_call_reports(bundle: &Path, reports: &Path) -> io::Result<()> { |
| 63 | if !reports.is_dir() { |
| 64 | return Ok(()); |
| 65 | } |
| 66 | let mut markdown: Vec<PathBuf> = fs::read_dir(reports)? |
| 67 | .filter_map(Result::ok) |
| 68 | .map(|entry| entry.path()) |
| 69 | .filter(|path| path.extension().is_some_and(|ext| ext == "md")) |
| 70 | .collect(); |
| 71 | markdown.sort(); |
| 72 | let start = markdown.len().saturating_sub(KEEP_CALL_REPORTS); |
| 73 | for path in &markdown[start..] { |
| 74 | let Some(name) = path.file_name() else { |
| 75 | continue; |
| 76 | }; |
| 77 | fs::copy(path, bundle.join(name))?; |
| 78 | let json = path.with_extension("json"); |
| 79 | if json.is_file() { |
| 80 | if let Some(json_name) = json.file_name() { |
| 81 | fs::copy(&json, bundle.join(json_name))?; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | Ok(()) |
| 86 | } |
| 87 | |
| 88 | fn copy_latest_call_timeline(bundle: &Path) -> io::Result<()> { |
| 89 | let Some(dumps) = support::call_dumps_dir() else { |
| 90 | return Ok(()); |
| 91 | }; |
| 92 | if !dumps.is_dir() { |
| 93 | return Ok(()); |
| 94 | } |
| 95 | let mut dirs: Vec<PathBuf> = fs::read_dir(&dumps)? |
| 96 | .filter_map(Result::ok) |
| 97 | .map(|entry| entry.path()) |
| 98 | .filter(|path| path.is_dir()) |
| 99 | .collect(); |
| 100 | dirs.sort(); |
| 101 | let Some(latest) = dirs.last() else { |
| 102 | return Ok(()); |
| 103 | }; |
| 104 | let timeline = latest.join("timeline.txt"); |
| 105 | if !timeline.is_file() { |
| 106 | return Ok(()); |
| 107 | } |
| 108 | let raw = fs::read_to_string(timeline)?; |
| 109 | write_scrubbed_text(&raw, &bundle.join(TIMELINE_NAME)) |
| 110 | } |
| 111 | |
| 112 | #[cfg(test)] |
| 113 | mod tests { |
| 114 | use super::*; |
| 115 | use tempfile::tempdir; |
| 116 | |
| 117 | fn sample_client() -> ClientInfo { |
| 118 | ClientInfo { |
| 119 | version: "0.1.0".into(), |
| 120 | build: "test".into(), |
| 121 | os: "linux".into(), |
| 122 | arch: "x86_64".into(), |
| 123 | session: "wayland".into(), |
| 124 | ui_locale: "en".into(), |
| 125 | timezone: "UTC+0".into(), |
| 126 | signed_in: true, |
| 127 | call_reports: true, |
| 128 | debug_logging: true, |
| 129 | audio_input: "Default device".into(), |
| 130 | audio_output: "Default device".into(), |
| 131 | camera: "none".into(), |
| 132 | logs_root: "/tmp/data".into(), |
| 133 | support_bundles_root: "/tmp/data/support-bundles".into(), |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn support_bundle_scrubs_secrets_and_skips_store() { |
| 139 | let root = tempdir().unwrap(); |
| 140 | let data = root.path().join("data"); |
| 141 | fs::create_dir_all(&data).unwrap(); |
| 142 | let diagnostics = data.join(DIAGNOSTICS_NAME); |
| 143 | fs::write( |
| 144 | &diagnostics, |
| 145 | "ok line\nbearer eyJhbGciOiJIUzI1NiJ9.aaa.bbb\nemail user@example.com\n", |
| 146 | ) |
| 147 | .unwrap(); |
| 148 | fs::write(data.join("store.db"), "secret-store").unwrap(); |
| 149 | |
| 150 | app_base::diagnostics::install(diagnostics.clone()); |
| 151 | app_base::diagnostics::set_enabled(true); |
| 152 | if let Some(live) = app_base::diagnostics::path() { |
| 153 | let _ = fs::write( |
| 154 | &live, |
| 155 | "ok line\nbearer eyJhbGciOiJIUzI1NiJ9.aaa.bbb\nemail user@example.com\n", |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | let reports = root.path().join("call-reports"); |
| 160 | fs::create_dir_all(&reports).unwrap(); |
| 161 | let bundle = write_support_bundle_from(&data, &sample_client(), &reports).unwrap(); |
| 162 | let client = fs::read_to_string(bundle.directory.join("client.md")).unwrap(); |
| 163 | assert!(client.contains("### Client")); |
| 164 | assert!(client.contains("Debug logging:")); |
| 165 | let log_path = bundle.directory.join(DIAGNOSTICS_NAME); |
| 166 | if log_path.is_file() { |
| 167 | let log = fs::read_to_string(&log_path).unwrap(); |
| 168 | assert!(!log.contains("eyJhbGciOiJIUzI1NiJ9")); |
| 169 | assert!(!log.contains("user@example.com")); |
| 170 | } |
| 171 | assert!(!bundle.directory.join("store.db").exists()); |
| 172 | assert!(bundle.directory.join("README.md").is_file()); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn support_bundle_copies_matching_call_report_json() { |
| 177 | let root = tempdir().unwrap(); |
| 178 | let data = root.path().join("data"); |
| 179 | fs::create_dir_all(&data).unwrap(); |
| 180 | let reports = root.path().join("call-reports"); |
| 181 | fs::create_dir_all(&reports).unwrap(); |
| 182 | fs::write(reports.join("20260101T000000Z-echo.md"), "# report\n").unwrap(); |
| 183 | fs::write( |
| 184 | reports.join("20260101T000000Z-echo.json"), |
| 185 | "{\"schema\":1}\n", |
| 186 | ) |
| 187 | .unwrap(); |
| 188 | |
| 189 | let bundle = write_support_bundle_from(&data, &sample_client(), &reports).unwrap(); |
| 190 | assert!(bundle.directory.join("20260101T000000Z-echo.md").is_file()); |
| 191 | assert!( |
| 192 | bundle |
| 193 | .directory |
| 194 | .join("20260101T000000Z-echo.json") |
| 195 | .is_file() |
| 196 | ); |
| 197 | } |
| 198 | } |