Add general probe endpoint
This commit is contained in:
39
src/main.rs
39
src/main.rs
@@ -42,6 +42,11 @@ use tokio::net::{
|
||||
TcpStream,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ProbeQuery {
|
||||
domain: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ProbeClientToServerQuery {
|
||||
domain: String,
|
||||
@@ -104,6 +109,7 @@ impl<E> From<E> for AppError where E: Into<anyhow::Error> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum XmppEndpointType {
|
||||
XmppClient,
|
||||
XmppsClient,
|
||||
@@ -151,12 +157,14 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(route_index))
|
||||
.route("/probe", get(route_probe))
|
||||
.route("/probe-client-to-server", get(route_probe_client_to_server))
|
||||
.route("/list-services", get(route_list_services));
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(listen).await?;
|
||||
println!("Server listening on: http://{}", listener.local_addr()?);
|
||||
println!("Probe with:");
|
||||
println!(" http://{}/probe?domain=fem-net.de", listener.local_addr()?);
|
||||
println!(" http://{}/probe-client-to-server?domain=fem-net.de&hostname=xmpp-2.fem-net.de&port=5222", listener.local_addr()?);
|
||||
println!("Debug helpers:");
|
||||
println!(" http://{}/list-services?domain=fem-net.de", listener.local_addr()?);
|
||||
@@ -202,6 +210,37 @@ async fn probe_client_to_server(domain: &str, hostname: &str, port: u16) -> anyh
|
||||
Ok(probe_facts)
|
||||
}
|
||||
|
||||
async fn route_probe(
|
||||
Query(query): Query<ProbeQuery>,
|
||||
) -> Result<impl IntoResponse, AppError> {
|
||||
|
||||
let services = list_services(&query.domain).await?;
|
||||
|
||||
let mut domain_success = false;
|
||||
|
||||
let mut out = String::new();
|
||||
|
||||
for service in services.iter().filter(|s| s.0 == XmppEndpointType::XmppClient) {
|
||||
|
||||
let probe_facts = probe_client_to_server(&query.domain, &service.1, service.2).await
|
||||
.context("Failed probing XMPP connection")?;
|
||||
|
||||
out.push_str(&format!("xmpp_endpoint_is_xmpp_client{{domain=\"{}\", hostname=\"{}\", port=\"{}\"}} {}\n", query.domain, service.1, service.2, probe_facts.is_xmpp_client as u8));
|
||||
out.push_str(&format!("xmpp_endpoint_is_xmpp_stream{{domain=\"{}\", hostname=\"{}\", port=\"{}\"}} {}\n", query.domain, service.1, service.2, probe_facts.is_xmpp_stream as u8));
|
||||
out.push_str(&format!("xmpp_endpoint_success{{domain=\"{}\", hostname=\"{}\", port=\"{}\"}} {}\n", query.domain, service.1, service.2, probe_facts.probe_success() as u8));
|
||||
|
||||
if probe_facts.probe_success() {
|
||||
domain_success = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
out.push_str(&format!("xmpp_domain_success{{domain=\"{}\"}} {}\n", query.domain, domain_success as u8));
|
||||
|
||||
return Ok(out);
|
||||
}
|
||||
|
||||
|
||||
async fn route_probe_client_to_server(
|
||||
Query(query): Query<ProbeClientToServerQuery>,
|
||||
) -> Result<impl IntoResponse, AppError> {
|
||||
|
Reference in New Issue
Block a user