Skip to content

Commit

Permalink
feat(http): add client ip
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarisW committed Nov 18, 2024
1 parent 1b87816 commit 8a4fd59
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
15 changes: 7 additions & 8 deletions volo-http/src/server/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,13 @@ impl FromContext for ClientIP {
cx: &mut ServerContext,
_: &mut Parts,
) -> Result<ClientIP, Self::Rejection> {
if let Some(client_ip) = cx.rpc_info.caller().tags.get::<ClientIP>() {
match client_ip.0 {
Some(ip) => Ok(ClientIP(Some(ip))),
None => Ok(ClientIP(None)),
}
} else {
Ok(ClientIP(None))
}
Ok(ClientIP(
cx.rpc_info
.caller()
.tags
.get::<ClientIP>()
.and_then(|v| v.0),
))
}
}

Expand Down
8 changes: 2 additions & 6 deletions volo-http/src/server/layer/client_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub struct ClientIPService<S> {

impl<S> ClientIPService<S> {
fn get_client_ip(&self, cx: &ServerContext, headers: &HeaderMap) -> ClientIP {
let remote_ip = match &cx.rpc_info().caller().address() {
let remote_ip = match &cx.rpc_info().caller().address {
Some(Address::Ip(socket_addr)) => Some(socket_addr.ip()),
Some(Address::Unix(_)) => None,
None => return ClientIP(None),
Expand Down Expand Up @@ -249,11 +249,7 @@ impl<S> ClientIPService<S> {
}
}

if let Some(remote_ip) = remote_ip {
return ClientIP(Some(remote_ip));
}

ClientIP(None)
ClientIP(remote_ip)
}
}

Expand Down

0 comments on commit 8a4fd59

Please sign in to comment.