Files
nym/nym-api/src/node_status_api/local_guard.rs
T
Dave Hrycyszyn f6a79ce7c3 Renaming validator-api to nym-api (#1863)
* Renaming validator-api to nym-api

* nym-api: simplified crate name

* Added nym-api rename to changelog

* Changed some output messages

* Renamed validator-api-requests to nym-api requests

* Removing more references to validator-api-requests

* Additional lockfile name changes after full build

* Removing mistakenly added merge files

* ibid

* ibid

* Getting rid of ref to validator_api deep inside validator-client

* Fixing file storage paths

* Renaming struct function names referring to validator_api

* Simplifying struct init

* Fixed up all other instances of nym_api.

* Renaming validatorApi to nymApi in TypeScript client for consistency

v

* Found a few more Rust instances

* Changed examples in TypeScript SDK

* Found one more instance of the use of validator instead of nym apis

* Aliasing config key name for deserialization to preserve compatibility with old configs
2022-12-14 15:05:01 +00:00

43 lines
1.2 KiB
Rust

// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use rocket::http::Status;
use rocket::request::{FromRequest, Outcome};
use rocket::Request;
use std::fmt::Debug;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
#[derive(Debug)]
pub struct NonLocalRequestError;
/// Request guard that only allows requests coming from a local address
pub(crate) struct LocalRequest;
fn is_local_address(ip: Option<IpAddr>) -> bool {
if let Some(address) = ip {
match address {
IpAddr::V4(ip) => ip == Ipv4Addr::LOCALHOST,
IpAddr::V6(ip) => ip == Ipv6Addr::LOCALHOST,
}
} else {
false
}
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for LocalRequest {
type Error = NonLocalRequestError;
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
if is_local_address(request.client_ip()) {
Outcome::Success(LocalRequest)
} else {
warn!(
"Received a request from {:?} for a local-only route",
request.client_ip()
);
Outcome::Failure((Status::Unauthorized, NonLocalRequestError))
}
}
}