From c9d526d6dc5d2c31abe689999497b00237a0bc12 Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Thu, 4 Dec 2025 23:37:33 +0300 Subject: Implemented full functional. Release v0.0.1 --- src/redirecting_i2p.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/redirecting_i2p.rs (limited to 'src/redirecting_i2p.rs') diff --git a/src/redirecting_i2p.rs b/src/redirecting_i2p.rs new file mode 100644 index 0000000..3feef83 --- /dev/null +++ b/src/redirecting_i2p.rs @@ -0,0 +1,76 @@ +use serde::Deserialize; +use std::fs; + +#[derive(Debug, Deserialize)] +pub struct I2ProxySettings { + pub i2p_port: Option, + pub panel_port: Option, +} + +#[derive(Deserialize, Debug)] +struct I2pdConfigFile { + httpproxy: Option, + http: Option, +} + +#[derive(Deserialize, Debug)] +struct HttpProxySection { + enabled: Option, + port: Option, + address: Option, +} + +#[derive(Deserialize, Debug)] +struct HttpSection { + enabled: Option, + port: Option, + address: Option, +} + +impl I2ProxySettings { + pub fn default() -> I2ProxySettings { + I2ProxySettings { + i2p_port: Some(4444), + panel_port: Some(7070), + } + } + + const DEFAULT_I2PD_CONFIG_PATH: &'static str = "/etc/i2pd/i2pd.conf"; + + pub fn from_i2pd_config( + config_path: Option<&str>, + ) -> Result> { + let path = config_path.unwrap_or(Self::DEFAULT_I2PD_CONFIG_PATH); + let content = fs::read_to_string(path)?; + + let parsed_config: I2pdConfigFile = serde_ini::from_str(&content)?; + + let i2p_port = parsed_config.httpproxy.and_then(|proxy| proxy.port); + let panel_port = parsed_config.http.and_then(|http| http.port); + + Ok(I2ProxySettings { + i2p_port, + panel_port, + }) + } + + fn parse_url(url: &str) -> url::Url { + url::Url::parse(url).expect("Failed to parse URL") + } + + pub fn redirecting_i2p(&mut self, url: &str) -> String { + if self.i2p_port.is_none() { + self.i2p_port = Some(4444); + } + + let parsed_url = Self::parse_url(url); + let host = parsed_url.host_str().unwrap_or(""); + let path = parsed_url.path(); + // Wrong logic, unused, now used the proxy, not webpath + let combined = format!("{}{}", host, path); + let i2p_url = format!("http://127.0.0.1:{}/{}", self.i2p_port.unwrap(), combined); + let i2p_url = I2ProxySettings::parse_url(&i2p_url); + + i2p_url.to_string() + } +} -- cgit v1.2.3