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/bin/git_remote_nostr/fetch.rs | 7 +- src/bin/git_remote_nostr/list.rs | 21 +++-- src/bin/git_remote_nostr/push.rs | 9 +- src/bin/git_remote_nostr/utils.rs | 18 +++- src/bin/ngit/sub_commands/init.rs | 179 +------------------------------------ src/lib/repo_ref.rs | 180 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 228 insertions(+), 186 deletions(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/fetch.rs b/src/bin/git_remote_nostr/fetch.rs index 34a02fc..adc68b3 100644 --- a/src/bin/git_remote_nostr/fetch.rs +++ b/src/bin/git_remote_nostr/fetch.rs @@ -18,7 +18,7 @@ use ngit::{ }, git_events::tag_value, login::get_curent_user, - repo_ref::RepoRef, + repo_ref::{RepoRef, is_ngit_relay}, }; use nostr::nips::nip19; use nostr_sdk::{Event, ToBech32}; @@ -54,6 +54,7 @@ pub async fn run_fetch( git_server_url, &repo_ref.to_nostr_git_url(&None), &term, + is_ngit_relay(git_server_url, &repo_ref.ngit_relays()), ) { errors.push(error); } else { @@ -163,6 +164,7 @@ pub fn fetch_from_git_server( git_server_url: &str, decoded_nostr_url: &NostrUrlDecoded, term: &console::Term, + is_ngit_relay: bool, ) -> Result<()> { let already_have_oids = oids .iter() @@ -173,7 +175,8 @@ pub fn fetch_from_git_server( let server_url = git_server_url.parse::()?; - let protocols_to_attempt = get_read_protocols_to_try(git_repo, &server_url, decoded_nostr_url); + let protocols_to_attempt = + get_read_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_ngit_relay); let mut failed_protocols = vec![]; let mut success = false; diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs index 2d233ae..bf16f89 100644 --- a/src/bin/git_remote_nostr/list.rs +++ b/src/bin/git_remote_nostr/list.rs @@ -13,7 +13,7 @@ use ngit::{ }, git_events::event_to_cover_letter, login::get_curent_user, - repo_ref, + repo_ref::{self, is_ngit_relay}, }; use nostr_sdk::hashes::sha1::Hash as Sha1Hash; use repo_ref::RepoRef; @@ -41,6 +41,7 @@ pub async fn run_list( git_repo, &repo_ref.git_server, &repo_ref.to_nostr_git_url(&None), + &repo_ref.ngit_relays(), ); let mut state = if let Some(nostr_state) = nostr_state { @@ -131,6 +132,7 @@ async fn get_open_and_draft_proposals_state( git_server_url, &repo_ref.to_nostr_git_url(&None), term, + is_ngit_relay(git_server_url, &repo_ref.ngit_relays()), ) .is_ok() { @@ -174,12 +176,19 @@ pub fn list_from_remotes( term: &console::Term, git_repo: &Repo, git_servers: &Vec, - decoded_nostr_url: &NostrUrlDecoded, // Add this parameter + decoded_nostr_url: &NostrUrlDecoded, + ngit_relays: &[String], ) -> HashMap> { let mut remote_states = HashMap::new(); let mut errors = HashMap::new(); for url in git_servers { - match list_from_remote(term, git_repo, url, decoded_nostr_url) { + match list_from_remote( + term, + git_repo, + url, + decoded_nostr_url, + is_ngit_relay(url, ngit_relays), + ) { Err(error) => { errors.insert(url, error); } @@ -195,10 +204,12 @@ pub fn list_from_remote( term: &console::Term, git_repo: &Repo, git_server_url: &str, - decoded_nostr_url: &NostrUrlDecoded, // Add this parameter + decoded_nostr_url: &NostrUrlDecoded, + is_ngit_relay: bool, ) -> Result> { let server_url = git_server_url.parse::()?; - let protocols_to_attempt = get_read_protocols_to_try(git_repo, &server_url, decoded_nostr_url); + let protocols_to_attempt = + get_read_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_ngit_relay); let mut failed_protocols = vec![]; let mut remote_state: Option> = None; diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 4eae92f..a34d729 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -27,7 +27,7 @@ use ngit::{ }, git_events::{self, event_to_cover_letter, get_event_root}, login::{self, user::UserRef}, - repo_ref::{self, get_repo_config_from_yaml}, + repo_ref::{self, get_repo_config_from_yaml, is_ngit_relay}, repo_state, }; use nostr::nips::nip10::Marker; @@ -49,6 +49,7 @@ use crate::{ }, }; +#[allow(clippy::too_many_lines)] pub async fn run_push( git_repo: &Repo, repo_ref: &RepoRef, @@ -79,6 +80,7 @@ pub async fn run_push( git_repo, &repo_ref.git_server, &repo_ref.to_nostr_git_url(&None), + &repo_ref.ngit_relays(), ) }); @@ -161,6 +163,7 @@ pub async fn run_push( &repo_ref.to_nostr_git_url(&None), &remote_refspecs, &term, + is_ngit_relay(&git_server_url, &repo_ref.ngit_relays()), ); } } @@ -412,9 +415,11 @@ fn push_to_remote( decoded_nostr_url: &NostrUrlDecoded, remote_refspecs: &[String], term: &Term, + is_ngit_relay: bool, ) -> Result<()> { let server_url = git_server_url.parse::()?; - let protocols_to_attempt = get_write_protocols_to_try(git_repo, &server_url, decoded_nostr_url); + let protocols_to_attempt = + get_write_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_ngit_relay); let mut failed_protocols = vec![]; let mut success = false; diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index 3a9f07d..2c77baa 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs @@ -216,8 +216,15 @@ pub fn get_read_protocols_to_try( git_repo: &Repo, server_url: &CloneUrl, decoded_nostr_url: &NostrUrlDecoded, + is_ngit_relay: bool, ) -> Vec { - if server_url.protocol() == ServerProtocol::Filesystem { + if is_ngit_relay { + if server_url.protocol() == ServerProtocol::Http { + vec![(ServerProtocol::UnauthHttp)] + } else { + vec![(ServerProtocol::UnauthHttps)] + } + } else if server_url.protocol() == ServerProtocol::Filesystem { vec![(ServerProtocol::Filesystem)] } else if let Some(protocol) = &decoded_nostr_url.protocol { vec![protocol.clone()] @@ -254,8 +261,15 @@ pub fn get_write_protocols_to_try( git_repo: &Repo, server_url: &CloneUrl, decoded_nostr_url: &NostrUrlDecoded, + is_ngit_relay: bool, ) -> Vec { - if server_url.protocol() == ServerProtocol::Filesystem { + if is_ngit_relay { + if server_url.protocol() == ServerProtocol::Http { + vec![(ServerProtocol::UnauthHttp)] + } else { + vec![(ServerProtocol::UnauthHttps)] + } + } else if server_url.protocol() == ServerProtocol::Filesystem { vec![(ServerProtocol::Filesystem)] } else if let Some(protocol) = &decoded_nostr_url.protocol { vec![protocol.clone()] diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index 41ed407..4b0f7b1 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs @@ -14,7 +14,10 @@ use ngit::{ cli_interactor::{PromptChoiceParms, PromptConfirmParms, PromptMultiChoiceParms}, client::{Params, send_events}, git::nostr_url::{CloneUrl, NostrUrlDecoded}, - repo_ref::{extract_pks, save_repo_config_to_yaml}, + repo_ref::{ + detect_existing_ngit_relays, extract_npub, extract_pks, normalize_ngit_relay_url, + save_repo_config_to_yaml, + }, }; use nostr::{ FromBech32, PublicKey, ToBech32, @@ -869,117 +872,6 @@ where Ok(selected_choices) } -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 -} - -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()) -} - fn format_ngit_relay_url_as_clone_url( url: &str, public_key: &PublicKey, @@ -1014,25 +906,6 @@ fn format_ngit_relay_url_as_blossom_url(url: &str) -> Result { Ok(format!("https://{ngit_relay_url}")) } -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") - } -} - fn parse_relay_url(s: &str) -> Result { // Attempt to parse the original string match RelayUrl::parse(s) { @@ -1098,47 +971,3 @@ fn push_main_or_master_branch(git_repo: &Repo) -> Result<()> { bail!("git push process exited with an error: {}", exit_status); } } - -#[cfg(test)] -mod tests { - use anyhow::Result; - - use super::*; - - #[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(()) - } -} 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