Files
nym/clients/socks5/src/socks/utils.rs
T
2020-09-09 09:47:38 +01:00

25 lines
774 B
Rust

use super::types::AddrType;
/// Convert an AddrType and address to String
pub(crate) fn pretty_print_addr(addr_type: &AddrType, addr: &[u8]) -> String {
match addr_type {
AddrType::Domain => String::from_utf8_lossy(addr).to_string(),
AddrType::V4 => addr
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<String>>()
.join("."),
AddrType::V6 => {
let addr_16 = (0..8)
.map(|x| (u16::from(addr[(x * 2)]) << 8) | u16::from(addr[(x * 2) + 1]))
.collect::<Vec<u16>>();
addr_16
.iter()
.map(|x| format!("{:x}", x))
.collect::<Vec<String>>()
.join(":")
}
}
}