Skip to content

Commit

Permalink
refactor: adjust upstream request filter
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 4, 2024
1 parent 38b0774 commit 8105dcd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pingap"
version = "0.1.11"
version = "0.1.12"
authors = ["Tree Xie <tree.xie@outlook.com>"]
edition = "2021"
categories = ["network-programming", "web-programming::http-server"]
Expand Down
46 changes: 30 additions & 16 deletions src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use http::StatusCode;
use log::{error, info};
use memory_stats::memory_stats;
use once_cell::sync::Lazy;
use pingora::http::ResponseHeader;
use pingora::http::{RequestHeader, ResponseHeader};
use pingora::listeners::TlsSettings;
use pingora::protocols::http::error_resp;
use pingora::protocols::Digest;
Expand Down Expand Up @@ -394,7 +394,6 @@ impl ProxyHttp for Server {
// TODO parse error
let _ = new_path.parse::<http::Uri>().map(|uri| header.set_uri(uri));
}
lo.insert_proxy_headers(header);
ctx.location_index = Some(location_index);
if let Some(dir) = lo.upstream.get_directory() {
let result = dir.handle(session, ctx).await?;
Expand Down Expand Up @@ -427,20 +426,6 @@ impl ProxyHttp for Server {
.new_http_peer(ctx, session)
.ok_or(pingora::Error::new_str("Upstream not found"))?;

// add x-forwarded-for
if let Some(addr) = utils::get_remote_addr(session) {
let value = if let Some(value) =
session.get_header(utils::HTTP_HEADER_X_FORWARDED_FOR.clone())
{
format!("{}, {}", value.to_str().unwrap_or_default(), addr)
} else {
addr.to_string()
};
let _ = session
.req_header_mut()
.insert_header(utils::HTTP_HEADER_X_FORWARDED_FOR.clone(), value);
}

Ok(Box::new(peer))
}
async fn connected_to_upstream(
Expand All @@ -459,6 +444,35 @@ impl ProxyHttp for Server {
ctx.upstream_address = peer.address().to_string();
Ok(())
}
async fn upstream_request_filter(
&self,
session: &mut Session,
header: &mut RequestHeader,
ctx: &mut Self::CTX,
) -> pingora::Result<()>
where
Self::CTX: Send + Sync,
{
// add x-forwarded-for
if let Some(addr) = utils::get_remote_addr(session) {
let value = if let Some(value) =
session.get_header(utils::HTTP_HEADER_X_FORWARDED_FOR.clone())
{
format!("{}, {}", value.to_str().unwrap_or_default(), addr)
} else {
addr.to_string()
};
let _ = header.insert_header(utils::HTTP_HEADER_X_FORWARDED_FOR.clone(), value);
}

if let Some(index) = ctx.location_index {
if let Some(lo) = self.locations.get(index) {
lo.insert_proxy_headers(header);
}
}

Ok(())
}
fn upstream_response_filter(
&self,
_session: &mut Session,
Expand Down
28 changes: 15 additions & 13 deletions web/src/components/main-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SettingsSuggestIcon from "@mui/icons-material/SettingsSuggest";
import SwipeableDrawer from "@mui/material/SwipeableDrawer";
import CardContent from "@mui/material/CardContent";
import Divider from "@mui/material/Divider";
import Card from "@mui/material/Card";

import useBasicStore from "../states/basic";

Expand Down Expand Up @@ -49,20 +50,21 @@ export default function MainHeader() {
setShowSetting(true);
}}
>
<CardContent>
<Typography gutterBottom variant="h5" component="div">
Informations
</Typography>
<Divider />
<Box pt={2}>
<Typography gutterBottom variant="body2">
Start time: {startAt}
</Typography>
<Typography gutterBottom variant="body2">
Memory: {memory}
<Card sx={{ minWidth: 275 }}>
<CardContent>
<Typography gutterBottom variant="h5" component="div">
Informations
</Typography>
</Box>
</CardContent>
<Box pt={2}>
<Typography gutterBottom variant="body2">
Start Time: {startAt}
</Typography>
<Typography gutterBottom variant="body2">
Memory: {memory}
</Typography>
</Box>
</CardContent>
</Card>
</SwipeableDrawer>
</React.Fragment>
);
Expand Down

0 comments on commit 8105dcd

Please sign in to comment.