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
4 changes: 2 additions & 2 deletions src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ fn add_addresses_to_map(
let incoming_host = match incoming_uri.host() {
Some(h) => h,
_ => {
return Err(Error::Custom(
"could not parse host from source uri".to_string(),
return Err(Error::Addresses(
"Could not parse host from source uri".to_string(),
));
}
};
Expand Down
11 changes: 5 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::errors::Error;
use serde::{Deserialize, Serialize};
use serde_json;
use std::path;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use tokio::fs;

#[derive(Clone, Serialize, Deserialize, Debug)]
Expand All @@ -20,8 +19,8 @@ pub struct Config {
pub dangerous_self_signed_addresses: Option<Vec<Address>>,
}

pub async fn from_filepath(filepath: &PathBuf) -> Result<Config, Error> {
let config_path = match path::absolute(filepath) {
pub async fn from_filepath(filepath: &Path) -> Result<Config, Error> {
let config_path = match filepath.canonicalize() {
Ok(pb) => pb,
Err(e) => return Err(Error::Io(e)),
};
Expand All @@ -34,8 +33,8 @@ pub async fn from_filepath(filepath: &PathBuf) -> Result<Config, Error> {
let parent_dir = match config_path.parent() {
Some(p) => p.to_path_buf(),
_ => {
return Err(Error::Custom(
"parent directory of config not found".to_string(),
return Err(Error::Config(
"Parent directory of config not found".to_string(),
));
}
};
Expand Down
14 changes: 8 additions & 6 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ use std::io::Error as IoError;

#[derive(Debug)]
pub enum Error {
Addresses(String),
Config(String),
Io(IoError),
SerdeJson(SerdeJsonError),
NativeTls(NativeTlsError),
Uri(InvalidUri),
Custom(String),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Io(err) => write!(f, "{}", err),
Error::NativeTls(err) => write!(f, "{}", err),
Error::SerdeJson(err) => write!(f, "{}", err),
Error::Uri(err) => write!(f, "{}", err),
Error::Custom(err) => write!(f, "{}", err),
Error::Addresses(err) => write!(f, "[Error::Addresses] {}", err),
Error::Config(err) => write!(f, "[Error::Config] {}", err),
Error::Io(err) => write!(f, "[Error::Io] {}", err),
Error::NativeTls(err) => write!(f, "[Error::Native_tls] {}", err),
Error::SerdeJson(err) => write!(f, "[Error::Serde_json] {}", err),
Error::Uri(err) => write!(f, "[Error::Uri] {}", err),
}
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async fn main() -> Result<(), Error> {
let args = match env::args().nth(1) {
Some(a) => path::PathBuf::from(a),
None => {
return Err(Error::Custom(
"argument error: argv[0] config path not provided".to_string(),
return Err(Error::Config(
"Config path not provided at argv[0]".to_string(),
));
}
};
Expand Down
10 changes: 5 additions & 5 deletions src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const FAILED_TO_PROCESS_REQUEST_ERROR: &str = "failed to process request";
fn get_host_and_authority<'a>(uri: &Uri) -> Result<(String, String), &'a str> {
let host = match uri.host() {
Some(h) => h.to_string(),
_ => return Err("failed to retrieve downstream URI from upstream URI"),
_ => return Err("Failed to foward incoming URI to outgoing URI"),
};

let port = match uri.port() {
Expand All @@ -40,7 +40,7 @@ fn get_host_and_authority<'a>(uri: &Uri) -> Result<(String, String), &'a str> {
async fn create_tcp_stream<'a>(addr: &str) -> Result<TokioIo<TcpStream>, &'a str> {
match TcpStream::connect(&addr).await {
Ok(client_stream) => Ok(TokioIo::new(client_stream)),
_ => Err("failed to establish tcp connection"),
_ => Err("Failed to establish tcp connection"),
}
}

Expand All @@ -54,18 +54,18 @@ async fn create_tls_stream<'a>(

let cx = match builder.build() {
Ok(c) => c,
_ => return Err("failed to build TLS connection"),
_ => return Err("Failed to build TLS connection"),
};

let tls_connector = tokio_native_tls::TlsConnector::from(cx);
let client_stream = match TcpStream::connect(addr).await {
Ok(s) => s,
_ => return Err("failed to establish TCP connection"),
_ => return Err("Failed to establish TCP connection"),
};

let tls_stream = match tls_connector.connect(host, client_stream).await {
Ok(s) => TokioIo::new(s),
_ => return Err("failed to establish TLS connection"),
_ => return Err("TLS connection failed"),
};

Ok(tls_stream)
Expand Down
7 changes: 3 additions & 4 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ use hyper::http::uri::InvalidUriParts;
use hyper::{Request, Response, StatusCode, Uri};
use std::collections::HashMap;

pub type BoxedResponse = Response<BoxBody<Bytes, hyper::Error>>;

pub const URI_FROM_REQUEST_ERROR: &str = "unable to parse upstream URI from request";
pub const UPSTREAM_URI_ERROR: &str = "falied to update request with upstream URI";

#[derive(Clone, Debug)]
pub struct AddressParams {
Expand All @@ -18,6 +14,9 @@ pub struct AddressParams {
}

pub type AddressMap = HashMap<String, AddressParams>;
pub type BoxedResponse = Response<BoxBody<Bytes, hyper::Error>>;

pub const UPSTREAM_URI_ERROR: &str = "Falied to update request with upstream URI";

fn update_request_with_dest_uri(
mut req: Request<Incoming>,
Expand Down
7 changes: 4 additions & 3 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::requests;
use crate::response;
use crate::response::{AddressMap, BoxedResponse, build_response};
use hyper::body::Incoming;
use hyper::http;
Expand All @@ -9,6 +8,8 @@ use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

pub const URI_FROM_REQUEST_ERROR: &str = "Unnable to parse upstream URI from request";

pub struct Svc {
pub addresses: Arc<AddressMap>,
}
Expand All @@ -27,7 +28,7 @@ impl Service<Request<Incoming>> for Svc {
return Box::pin(async move {
requests::create_fallback_response(
&StatusCode::BAD_REQUEST,
&response::URI_FROM_REQUEST_ERROR,
URI_FROM_REQUEST_ERROR,
)
});
}
Expand All @@ -40,7 +41,7 @@ impl Service<Request<Incoming>> for Svc {
return Box::pin(async move {
requests::create_fallback_response(
&StatusCode::BAD_GATEWAY,
&response::URI_FROM_REQUEST_ERROR,
URI_FROM_REQUEST_ERROR,
)
});
}
Expand Down
Loading