fix(app): collection connects only to the addresses it checked, within 15 s, logging no address

- the fetcher resolves once, requires every address to be public and connects only to those
  (TLS still checks the name), so DNS rebinding cannot reach the local network; redirects too
- 15 s for the whole fetch also against servers that send slowly; no URL in any log line
- a site whose title alone changed is kept again
- target names that differ only in case are refused (one Windows folder)
- the worker always gets the collection file, so results older than the keep days go also
  with no targets or the feature off
- the results screen lists every target with results, not only those in the newest 100

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-13 21:39:04 +09:00
parent b94e9dc112
commit 9185265010
10 changed files with 273 additions and 66 deletions

View File

@@ -10,25 +10,19 @@ use crate::settings::{valid_collect_name, Settings};
/// The worker's environment variable with the collection file (`voice_worker/collect.py`).
pub const COLLECT_ENV_VAR: &str = "JARVIS_COLLECT_PATH";
/// Write `config/collect.json` with the enabled targets and return it; with the feature off or no
/// enabled target, remove it and return None (the worker then collects nothing).
pub fn write_config(data: &Path, settings: &Settings) -> std::io::Result<Option<PathBuf>> {
/// Write `config/collect.json` and return it. With the feature off it lists no targets: the worker
/// then collects nothing but still deletes day files older than the keep days.
pub fn write_config(data: &Path, settings: &Settings) -> std::io::Result<PathBuf> {
let file = data.join("config").join("collect.json");
let targets: Vec<Value> = settings.collect.targets.iter().filter(|t| t.enabled)
let targets: Vec<Value> = settings.collect.targets.iter().filter(|t| t.enabled && settings.features.collect)
.map(|t| json!({"name": t.name, "kind": t.kind, "value": t.value, "everyMinutes": t.every_minutes}))
.collect();
if !settings.features.collect || targets.is_empty() {
match std::fs::remove_file(&file) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e),
_ => return Ok(None),
}
}
let cfg = json!({"dir": data.join("collect").to_string_lossy(), "keepDays": settings.collect.keep_days,
"targets": targets});
let mut text = serde_json::to_string_pretty(&cfg).unwrap();
text.push('\n');
crate::datadir::write_atomic(&file, text.as_bytes())?;
Ok(Some(file))
Ok(file)
}
/// The day files of one target folder, newest first.
@@ -41,17 +35,25 @@ fn day_files(folder: &Path) -> Vec<PathBuf> {
files
}
/// The target folders that hold results, sorted.
pub fn result_targets(data: &Path) -> Vec<String> {
let root = data.join("collect");
let mut names: Vec<String> = std::fs::read_dir(&root).into_iter().flatten().flatten()
.filter(|e| e.path().is_dir())
.filter_map(|e| e.file_name().to_str().map(str::to_owned))
.filter(|n| valid_collect_name(n) && !day_files(&root.join(n)).is_empty())
.collect();
names.sort();
names
}
/// Collected items newest first, each with its `target`; `target` None reads every target folder.
pub fn results(data: &Path, target: Option<&str>, limit: usize) -> Result<Vec<Value>, String> {
let root = data.join("collect");
let names: Vec<String> = match target {
Some(t) if !valid_collect_name(t) => return Err(format!("not a collection target name: {t}")),
Some(t) => vec![t.to_owned()],
None => std::fs::read_dir(&root).into_iter().flatten().flatten()
.filter(|e| e.path().is_dir())
.filter_map(|e| e.file_name().to_str().map(str::to_owned))
.filter(|n| valid_collect_name(n))
.collect(),
None => result_targets(data),
};
let mut out: Vec<Value> = Vec::new();
for name in names {
@@ -87,23 +89,21 @@ mod tests {
}
#[test]
fn the_worker_gets_only_enabled_targets_and_nothing_without_them() {
fn the_worker_gets_only_enabled_targets_and_the_keep_days_also_without_targets() {
let data = temp_dir("collect-cfg");
let s = settings(json!({"collect": {"keepDays": 3, "targets": [
{"name": "a", "kind": "rss", "value": "https://a.example/rss", "everyMinutes": 30},
{"name": "b", "kind": "site", "value": "https://b.example", "enabled": false}]}}));
let file = write_config(&data, &s).unwrap().unwrap();
let cfg: Value = serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap();
assert_eq!(cfg, json!({"dir": data.join("collect").to_string_lossy(), "keepDays": 3,
let read = |f: &Path| serde_json::from_str::<Value>(&std::fs::read_to_string(f).unwrap()).unwrap();
let dir = data.join("collect").to_string_lossy().into_owned();
let file = write_config(&data, &s).unwrap();
assert_eq!(read(&file), json!({"dir": dir, "keepDays": 3,
"targets": [{"name": "a", "kind": "rss", "value": "https://a.example/rss", "everyMinutes": 30}]}));
// Feature off or no targets: nothing to collect, but old results still go after keepDays.
let mut off = s.clone();
off.features.collect = false;
assert_eq!(write_config(&data, &off).unwrap(), None);
assert!(!file.exists());
write_config(&data, &s).unwrap();
assert_eq!(write_config(&data, &settings(json!({}))).unwrap(), None);
assert!(!file.exists());
assert_eq!(read(&write_config(&data, &off).unwrap()), json!({"dir": dir, "keepDays": 3, "targets": []}));
assert_eq!(read(&write_config(&data, &settings(json!({}))).unwrap()), json!({"dir": dir, "keepDays": 30, "targets": []}));
}
#[test]
@@ -125,6 +125,9 @@ mod tests {
assert_eq!(titles(results(&data, Some("뉴스"), 2).unwrap()), ["뉴스:n2", "뉴스:n1"]);
assert_eq!(titles(results(&data, None, 2).unwrap()), ["뉴스:n2", "site:s1"]);
assert!(results(&data, Some("missing"), 10).unwrap().is_empty());
// Every target with results is listed, also one whose items are all older than the newest.
std::fs::create_dir_all(data.join("collect").join("empty")).unwrap();
assert_eq!(result_targets(&data), ["site", "뉴스"]);
assert!(results(&data, Some(".."), 10).is_err());
assert!(results(&data, Some("../config"), 10).is_err());
}