Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hyperware_process_lib"
authors = ["Sybil Technologies AG"]
version = "2.3.1"
version = "3.0.0"
edition = "2021"
description = "A library for writing Hyperware processes in Rust."
homepage = "https://hyperware.ai"
Expand Down
36 changes: 33 additions & 3 deletions src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ pub enum HttpServerRequest {
WebSocketOpen {
path: String,
channel_id: u32,
#[serde(default)]
source_socket_addr: Option<String>,
/// IP address from proxy headers (X-Forwarded-For, X-Real-IP, Cf-Connecting-Ip)
#[serde(default)]
forwarded_for: Option<String>,
},
/// Processes can both SEND and RECEIVE this kind of [`crate::Request`]
/// (send as [`HttpServerAction::WebSocketPush`]).
Expand Down Expand Up @@ -300,6 +305,8 @@ pub struct HttpServer {
ws_paths: HashMap<String, WsBindingConfig>,
/// A mapping of WebSocket paths to the channels that are open on them.
ws_channels: HashMap<String, HashSet<u32>>,
/// A mapping of WebSocket channel IDs to their client socket addresses.
ws_channel_addrs: HashMap<u32, String>,
/// The timeout given for `http-server:distro:sys` to respond to a configuration request.
pub timeout: u64,
}
Expand Down Expand Up @@ -451,6 +458,7 @@ impl HttpServer {
http_paths: HashMap::new(),
ws_paths: HashMap::new(),
ws_channels: HashMap::new(),
ws_channel_addrs: HashMap::new(),
timeout,
}
}
Expand Down Expand Up @@ -983,18 +991,35 @@ impl HttpServer {
}

/// Handle a WebSocket open event from the HTTP server.
pub fn handle_websocket_open(&mut self, path: &str, channel_id: u32) {
pub fn handle_websocket_open(
&mut self,
path: &str,
channel_id: u32,
source_socket_addr: Option<String>,
forwarded_for: Option<String>,
) {
self.ws_channels
.entry(path.to_string())
.or_insert(HashSet::new())
.insert(channel_id);
// Store the client IP, preferring forwarded_for (from proxy headers) over socket addr
let client_ip = forwarded_for.or(source_socket_addr);
if let Some(ip) = client_ip {
self.ws_channel_addrs.insert(channel_id, ip);
}
}

/// Get the socket address for a WebSocket channel.
pub fn get_ws_channel_addr(&self, channel_id: u32) -> Option<&String> {
self.ws_channel_addrs.get(&channel_id)
}

/// Handle a WebSocket close event from the HTTP server.
pub fn handle_websocket_close(&mut self, channel_id: u32) {
self.ws_channels.iter_mut().for_each(|(_, channels)| {
channels.remove(&channel_id);
});
self.ws_channel_addrs.remove(&channel_id);
}

pub fn parse_request(&self, body: &[u8]) -> Result<HttpServerRequest, HttpServerError> {
Expand Down Expand Up @@ -1024,8 +1049,13 @@ impl HttpServer {
channel_id,
message_type,
} => ws_handler(channel_id, message_type, last_blob().unwrap_or_default()),
HttpServerRequest::WebSocketOpen { path, channel_id } => {
self.handle_websocket_open(&path, channel_id);
HttpServerRequest::WebSocketOpen {
path,
channel_id,
source_socket_addr,
forwarded_for,
} => {
self.handle_websocket_open(&path, channel_id, source_socket_addr, forwarded_for);
}
HttpServerRequest::WebSocketClose(channel_id) => {
self.handle_websocket_close(channel_id);
Expand Down
16 changes: 16 additions & 0 deletions src/hyperapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ pub fn get_server() -> Option<&'static mut HttpServer> {
APP_HELPERS.with(|ctx| ctx.borrow().current_server.map(|ptr| unsafe { &mut *ptr }))
}

/// Get the socket address for a WebSocket channel by its ID.
/// Returns None if the channel doesn't exist or has no recorded address.
pub fn get_ws_channel_addr(channel_id: u32) -> Option<String> {
get_server().and_then(|server| server.get_ws_channel_addr(channel_id).cloned())
}

pub fn get_http_request() -> Option<IncomingHttpRequest> {
APP_HELPERS.with(|helpers| {
helpers
.borrow()
.current_http_context
.as_ref()
.map(|ctx| ctx.request.clone())
})
}

pub fn get_http_method() -> Option<String> {
APP_HELPERS.with(|helpers| {
helpers
Expand Down