mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 15:51:23 +01:00
Extract source address for requests
This commit is contained in:
parent
3247c64cd8
commit
70ee206031
3 changed files with 34 additions and 6 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -158,6 +158,7 @@ dependencies = [
|
||||||
"serde_path_to_error",
|
"serde_path_to_error",
|
||||||
"serde_urlencoded",
|
"serde_urlencoded",
|
||||||
"sync_wrapper 1.0.1",
|
"sync_wrapper 1.0.1",
|
||||||
|
"tokio",
|
||||||
"tower 0.5.1",
|
"tower 0.5.1",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
|
|
@ -3069,6 +3070,7 @@ dependencies = [
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"sync_wrapper 0.1.2",
|
"sync_wrapper 0.1.2",
|
||||||
|
"tokio",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ workspace = true
|
||||||
[dependencies]
|
[dependencies]
|
||||||
argon2 = "0.5.3"
|
argon2 = "0.5.3"
|
||||||
async-trait = "0.1.82"
|
async-trait = "0.1.82"
|
||||||
axum = { version = "0.7.6", default-features = false, features = ["form", "http1", "http2", "json", "matched-path", "tracing"] }
|
axum = { version = "0.7.6", default-features = false, features = ["form", "http1", "http2", "json", "matched-path", "tokio", "tracing"] }
|
||||||
axum-extra = { version = "0.9.4", features = ["typed-header"] }
|
axum-extra = { version = "0.9.4", features = ["typed-header"] }
|
||||||
axum-server = { version = "0.7.1", features = ["tls-rustls-no-provider"] }
|
axum-server = { version = "0.7.1", features = ["tls-rustls-no-provider"] }
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,13 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{DefaultBodyLimit, FromRequestParts, MatchedPath},
|
extract::{
|
||||||
|
connect_info::IntoMakeServiceWithConnectInfo, ConnectInfo,
|
||||||
|
DefaultBodyLimit, FromRequestParts, MatchedPath,
|
||||||
|
},
|
||||||
|
middleware::AddExtension,
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
routing::{any, get, on, IntoMakeService, MethodFilter, Route},
|
routing::{any, get, on, MethodFilter, Route},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use axum_server::{
|
use axum_server::{
|
||||||
|
|
@ -21,6 +25,7 @@ use http::{
|
||||||
Method, StatusCode, Uri,
|
Method, StatusCode, Uri,
|
||||||
};
|
};
|
||||||
use hyper::body::Incoming;
|
use hyper::body::Incoming;
|
||||||
|
use proxy_header::ProxyHeader;
|
||||||
use ruma::api::{
|
use ruma::api::{
|
||||||
client::{
|
client::{
|
||||||
error::{Error as RumaError, ErrorBody, ErrorKind},
|
error::{Error as RumaError, ErrorBody, ErrorKind},
|
||||||
|
|
@ -212,9 +217,13 @@ where
|
||||||
&mut self,
|
&mut self,
|
||||||
listen: ListenConfig,
|
listen: ListenConfig,
|
||||||
server: Server<A>,
|
server: Server<A>,
|
||||||
app: IntoMakeService<Router>,
|
app: IntoMakeServiceWithConnectInfo<Router, SocketAddr>,
|
||||||
) where
|
) where
|
||||||
A: Accept<TcpStream, Router> + Clone + Send + Sync + 'static,
|
A: Accept<TcpStream, AddExtension<Router, ConnectInfo<SocketAddr>>>
|
||||||
|
+ Clone
|
||||||
|
+ Send
|
||||||
|
+ Sync
|
||||||
|
+ 'static,
|
||||||
A::Stream: AsyncRead + AsyncWrite + Unpin + Send + Sync,
|
A::Stream: AsyncRead + AsyncWrite + Unpin + Send + Sync,
|
||||||
A::Service: SendService<http::Request<Incoming>> + Send,
|
A::Service: SendService<http::Request<Incoming>> + Send,
|
||||||
A::Future: Send,
|
A::Future: Send,
|
||||||
|
|
@ -235,7 +244,7 @@ where
|
||||||
) -> Result<(), error::Serve> {
|
) -> Result<(), error::Serve> {
|
||||||
let app = routes(self.config, &listen.components)
|
let app = routes(self.config, &listen.components)
|
||||||
.layer(self.middlewares.clone())
|
.layer(self.middlewares.clone())
|
||||||
.into_make_service();
|
.into_make_service_with_connect_info::<SocketAddr>();
|
||||||
|
|
||||||
match listen.transport {
|
match listen.transport {
|
||||||
ListenTransport::Tcp {
|
ListenTransport::Tcp {
|
||||||
|
|
@ -298,11 +307,28 @@ async fn run_server() -> Result<(), error::Serve> {
|
||||||
|
|
||||||
let method = request.method();
|
let method = request.method();
|
||||||
|
|
||||||
|
let source_address = request
|
||||||
|
.extensions()
|
||||||
|
.get::<ProxyHeader<'_>>()
|
||||||
|
.map_or_else(
|
||||||
|
|| {
|
||||||
|
request
|
||||||
|
.extensions()
|
||||||
|
.get::<ConnectInfo<SocketAddr>>()
|
||||||
|
.map(|&ConnectInfo(addr)| addr)
|
||||||
|
},
|
||||||
|
|h| h.proxied_address().map(|addr| addr.source),
|
||||||
|
);
|
||||||
|
|
||||||
tracing::info_span!(
|
tracing::info_span!(
|
||||||
"http_request",
|
"http_request",
|
||||||
otel.name = format!("{method} {endpoint}"),
|
otel.name = format!("{method} {endpoint}"),
|
||||||
%method,
|
%method,
|
||||||
%endpoint,
|
%endpoint,
|
||||||
|
source_address = source_address.map_or(
|
||||||
|
"[unknown]".to_owned(),
|
||||||
|
|a| a.to_string()
|
||||||
|
),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.on_request(
|
.on_request(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue