diff options
| author | Namilskyy <alive6863@gmail.com> | 2025-11-26 21:41:04 +0300 |
|---|---|---|
| committer | Namilskyy <alive6863@gmail.com> | 2025-11-26 21:41:04 +0300 |
| commit | 691aadb4c28a93be7bfb7d87b245702795cfd4ca (patch) | |
| tree | 491737e9029bfbb8457ef4e66a73ac935545e2e4 /src/i2impl/mi2p.rs | |
| parent | 2e86f06da02d2cbe0b16a90eab7e33fdcf50f5f7 (diff) | |
Refactor i2p/fetch() function
Diffstat (limited to 'src/i2impl/mi2p.rs')
| -rw-r--r-- | src/i2impl/mi2p.rs | 47 |
1 files changed, 28 insertions, 19 deletions
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<S> I2P<S> { /// /// Returns an error if the repository URL is invalid or if the request fails. /// - async fn fetch_repo_list(&mut self) -> Result<bool, std::io::Error> { - let repo_url = &self.config.repo.repo_url; - - let mut session = Session::<Stream>::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<bool, std::io::Error> { + 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::<Stream>::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 |
