Try to fix things

This commit is contained in:
Alex 2021-12-08 13:28:07 +01:00
parent 35b46e64e7
commit c49b2075a3
No known key found for this signature in database
GPG key ID: EDABF9711E244EB1
5 changed files with 46 additions and 7 deletions

View file

@ -25,7 +25,11 @@ pub struct CertStore {
}
impl CertStore {
pub fn new(consul: Consul, rx_proxy_config: watch::Receiver<Arc<ProxyConfig>>, letsencrypt_email: String) -> Arc<Self> {
pub fn new(
consul: Consul,
rx_proxy_config: watch::Receiver<Arc<ProxyConfig>>,
letsencrypt_email: String,
) -> Arc<Self> {
Arc::new(Self {
consul,
certs: RwLock::new(HashMap::new()),

View file

@ -117,6 +117,7 @@ async fn handle(
let to_addr = format!("http://{}", proxy_to.target_addr);
info!("Proxying {} {} -> {}", host, path, to_addr);
trace!("Request: {:?}", req);
let mut response = reverse_proxy::call(remote_addr.ip(), &to_addr, req).await?;
@ -126,6 +127,7 @@ async fn handle(
HeaderValue::from_str(value)?,
);
}
trace!("Response: {:?}", response);
Ok(response)
} else {

View file

@ -54,10 +54,7 @@ struct Opt {
pub https_bind_addr: SocketAddr,
/// E-mail address for Let's Encrypt certificate requests
#[structopt(
long = "letsencrypt-email",
env = "TRICOT_LETSENCRYPT_EMAIL",
)]
#[structopt(long = "letsencrypt-email", env = "TRICOT_LETSENCRYPT_EMAIL")]
pub letsencrypt_email: String,
}
@ -75,7 +72,11 @@ async fn main() {
let consul = consul::Consul::new(&opt.consul_addr, &opt.consul_kv_prefix, &opt.node_name);
let mut rx_proxy_config = proxy_config::spawn_proxy_config_task(consul.clone());
let cert_store = cert_store::CertStore::new(consul.clone(), rx_proxy_config.clone(), opt.letsencrypt_email.clone());
let cert_store = cert_store::CertStore::new(
consul.clone(),
rx_proxy_config.clone(),
opt.letsencrypt_email.clone(),
);
tokio::spawn(cert_store.clone().watch_proxy_config());
tokio::spawn(http::serve_http(opt.http_bind_addr, consul.clone()));

View file

@ -136,6 +136,8 @@ fn parse_tricot_add_header_tag(tag: &str) -> Option<(String, String)> {
}
fn parse_consul_catalog(catalog: &ConsulNodeCatalog) -> Vec<ProxyEntry> {
trace!("Parsing node catalog: {:#?}", catalog);
let mut entries = vec![];
for (_, svc) in catalog.services.iter() {
@ -168,6 +170,11 @@ fn parse_consul_catalog(catalog: &ConsulNodeCatalog) -> Vec<ProxyEntry> {
}
}
trace!("Result of parsing catalog:");
for ent in entries.iter() {
trace!(" {}", ent);
}
entries
}

View file

@ -2,7 +2,10 @@
//! See there for original Copyright notice
use anyhow::Result;
use log::*;
use std::convert::TryInto;
use http::header::HeaderName;
use hyper::header::{HeaderMap, HeaderValue};
use hyper::{Body, Client, Request, Response, Uri};
use lazy_static::lazy_static;
@ -63,7 +66,9 @@ fn create_proxied_request<B>(
forward_url: &str,
request: Request<B>,
) -> Result<Request<B>> {
let mut builder = Request::builder().uri(forward_uri(forward_url, &request)?);
let mut builder = Request::builder()
.method(request.method())
.uri(forward_uri(forward_url, &request)?);
*builder.headers_mut().unwrap() = remove_hop_headers(request.headers());
@ -95,6 +100,21 @@ fn create_proxied_request<B>(
}
}
if let Some(conn) = request.headers().get("connection") {
if conn.to_str()?.to_lowercase() == "upgrade" {
if let Some(upgrade) = request.headers().get("upgrade") {
builder.headers_mut().unwrap().insert(
HeaderName::from_bytes(b"connection")?,
"Upgrade".try_into()?,
);
builder
.headers_mut()
.unwrap()
.insert(HeaderName::from_bytes(b"upgrade")?, upgrade.clone());
}
}
}
Ok(builder.body(request.into_body())?)
}
@ -105,8 +125,13 @@ pub async fn call(
) -> Result<Response<Body>> {
let proxied_request = create_proxied_request(client_ip, &forward_uri, request)?;
trace!("Proxied request: {:?}", proxied_request);
let client = Client::new();
let response = client.request(proxied_request).await?;
trace!("Inner response: {:?}", response);
let proxied_response = create_proxied_response(response);
Ok(proxied_response)
}