diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-05-23 10:01:29 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-05-23 10:01:29 +0100 |
| commit | 686604665395385600ef8f1b5238a775249552a1 (patch) | |
| tree | 56a4c7a956e14dfbcdd4a518096968abc69583a6 /src/bin/ngit | |
| parent | cc9a3a1d8526373625246504f72f338fd89c8d8b (diff) | |
feat: only try http(s) for ngit-relays
otherwise it tries all the protocols and reprots on each
Diffstat (limited to 'src/bin/ngit')
| -rw-r--r-- | src/bin/ngit/sub_commands/init.rs | 179 |
1 files changed, 4 insertions, 175 deletions
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::{ | |||
| 14 | cli_interactor::{PromptChoiceParms, PromptConfirmParms, PromptMultiChoiceParms}, | 14 | cli_interactor::{PromptChoiceParms, PromptConfirmParms, PromptMultiChoiceParms}, |
| 15 | client::{Params, send_events}, | 15 | client::{Params, send_events}, |
| 16 | git::nostr_url::{CloneUrl, NostrUrlDecoded}, | 16 | git::nostr_url::{CloneUrl, NostrUrlDecoded}, |
| 17 | repo_ref::{extract_pks, save_repo_config_to_yaml}, | 17 | repo_ref::{ |
| 18 | detect_existing_ngit_relays, extract_npub, extract_pks, normalize_ngit_relay_url, | ||
| 19 | save_repo_config_to_yaml, | ||
| 20 | }, | ||
| 18 | }; | 21 | }; |
| 19 | use nostr::{ | 22 | use nostr::{ |
| 20 | FromBech32, PublicKey, ToBech32, | 23 | FromBech32, PublicKey, ToBech32, |
| @@ -869,117 +872,6 @@ where | |||
| 869 | Ok(selected_choices) | 872 | Ok(selected_choices) |
| 870 | } | 873 | } |
| 871 | 874 | ||
| 872 | fn detect_existing_ngit_relays( | ||
| 873 | repo_ref: Option<&RepoRef>, | ||
| 874 | args_relays: &[String], | ||
| 875 | args_clone_url: &[String], | ||
| 876 | args_blossoms: &[String], | ||
| 877 | identifier: &str, | ||
| 878 | ) -> Vec<String> { | ||
| 879 | // Collect clone URLs from arguments or repo_ref | ||
| 880 | let clone_urls: Vec<String> = if !args_clone_url.is_empty() { | ||
| 881 | args_clone_url.to_vec() | ||
| 882 | } else if let Some(repo) = repo_ref { | ||
| 883 | repo.git_server.clone() | ||
| 884 | } else { | ||
| 885 | Vec::new() | ||
| 886 | }; | ||
| 887 | |||
| 888 | // Collect relays from arguments or repo_ref | ||
| 889 | let relays: Vec<RelayUrl> = if !args_relays.is_empty() { | ||
| 890 | args_relays | ||
| 891 | .iter() | ||
| 892 | .filter_map(|r| RelayUrl::parse(r).ok()) | ||
| 893 | .collect() | ||
| 894 | } else if let Some(repo) = repo_ref { | ||
| 895 | repo.relays.clone() | ||
| 896 | } else { | ||
| 897 | Vec::new() | ||
| 898 | }; | ||
| 899 | |||
| 900 | // Collect blossom server URLs from arguments or repo_ref | ||
| 901 | let blossoms: Vec<Url> = if !args_blossoms.is_empty() { | ||
| 902 | args_blossoms | ||
| 903 | .iter() | ||
| 904 | .filter_map(|r| Url::parse(r).ok()) | ||
| 905 | .collect() | ||
| 906 | } else if let Some(repo) = repo_ref { | ||
| 907 | repo.blossoms.clone() | ||
| 908 | } else { | ||
| 909 | Vec::new() | ||
| 910 | }; | ||
| 911 | |||
| 912 | let mut existing_ngit_relays = Vec::new(); | ||
| 913 | for url in &clone_urls { | ||
| 914 | let Ok(formatted_as_ngit_relay_url) = normalize_ngit_relay_url(url) else { | ||
| 915 | continue; | ||
| 916 | }; | ||
| 917 | if existing_ngit_relays.contains(&formatted_as_ngit_relay_url) { | ||
| 918 | continue; | ||
| 919 | } | ||
| 920 | |||
| 921 | let clone_url_is_ngit_relay_format = if let Ok(npub) = extract_npub(url) { | ||
| 922 | url.contains(&format!("/{npub}/{identifier}.git")) | ||
| 923 | } else { | ||
| 924 | false | ||
| 925 | }; | ||
| 926 | if !clone_url_is_ngit_relay_format { | ||
| 927 | continue; | ||
| 928 | } | ||
| 929 | |||
| 930 | let matches_relay = relays.iter().any(|r| { | ||
| 931 | normalize_ngit_relay_url(&r.to_string()) | ||
| 932 | .is_ok_and(|r| r.eq(&formatted_as_ngit_relay_url)) | ||
| 933 | }); | ||
| 934 | if !matches_relay { | ||
| 935 | continue; | ||
| 936 | } | ||
| 937 | |||
| 938 | let matches_blossoms = blossoms.iter().any(|r| { | ||
| 939 | normalize_ngit_relay_url(r.as_str()).is_ok_and(|r| r.eq(&formatted_as_ngit_relay_url)) | ||
| 940 | }); | ||
| 941 | if !matches_blossoms { | ||
| 942 | continue; | ||
| 943 | } | ||
| 944 | |||
| 945 | existing_ngit_relays.push(formatted_as_ngit_relay_url); | ||
| 946 | } | ||
| 947 | existing_ngit_relays | ||
| 948 | } | ||
| 949 | |||
| 950 | fn normalize_ngit_relay_url(url: &str) -> Result<String> { | ||
| 951 | // Parse the URL and handle errors | ||
| 952 | let mut parsed = Url::parse(url) | ||
| 953 | .or_else(|_| Url::parse(&format!("https://{url}"))) | ||
| 954 | .context(format!("{url} not a valid ngit relay URL"))?; | ||
| 955 | if parsed.host_str().is_none() { | ||
| 956 | // so sub.domain.org gets identifier as host in "sub.domain.org" | ||
| 957 | parsed = Url::parse(&format!("https://{url}"))?; | ||
| 958 | } | ||
| 959 | |||
| 960 | // Extract the scheme, host, port, and path | ||
| 961 | let scheme = parsed.scheme(); | ||
| 962 | let host = parsed.host_str().context(format!( | ||
| 963 | "{url} not a ngit relay url reference: missing host in URL {parsed}" | ||
| 964 | ))?; | ||
| 965 | let port = parsed.port().map(|p| format!(":{p}")).unwrap_or_default(); | ||
| 966 | let path = parsed.path(); | ||
| 967 | |||
| 968 | // Normalize the URL based on the scheme and path | ||
| 969 | let mut normalized_url = match scheme { | ||
| 970 | "ws" | "http" => format!("http://{host}{port}{path}"), | ||
| 971 | _ => format!("{host}{port}{path}"), | ||
| 972 | }; | ||
| 973 | |||
| 974 | // If the normalized URL contains "npub1", remove "npub1" and everything after | ||
| 975 | // it | ||
| 976 | if let Some(pos) = normalized_url.find("npub1") { | ||
| 977 | normalized_url.truncate(pos); // Keep everything before "npub1" | ||
| 978 | } | ||
| 979 | // Return the normalized URL | ||
| 980 | Ok(normalized_url.trim_end_matches('/').to_string()) | ||
| 981 | } | ||
| 982 | |||
| 983 | fn format_ngit_relay_url_as_clone_url( | 875 | fn format_ngit_relay_url_as_clone_url( |
| 984 | url: &str, | 876 | url: &str, |
| 985 | public_key: &PublicKey, | 877 | public_key: &PublicKey, |
| @@ -1014,25 +906,6 @@ fn format_ngit_relay_url_as_blossom_url(url: &str) -> Result<String> { | |||
| 1014 | Ok(format!("https://{ngit_relay_url}")) | 906 | Ok(format!("https://{ngit_relay_url}")) |
| 1015 | } | 907 | } |
| 1016 | 908 | ||
| 1017 | fn extract_npub(s: &str) -> Result<&str> { | ||
| 1018 | // Find the starting index of "npub1" | ||
| 1019 | if let Some(start) = s.find("npub1") { | ||
| 1020 | let mut end = start + 5; // Start after "npub1" | ||
| 1021 | |||
| 1022 | // Move the end index to include valid characters (0-9, a-z) | ||
| 1023 | while end < s.len() && s[end..=end].chars().all(|c| c.is_ascii_alphanumeric()) { | ||
| 1024 | end += 1; | ||
| 1025 | } | ||
| 1026 | // Extract the npub substring | ||
| 1027 | let npub = &s[start..end]; | ||
| 1028 | // Attempt to create a PublicKey from the extracted npub | ||
| 1029 | PublicKey::from_bech32(npub).context("invalid npub")?; | ||
| 1030 | Ok(npub) | ||
| 1031 | } else { | ||
| 1032 | bail!("No npub found") | ||
| 1033 | } | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | fn parse_relay_url(s: &str) -> Result<RelayUrl> { | 909 | fn parse_relay_url(s: &str) -> Result<RelayUrl> { |
| 1037 | // Attempt to parse the original string | 910 | // Attempt to parse the original string |
| 1038 | match RelayUrl::parse(s) { | 911 | match RelayUrl::parse(s) { |
| @@ -1098,47 +971,3 @@ fn push_main_or_master_branch(git_repo: &Repo) -> Result<()> { | |||
| 1098 | bail!("git push process exited with an error: {}", exit_status); | 971 | bail!("git push process exited with an error: {}", exit_status); |
| 1099 | } | 972 | } |
| 1100 | } | 973 | } |
| 1101 | |||
| 1102 | #[cfg(test)] | ||
| 1103 | mod tests { | ||
| 1104 | use anyhow::Result; | ||
| 1105 | |||
| 1106 | use super::*; | ||
| 1107 | |||
| 1108 | #[test] | ||
| 1109 | fn normalize_ngit_relay_url_all_checks() -> Result<()> { | ||
| 1110 | let test_cases = vec![ | ||
| 1111 | ("https://sub.domain.org", "sub.domain.org"), | ||
| 1112 | ("wss://sub.domain.org", "sub.domain.org"), | ||
| 1113 | ("sub.domain.org", "sub.domain.org"), | ||
| 1114 | ("http://sub.domain.org", "http://sub.domain.org"), | ||
| 1115 | ("ws://sub.domain.org", "http://sub.domain.org"), | ||
| 1116 | ("http://localhost", "http://localhost"), | ||
| 1117 | ("localhost", "localhost"), | ||
| 1118 | ("https://sub.domain.org:8080", "sub.domain.org:8080"), | ||
| 1119 | ("http://sub.domain.org:8080", "http://sub.domain.org:8080"), | ||
| 1120 | ("sub.domain.org:8080", "sub.domain.org:8080"), | ||
| 1121 | ("https://sub.domain.org/path/to", "sub.domain.org/path/to"), | ||
| 1122 | ( | ||
| 1123 | "https://sub.domain.org:8080/path/to", | ||
| 1124 | "sub.domain.org:8080/path/to", | ||
| 1125 | ), | ||
| 1126 | ( | ||
| 1127 | "https://sub.domain.org/npub143675782648/to.git", | ||
| 1128 | "sub.domain.org", | ||
| 1129 | ), | ||
| 1130 | ( | ||
| 1131 | "https://sub.domain.org/path/npub143675782648/to.git", | ||
| 1132 | "sub.domain.org/path", | ||
| 1133 | ), | ||
| 1134 | ("https://sub.domain.org/", "sub.domain.org"), | ||
| 1135 | ("http://sub.domain.org/", "http://sub.domain.org"), | ||
| 1136 | ]; | ||
| 1137 | |||
| 1138 | for (input, expected) in test_cases { | ||
| 1139 | let normalized = normalize_ngit_relay_url(input)?; | ||
| 1140 | assert_eq!(normalized, expected); | ||
| 1141 | } | ||
| 1142 | Ok(()) | ||
| 1143 | } | ||
| 1144 | } | ||