1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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(())
}
}
|