feat: updates
This commit is contained in:
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -146,6 +146,12 @@ version = "0.21.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@@ -1364,6 +1370,7 @@ name = "proxy-inspector"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.0",
|
||||
"http 1.1.0",
|
||||
"log",
|
||||
"pingora",
|
||||
@@ -1455,7 +1462,7 @@ version = "0.11.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"base64 0.21.7",
|
||||
"bytes",
|
||||
"encoding_rs",
|
||||
"futures-core",
|
||||
@@ -1570,7 +1577,7 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"base64 0.21.7",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -13,3 +13,4 @@ async-trait = "0.1"
|
||||
log = "0.4"
|
||||
http = "1.1"
|
||||
structopt = "0.3"
|
||||
base64 = "0.22.0"
|
||||
|
||||
39
src/app.rs
39
src/app.rs
@@ -1,6 +1,9 @@
|
||||
use async_trait::async_trait;
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use http::HeaderName;
|
||||
use log::debug;
|
||||
use log::{debug, info};
|
||||
use pingora::{Error, ErrorType};
|
||||
use pingora::prelude::{HttpPeer, ProxyHttp, Result, Session};
|
||||
|
||||
use super::service::HostConfig;
|
||||
@@ -28,6 +31,10 @@ impl ProxyHttp for ProxyApp {
|
||||
.expect("get host from http header failed");
|
||||
debug!("host header: {host_header}");
|
||||
|
||||
if host_header == "localhost" || host_header.starts_with("localhost:") {
|
||||
return Err(Error::new(ErrorType::CustomCode("bad host", 400)));
|
||||
}
|
||||
|
||||
let host_config = self
|
||||
.host_configs
|
||||
.iter()
|
||||
@@ -41,4 +48,34 @@ impl ProxyHttp for ProxyApp {
|
||||
let peer = Box::new(proxy_to);
|
||||
Ok(peer)
|
||||
}
|
||||
|
||||
async fn request_filter(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result<bool>
|
||||
where Self::CTX: Send + Sync,
|
||||
{
|
||||
let request_header = session.req_header();
|
||||
let mut req = String::with_capacity(512);
|
||||
req.push_str(request_header.method.as_str());
|
||||
req.push(' ');
|
||||
req.push_str(&request_header.uri.to_string());
|
||||
req.push(' ');
|
||||
req.push_str(&format!("{:?}\n", request_header.version));
|
||||
let header_len = request_header.headers.len();
|
||||
request_header.headers.iter().enumerate().for_each(|(i, (n, v))| {
|
||||
req.push_str(
|
||||
&format!("{}: {}{}",
|
||||
n.as_str(),
|
||||
v.to_str().unwrap_or("ERROR!BAD-VALUE!"),
|
||||
if i < header_len - 1 { "\n" } else { "" }
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
let body = match session.read_request_body().await {
|
||||
Ok(Some(body_bytes)) => Some(STANDARD.encode(body_bytes)),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
info!("Request:\n{}\n\n{}", req, body.unwrap_or_else(|| "<None>".into()));
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
52
src/main.rs
52
src/main.rs
@@ -1,9 +1,9 @@
|
||||
use log::{info, LevelFilter};
|
||||
use pingora::{
|
||||
server::{configuration::Opt, Server},
|
||||
services::{listening::Service as ListeningService, Service},
|
||||
};
|
||||
|
||||
use service::HostConfig;
|
||||
use pretty_env_logger::env_logger::Builder;
|
||||
use structopt::StructOpt;
|
||||
|
||||
mod app;
|
||||
@@ -16,31 +16,39 @@ pub fn main() {
|
||||
let mut my_server = Server::new(opt).unwrap();
|
||||
my_server.bootstrap();
|
||||
|
||||
let proxy_service_tcp = service::proxy_service_tcp(
|
||||
&my_server.configuration,
|
||||
"0.0.0.0:8800",
|
||||
vec![],
|
||||
);
|
||||
|
||||
let proxy_service_ssl2 = service::proxy_service_tls(
|
||||
&my_server.configuration,
|
||||
"0.0.0.0:4430",
|
||||
vec![
|
||||
HostConfig {
|
||||
proxy_addr: "127.0.0.1:4000".to_owned(),
|
||||
proxy_tls: false,
|
||||
proxy_hostname: "somedomain.com".to_owned(),
|
||||
cert_path: format!("{}/keys/some_domain_cert.crt", env!("CARGO_MANIFEST_DIR")),
|
||||
key_path: format!("{}/keys/some_domain_key.pem", env!("CARGO_MANIFEST_DIR")),
|
||||
},
|
||||
HostConfig {
|
||||
proxy_addr: "1.1.1.1:443".to_owned(),
|
||||
proxy_tls: true,
|
||||
proxy_hostname: "one.one.one.one".to_owned(),
|
||||
cert_path: format!("{}/keys/one_cert.crt", env!("CARGO_MANIFEST_DIR")),
|
||||
key_path: format!("{}/keys/one_key.pem", env!("CARGO_MANIFEST_DIR")),
|
||||
},
|
||||
// HostConfig {
|
||||
// proxy_addr: "127.0.0.1:4000".to_owned(),
|
||||
// proxy_tls: false,
|
||||
// proxy_hostname: "somedomain.com".to_owned(),
|
||||
// cert_path: format!("{}/keys/some_domain_cert.crt", env!("CARGO_MANIFEST_DIR")),
|
||||
// key_path: format!("{}/keys/some_domain_key.pem", env!("CARGO_MANIFEST_DIR")),
|
||||
// },
|
||||
// HostConfig {
|
||||
// proxy_addr: "1.1.1.1:443".to_owned(),
|
||||
// proxy_tls: true,
|
||||
// proxy_hostname: "one.one.one.one".to_owned(),
|
||||
// cert_path: format!("{}/keys/one_cert.crt", env!("CARGO_MANIFEST_DIR")),
|
||||
// key_path: format!("{}/keys/one_key.pem", env!("CARGO_MANIFEST_DIR")),
|
||||
// },
|
||||
],
|
||||
);
|
||||
|
||||
let mut prometheus_service_http = ListeningService::prometheus_http_service();
|
||||
prometheus_service_http.add_tcp("127.0.0.1:6150");
|
||||
|
||||
info!("start listen...");
|
||||
let services: Vec<Box<dyn Service>> = vec![
|
||||
Box::new(proxy_service_tcp),
|
||||
Box::new(proxy_service_ssl2),
|
||||
Box::new(prometheus_service_http),
|
||||
];
|
||||
@@ -49,8 +57,12 @@ pub fn main() {
|
||||
}
|
||||
|
||||
fn init_logger() {
|
||||
if std::env::var("RUST_LOG").is_err() {
|
||||
std::env::set_var("RUST_LOG", "pingora_reverse_proxy=debug");
|
||||
}
|
||||
pretty_env_logger::init_timed();
|
||||
let mut builder = Builder::new();
|
||||
builder.filter_level(LevelFilter::Info);
|
||||
let _ = builder.try_init();
|
||||
|
||||
// if std::env::var("RUST_LOG").is_err() {
|
||||
// std::env::set_var("RUST_LOG", "pingora_reverse_proxy=debug");
|
||||
// }
|
||||
// pretty_env_logger::init_timed();
|
||||
}
|
||||
@@ -69,6 +69,19 @@ pub struct HostConfig {
|
||||
pub key_path: String,
|
||||
}
|
||||
|
||||
pub fn proxy_service_tcp(
|
||||
server_conf: &Arc<ServerConf>,
|
||||
listen_addr: &str,
|
||||
host_configs: Vec<HostConfig>,
|
||||
) -> impl pingora::services::Service {
|
||||
let proxy_app = ProxyApp::new(host_configs.clone());
|
||||
let mut service = http_proxy_service(server_conf, proxy_app);
|
||||
|
||||
service.add_tcp(listen_addr);
|
||||
|
||||
service
|
||||
}
|
||||
|
||||
pub fn proxy_service_tls(
|
||||
server_conf: &Arc<ServerConf>,
|
||||
listen_addr: &str,
|
||||
|
||||
Reference in New Issue
Block a user