From 0ec73283964193a45cdcbb59879563153c6828e9 Mon Sep 17 00:00:00 2001 From: Maple Nebel Date: Fri, 29 May 2026 09:00:10 +0200 Subject: [PATCH] more ip resolution services --- proxy-common/src/lib.rs | 6 +++--- rnex-core/src/executables/common.rs | 32 ++++++++++++++++++++++++----- test-edition.sh | 2 +- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/proxy-common/src/lib.rs b/proxy-common/src/lib.rs index f614de1..20f90c1 100644 --- a/proxy-common/src/lib.rs +++ b/proxy-common/src/lib.rs @@ -40,9 +40,9 @@ pub enum Error { #[error("error parsing ip address environment variable \"{0}\": {1}")] AddrParse(&'static str, AddrParseError), #[error( - "error error getting public ip address: \n\tattempted to read from env var \"SERVER_IP_PUBLIC\" and got: {0} \n\tattempted to request from internet and failed with: {1}" + "error error getting public ip address: \n\tattempted to read from env var \"SERVER_IP_PUBLIC\" and got: {0}\n\tfor other attempts check logs" )] - PubAddrGetErr(Box, Box), + PubAddrGetErr(Box), } impl Into for (&'static str, AddrParseError) { fn into(self) -> Error { @@ -88,7 +88,7 @@ impl ProxyStartupParam { Ok(v) => v, Err(e) => try_get_ip() .map(|v| SocketAddrV4::new(v, self_private.port())) - .map_err(move |v| Error::PubAddrGetErr(Box::new(e), v))?, + .ok_or(Error::PubAddrGetErr(Box::new(e)))?, }; Ok(Self { diff --git a/rnex-core/src/executables/common.rs b/rnex-core/src/executables/common.rs index fc9f6e3..e300b04 100644 --- a/rnex-core/src/executables/common.rs +++ b/rnex-core/src/executables/common.rs @@ -4,6 +4,7 @@ use rnex_core::rmc::protocols::{RmcCallable, RmcConnection, new_rmc_gateway_conn use rnex_core::rmc::structures::RmcSerialize; use rnex_core::rnex_proxy_common::ConnectionInitData; use std::env; +use std::fmt::Display; use std::io::Cursor; use std::net::{Ipv4Addr, SocketAddrV4}; use std::sync::Arc; @@ -18,7 +19,12 @@ use cfg_if::cfg_if; use log::error; use std::error::Error; -const IP_REQ_SERVICE_URL: &str = "https://ipinfo.io/ip"; +const IP_REQ_SERVICE_URLS: &[&str] = &[ + "https://ipinfo.io/ip", + "https://api.ipify.org", + "http://ipinfo.io/ip", + "http://api.ipify.org", +]; cfg_if! { if #[cfg(feature = "datastore")] { @@ -44,10 +50,26 @@ cfg_if! { } } -pub fn try_get_ip() -> Result> { - let mut req = ureq::get(IP_REQ_SERVICE_URL).call()?; +pub fn try_to_log(fun: impl FnOnce() -> Result) -> Option { + match fun() { + Ok(v) => Some(v), + Err(e) => { + error!("{}", e); + None + } + } +} - Ok(req.body_mut().read_to_string()?.parse()?) +pub fn try_get_ip() -> Option { + for url in IP_REQ_SERVICE_URLS { + if let Some(v) = try_to_log::<_, Box>(|| { + let mut req = ureq::get(*url).call()?; + Ok(req.body_mut().read_to_string()?.parse()?) + }) { + return Some(v); + } + } + None } pub static OWN_IP_PRIVATE: Lazy = Lazy::new(|| { @@ -120,7 +142,7 @@ mod test { use crate::executables::common::try_get_ip; #[test] - fn test() { + fn get_ip() { try_get_ip().unwrap(); } } diff --git a/test-edition.sh b/test-edition.sh index 8b1140f..39a46fd 100755 --- a/test-edition.sh +++ b/test-edition.sh @@ -14,7 +14,7 @@ echo ENV SETTINGS: env if [[ ! -v RNEX_STATIC ]]; then - cargo test --features "$EDITION_FEATURES" + cargo test --features "$EDITION_FEATURES" --no-run --verbose else OPENSSL_LIB_DIR=/usr/lib OPENSSL_INCLUDE_DIR=/usr/include/openssl OPENSSL_STATIC=1 RUSTFLAGS="-C relocation-model=static -C linker=ld.lld" cargo test --features "$EDITION_FEATURES" --target x86_64-unknown-linux-musl fi