add public ip inferrence

This commit is contained in:
Maple Nebel 2025-11-06 22:55:41 +00:00
commit e2b564cc48
4 changed files with 17 additions and 4 deletions

2
Cargo.lock generated
View file

@ -1663,7 +1663,9 @@ dependencies = [
"base64", "base64",
"bytes", "bytes",
"encoding_rs", "encoding_rs",
"futures-channel",
"futures-core", "futures-core",
"futures-util",
"h2 0.4.11", "h2 0.4.11",
"http 1.3.1", "http 1.3.1",
"http-body 1.0.1", "http-body 1.0.1",

View file

@ -26,7 +26,7 @@ rocket = { version = "0.5.1", features = ["json", "serde_json"] }
async-trait = "0.1.86" async-trait = "0.1.86"
paste = "1.0.15" paste = "1.0.15"
typenum = "1.18.0" typenum = "1.18.0"
reqwest = "0.12.18" reqwest = { version= "0.12.18", features = ["blocking"]}
json = "0.12.4" json = "0.12.4"
ctrlc = "3.4.7" ctrlc = "3.4.7"

View file

@ -2,6 +2,16 @@ use std::env;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::nex::account::Account; use crate::nex::account::Account;
use std::error::Error;
const IP_REQ_SERVICE_URL: &str = "https://ipinfo.io/ip";
fn try_get_ip() -> Result<Ipv4Addr, Box<dyn Error>> {
let req = reqwest::blocking::get(IP_REQ_SERVICE_URL)?;
Ok(req.text()?.parse()?)
}
pub static OWN_IP_PRIVATE: Lazy<Ipv4Addr> = Lazy::new(|| { pub static OWN_IP_PRIVATE: Lazy<Ipv4Addr> = Lazy::new(|| {
env::var("SERVER_IP") env::var("SERVER_IP")
@ -13,8 +23,10 @@ pub static OWN_IP_PRIVATE: Lazy<Ipv4Addr> = Lazy::new(|| {
pub static OWN_IP_PUBLIC: Lazy<Ipv4Addr> = Lazy::new(|| { pub static OWN_IP_PUBLIC: Lazy<Ipv4Addr> = Lazy::new(|| {
env::var("SERVER_IP_PUBLIC") env::var("SERVER_IP_PUBLIC")
.ok() .ok()
.and_then(|s| s.parse().ok()) .map(|s| s.parse().expect("invalid ip address"))
.expect("SERVER_IP_PUBLIC not specified") .unwrap_or_else(||{
try_get_ip().unwrap()
})
}); });
pub static SERVER_PORT: Lazy<u16> = Lazy::new(|| { pub static SERVER_PORT: Lazy<u16> = Lazy::new(|| {

View file

@ -17,7 +17,6 @@ pub struct PRUDPSockAddr{
impl PRUDPSockAddr{ impl PRUDPSockAddr{
pub fn new(regular_socket_addr: SocketAddrV4, virtual_port: VirtualPort) -> Self{ pub fn new(regular_socket_addr: SocketAddrV4, virtual_port: VirtualPort) -> Self{
Self{ Self{
regular_socket_addr, regular_socket_addr,