Skip to content

Commit

Permalink
feat(http): add remote_addr to logs
Browse files Browse the repository at this point in the history
With this change, the remote address will be logged. This enables other
software, such as fail2ban, to monitor the logs and make actions if
required.

Closes #944
  • Loading branch information
NefixEstrada committed Feb 2, 2024
1 parent 79bfa3c commit de556ac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 7 additions & 2 deletions warpgate-protocol-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ impl ProtocolServer for HTTPProtocolServer {
let method = req.method().clone();
let url = req.original_uri().clone();
let response = ep.call(req).await?;
log_request_result(&method, &url, &response.status());
let remote_addr = req.remote_addr().clone();

Check failure on line 126 in warpgate-protocol-http/src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (x86_64-linux)

borrow of moved value: `req`

Check failure on line 126 in warpgate-protocol-http/src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (arm64-linux)

borrow of moved value: `req`

Check failure on line 126 in warpgate-protocol-http/src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (arm64-macos)

borrow of moved value: `req`

log_request_result(&method, &url, &response.status(), &remote_addr);

Ok(response)
}),
)
Expand Down Expand Up @@ -206,7 +209,9 @@ impl ProtocolServer for HTTPProtocolServer {

async fn test_target(&self, target: Target) -> Result<(), TargetTestError> {
let TargetOptions::Http(options) = target.options else {
return Err(TargetTestError::Misconfigured("Not an HTTP target".to_owned()));
return Err(TargetTestError::Misconfigured(
"Not an HTTP target".to_owned(),
));
};
let request = poem::Request::builder().uri_str("http://host/").finish();
crate::proxy::proxy_normal_request(&request, poem::Body::empty(), &options)
Expand Down
13 changes: 9 additions & 4 deletions warpgate-protocol-http/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use http::{Method, StatusCode, Uri};
use poem::web::Data;
use poem::web::{Data, RemoteAddr};
use poem::{FromRequest, Request};
use tracing::*;
use warpgate_core::Services;
Expand Down Expand Up @@ -40,10 +40,15 @@ pub async fn span_for_request(req: &Request) -> poem::Result<Span> {
})
}

pub fn log_request_result(method: &Method, url: &Uri, status: &StatusCode) {
pub fn log_request_result(
method: &Method,
url: &Uri,
status: &StatusCode,
remote_addr: &RemoteAddr,
) {
if status.is_server_error() || status.is_client_error() {
warn!(%method, %url, %status, "Request failed");
warn!(%method, %url, %status, %remote_addr, "Request failed");
} else {
info!(%method, %url, %status, "Request");
info!(%method, %url, %status, %remote_addr, "Request");
}
}
2 changes: 1 addition & 1 deletion warpgate-protocol-http/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ pub async fn proxy_normal_request(
copy_client_response(&client_response, &mut response);
copy_client_body(client_response, &mut response).await?;

log_request_result(req.method(), req.original_uri(), &status);
log_request_result(req.method(), req.original_uri(), &status, req.remote_addr());

rewrite_response(&mut response, options, &uri)?;
Ok(response)
Expand Down

0 comments on commit de556ac

Please sign in to comment.