Skip to content

Commit

Permalink
fix(web): use session id instead of remote_addr
Browse files Browse the repository at this point in the history
  • Loading branch information
alixinne committed Nov 8, 2021
1 parent d077b24 commit 282f8da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ pub enum InputSourceName {
Json { peer_addr: SocketAddr },
#[display("Protobuf({peer_addr})")]
Protobuf { peer_addr: SocketAddr },
#[display("Web({peer_addr})")]
Web { peer_addr: SocketAddr },
#[display("Web({session_id})")]
Web { session_id: uuid::Uuid },
#[display("PriorityMuxer")]
PriorityMuxer,
}
Expand Down
9 changes: 3 additions & 6 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn bind(
.map(
|ws: warp::ws::Ws,
session: SessionInstance,
remote: Option<SocketAddr>,
_remote: Option<SocketAddr>,
global: Global| {
(
ws.on_upgrade({
Expand All @@ -40,11 +40,8 @@ pub async fn bind(

async move {
while let Some(result) = rx.next().await {
if let Some(message) = session
.write()
.await
.handle_result(&global, remote.unwrap(), result)
.await
if let Some(message) =
session.write().await.handle_result(&global, result).await
{
if let Err(error) = tx.send(message).await {
warn!(error = %error, "websocket error");
Expand Down
19 changes: 8 additions & 11 deletions src/web/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, convert::TryInto, fmt::Display, net::SocketAddr, sync::Arc};
use std::{collections::HashMap, convert::TryInto, fmt::Display, sync::Arc};

use thiserror::Error;
use tokio::sync::RwLock;
Expand Down Expand Up @@ -28,16 +28,15 @@ pub struct Session {
}

impl Session {
async fn json_api(
&mut self,
global: &Global,
addr: SocketAddr,
) -> Result<&mut ClientConnection, SessionError> {
async fn json_api(&mut self, global: &Global) -> Result<&mut ClientConnection, SessionError> {
if self.json_api.is_none() {
// Can't use SocketAddr, see https://github.com/seanmonstar/warp/issues/830
self.json_api = Some(ClientConnection::new(
global
.register_input_source(
crate::global::InputSourceName::Web { peer_addr: addr },
crate::global::InputSourceName::Web {
session_id: self.id,
},
None,
)
.await?,
Expand All @@ -50,10 +49,9 @@ impl Session {
async fn handle_message(
&mut self,
global: &Global,
addr: SocketAddr,
message: Message,
) -> Result<Message, SessionError> {
let json_api = self.json_api(global, addr).await?;
let json_api = self.json_api(global).await?;

if message.is_text() {
let request: HyperionMessage = serde_json::from_str(message.to_str().unwrap())?;
Expand All @@ -74,7 +72,6 @@ impl Session {
pub async fn handle_result(
&mut self,
global: &Global,
addr: SocketAddr,
result: Result<Message, warp::Error>,
) -> Option<Message> {
match result {
Expand All @@ -85,7 +82,7 @@ impl Session {
return None;
}

let response = self.handle_message(&global, addr, message).await;
let response = self.handle_message(&global, message).await;

trace!(response = ?response, "ws response");

Expand Down

0 comments on commit 282f8da

Please sign in to comment.