From e2b564cc48cfecacb3b0a68ae01de705536871af Mon Sep 17 00:00:00 2001 From: Maple Nebel Date: Thu, 6 Nov 2025 22:55:41 +0000 Subject: [PATCH] add public ip inferrence --- Cargo.lock | 2 ++ rnex-core/Cargo.toml | 2 +- rnex-core/src/executables/common.rs | 16 ++++++++++++++-- rnex-core/src/prudp/socket_addr.rs | 1 - 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb9c740..82797de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1663,7 +1663,9 @@ dependencies = [ "base64", "bytes", "encoding_rs", + "futures-channel", "futures-core", + "futures-util", "h2 0.4.11", "http 1.3.1", "http-body 1.0.1", diff --git a/rnex-core/Cargo.toml b/rnex-core/Cargo.toml index b41bc8d..90ce871 100644 --- a/rnex-core/Cargo.toml +++ b/rnex-core/Cargo.toml @@ -26,7 +26,7 @@ rocket = { version = "0.5.1", features = ["json", "serde_json"] } async-trait = "0.1.86" paste = "1.0.15" typenum = "1.18.0" -reqwest = "0.12.18" +reqwest = { version= "0.12.18", features = ["blocking"]} json = "0.12.4" ctrlc = "3.4.7" diff --git a/rnex-core/src/executables/common.rs b/rnex-core/src/executables/common.rs index 5697d7c..0c553b4 100644 --- a/rnex-core/src/executables/common.rs +++ b/rnex-core/src/executables/common.rs @@ -2,6 +2,16 @@ use std::env; use std::net::Ipv4Addr; use once_cell::sync::Lazy; use crate::nex::account::Account; +use std::error::Error; + +const IP_REQ_SERVICE_URL: &str = "https://ipinfo.io/ip"; + + + +fn try_get_ip() -> Result> { + let req = reqwest::blocking::get(IP_REQ_SERVICE_URL)?; + Ok(req.text()?.parse()?) +} pub static OWN_IP_PRIVATE: Lazy = Lazy::new(|| { env::var("SERVER_IP") @@ -13,8 +23,10 @@ pub static OWN_IP_PRIVATE: Lazy = Lazy::new(|| { pub static OWN_IP_PUBLIC: Lazy = Lazy::new(|| { env::var("SERVER_IP_PUBLIC") .ok() - .and_then(|s| s.parse().ok()) - .expect("SERVER_IP_PUBLIC not specified") + .map(|s| s.parse().expect("invalid ip address")) + .unwrap_or_else(||{ + try_get_ip().unwrap() + }) }); pub static SERVER_PORT: Lazy = Lazy::new(|| { diff --git a/rnex-core/src/prudp/socket_addr.rs b/rnex-core/src/prudp/socket_addr.rs index 5c55e88..f5eb179 100644 --- a/rnex-core/src/prudp/socket_addr.rs +++ b/rnex-core/src/prudp/socket_addr.rs @@ -17,7 +17,6 @@ pub struct PRUDPSockAddr{ impl PRUDPSockAddr{ - pub fn new(regular_socket_addr: SocketAddrV4, virtual_port: VirtualPort) -> Self{ Self{ regular_socket_addr,