summaryrefslogtreecommitdiff
path: root/src/redirecting_i2p.rs
diff options
context:
space:
mode:
authorNamilskyy <alive6863@gmail.com>2025-12-04 23:37:33 +0300
committerNamilskyy <alive6863@gmail.com>2025-12-04 23:37:33 +0300
commitc9d526d6dc5d2c31abe689999497b00237a0bc12 (patch)
tree96395cb62bdc904f1046c17240de24eeda48f1df /src/redirecting_i2p.rs
parentd5abad30ba847fa0e09aa711d90db29a473e9bef (diff)
Implemented full functional. Release v0.0.1
Diffstat (limited to 'src/redirecting_i2p.rs')
-rw-r--r--src/redirecting_i2p.rs76
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()
+ }
+}