oa_gateway_owp/server.rs
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
//! Accept loop and WebSocket handshake. Sessions live in `session`.
//!
//! This module refuses extra connections instead of queueing them, so a
//! caller learns immediately and the accept loop keeps draining.
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use oa_gateway_adapter::tls::{MaybeTlsStream, ServerTls};
use oa_gateway_adapter::AdapterError;
use oa_gateway_core::{AdapterId, Engine};
use oa_gateway_uci::Schema;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::Semaphore;
use tokio::time::timeout;
use tokio_tungstenite::accept_hdr_async_with_config;
use tokio_tungstenite::tungstenite::handshake::server::{ErrorResponse, Request, Response};
use tokio_tungstenite::tungstenite::http::{HeaderValue, StatusCode};
use tokio_tungstenite::tungstenite::protocol::WebSocketConfig;
use tokio_util::sync::CancellationToken;
use tracing::{debug, info, warn};
use crate::config::OwpConfig;
use crate::session::{self, Session};
/// Budget for a client to complete the TLS handshake, when TLS is
/// configured. Without a limit, a peer that opens TCP and never speaks TLS
/// would hold a connection permit forever.
const TLS_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);
/// OWP/WebSocket server adapter.
///
/// [`Self::new`] does not bind. [`Self::serve`] takes a listener the
/// host already bound. A compiled UCI schema is optional and is
/// attached with [`Self::with_schema`].
pub struct OwpAdapter {
id: AdapterId,
config: OwpConfig,
conn_seq: AtomicU64,
schema: Option<Arc<Schema>>,
tls: Option<ServerTls>,
/// One permit per allowed connection, held for the life of the session.
connections: Arc<Semaphore>,
/// Set while connections are being refused, so saturation is logged on the
/// way in and on the way out instead of once per rejected connection.
at_capacity: AtomicBool,
}
impl OwpAdapter {
/// Builds an adapter that is not yet listening.
///
/// The connection semaphore is sized from
/// [`OwpConfig::max_connections`]. No schema is attached until
/// [`Self::with_schema`], and the listener is plaintext until
/// [`Self::with_tls`].
#[must_use]
pub fn new(id: impl Into<AdapterId>, config: OwpConfig) -> Self {
let connections = Arc::new(Semaphore::new(config.max_connections));
Self {
id: id.into(),
config,
conn_seq: AtomicU64::new(1),
schema: None,
tls: None,
connections,
at_capacity: AtomicBool::new(false),
}
}
/// Supply the UCI schema used to convert between OMS JSON and UCI XML.
///
/// Without one the adapter still routes, but it cannot convert: XML payloads
/// keep their topic as the type hint and are forwarded verbatim. A schema is
/// mandatory for [`OwpConfig::xml_baseline`], which the host enforces at
/// startup so the failure surfaces before any traffic arrives.
#[must_use]
pub fn with_schema(mut self, schema: Arc<Schema>) -> Self {
self.schema = Some(schema);
self
}
/// Terminate TLS on every accepted connection.
///
/// Without this the listener stays plaintext, which is unchanged
/// behavior for a deployment that configures no certificate.
#[must_use]
pub fn with_tls(mut self, tls: ServerTls) -> Self {
self.tls = Some(tls);
self
}
#[must_use]
pub fn id(&self) -> &AdapterId {
&self.id
}
#[must_use]
pub fn config(&self) -> &OwpConfig {
&self.config
}
/// Accepts connections on `listener` until `shutdown` is cancelled.
///
/// A failed `accept` is logged and the loop continues. At the
/// connection limit the TCP stream is dropped immediately; the
/// saturation warning is logged once on the way in and once on the
/// way out, not once per refused peer.
///
/// # Errors
///
/// Returns [`AdapterError::Io`] if the local address of `listener`
/// cannot be read. Handshake and session failures do not fail
/// `serve`.
pub async fn serve(
self: Arc<Self>,
listener: TcpListener,
engine: Arc<Engine>,
shutdown: CancellationToken,
) -> Result<(), AdapterError> {
let local = listener.local_addr()?;
info!(%local, adapter = %self.id, tls = self.tls.is_some(), "owp listening");
loop {
tokio::select! {
() = shutdown.cancelled() => {
info!(adapter = %self.id, "owp shutting down");
return Ok(());
}
accepted = listener.accept() => {
let (stream, peer) = match accepted {
Ok(v) => v,
Err(err) => {
warn!(error = %err, "accept failed");
continue;
}
};
// Refuse rather than queue: a caller that cannot get a slot
// learns immediately, and the accept loop keeps draining so
// the backlog does not become the queue instead.
let Ok(permit) = Arc::clone(&self.connections).try_acquire_owned() else {
if !self.at_capacity.swap(true, Ordering::Relaxed) {
warn!(
adapter = %self.id,
limit = self.config.max_connections,
"at the connection limit, refusing new connections"
);
}
drop(stream);
continue;
};
if self.at_capacity.swap(false, Ordering::Relaxed) {
info!(adapter = %self.id, "below the connection limit, accepting again");
}
let conn_id = self.conn_seq.fetch_add(1, Ordering::Relaxed);
let this = Arc::clone(&self);
let engine = Arc::clone(&engine);
let shutdown = shutdown.clone();
tokio::spawn(async move {
if let Err(err) = this.handle_connection(stream, peer, conn_id, engine, shutdown).await {
debug!(%peer, conn_id, error = %err, "owp connection ended");
}
drop(permit);
});
}
}
}
}
/// Handshakes one TCP stream and runs its OWP session.
///
/// A failed WebSocket handshake is logged and returns [`Ok`], so a
/// bad client does not take the accept loop down. Oversized frames
/// are rejected by the WebSocket cap before the payload is fully
/// buffered.
async fn handle_connection(
&self,
stream: TcpStream,
peer: SocketAddr,
conn_id: u64,
engine: Arc<Engine>,
shutdown: CancellationToken,
) -> Result<(), AdapterError> {
let stream: MaybeTlsStream<TcpStream> = match &self.tls {
Some(tls) => match timeout(TLS_HANDSHAKE_TIMEOUT, tls.accept(stream)).await {
Ok(Ok(stream)) => stream,
Ok(Err(err)) => {
debug!(%peer, error = %err, "tls handshake failed");
return Ok(());
}
Err(_) => {
debug!(%peer, "tls handshake timed out");
return Ok(());
}
},
None => MaybeTlsStream::Plain(stream),
};
// A frame over the cap is a protocol error the library reports on read,
// which ends the session — the same outcome as an unparseable STOMP
// frame, and it happens before the payload is fully buffered.
let ws_config = WebSocketConfig::default()
.max_message_size(Some(self.config.max_frame_size))
.max_frame_size(Some(self.config.max_frame_size));
let allowed_origins = &self.config.allowed_origins;
let check =
|req: &Request, response: Response| handshake_check(allowed_origins, req, response);
let ws = match accept_hdr_async_with_config(stream, check, Some(ws_config)).await {
Ok(ws) => ws,
Err(err) => {
debug!(%peer, error = %err, "websocket handshake failed");
return Ok(());
}
};
session::run(Session {
adapter_id: self.id.clone(),
config: self.config.clone(),
schema: self.schema.clone(),
conn_id,
ws,
engine,
shutdown,
})
.await
}
}
#[allow(clippy::result_large_err)]
/// Vets one WebSocket handshake: the `owp` subprotocol, then the `Origin`.
///
/// Missing or unmatched `Sec-WebSocket-Protocol` is `400`. When
/// `allowed_origins` is non-empty, an `Origin` header that is not one of
/// its entries verbatim (a missing `Origin` included) is `403`; an empty
/// `allowed_origins` skips the check and accepts any origin. On success
/// the response echoes `owp`.
fn handshake_check(
allowed_origins: &[String],
req: &Request,
mut response: Response,
) -> Result<Response, ErrorResponse> {
let has_owp = req
.headers()
.get("Sec-WebSocket-Protocol")
.and_then(|v| v.to_str().ok())
.is_some_and(|s| {
s.split(',')
.map(str::trim)
.any(|p| p.eq_ignore_ascii_case("owp"))
});
if !has_owp {
return Err(error_response(
StatusCode::BAD_REQUEST,
"missing owp subprotocol",
));
}
if !allowed_origins.is_empty() {
let origin = req.headers().get("Origin").and_then(|v| v.to_str().ok());
if !origin.is_some_and(|o| allowed_origins.iter().any(|a| a == o)) {
return Err(error_response(StatusCode::FORBIDDEN, "origin not allowed"));
}
}
response
.headers_mut()
.insert("Sec-WebSocket-Protocol", HeaderValue::from_static("owp"));
Ok(response)
}
/// A handshake rejection with `status` and a short reason line.
fn error_response(status: StatusCode, reason: &'static str) -> ErrorResponse {
let mut err = ErrorResponse::new(Some(reason.into()));
*err.status_mut() = status;
err
}