8000 Improve header management on websocket proxying by ostenbom · Pull Request #241 · mentimeter/linkup · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve header management on websocket proxying #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
5 changes: 4 additions & 1 deletion linkup-cli/src/services/local_server.rs
8000
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ impl LocalServer {
let mut command = process::Command::new(
env::current_exe().context("Failed to get the current executable")?,
);
command.env("RUST_LOG", "debug");
command.env(
"RUST_LOG",
"info,hickory_server=warn,hyper_util=warn,h2=warn,tower_http=info",
);
Comment on lines +62 to +65
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently stderr is very noisy, this makes it slightly less noisy

command.env("LINKUP_SERVICE_ID", Self::ID);
command.args([
"server",
Expand Down
31 changes: 21 additions & 10 deletions local-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use hickory_server::{
},
ServerFuture,
};
use http::{header::HeaderMap, Uri};
use http::{header::HeaderMap, HeaderValue, Uri};
use hyper_rustls::HttpsConnector;
use hyper_util::{
client::legacy::{connect::HttpConnector, Client},
Expand Down Expand Up @@ -237,13 +237,25 @@ async fn linkup_request_handler(
}

let uri = url.parse::<Uri>().unwrap();
let host = uri.host().unwrap().to_string();
let mut upstream_request = uri.into_client_request().unwrap();

// Copy over all headers from the incoming request
for (key, value) in req.headers() {
upstream_request.headers_mut().insert(key, value.clone());
}

// add the extra headers that linkup wants
let extra_http_headers: HeaderMap = extra_headers.into();
for (key, value) in extra_http_headers.iter() {
upstream_request.headers_mut().insert(key, value.clone());
}

// Overriding host header neccesary for tokio_tungstenite
upstream_request
.headers_mut()
.insert(http::header::HOST, HeaderValue::from_str(&host).unwrap());

let (upstream_ws_stream, upstream_response) =
match tokio_tungstenite::connect_async(upstream_request).await {
Ok(connection) => connection,
Expand All @@ -258,25 +270,24 @@ async fn linkup_request_handler(
return Response::builder()
.status(StatusCode::BAD_GATEWAY)
.body(Body::from(error.to_string()))
.unwrap()
.unwrap();
}
},
};

let mut upstream_upgrade_response =
let mut downstream_upgrade_response =
downstream_upgrade.on_upgrade(ws::context_handle_socket(upstream_ws_stream));

let websocket_upgrade_response_headers = upstream_upgrade_response.headers_mut();
let downstream_response_headers = downstream_upgrade_response.headers_mut();

// The headers from the upstream response are more important - trust the upstream server
for upstream_header in upstream_response.headers() {
if !websocket_upgrade_response_headers.contains_key(upstream_header.0) {
websocket_upgrade_response_headers
.append(upstream_header.0, upstream_header.1.clone());
}
downstream_response_headers.insert(upstream_header.0, upstream_header.1.clone());
}

websocket_upgrade_response_headers.extend(allow_all_cors());
downstream_response_headers.extend(allow_all_cors());

upstream_upgrade_response
downstream_upgrade_response
}
None => handle_http_req(req, target_service, extra_headers, client).await,
}
Expand Down
10 changes: 5 additions & 5 deletions local-server/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ pub fn context_handle_socket(
}
_ => {
if let Err(e) = upstream_write.send(tungstenite_message).await {
eprintln!("Error sending message to upstream: {}", e);
println!("Error sending message to upstream: {}", e);
break;
}
}
}
}
Err(e) => {
eprint!("Got error on reading message from downstream: {}", e);
println!("Got error on reading message from downstream: {}", e);
break;
}
},
Expand All @@ -128,14 +128,14 @@ pub fn context_handle_socket(
}
_ => {
if let Err(e) = downstream_write.send(axum_message).await {
eprintln!("Error sending message to upstream: {}", e);
println!("Error sending message to upstream: {}", e);
break;
}
}
}
}
Err(e) => {
eprint!("Got error on reading message from upstream: {}", e);
println!("Got error on reading message from upstream: {}", e);
break;
}
},
Expand All @@ -144,7 +144,7 @@ pub fn context_handle_socket(
// this might be better than panicking? Or do we want to "fail loudly" here?
//
// https://docs.rs/tokio/latest/tokio/macro.select.html#panics
eprint!("Received unexpected message: {other:?}");
println!("Received unexpected message: {other:?}");

break;
}
Expand Down
0