1
0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden.git synced 2025-06-03 01:13:57 +00:00

Config option for client IP header

This commit is contained in:
Daniel García 2019-12-27 18:42:39 +01:00
parent e274af6e3d
commit 88c56de97b
No known key found for this signature in database
GPG key ID: FC8A7D14C3CD543A
3 changed files with 29 additions and 6 deletions

View file

@ -426,12 +426,21 @@ pub struct ClientIp {
impl<'a, 'r> FromRequest<'a, 'r> for ClientIp {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let ip = match request.client_ip() {
Some(addr) => addr,
None => "0.0.0.0".parse().unwrap(),
fn from_request(req: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let ip = if CONFIG._ip_header_enabled() {
req.headers().get_one(&CONFIG.ip_header()).and_then(|ip| {
ip.parse()
.map_err(|_| warn_!("'{}' header is malformed: {}", CONFIG.ip_header(), ip))
.ok()
})
} else {
None
};
let ip = ip
.or_else(|| req.remote().map(|r| r.ip()))
.unwrap_or_else(|| "0.0.0.0".parse().unwrap());
Outcome::Success(ClientIp { ip })
}
}