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();
}