1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::manifest::Manifest;
use crate::urlvalidation;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "status")]
pub enum PingStatus {
/// Got an HTTP response (any status code — the service is reachable).
Reachable { code: u16 },
/// Connection failed (timeout, DNS error, TLS error, etc.).
Unreachable { reason: String },
/// URL failed validation (e.g., private IP address).
Invalid { reason: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PingResult {
pub service: String,
pub url: String,
pub ping: PingStatus,
}
impl PingResult {
pub fn is_ok(&self) -> bool {
matches!(self.ping, PingStatus::Reachable { .. })
}
}
/// Ping every service that has a `url` field and return the results.
///
/// URLs are validated to prevent SSRF attacks:
/// - Private/internal IP addresses are rejected
/// - URLs must have http:// or https:// scheme
pub fn ping_services(manifest: &Manifest) -> Vec<PingResult> {
manifest
.services
.iter()
.filter_map(|svc| svc.url.as_deref().map(|url| (svc, url.to_string())))
.map(|(svc, url)| {
// Validate URL to prevent SSRF attacks (allow http for this context)
let ping = if let Err(e) = urlvalidation::validate_url(&url, false) {
PingStatus::Invalid {
reason: e.to_string(),
}
} else {
match ureq::get(&url).timeout(Duration::from_secs(5)).call() {
Ok(resp) => PingStatus::Reachable {
code: resp.status(),
},
// 4xx/5xx: got a response, service is up
Err(ureq::Error::Status(code, _)) => PingStatus::Reachable { code },
Err(e) => PingStatus::Unreachable {
reason: e.to_string(),
},
}
};
PingResult {
service: svc.name.clone(),
url,
ping,
}
})
.collect()
}