Rootster
Rootster
CDCloudflare Developers
Created by Rootster on 3/11/2024 in #general-help
Adding OpenSsl to pingora reverse proxy (beginner)
Hi, Im trying to replace nginx reverse proxy/caddyV2 reverse proxy with simple implementation of pingora reverse proxy. So far I've managed to serve my Nuxt3 app with pingora but now I'm stuck with pingora-openssl integration and I couldn't find any examples for it. This is my code so far:
use async_trait::async_trait;

use pingora_core::server::Server;
use pingora_core::upstreams::peer::HttpPeer;
use pingora_core::Result;
use pingora_openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use pingora_proxy::{ProxyHttp, Session};

pub struct MyProxy {}

#[async_trait]
impl ProxyHttp for MyProxy {
type CTX = ();
fn new_ctx(&self) -> Self::CTX {
()
}

async fn request_filter(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result<bool> {
session
.req_header_mut()
.insert_header("Host", "127.0.0.1")
.unwrap();
Ok(false)
}

async fn upstream_peer(
&self,
_session: &mut Session,
_ctx: &mut Self::CTX,
) -> Result<Box<HttpPeer>> {
let addr = ("127.0.0.1", 3000);

let peer = Box::new(HttpPeer::new(addr, false, "127.0.0.1".to_string()));
Ok(peer)
}
}
fn main() {
env_logger::init();
let mut my_server = Server::new(None).unwrap();
my_server.bootstrap();
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
acceptor
.set_private_key_file("key.pem", SslFiletype::PEM)
.unwrap();
acceptor.set_certificate_chain_file("cert.pem").unwrap();
let acceptor = acceptor.build();
let mut my_proxy = pingora_proxy::http_proxy_service(&my_server.configuration, MyProxy {});
my_proxy.add_tcp("0.0.0.0:8888");
my_server.add_service(my_proxy);
my_server.run_forever();
}
use async_trait::async_trait;

use pingora_core::server::Server;
use pingora_core::upstreams::peer::HttpPeer;
use pingora_core::Result;
use pingora_openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use pingora_proxy::{ProxyHttp, Session};

pub struct MyProxy {}

#[async_trait]
impl ProxyHttp for MyProxy {
type CTX = ();
fn new_ctx(&self) -> Self::CTX {
()
}

async fn request_filter(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result<bool> {
session
.req_header_mut()
.insert_header("Host", "127.0.0.1")
.unwrap();
Ok(false)
}

async fn upstream_peer(
&self,
_session: &mut Session,
_ctx: &mut Self::CTX,
) -> Result<Box<HttpPeer>> {
let addr = ("127.0.0.1", 3000);

let peer = Box::new(HttpPeer::new(addr, false, "127.0.0.1".to_string()));
Ok(peer)
}
}
fn main() {
env_logger::init();
let mut my_server = Server::new(None).unwrap();
my_server.bootstrap();
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
acceptor
.set_private_key_file("key.pem", SslFiletype::PEM)
.unwrap();
acceptor.set_certificate_chain_file("cert.pem").unwrap();
let acceptor = acceptor.build();
let mut my_proxy = pingora_proxy::http_proxy_service(&my_server.configuration, MyProxy {});
my_proxy.add_tcp("0.0.0.0:8888");
my_server.add_service(my_proxy);
my_server.run_forever();
}
Can anybody help me to finish Pingora-OpenSsl integration so I can learn how to do it? Thank you!
5 replies