From 691aadb4c28a93be7bfb7d87b245702795cfd4ca Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Wed, 26 Nov 2025 21:41:04 +0300 Subject: Refactor i2p/fetch() function --- src/i2impl/mi2p.rs | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) (limited to 'src/i2impl') diff --git a/src/i2impl/mi2p.rs b/src/i2impl/mi2p.rs index 5baff0f..e0bbe3d 100644 --- a/src/i2impl/mi2p.rs +++ b/src/i2impl/mi2p.rs @@ -7,13 +7,12 @@ use emissary_core::runtime::{ AsyncRead, AsyncWrite, }; - +use std::io; use emissary_core::Profile; use emissary_core::i2np::Message; use tokio::io::AsyncWriteExt; use yosemite::SessionOptions; use yosemite::{Session, style::Stream}; - /* use i2p_client::ClientType; use i2p_client::I2PClient; @@ -71,22 +70,32 @@ impl I2P { /// /// Returns an error if the repository URL is invalid or if the request fails. /// - async fn fetch_repo_list(&mut self) -> Result { - let repo_url = &self.config.repo.repo_url; - - let mut session = Session::::new(SessionOptions::default()).await.unwrap(); - let mut stream = session.connect(&self.config.repo.repo_url).await.unwrap(); - - let formatted_repo_url; - - if let Some(pos) = repo_url.find("/repo") { - let start_index = pos + "/repo".len(); - formatted_repo_url = repo_url[start_index..].to_string(); - } else { - return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Invalid repo URL")); - }; - stream.write_all(format!("GET {} HTTP/1.1\r\n\r\n", formatted_repo_url).as_bytes()).await.unwrap(); - + async fn fetch(&mut self) -> Result { + let repo_url_str = &self.config.repo.repo_url; + + let repo_pos = repo_url_str.find("/repo").ok_or_else(|| { + io::Error::new(io::ErrorKind::InvalidData, "URL does not contain '/repo'") + })?; + let base_url = &repo_url_str[..repo_pos]; + let path_suffix = &repo_url_str[repo_pos + "/repo".len()..]; + let request_path = format!("/repo{}{}", path_suffix, if path_suffix.ends_with(".tar.gz") { "" } else { "/INDEX.tar.gz" }); + let opts = SessionOptions::default(); + + // opts.set_host("127.0.0.1"); + // opts.set_port(7656); + let mut session = Session::::new(opts).await.map_err(|e| { + io::Error::new(io::ErrorKind::Other, format!("Failed to create session: {}", e)) + })?; + + let mut stream = session.connect(base_url).await.map_err(|e| { + io::Error::new(io::ErrorKind::ConnectionReset, format!("Failed to connect: {}", e)) + })?; + + let request = format!("GET {} HTTP/1.1\r\nHost: {}\r\n\r\n", request_path, base_url); + stream.write_all(request.as_bytes()).await.map_err(|e| { + io::Error::new(io::ErrorKind::Other, format!("Failed to write request: {}", e)) + })?; + Ok(true) - } +} } \ No newline at end of file -- cgit v1.2.3