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

Refactor proxy #814

Merged
merged 67 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
dee7ae3
Add dynamic-proxy
paulgb Sep 23, 2024
ecd45f8
new proxy is integrated
paulgb Sep 23, 2024
bc40f2c
remove extra imports
paulgb Sep 23, 2024
f1a0649
use appropriate imports
paulgb Sep 23, 2024
3aa829a
basic proxy works
paulgb Sep 23, 2024
cb96f31
integrate connection monitor
paulgb Sep 23, 2024
8ccfb1b
redirect test work
paulgb Sep 23, 2024
768880c
https redirect work
paulgb Sep 23, 2024
b1d281a
proxy progress
paulgb Sep 23, 2024
a3129a2
progress
paulgb Sep 23, 2024
11cb6e2
lifecycle test with dummy drone
paulgb Sep 23, 2024
3b40d79
pass test with no connection token
paulgb Sep 24, 2024
779d1ee
get rid of warnings
paulgb Sep 24, 2024
f6b52da
add test for bad connection string
paulgb Sep 24, 2024
77dada1
add bad gateway test
paulgb Sep 24, 2024
b9a7705
add backend timeout test
paulgb Sep 24, 2024
1e12459
add backend accepts test
paulgb Sep 24, 2024
a367217
subdomain verification
paulgb Sep 24, 2024
00a65ff
remove dead code
paulgb Sep 24, 2024
a2f9781
add x-verified- headers
paulgb Sep 24, 2024
dd2dc07
add proxy_headers tests
paulgb Sep 24, 2024
61a770a
add test of CORS headers in error response
paulgb Sep 24, 2024
f7b5062
valid response has CORS headers
paulgb Sep 24, 2024
27d33e5
x-forwarded-* headers
paulgb Sep 24, 2024
ff50340
x-plane-backend-id header
paulgb Sep 24, 2024
6970b6e
error handling in https redirect service
paulgb Sep 24, 2024
0a34df9
nit
paulgb Sep 24, 2024
29b9ff2
proxy comments
paulgb Sep 24, 2024
a9f8b8e
request docs
paulgb Sep 24, 2024
3740756
server
paulgb Sep 24, 2024
b36046a
upgrade
paulgb Sep 24, 2024
aff6fcd
plane-test deps
paulgb Sep 24, 2024
f5e36fb
more cleanup
paulgb Sep 24, 2024
5f5e400
clean up unwraps
paulgb Sep 24, 2024
e580381
format
paulgb Sep 24, 2024
5c445c1
replace unwraps
paulgb Sep 24, 2024
c16a88a
some pr nits
paulgb Sep 24, 2024
b3c7ab3
use consts for headers
paulgb Sep 24, 2024
ef07e70
undo macos bug fix
paulgb Sep 24, 2024
d1f467c
send server header
paulgb Sep 24, 2024
89c0ea0
use GONE header
paulgb Sep 24, 2024
5ddbffb
return MOVED_PERMANENTLY instead of FOUND
paulgb Sep 24, 2024
c11cb6c
proxy ready endpoint
paulgb Sep 24, 2024
ff8b0b9
fix proxy test
paulgb Sep 24, 2024
79508d5
race comment
paulgb Sep 24, 2024
73d4e42
fix root redirect test
paulgb Sep 24, 2024
74d1661
add static token test
paulgb Sep 25, 2024
d17025a
connection monitor touch test
paulgb Sep 25, 2024
404c031
add test for websocket connection monitor
paulgb Sep 25, 2024
8929e2d
remove access-control-allow-credentials header
paulgb Sep 29, 2024
8331c0b
cleaner assert
paulgb Sep 29, 2024
dac2349
remove access-control-allow-credentials check
paulgb Sep 29, 2024
d558c9a
clippy dynamic-proxy
paulgb Sep 30, 2024
b2efbeb
plane-tests clippy
paulgb Oct 1, 2024
f4c8199
add tests for static token and other PR fixes
paulgb Oct 1, 2024
870236c
mark parts added in plane
paulgb Oct 1, 2024
cb9d5b6
pr
paulgb Oct 1, 2024
8a586fa
more nits from pr
paulgb Oct 1, 2024
8933a3d
complete thought
paulgb Oct 1, 2024
b7449be
use expect
paulgb Oct 7, 2024
0844875
use panic in websocket echo server
paulgb Oct 7, 2024
0e0ec60
nits from pr
paulgb Oct 7, 2024
97b0078
nit
paulgb Oct 7, 2024
dc9d644
unify logging
paulgb Oct 7, 2024
ffa530c
Merge branch 'main' into paul/dis-2702-plane-proxy-refactor-to-latest…
paulgb Oct 7, 2024
11c3d5e
add dynamic-proxy to workspace
paulgb Oct 7, 2024
bf17872
add dynamic-proxy to default-members
paulgb Oct 7, 2024
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
784 changes: 688 additions & 96 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"dynamic-proxy",
"plane/plane-tests",
"plane/plane-dynamic",
"plane",
Expand All @@ -12,5 +13,6 @@ members = [
# https://github.com/rust-lang/cargo/pull/9252/files
default-members = [
"plane",
"plane/plane-tests"
"plane/plane-tests",
"dynamic-proxy",
]
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,27 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---

Contains code from hyperium/hyper-util, licensed under the MIT license:

Copyright (c) 2023 Sean McArthur

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions dynamic-proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
29 changes: 29 additions & 0 deletions dynamic-proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "dynamic-proxy"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.89"
bytes = "1.7.2"
http = "1.1.0"
http-body = "1.0.1"
http-body-util = "0.1.2"
hyper = "1.4.1"
hyper-util = { version = "0.1.8", features = ["http1", "http2", "server", "server-graceful", "server-auto", "client", "client-legacy"] }
pin-project-lite = "0.2.14"
rustls = { version = "0.23.13", features = ["ring"] }
thiserror = "1.0.63"
serde = { version = "1.0.210", features = ["derive"] }
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
tokio-rustls = "0.26.0"
tracing = "0.1.40"

[dev-dependencies]
axum = { version = "0.7.6", features = ["http2", "ws"] }
futures-util = "0.3.30"
http = "1.1.0"
rcgen = "0.13.1"
reqwest = { version = "0.12.7", features = ["http2", "stream"] }
serde_json = "1.0.128"
tokio-tungstenite = "0.24.0"
24 changes: 24 additions & 0 deletions dynamic-proxy/src/body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Provides a concrete, boxed body and error type.

use bytes::Bytes;
use http_body::Body;
use http_body_util::combinators::BoxBody;
use http_body_util::{BodyExt, Empty};

pub type BoxedError = Box<dyn std::error::Error + Send + Sync>;

pub type SimpleBody = BoxBody<Bytes, BoxedError>;

pub fn to_simple_body<B>(body: B) -> SimpleBody
where
B: Body<Data = Bytes> + Send + Sync + 'static,
B::Error: Into<BoxedError>,
{
body.map_err(|e| e.into() as BoxedError).boxed()
}

pub fn simple_empty_body() -> SimpleBody {
Empty::<Bytes>::new()
.map_err(|_| unreachable!("Infallable"))
.boxed()
}
101 changes: 101 additions & 0 deletions dynamic-proxy/src/graceful_shutdown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//! Near-identical copy of hyper_util::server::graceful::GracefulShutdown
//! that derives `Clone` and adds a `subscribe` method.
//! https://github.com/hyperium/hyper-util/blob/master/src/server/graceful.rs
michaelsilver marked this conversation as resolved.
Show resolved Hide resolved

paulgb marked this conversation as resolved.
Show resolved Hide resolved
use hyper_util::server::graceful::GracefulConnection;
use pin_project_lite::pin_project;
use std::{
fmt::{self, Debug},
future::Future,
pin::Pin,
task::{self, Poll},
};
use tokio::sync::watch;

#[derive(Clone)] // Added in Plane
pub struct GracefulShutdown {
tx: watch::Sender<()>,
}

impl GracefulShutdown {
/// Create a new graceful shutdown helper.
pub fn new() -> Self {
let (tx, _) = watch::channel(());
Self { tx }
}

/// Wrap a future for graceful shutdown watching.
pub fn watch<C: GracefulConnection>(&self, conn: C) -> impl Future<Output = C::Output> {
let mut rx = self.tx.subscribe();
GracefulConnectionFuture::new(conn, async move {
let _ = rx.changed().await;
// hold onto the rx until the watched future is completed
rx
})
}

// Added in Plane
pub fn subscribe(&self) -> watch::Receiver<()> {
self.tx.subscribe()
}
michaelsilver marked this conversation as resolved.
Show resolved Hide resolved

/// Signal shutdown for all watched connections.
///
/// This returns a `Future` which will complete once all watched
/// connections have shutdown.
pub async fn shutdown(self) {
let Self { tx } = self;

// signal all the watched futures about the change
let _ = tx.send(());
// and then wait for all of them to complete
tx.closed().await;
}
paulgb marked this conversation as resolved.
Show resolved Hide resolved
}

pin_project! {
struct GracefulConnectionFuture<C, F: Future> {
#[pin]
conn: C,
#[pin]
cancel: F,
#[pin]
// If cancelled, this is held until the inner conn is done.
cancelled_guard: Option<F::Output>,
}
}

impl<C, F: Future> GracefulConnectionFuture<C, F> {
fn new(conn: C, cancel: F) -> Self {
Self {
conn,
cancel,
cancelled_guard: None,
}
}
}

impl<C, F: Future> Debug for GracefulConnectionFuture<C, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GracefulConnectionFuture").finish()
}
}

impl<C, F> Future for GracefulConnectionFuture<C, F>
where
C: GracefulConnection,
F: Future,
{
type Output = C::Output;

fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
if this.cancelled_guard.is_none() {
if let Poll::Ready(guard) = this.cancel.poll(cx) {
this.cancelled_guard.set(Some(guard));
this.conn.as_mut().graceful_shutdown();
}
}
this.conn.poll(cx)
}
}
70 changes: 70 additions & 0 deletions dynamic-proxy/src/https_redirect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::body::{simple_empty_body, BoxedError, SimpleBody};
use http::{
header,
uri::{Authority, Scheme},
Request, Response, StatusCode, Uri,
};
use hyper::{body::Incoming, service::Service};
use std::{future::ready, pin::Pin, str::FromStr};

/// A hyper service that redirects HTTP requests to HTTPS.
#[derive(Debug, Clone)]
pub struct HttpsRedirectService;

impl HttpsRedirectService {
fn call_inner(request: Request<Incoming>) -> Result<Response<SimpleBody>, StatusCode> {
// Get the host header.
let hostname = request
.headers()
.get(header::HOST)
.ok_or(StatusCode::BAD_REQUEST)?;
// Parse the host header into an authority.
let authority =
Authority::from_str(hostname.to_str().map_err(|_| StatusCode::BAD_REQUEST)?)
.map_err(|_| StatusCode::BAD_REQUEST)?;
// Strip the port.
let authority =
Authority::from_str(authority.host()).expect("Valid host is always valid authority.");
paulgb marked this conversation as resolved.
Show resolved Hide resolved

let request_uri = request.uri().clone();

// Set the scheme to HTTPS
let mut parts = request_uri.into_parts();
parts.scheme = Some(Scheme::HTTPS);

parts.authority = Some(authority);

// Build the new URI
let new_uri = Uri::from_parts(parts).expect("URI is always valid");

let response = Response::builder()
.status(StatusCode::MOVED_PERMANENTLY)
.header(header::LOCATION, new_uri.to_string())
.body(simple_empty_body());

response.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
}

impl Service<Request<Incoming>> for HttpsRedirectService {
type Response = Response<SimpleBody>;
type Error = BoxedError;
type Future = Pin<Box<std::future::Ready<Result<Response<SimpleBody>, BoxedError>>>>;

fn call(&self, request: Request<Incoming>) -> Self::Future {
let result = Self::call_inner(request);

let result = match result {
Ok(response) => response,
Err(status) => {
tracing::error!("Error redirecting to HTTPS: {}", status);
Response::builder()
.status(status)
.body(simple_empty_body())
.expect("Response is always valid")
}
};

Box::pin(ready(Ok(result)))
}
}
paulgb marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 11 additions & 0 deletions dynamic-proxy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod body;
mod graceful_shutdown;
pub mod https_redirect;
pub mod proxy;
pub mod request;
pub mod server;
mod upgrade;

pub use hyper;
pub use rustls;
pub use tokio_rustls;
Loading
Loading