[MIRROR ONLY] A correct and efficient ATProto blob proxy for secure content delivery. codeberg.org/Blooym/porxie
36
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: improve socket_address

Lyna c66e5146 b66567f6

+83 -41
+2 -2
crates/porxie/src/main.rs
··· 14 14 cache::compute_cache_sizes, 15 15 identity_service::{IdentityService, IdentityServiceOptions}, 16 16 policy_client::{PolicyClient, PolicyClientOptions}, 17 - server::{PorxieServer, PorxieServerOptions, address::Address}, 17 + server::{PorxieServer, PorxieServerOptions, SocketAddress}, 18 18 }; 19 19 use ::mime::Mime; 20 20 use axum::http::{HeaderName, HeaderValue}; ··· 71 71 env = "PORXIE_SERVER_ADDRESS", 72 72 default_value = "ip:127.0.0.1:6314" 73 73 )] 74 - address: Address, 74 + address: SocketAddress, 75 75 76 76 /// Admin password for authenticating priviledged requests. 77 77 ///
-34
crates/porxie/src/server/address.rs
··· 1 - use anyhow::bail; 2 - use core::{net::SocketAddr, str::FromStr}; 3 - 4 - #[cfg(unix)] 5 - use std::path::PathBuf; 6 - 7 - #[derive(Debug, Clone)] 8 - pub enum Address { 9 - /// An IP socket address. 10 - Ip(SocketAddr), 11 - 12 - /// A UNIX socket path. 13 - #[cfg(unix)] 14 - Unix(PathBuf), 15 - } 16 - 17 - impl FromStr for Address { 18 - type Err = anyhow::Error; 19 - 20 - fn from_str(s: &str) -> Result<Self, Self::Err> { 21 - #[cfg(unix)] 22 - if let Some(path) = s.strip_prefix("unix:") { 23 - return Ok(Address::Unix(PathBuf::from(path))); 24 - } 25 - if let Some(ip) = s.strip_prefix("ip:") { 26 - return Ok(ip.parse::<SocketAddr>().map(Address::Ip)?); 27 - } 28 - 29 - #[cfg(unix)] 30 - bail!("unknown address binding type, expected 'ip:<addr>' or 'unix:<path>'".to_string(),); 31 - #[cfg(not(unix))] 32 - bail!("unknown address binding type, expected 'ip:<addr>'".to_string()); 33 - } 34 - }
+6 -5
crates/porxie/src/server/mod.rs
··· 1 - pub mod address; 2 1 mod extractors; 3 2 mod middlewares; 4 3 mod routes; 4 + mod socket_address; 5 + 6 + pub use socket_address::SocketAddress; 5 7 6 8 use crate::{ 7 9 blob_service::BlobService, 8 10 identity_service::IdentityService, 9 11 policy_client::PolicyClient, 10 12 server::{ 11 - address::Address, 12 13 middlewares::server_headers_middleware, 13 14 routes::{ 14 15 get_index_handler, ··· 129 130 /// Start server listener on specified address. 130 131 pub async fn start<F: Future<Output = ()> + Send + 'static>( 131 132 self, 132 - address: Address, 133 + address: SocketAddress, 133 134 shutdown_signal: F, 134 135 ) -> anyhow::Result<()> { 135 136 match address { 136 - Address::Ip(ip) => { 137 + SocketAddress::Ip(ip) => { 137 138 let listener = tokio::net::TcpListener::bind(ip) 138 139 .await 139 140 .context("failed to bind tcp listener")?; ··· 144 145 Ok(()) 145 146 } 146 147 #[cfg(unix)] 147 - Address::Unix(path) => { 148 + SocketAddress::Unix(path) => { 148 149 use anyhow::Context; 149 150 150 151 let _ = std::fs::remove_file(&path);
+75
crates/porxie/src/server/socket_address.rs
··· 1 + use anyhow::bail; 2 + use core::str::FromStr; 3 + 4 + #[derive(Debug, Clone)] 5 + pub enum SocketAddress { 6 + /// An IP socket address. 7 + Ip(std::net::SocketAddr), 8 + 9 + /// A UNIX socket path. 10 + #[cfg(unix)] 11 + Unix(std::path::PathBuf), 12 + } 13 + 14 + impl FromStr for SocketAddress { 15 + type Err = anyhow::Error; 16 + 17 + fn from_str(s: &str) -> Result<Self, Self::Err> { 18 + #[cfg(unix)] 19 + if let Some(path) = s.strip_prefix("unix:") { 20 + if path.ends_with("/") { 21 + bail!("unix socket path cannot be a directory") 22 + } 23 + return Ok(SocketAddress::Unix(std::path::PathBuf::from(path))); 24 + } 25 + if let Some(ip) = s.strip_prefix("ip:") { 26 + return Ok(ip.parse::<std::net::SocketAddr>().map(SocketAddress::Ip)?); 27 + } 28 + 29 + #[cfg(unix)] 30 + bail!("unknown address binding type, expected 'ip:<addr>' or 'unix:<path>'".to_string(),); 31 + #[cfg(not(unix))] 32 + bail!("unknown address binding type, expected 'ip:<addr>'".to_string()); 33 + } 34 + } 35 + 36 + impl From<std::net::SocketAddr> for SocketAddress { 37 + fn from(value: std::net::SocketAddr) -> Self { 38 + Self::Ip(value) 39 + } 40 + } 41 + 42 + #[cfg(unix)] 43 + impl From<std::path::PathBuf> for SocketAddress { 44 + fn from(value: std::path::PathBuf) -> Self { 45 + Self::Unix(value) 46 + } 47 + } 48 + 49 + #[cfg(unix)] 50 + impl From<&std::path::Path> for SocketAddress { 51 + fn from(value: &std::path::Path) -> Self { 52 + Self::Unix(value.to_path_buf()) 53 + } 54 + } 55 + 56 + #[cfg(test)] 57 + mod tests { 58 + use crate::server::socket_address::SocketAddress; 59 + use core::str::FromStr; 60 + 61 + #[test] 62 + fn parse_ip_address() { 63 + assert!(SocketAddress::from_str("ip:127.0.0.1:3000").is_ok()); 64 + assert!(SocketAddress::from_str("ip:1.1.1.1:80").is_ok()); 65 + assert!(SocketAddress::from_str("ip:1.1.1.1").is_err()); 66 + assert!(SocketAddress::from_str("ip:1.1.1.1.2").is_err()); 67 + } 68 + 69 + #[test] 70 + #[cfg(unix)] 71 + fn parse_unix_path() { 72 + assert!(SocketAddress::from_str("unix:/run/porxie/porxie.sock").is_ok()); 73 + assert!(SocketAddress::from_str("unix:/just/a/directory/").is_err()); 74 + } 75 + }