From 0cca1f8da30234e9c2f6adab671a066ecc996af2 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Sat, 6 Dec 2025 20:43:49 -0700 Subject: [PATCH] remove admin-netcat thing --- signal-gateway-bin/src/admin_netcat/mod.rs | 84 ---------------------- signal-gateway-bin/src/main.rs | 37 +++------- 2 files changed, 11 insertions(+), 110 deletions(-) delete mode 100644 signal-gateway-bin/src/admin_netcat/mod.rs diff --git a/signal-gateway-bin/src/admin_netcat/mod.rs b/signal-gateway-bin/src/admin_netcat/mod.rs deleted file mode 100644 index 4b4f73a..0000000 --- a/signal-gateway-bin/src/admin_netcat/mod.rs +++ /dev/null @@ -1,84 +0,0 @@ -//! Admin netcat TCP client for forwarding messages to a TCP server -//! -//! This module handles admin messages not handled by the gateway by opening a TCP connection, -//! writing the message terminated with CRLF, and reading the response until CRLF. - -use async_trait::async_trait; -use conf::Conf; -use signal_gateway::{ - AdminMessage, AdminMessageResponse, Context, MessageHandler, MessageHandlerResult, -}; -use std::time::Duration; -use tokio::{ - io::{AsyncBufReadExt, AsyncWriteExt, BufReader}, - net::TcpStream, - time::timeout, -}; - -/// Configuration for the admin netcat TCP client -#[derive(Clone, Conf, Debug)] -#[conf(serde)] -pub struct AdminNetcatConfig { - /// TCP address to forward admin commands to - #[conf(long, env)] - pub tcp_addr: String, - /// Timeout for connecting, writing, and reading - #[conf(long, env, default_value = "5s", value_parser = conf_extra::parse_duration)] - pub timeout: Duration, -} - -impl AdminNetcatConfig { - /// Create a message handler from this config. - /// - /// The returned handler opens a TCP connection to the configured address, - /// writes the message terminated with CRLF, and reads the response until CRLF. - pub fn into_handler(self) -> Box { - Box::new(AdminNetcatHandler { config: self }) - } -} - -/// Message handler that forwards messages to a TCP server. -struct AdminNetcatHandler { - config: AdminNetcatConfig, -} - -#[async_trait] -impl MessageHandler for AdminNetcatHandler { - async fn handle_verified_signal_message( - &self, - msg: AdminMessage, - _context: &dyn Context, - ) -> MessageHandlerResult { - // Connect to server - let mut stream = timeout( - self.config.timeout, - TcpStream::connect(&self.config.tcp_addr), - ) - .await - .map_err(|_| (504u16, "connecting: timeout".into()))? - .map_err(|err| (502u16, format!("connecting: {err}").into()))?; - - // Write message with CRLF terminator - let message = format!("{}\r\n", msg.message); - timeout(self.config.timeout, stream.write_all(message.as_bytes())) - .await - .map_err(|_| (504u16, "writing: timeout".into()))? - .map_err(|err| (502u16, format!("writing: {err}").into()))?; - - // Read response until CR - let mut reader = BufReader::new(stream); - let mut buf = Vec::new(); - timeout(self.config.timeout, reader.read_until(b'\r', &mut buf)) - .await - .map_err(|_| (504u16, "reading: timeout".into()))? - .map_err(|err| (502u16, format!("reading: {err}").into()))?; - - // Convert to string and trim the trailing CR - let text = std::str::from_utf8(&buf) - .map_err(|err| (502u16, format!("utf8: {err}").into()))? - .trim_end_matches(['\r', '\n']) - .to_owned(); - - Ok(AdminMessageResponse::new(text)) - } -} diff --git a/signal-gateway-bin/src/main.rs b/signal-gateway-bin/src/main.rs index 42efb6b..d1f3131 100644 --- a/signal-gateway-bin/src/main.rs +++ b/signal-gateway-bin/src/main.rs @@ -15,8 +15,13 @@ use tracing_subscriber::EnvFilter; mod admin_http; use admin_http::AdminHttpConfig; -mod admin_netcat; -use admin_netcat::AdminNetcatConfig; +/// Handler for admin messages that don't match built-in commands. +#[derive(Subcommands, Debug)] +#[conf(serde)] +pub enum AdminHandlerCommand { + /// Forward unhandled admin messages to an HTTP endpoint. + AdminHttp(AdminHttpConfig), +} mod syslog; use syslog::SyslogConfig; @@ -24,28 +29,6 @@ use syslog::SyslogConfig; pub mod json; use json::JsonConfig; -/// Admin message handler configuration - select how non-command messages are handled -#[derive(Clone, Debug, Subcommands)] -#[conf(serde)] -enum AdminHandlerCommand { - /// Forward (unhandled) admin messages to a TCP endpoint (netcat-style) - /// Useful if an http server would be heavy in the target process - #[conf(name = "admin-netcat")] - Netcat(AdminNetcatConfig), - /// Forward (unhandled) admin messages to an HTTP endpoint via POST - #[conf(name = "admin-http")] - Http(AdminHttpConfig), -} - -impl AdminHandlerCommand { - fn into_handler(self) -> Box { - match self { - AdminHandlerCommand::Netcat(config) => config.into_handler(), - AdminHandlerCommand::Http(config) => config.into_handler(), - } - } -} - /// Top-level configuration for signal-gateway. #[derive(Conf, Debug)] #[conf(serde, test)] @@ -60,7 +43,7 @@ pub struct Config { syslog: Option, #[conf(flatten, prefix)] json: Option, - /// Optional admin message handler (netcat or http) + /// Optional handler for admin messages that don't match built-in commands. #[conf(subcommands)] admin_handler: Option, #[conf(flatten, serde(flatten))] @@ -109,7 +92,9 @@ async fn main() { let token = CancellationToken::new(); - let message_handler = config.admin_handler.map(|c| c.into_handler()); + let message_handler = config.admin_handler.map(|cmd| match cmd { + AdminHandlerCommand::AdminHttp(config) => config.into_handler(), + }); let gateway = Arc::new(Gateway::new(config.gateway, token.clone(), message_handler).await); let listener = TcpListener::bind(config.http_listen_addr).await.unwrap();