|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use hyper::{Response, StatusCode, body::Bytes, server::conn::http1, service::service_fn}; |
| 3 | +use hyper_util::rt::TokioIo; |
| 4 | +use soketto::{ |
| 5 | + BoxedError, |
| 6 | + handshake::http::{Server, is_upgrade_request}, |
| 7 | +}; |
| 8 | +use std::{io, net::SocketAddr}; |
| 9 | +use tokio::net::{TcpListener, TcpStream}; |
| 10 | +use tracing::{debug, info, instrument, warn}; |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + config::Config, |
| 14 | + lazer_publisher::LazerPublisher, |
| 15 | + publisher_handle::{PublisherConnectionContext, handle_publisher}, |
| 16 | +}; |
| 17 | + |
| 18 | +type FullBody = http_body_util::Full<Bytes>; |
| 19 | + |
| 20 | +#[derive(Debug)] |
| 21 | +pub enum Request { |
| 22 | + PublisherV1, |
| 23 | + PublisherV2, |
| 24 | +} |
| 25 | + |
| 26 | +pub struct RelayerRequest(pub http::Request<hyper::body::Incoming>); |
| 27 | + |
| 28 | +const PUBLISHER_WS_URI: &str = "/v1/publisher"; |
| 29 | +const PUBLISHER_WS_URI_V2: &str = "/v2/publisher"; |
| 30 | + |
| 31 | +pub async fn run(config: Config, lazer_publisher: LazerPublisher) -> Result<()> { |
| 32 | + let listener = TcpListener::bind(&config.listen_address).await?; |
| 33 | + info!("listening on {:?}", &config.listen_address); |
| 34 | + |
| 35 | + loop { |
| 36 | + let stream_addr = listener.accept().await; |
| 37 | + let config_clone = config.clone(); |
| 38 | + let lazer_publisher_clone = lazer_publisher.clone(); |
| 39 | + tokio::spawn(async { |
| 40 | + if let Err(err) = |
| 41 | + try_handle_connection(stream_addr, config_clone, lazer_publisher_clone).await |
| 42 | + { |
| 43 | + warn!("error while handling connection: {err:?}"); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +async fn try_handle_connection( |
| 50 | + stream_addr: io::Result<(TcpStream, SocketAddr)>, |
| 51 | + config: Config, |
| 52 | + lazer_publisher: LazerPublisher, |
| 53 | +) -> Result<()> { |
| 54 | + let (stream, remote_addr) = stream_addr?; |
| 55 | + debug!("accepted connection from {}", remote_addr); |
| 56 | + stream.set_nodelay(true)?; |
| 57 | + http1::Builder::new() |
| 58 | + .serve_connection( |
| 59 | + TokioIo::new(stream), |
| 60 | + service_fn(move |r| { |
| 61 | + let request = RelayerRequest(r); |
| 62 | + request_handler( |
| 63 | + request, |
| 64 | + remote_addr, |
| 65 | + config.clone(), |
| 66 | + lazer_publisher.clone(), |
| 67 | + ) |
| 68 | + }), |
| 69 | + ) |
| 70 | + .with_upgrades() |
| 71 | + .await?; |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +#[instrument(skip_all, fields(component = "http_server", remote_addr = remote_addr.to_string()))] |
| 76 | +async fn request_handler( |
| 77 | + request: RelayerRequest, |
| 78 | + remote_addr: SocketAddr, |
| 79 | + config: Config, |
| 80 | + lazer_publisher: LazerPublisher, |
| 81 | +) -> Result<Response<FullBody>, BoxedError> { |
| 82 | + let path = request.0.uri().path(); |
| 83 | + |
| 84 | + let request_type = match path { |
| 85 | + PUBLISHER_WS_URI => Request::PublisherV1, |
| 86 | + PUBLISHER_WS_URI_V2 => Request::PublisherV2, |
| 87 | + _ => { |
| 88 | + return Ok(Response::builder() |
| 89 | + .status(StatusCode::NOT_FOUND) |
| 90 | + .body(FullBody::from("not found")) |
| 91 | + .context("builder failed")?); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + if !is_upgrade_request(&request.0) { |
| 96 | + return Ok(Response::builder() |
| 97 | + .status(StatusCode::BAD_REQUEST) |
| 98 | + .body(FullBody::from("bad request")) |
| 99 | + .context("builder failed")?); |
| 100 | + } |
| 101 | + |
| 102 | + let mut server = Server::new(); |
| 103 | + match server.receive_request(&request.0) { |
| 104 | + Ok(response) => { |
| 105 | + info!("accepted connection from publisher"); |
| 106 | + match request_type { |
| 107 | + Request::PublisherV1 | Request::PublisherV2 => { |
| 108 | + let publisher_connection_context = PublisherConnectionContext { |
| 109 | + request_type, |
| 110 | + publisher_id: config.publisher_id, |
| 111 | + _remote_addr: remote_addr, |
| 112 | + }; |
| 113 | + tokio::spawn(handle_publisher( |
| 114 | + server, |
| 115 | + request.0, |
| 116 | + publisher_connection_context, |
| 117 | + lazer_publisher, |
| 118 | + )); |
| 119 | + Ok(response.map(|()| FullBody::default())) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + Err(e) => { |
| 124 | + warn!("Could not upgrade connection: {}", e); |
| 125 | + Ok(Response::builder() |
| 126 | + .status(StatusCode::INTERNAL_SERVER_ERROR) |
| 127 | + .body(FullBody::from("internal server error")) |
| 128 | + .context("builder failed")?) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments