Check server response for some simple payload
This commit is contained in:
42
src/main.rs
42
src/main.rs
@@ -3,6 +3,7 @@ use axum::{
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
use tokio::io::{
|
||||
AsyncBufReadExt,
|
||||
@@ -20,6 +21,24 @@ struct ProbeClientToServerQuery {
|
||||
port: u16,
|
||||
}
|
||||
|
||||
struct ProbeFacts {
|
||||
is_xmpp_client: bool,
|
||||
is_xmpp_stream: bool,
|
||||
}
|
||||
|
||||
impl ProbeFacts {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
is_xmpp_client: false,
|
||||
is_xmpp_stream: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn probe_success(&self) -> bool {
|
||||
self.is_xmpp_client
|
||||
&& self.is_xmpp_stream
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -63,7 +82,9 @@ async fn route_index() -> String {
|
||||
return String::from("Prometheus exporter for checking XMPP server availability");
|
||||
}
|
||||
|
||||
async fn probe_client_to_server(domain: &str, hostname: &str, port: u16) -> Result<(), String> {
|
||||
async fn probe_client_to_server(domain: &str, hostname: &str, port: u16) -> Result<ProbeFacts, String> {
|
||||
let mut probe_facts = ProbeFacts::new();
|
||||
|
||||
let mut stream = TcpStream::connect((hostname, port)).await.unwrap();
|
||||
|
||||
let connect_string = format!("<stream:stream to='{}' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0'></stream:stream>", domain);
|
||||
@@ -77,17 +98,30 @@ async fn probe_client_to_server(domain: &str, hostname: &str, port: u16) -> Resu
|
||||
|
||||
println!("{}", response);
|
||||
|
||||
Ok(())
|
||||
let re_match_xmpp_stream = Regex::new(r"http://etherx\.jabber\.org/streams").unwrap();
|
||||
if re_match_xmpp_stream.is_match(&response) {
|
||||
probe_facts.is_xmpp_stream = true;
|
||||
}
|
||||
|
||||
let re_match_xmpp_client = Regex::new(r"jabber:client").unwrap();
|
||||
if re_match_xmpp_client.is_match(&response) {
|
||||
probe_facts.is_xmpp_client = true;
|
||||
}
|
||||
|
||||
Ok(probe_facts)
|
||||
}
|
||||
|
||||
async fn route_probe_client_to_server(
|
||||
Query(query): Query<ProbeClientToServerQuery>,
|
||||
) -> Result<String, String> {
|
||||
|
||||
probe_client_to_server(&query.domain, &query.hostname, query.port).await.unwrap();
|
||||
let probe_facts = probe_client_to_server(&query.domain, &query.hostname, query.port).await.unwrap();
|
||||
|
||||
let mut out = String::new();
|
||||
out.push_str(&format!("{} {}:{}", query.domain, query.hostname, query.port));
|
||||
|
||||
out.push_str(&format!("xmpp_probe_is_xmpp_client{{domain=\"{}\", hostname=\"{}\", port=\"{}\"}} {}\n", query.domain, query.hostname, query.port, probe_facts.is_xmpp_client as u8));
|
||||
out.push_str(&format!("xmpp_probe_is_xmpp_stream{{domain=\"{}\", hostname=\"{}\", port=\"{}\"}} {}\n", query.domain, query.hostname, query.port, probe_facts.is_xmpp_stream as u8));
|
||||
out.push_str(&format!("xmpp_probe_success{{domain=\"{}\", hostname=\"{}\", port=\"{}\"}} {}\n", query.domain, query.hostname, query.port, probe_facts.probe_success() as u8));
|
||||
|
||||
return Ok(out);
|
||||
}
|
||||
|
Reference in New Issue
Block a user