Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: ethers_providers::is_local_endpoint with rust matching pattern o…
Browse files Browse the repository at this point in the history
…n URL::parse (#2351)

* handling localhost, ipv4 and localhost ips

* adjusted naming

* havenofearlucishere

* enabling also loopback and private addresses

* supporting also ipv6

* removed line
  • Loading branch information
loocapro authored Apr 24, 2023
1 parent 7ba514f commit 81ad822
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions ethers-providers/src/rpc/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{

#[cfg(not(target_arch = "wasm32"))]
use crate::{HttpRateLimitRetryPolicy, RetryClient};
use std::net::Ipv4Addr;

#[cfg(feature = "celo")]
pub use crate::CeloMiddleware;
Expand Down Expand Up @@ -40,7 +41,7 @@ use std::{
};
use tracing::trace;
use tracing_futures::Instrument;
use url::{ParseError, Url};
use url::{Host, ParseError, Url};

/// Node Clients
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -1443,11 +1444,30 @@ impl ProviderExt for Provider<HttpProvider> {
/// ```
/// use ethers_providers::is_local_endpoint;
/// assert!(is_local_endpoint("http://localhost:8545"));
/// assert!(is_local_endpoint("http://169.254.0.0:8545"));
/// assert!(is_local_endpoint("http://127.0.0.1:8545"));
/// assert!(!is_local_endpoint("http://206.71.50.230:8545"));
/// assert!(!is_local_endpoint("http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"));
/// assert!(is_local_endpoint("http://[::1]"));
/// assert!(!is_local_endpoint("havenofearlucishere"));
/// ```
#[inline]
pub fn is_local_endpoint(url: &str) -> bool {
url.contains("127.0.0.1") || url.contains("localhost")
pub fn is_local_endpoint(endpoint: &str) -> bool {
if let Ok(url) = Url::parse(endpoint) {
if let Some(host) = url.host() {
match host {
Host::Domain(domain) => return domain.contains("localhost"),
Host::Ipv4(ipv4) => {
return ipv4 == Ipv4Addr::LOCALHOST ||
ipv4.is_link_local() ||
ipv4.is_loopback() ||
ipv4.is_private()
}
Host::Ipv6(ipv6) => return ipv6.is_loopback(),
}
}
}
false
}

#[cfg(test)]
Expand Down

0 comments on commit 81ad822

Please sign in to comment.