Skip to content
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

Feature/tls #5

Merged
merged 7 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: add test for tls connection
  • Loading branch information
nixpig committed Jan 20, 2024
commit c4258739c74c51eebb3b5228af1990c41aa771cc
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ tokio-native-tls = "0.3.1"

[dev-dependencies]
actix-web = "4.4.1"
reqwest = { version = "0.11.23", features = ["json"] }
reqwest = { version = "0.11.23", features = ["json", "native-tls"] }
17 changes: 5 additions & 12 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,12 @@ fn spawn_regular_server(stream: TcpStream, settings: Arc<Settings>) {
service_fn(move |req| handle(req, settings.clone())),
)
.await
{}
{
eprintln!("\x1b[31mERR\x1b[0m Error serving connection: {}", e);
}
});
}

async fn hello(
_: Request<hyper::body::Incoming>,
) -> Result<
Response<http_body_util::Full<hyper::body::Bytes>>,
std::convert::Infallible,
> {
Ok(Response::new(http_body_util::Full::new(
hyper::body::Bytes::from("Hello, World!"),
)))
}

async fn handle(
req: Request<Incoming>,
settings: Arc<Settings>,
Expand Down Expand Up @@ -135,7 +126,9 @@ async fn handle(

let proxy_uri = proxy_request.uri().clone();

println!("sending request");
let res = send_request(client, proxy_request).await?;
println!("got response");
let status = res.status().as_u16();

println!(
Expand Down
49 changes: 49 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use reqwest::StatusCode;
use serial_test::serial;
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::net::TcpListener;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -546,6 +547,54 @@ async fn test_response_codes() -> Result<(), Box<dyn Error>> {
Ok(())
}

#[serial]
#[tokio::test]
async fn test_tls_server() -> Result<(), Box<dyn Error>> {
let settings = Settings {
host: String::from("localhost"),
local_port: 7878,
proxies: vec![ProxyConfig::from_str(":3016").unwrap()],
config: None,
tls: true,
};

start_remote(3016, "/").await;
start_joubini(settings).await;

let pem = fs::read("localhost.crt").unwrap();
let key = fs::read("localhost.key").unwrap();
let root = fs::read("myCA.pem").unwrap();

let cert = reqwest::Identity::from_pkcs8_pem(&pem, &key)?;
let ca = reqwest::Certificate::from_pem(&root)?;

let client = reqwest::Client::builder()
.use_native_tls()
.identity(cert)
.add_root_certificate(ca)
.pool_max_idle_per_host(0)
.build()?;

let res = client.get("https://localhost:7878").send().await?;

let status = res.status();
assert_eq!(status, reqwest::StatusCode::OK);

let body = res.text().await?;
assert_eq!(body, "get_ok");

Ok(())
}

// TODO: http2
// let client = reqwest::Client::builder()
// .use_native_tls()
// .identity(cert)
// .add_root_certificate(ca)
// .pool_max_idle_per_host(0)
// .http2_prior_knowledge()
// .build()?;

async fn start_joubini(settings: Settings) {
let listener = Arc::new(
tokio::net::TcpListener::bind(format!(
Expand Down