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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
use crate::router::router::RouterUtils;
use crate::router::router::{Emissary, EmissaryConfig};
use log::{debug, error, info};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
#[derive(Debug)]
pub enum RouterMessage {
Start,
Stop,
Status,
}
pub struct RouterManager {
handle: Option<JoinHandle<()>>,
tx: mpsc::Sender<RouterMessage>,
is_running: Arc<AtomicBool>,
}
impl RouterManager {
pub async fn new(
config: &crate::cfg::config::Config,
) -> Result<Self, Box<dyn std::error::Error>> {
if !config.router.integrated_router {
return Ok(Self {
handle: None,
tx: {
let (tx, _) = mpsc::channel(1);
tx
},
is_running: Arc::new(AtomicBool::new(false)),
});
}
let (tx, mut rx) = mpsc::channel::<RouterMessage>(1);
let is_running = Arc::new(AtomicBool::new(false));
let is_running_clone = is_running.clone();
let router_config = config.router.clone();
let handle = tokio::spawn(async move {
info!("Router manager task started");
let mut router = Emissary::new(EmissaryConfig {
storage: Some(std::path::PathBuf::from(&router_config.storage_path)),
auto_update: Some(router_config.auto_update),
http_proxy_port: Some(router_config.http_proxy_port),
socks_proxy_port: Some(router_config.socks_proxy_port),
});
if let Err(e) = router.config().await {
error!("Failed to configure router: {}", e);
return;
}
let storage_path = std::path::Path::new(&router_config.storage_path);
if !storage_path.exists()
|| storage_path
.read_dir()
.ok()
.is_none_or(|mut d| d.next().is_none())
{
info!("Router storage is empty, performing initial reseed...");
if let Err(e) = router.reseed().await {
error!("Failed to reseed router: {}", e);
return;
}
}
let router_task = tokio::spawn(async move {
info!("Starting router service...");
match router.start().await {
Ok(_) => info!("Router service stopped"),
Err(e) => error!("Router service error: {}", e),
}
});
is_running_clone.store(true, Ordering::SeqCst);
info!("Router manager is now running");
while let Some(msg) = rx.recv().await {
match msg {
RouterMessage::Stop => {
info!("Stopping router...");
// TODO: Implement proper router shutdown
is_running_clone.store(false, Ordering::SeqCst);
break;
}
RouterMessage::Status => {
debug!(
"Router status: {}",
if is_running_clone.load(Ordering::SeqCst) {
"running"
} else {
"stopped"
}
);
}
_ => {}
}
}
if let Err(e) = router_task.await {
error!("Router task panicked: {}", e);
}
info!("Router manager task finished");
});
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
Ok(Self {
handle: Some(handle),
tx,
is_running,
})
}
pub async fn stop(&self) {
if self.is_running.load(Ordering::SeqCst) {
if let Err(e) = self.tx.send(RouterMessage::Stop).await {
error!("Failed to send stop message to router: {}", e);
}
}
}
pub fn is_running(&self) -> bool {
self.is_running.load(Ordering::SeqCst)
}
}
impl Drop for RouterManager {
fn drop(&mut self) {
if self.is_running() {
let tx = self.tx.clone();
tokio::spawn(async move {
if let Err(e) = tx.send(RouterMessage::Stop).await {
error!("Failed to send stop message during drop: {}", e);
}
});
}
}
}
|