Browse Source

use std to parse ipaddr

main
ryexandrite 6 months ago
parent
commit
a94ff94172
  1. 17
      server/lemmy_utils/src/lib.rs

17
server/lemmy_utils/src/lib.rs

@ -31,7 +31,10 @@ use lettre_email::Email;
use openssl::{pkey::PKey, rsa::Rsa};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use regex::{Regex, RegexBuilder};
use std::io::{Error, ErrorKind};
use std::{
io::{Error, ErrorKind},
net::{IpAddr, SocketAddr},
};
use url::Url;
pub type ConnectionId = usize;
@ -422,9 +425,11 @@ pub fn make_apub_endpoint(endpoint_type: EndpointType, name: &str) -> Url {
pub fn get_ip(conn_info: &ConnectionInfo) -> String {
conn_info
.realip_remote_addr()
.unwrap_or("127.0.0.1:12345")
.split(':')
.next()
.unwrap_or("127.0.0.1")
.to_string()
.and_then(|real_ip| {
real_ip
.parse::<IpAddr>()
.or_else(|_| real_ip.parse::<SocketAddr>().map(|sa| sa.ip()))
.ok()
})
.map_or("0.0.0.0".to_string(), |ip_addr| ip_addr.to_string())
}
Loading…
Cancel
Save