summaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/net')
-rw-r--r--src/net/http_packages.rs49
-rw-r--r--src/net/i2p_package.rs121
-rw-r--r--src/net/mod.rs4
3 files changed, 108 insertions, 66 deletions
diff --git a/src/net/http_packages.rs b/src/net/http_packages.rs
index 1e3c6ad..fe1833d 100644
--- a/src/net/http_packages.rs
+++ b/src/net/http_packages.rs
@@ -1,19 +1,21 @@
+use crate::cfg::config::Config;
use reqwest;
use std::fs::File;
use std::io::Write;
use std::path::Path;
-use crate::cfg::config::Config;
+#[allow(dead_code)]
pub struct HTTPPackage {
config: Config,
}
+#[allow(dead_code)]
impl HTTPPackage {
/// Creates a new Downloader object with the given configuration.
///
/// # Arguments
///
- /// * `config` - The full mesk
+ /// * `config` - The full mesk
///
/// # Returns
///
@@ -33,12 +35,11 @@ impl HTTPPackage {
/// # Returns
///
/// A new Config object with the parsed configuration. If there's an error while reading or parsing the file, returns an Err containing a Box<dyn std::error::Error>.
- pub fn parse_config(config_path: &str) -> Result<Config, Box<dyn std::error::Error>> {
+ pub fn parse_config() -> Result<Config, Box<dyn std::error::Error>> {
let config = Config::parse()?;
Ok(config)
}
-
/// Downloads the INDEX.tar.gz file from the configured repository
/// and stores it in the configured cache directory.
///
@@ -58,11 +59,9 @@ impl HTTPPackage {
let client = reqwest::Client::new();
- let response = client
- .get(&index_url)
- .send()
- .await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Request failed: {}", e)))?;
+ let response = client.get(&index_url).send().await.map_err(|e| {
+ std::io::Error::new(std::io::ErrorKind::Other, format!("Request failed: {}", e))
+ })?;
if !response.status().is_success() {
return Err(std::io::Error::new(
@@ -74,19 +73,35 @@ impl HTTPPackage {
let index_data = response
.bytes()
.await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to read response body: {}", e)))?
+ .map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to read response body: {}", e),
+ )
+ })?
.to_vec();
let file_path = Path::new(cache_dir).join("INDEX.tar.gz");
- let mut file = File::create(&file_path)
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to create file: {}", e)))?;
- file.write_all(&index_data)
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to write file: {}", e)))?;
- file.flush()
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to flush file: {}", e)))?;
+ let mut file = File::create(&file_path).map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to create file: {}", e),
+ )
+ })?;
+ file.write_all(&index_data).map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to write file: {}", e),
+ )
+ })?;
+ file.flush().map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to flush file: {}", e),
+ )
+ })?;
Ok(true)
}
}
-
diff --git a/src/net/i2p_package.rs b/src/net/i2p_package.rs
index 2776eea..d149454 100644
--- a/src/net/i2p_package.rs
+++ b/src/net/i2p_package.rs
@@ -1,33 +1,26 @@
-
-use crate::cfg::config::Config;
+use crate::cfg::config::Config;
use tokio;
-/*
+/*
use emissary_core::runtime::{
- AsyncRead,
- AsyncWrite,
+ AsyncRead,
+ AsyncWrite,
};
*/
-use std::{fs::File,
- path::Path,
- io::Write};
-// use emissary_core::Profile;
+use std::{fs::File, io::Write, path::Path};
+// use emissary_core::Profile;
// use emissary_core::i2np::Message;
-use tokio::io::{AsyncReadExt,
- AsyncWriteExt,
- BufReader};
+use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
+use url;
+use yosemite::Session;
use yosemite::SessionOptions;
-use yosemite::{Session, style::Stream};
-use url;
pub struct I2PPackage {
config: Config,
-
}
-
impl I2PPackage {
/// Creates a new I2P object with the given configuration.
///
@@ -35,13 +28,9 @@ impl I2PPackage {
///
/// A new I2P object with the given configuration. The session is initially set to None and the connected status is set to false.
pub fn new(config: Config) -> Self {
- I2PPackage{
-
- config: config,
- }
+ I2PPackage { config: config }
}
-
/// Downloads the INDEX.tar.gz file from the configured repository
/// and stores it in the configured cache directory.
///
@@ -52,11 +41,13 @@ impl I2PPackage {
let repo_url_str = &self.config.repo.repo_url;
let cache_dir = &self.config.paths.cache_dir;
- let url = url::Url::parse(repo_url_str)
- .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid repo URL"))?;
+ let url = url::Url::parse(repo_url_str).map_err(|_| {
+ std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid repo URL")
+ })?;
- let host = url.host_str()
- .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "No host in URL"))?;
+ let host = url.host_str().ok_or_else(|| {
+ std::io::Error::new(std::io::ErrorKind::InvalidInput, "No host in URL")
+ })?;
let request_path = url.path();
let request_path = if request_path.ends_with(".tar.gz") {
@@ -65,55 +56,91 @@ impl I2PPackage {
format!("{}/INDEX.tar.gz", request_path.trim_end_matches('/'))
};
- let sam_host = "127.0.0.1";
- let sam_port = 7656;
let session_options = SessionOptions::default();
- let mut session = Session::new(session_options).await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to create SAM session: {}", e)))?;
+ let mut session = Session::new(session_options).await.map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to create SAM session: {}", e),
+ )
+ })?;
- let mut stream = session.connect(host).await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::ConnectionAborted, format!("Failed to connect: {}", e)))?;
+ let mut stream = session.connect(host).await.map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::ConnectionAborted,
+ format!("Failed to connect: {}", e),
+ )
+ })?;
let request = format!(
"GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n",
request_path, host
);
-
- stream.write_all(request.as_bytes()).await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to write request: {}", e)))?;
+ stream.write_all(request.as_bytes()).await.map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to write request: {}", e),
+ )
+ })?;
let mut reader = BufReader::new(stream);
let mut response_buffer = Vec::new();
- reader.read_to_end(&mut response_buffer).await
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to read response: {}", e)))?;
-
+ reader
+ .read_to_end(&mut response_buffer)
+ .await
+ .map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to read response: {}", e),
+ )
+ })?;
let headers_end = response_buffer
.windows(4)
.position(|window| window == b"\r\n\r\n")
- .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid response: no headers end"))?;
+ .ok_or_else(|| {
+ std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "Invalid response: no headers end",
+ )
+ })?;
- let headers_str = std::str::from_utf8(&response_buffer[..headers_end])
- .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid header encoding"))?;
+ let headers_str = std::str::from_utf8(&response_buffer[..headers_end]).map_err(|_| {
+ std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid header encoding")
+ })?;
if !headers_str.starts_with("HTTP/1.1 200") && !headers_str.starts_with("HTTP/1.0 200") {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
- format!("HTTP Error: {}", headers_str.lines().next().unwrap_or("Unknown")),
+ format!(
+ "HTTP Error: {}",
+ headers_str.lines().next().unwrap_or("Unknown")
+ ),
));
}
let body_start = headers_end + 4;
let file_path = Path::new(cache_dir).join("INDEX.tar.gz");
-
- let mut file = File::create(&file_path)
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to create file: {}", e)))?;
+ let mut file = File::create(&file_path).map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to create file: {}", e),
+ )
+ })?;
file.write_all(&response_buffer[body_start..])
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to write file: {}", e)))?;
- file.flush()
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to flush file: {}", e)))?;
+ .map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to write file: {}", e),
+ )
+ })?;
+ file.flush().map_err(|e| {
+ std::io::Error::new(
+ std::io::ErrorKind::Other,
+ format!("Failed to flush file: {}", e),
+ )
+ })?;
Ok(true)
}
diff --git a/src/net/mod.rs b/src/net/mod.rs
index 54f5e7f..5a5aca0 100644
--- a/src/net/mod.rs
+++ b/src/net/mod.rs
@@ -1,2 +1,2 @@
-pub mod i2p_package;
-pub mod http_packages; \ No newline at end of file
+pub mod http_packages;
+pub mod i2p_package;