From 686604665395385600ef8f1b5238a775249552a1 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 23 May 2025 10:01:29 +0100 Subject: feat: only try http(s) for ngit-relays otherwise it tries all the protocols and reprots on each --- src/lib/repo_ref.rs | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) (limited to 'src/lib') diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index df1427a..aa36bbb 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs @@ -290,6 +290,10 @@ impl RepoRef { user: None, } } + + pub fn ngit_relays(&self) -> Vec { + detect_existing_ngit_relays(Some(self), &[], &[], &[], &self.identifier) + } } pub async fn get_repo_coordinates_when_remote_unknown( @@ -565,6 +569,145 @@ pub fn save_repo_config_to_yaml( .context("failed to write maintainers to maintainers.yaml file serde_yaml") } +pub fn detect_existing_ngit_relays( + repo_ref: Option<&RepoRef>, + args_relays: &[String], + args_clone_url: &[String], + args_blossoms: &[String], + identifier: &str, +) -> Vec { + // Collect clone URLs from arguments or repo_ref + let clone_urls: Vec = if !args_clone_url.is_empty() { + args_clone_url.to_vec() + } else if let Some(repo) = repo_ref { + repo.git_server.clone() + } else { + Vec::new() + }; + + // Collect relays from arguments or repo_ref + let relays: Vec = if !args_relays.is_empty() { + args_relays + .iter() + .filter_map(|r| RelayUrl::parse(r).ok()) + .collect() + } else if let Some(repo) = repo_ref { + repo.relays.clone() + } else { + Vec::new() + }; + + // Collect blossom server URLs from arguments or repo_ref + let blossoms: Vec = if !args_blossoms.is_empty() { + args_blossoms + .iter() + .filter_map(|r| Url::parse(r).ok()) + .collect() + } else if let Some(repo) = repo_ref { + repo.blossoms.clone() + } else { + Vec::new() + }; + + let mut existing_ngit_relays = Vec::new(); + for url in &clone_urls { + let Ok(formatted_as_ngit_relay_url) = normalize_ngit_relay_url(url) else { + continue; + }; + if existing_ngit_relays.contains(&formatted_as_ngit_relay_url) { + continue; + } + + let clone_url_is_ngit_relay_format = if let Ok(npub) = extract_npub(url) { + url.contains(&format!("/{npub}/{identifier}.git")) + } else { + false + }; + if !clone_url_is_ngit_relay_format { + continue; + } + + let matches_relay = relays.iter().any(|r| { + normalize_ngit_relay_url(&r.to_string()) + .is_ok_and(|r| r.eq(&formatted_as_ngit_relay_url)) + }); + if !matches_relay { + continue; + } + + let matches_blossoms = blossoms.iter().any(|r| { + normalize_ngit_relay_url(r.as_str()).is_ok_and(|r| r.eq(&formatted_as_ngit_relay_url)) + }); + if !matches_blossoms { + continue; + } + + existing_ngit_relays.push(formatted_as_ngit_relay_url); + } + existing_ngit_relays +} + +pub fn normalize_ngit_relay_url(url: &str) -> Result { + // Parse the URL and handle errors + let mut parsed = Url::parse(url) + .or_else(|_| Url::parse(&format!("https://{url}"))) + .context(format!("{url} not a valid ngit relay URL"))?; + if parsed.host_str().is_none() { + // so sub.domain.org gets identifier as host in "sub.domain.org" + parsed = Url::parse(&format!("https://{url}"))?; + } + + // Extract the scheme, host, port, and path + let scheme = parsed.scheme(); + let host = parsed.host_str().context(format!( + "{url} not a ngit relay url reference: missing host in URL {parsed}" + ))?; + let port = parsed.port().map(|p| format!(":{p}")).unwrap_or_default(); + let path = parsed.path(); + + // Normalize the URL based on the scheme and path + let mut normalized_url = match scheme { + "ws" | "http" => format!("http://{host}{port}{path}"), + _ => format!("{host}{port}{path}"), + }; + + // If the normalized URL contains "npub1", remove "npub1" and everything after + // it + if let Some(pos) = normalized_url.find("npub1") { + normalized_url.truncate(pos); // Keep everything before "npub1" + } + // Return the normalized URL + Ok(normalized_url.trim_end_matches('/').to_string()) +} + +pub fn extract_npub(s: &str) -> Result<&str> { + // Find the starting index of "npub1" + if let Some(start) = s.find("npub1") { + let mut end = start + 5; // Start after "npub1" + + // Move the end index to include valid characters (0-9, a-z) + while end < s.len() && s[end..=end].chars().all(|c| c.is_ascii_alphanumeric()) { + end += 1; + } + // Extract the npub substring + let npub = &s[start..end]; + // Attempt to create a PublicKey from the extracted npub + PublicKey::from_bech32(npub).context("invalid npub")?; + Ok(npub) + } else { + bail!("No npub found") + } +} + +pub fn is_ngit_relay(url: &str, ngit_relays: &[String]) -> bool { + if !ngit_relays.is_empty() { + if let Ok(n) = normalize_ngit_relay_url(url) { + return ngit_relays.contains(&n); + } + } + false +} + #[cfg(test)] mod tests { use test_utils::*; @@ -842,4 +985,41 @@ mod tests { } } } + + #[test] + fn normalize_ngit_relay_url_all_checks() -> Result<()> { + let test_cases = vec![ + ("https://sub.domain.org", "sub.domain.org"), + ("wss://sub.domain.org", "sub.domain.org"), + ("sub.domain.org", "sub.domain.org"), + ("http://sub.domain.org", "http://sub.domain.org"), + ("ws://sub.domain.org", "http://sub.domain.org"), + ("http://localhost", "http://localhost"), + ("localhost", "localhost"), + ("https://sub.domain.org:8080", "sub.domain.org:8080"), + ("http://sub.domain.org:8080", "http://sub.domain.org:8080"), + ("sub.domain.org:8080", "sub.domain.org:8080"), + ("https://sub.domain.org/path/to", "sub.domain.org/path/to"), + ( + "https://sub.domain.org:8080/path/to", + "sub.domain.org:8080/path/to", + ), + ( + "https://sub.domain.org/npub143675782648/to.git", + "sub.domain.org", + ), + ( + "https://sub.domain.org/path/npub143675782648/to.git", + "sub.domain.org/path", + ), + ("https://sub.domain.org/", "sub.domain.org"), + ("http://sub.domain.org/", "http://sub.domain.org"), + ]; + + for (input, expected) in test_cases { + let normalized = normalize_ngit_relay_url(input)?; + assert_eq!(normalized, expected); + } + Ok(()) + } } -- cgit v1.2.3