diff options
Diffstat (limited to 'src/redirecting_i2p.rs')
| -rw-r--r-- | src/redirecting_i2p.rs | 76 |
1 files changed, 76 insertions, 0 deletions
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<isize>, + pub panel_port: Option<isize>, +} + +#[derive(Deserialize, Debug)] +struct I2pdConfigFile { + httpproxy: Option<HttpProxySection>, + http: Option<HttpSection>, +} + +#[derive(Deserialize, Debug)] +struct HttpProxySection { + enabled: Option<bool>, + port: Option<isize>, + address: Option<String>, +} + +#[derive(Deserialize, Debug)] +struct HttpSection { + enabled: Option<bool>, + port: Option<isize>, + address: Option<String>, +} + +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<I2ProxySettings, Box<dyn std::error::Error>> { + 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() + } +} |
