diff options
| author | Namilskyy <alive6863@gmail.com> | 2025-12-27 22:26:52 +0300 |
|---|---|---|
| committer | Namilskyy <alive6863@gmail.com> | 2025-12-27 22:26:52 +0300 |
| commit | 007ee0bc3534218b2d084f368444d96c16d9d7f9 (patch) | |
| tree | 4de58305c0d57daa226b4cff7eee3b6055e1c6c3 /src/router/router.rs | |
| parent | 12528fcaf99763fc25df694d31208a6fa729d0a3 (diff) | |
An integrated i2p router is being developed
Diffstat (limited to 'src/router/router.rs')
| -rw-r--r-- | src/router/router.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/router/router.rs b/src/router/router.rs new file mode 100644 index 0000000..c438d75 --- /dev/null +++ b/src/router/router.rs @@ -0,0 +1,92 @@ +use emissary_util::storage::{Storage, StorageBundle}; +use emissary_util::reseeder::Reseeder; +use std::path::PathBuf; + + +pub trait RouterConfigUtils { + async fn config(&mut self) -> Result<(StorageBundle, Storage), Box<dyn std::error::Error>>; + async fn prepare_router(&mut self) -> Result<(), Box<dyn std::error::Error>>; + async fn reseed(&mut self, config: &StorageBundle, storage: &Storage) -> Result<(), Box<dyn std::error::Error>>; +} + +pub struct EmissaryConfig { + Storage: Option<PathBuf>, + AutoUpdate: Option<bool>, + HttpProxtPort: Option<u16>, + SocksProxyPort: Option<u16>, +} + +impl RouterConfigUtils for EmissaryConfig { + /// Configures the router with the given configuration. + /// + /// If the `Storage` field is `None`, it will be set to `/var/lib/mesk/router/`. + /// + /// # Errors + /// + /// Returns an error if there's an issue while configuring the router. + async fn config(&mut self) -> Result<(StorageBundle, Storage), Box<dyn std::error::Error>> { + self.Storage.get_or_insert_with(|| PathBuf::from("/var/lib/mesk/router/")); + self.AutoUpdate.get_or_insert(true); + self.HttpProxtPort.get_or_insert(4445); + self.SocksProxyPort.get_or_insert(4446); + + let storage = Storage::new(self.Storage.clone().into()).await?; + + let StorageBundle { + ntcp2_iv, + ntcp2_key, + profiles, + router_info, + routers, + signing_key, + static_key, + ssu2_intro_key, + ssu2_static_key, + } = storage.load().await; + + Ok((StorageBundle { + ntcp2_iv, + ntcp2_key, + profiles, + router_info, + routers, + signing_key, + static_key, + ssu2_intro_key, + ssu2_static_key, + }, storage)) + } + + async fn reseed(&mut self, config: &StorageBundle, storage: &Storage) -> Result<(), Box<dyn std::error::Error>> { + let mut routers = config.routers.clone(); + + + if routers.is_empty() { + match Reseeder::reseed(None, false).await { + Ok(reseed_routers) => { + for info in reseed_routers { + let _ = storage + .store_router_info(info.name.to_string(), info.router_info.clone()) + .await; + routers.push(info.router_info); + } + } + Err(_) if routers.is_empty() => { + return Err("Could not reseed routers.".into()); + } + Err(error) => { + log::warn!( + "Failed to reseed routers: {} - using existing routers", error.to_string(), + ); + } + } + } + Ok(()) + } + + async fn prepare_router(&mut self) -> Result<(), Box<dyn std::error::Error>> { + + + Ok(()) + } +}
\ No newline at end of file |
