Add flag to read http headers from a file

This commit is contained in:
Σrebe - Romain GERARD 2024-01-25 19:16:11 +01:00
parent 13aa664caf
commit f0cb4ab671
No known key found for this signature in database
GPG key ID: 7A42B4B97E0332F4
4 changed files with 59 additions and 2 deletions

View file

@ -1,4 +1,4 @@
use crate::tunnel::transport::{TunnelRead, TunnelWrite, MAX_PACKET_LENGTH};
use crate::tunnel::transport::{headers_from_file, TunnelRead, TunnelWrite, MAX_PACKET_LENGTH};
use crate::tunnel::{tunnel_to_jwt_token, RemoteAddr};
use crate::WsClientConfig;
use anyhow::{anyhow, Context};
@ -124,11 +124,19 @@ pub async fn connect(
let _ = headers.remove(k);
headers.append(k, v.clone());
}
if let Some(auth) = &client_cfg.http_upgrade_credentials {
let _ = headers.remove(AUTHORIZATION);
headers.append(AUTHORIZATION, auth.clone());
}
if let Some(headers_file_path) = &client_cfg.http_headers_file {
for (k, v) in headers_from_file(headers_file_path) {
let _ = headers.remove(&k);
headers.append(k, v);
}
}
let (tx, rx) = mpsc::channel::<Bytes>(1024);
let body = StreamBody::new(ReceiverStream::new(rx).map(|s| -> anyhow::Result<Frame<Bytes>> { Ok(Frame::data(s)) }));
let req = req.body(body).with_context(|| {

View file

@ -1,8 +1,14 @@
use crate::tunnel::transport::http2::{Http2TunnelRead, Http2TunnelWrite};
use crate::tunnel::transport::websocket::{WebsocketTunnelRead, WebsocketTunnelWrite};
use bytes::BytesMut;
use hyper::http::{HeaderName, HeaderValue};
use std::future::Future;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::str::FromStr;
use tokio::io::AsyncWrite;
use tracing::error;
pub mod http2;
pub mod io;
@ -72,3 +78,25 @@ impl TunnelWrite for TunnelWriter {
}
}
}
#[inline]
pub fn headers_from_file(path: &Path) -> Vec<(HeaderName, HeaderValue)> {
let file = match std::fs::File::open(path) {
Ok(file) => file,
Err(err) => {
error!("Cannot read headers from file: {:?}: {:?}", path, err);
return vec![];
}
};
BufReader::new(file)
.lines()
.filter_map(|line| {
let line = line.ok()?;
let (header, value) = line.split_once(':')?;
let header = HeaderName::from_str(header.trim()).ok()?;
let value = HeaderValue::from_str(value.trim()).ok()?;
Some((header, value))
})
.collect()
}

View file

@ -1,4 +1,4 @@
use crate::tunnel::transport::{TunnelRead, TunnelWrite, MAX_PACKET_LENGTH};
use crate::tunnel::transport::{headers_from_file, TunnelRead, TunnelWrite, MAX_PACKET_LENGTH};
use crate::tunnel::{tunnel_to_jwt_token, RemoteAddr, JWT_HEADER_PREFIX};
use crate::WsClientConfig;
use anyhow::{anyhow, Context};
@ -160,11 +160,19 @@ pub async fn connect(
let _ = headers.remove(k);
headers.append(k, v.clone());
}
if let Some(auth) = &client_cfg.http_upgrade_credentials {
let _ = headers.remove(AUTHORIZATION);
headers.append(AUTHORIZATION, auth.clone());
}
if let Some(headers_file_path) = &client_cfg.http_headers_file {
for (k, v) in headers_from_file(headers_file_path) {
let _ = headers.remove(&k);
headers.append(k, v);
}
}
let req = req.body(Empty::<Bytes>::new()).with_context(|| {
format!(
"failed to build HTTP request to contact the server {:?}",