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() } }