Ratelimit acct creation (#2933)

This commit is contained in:
Geometrically
2024-11-10 22:47:58 -08:00
committed by GitHub
parent 648b40a8f5
commit 3fa07f64d3
6 changed files with 76 additions and 44 deletions

View File

@@ -0,0 +1,25 @@
use std::net::{AddrParseError, IpAddr, Ipv6Addr};
pub fn convert_to_ip_v6(src: &str) -> Result<Ipv6Addr, AddrParseError> {
let ip_addr: IpAddr = src.parse()?;
Ok(match ip_addr {
IpAddr::V4(x) => x.to_ipv6_mapped(),
IpAddr::V6(x) => x,
})
}
pub fn strip_ip(ip: Ipv6Addr) -> u64 {
if let Some(ip) = ip.to_ipv4_mapped() {
let octets = ip.octets();
u64::from_be_bytes([
octets[0], octets[1], octets[2], octets[3], 0, 0, 0, 0,
])
} else {
let octets = ip.octets();
u64::from_be_bytes([
octets[0], octets[1], octets[2], octets[3], octets[4], octets[5],
octets[6], octets[7],
])
}
}

View File

@@ -7,6 +7,7 @@ pub mod env;
pub mod ext;
pub mod guards;
pub mod img;
pub mod ip;
pub mod ratelimit;
pub mod redis;
pub mod routes;