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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::{convert::Infallible, thread::JoinHandle, time::Duration};
use async_trait::async_trait;
use nanorpc::nanorpc_derive;
use nanorpc::RpcService;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use super::{CONNECT_CONFIG, TUNNEL};
/// The main stats-serving thread.
pub static STATS_THREAD: Lazy<JoinHandle<Infallible>> = Lazy::new(|| {
std::thread::spawn(|| loop {
let server = tiny_http::Server::http(CONNECT_CONFIG.stats_listen).unwrap();
for mut request in server.incoming_requests() {
smolscale::spawn(async move {
if let Ok(key) = std::env::var("GEPH_RPC_KEY") {
if !request.url().contains(&key) {
anyhow::bail!("missing rpc key")
}
}
let mut s = String::new();
request.as_reader().read_to_string(&mut s)?;
let resp = StatsControlService(DummyImpl)
.respond_raw(serde_json::from_str(&s)?)
.await;
request.respond(tiny_http::Response::from_data(serde_json::to_vec(&resp)?))?;
anyhow::Ok(())
})
.detach()
}
})
});
/// Basic tunnel statistics.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BasicStats {
pub total_sent_bytes: f32,
pub total_recv_bytes: f32,
pub last_loss: f32,
pub last_ping: f32, // latency
pub protocol: String,
pub address: String,
}
#[derive(Copy, Clone)]
struct DummyImpl;
impl StatsControlProtocol for DummyImpl {}
#[nanorpc_derive]
#[async_trait]
pub trait StatsControlProtocol {
/// Obtains whether or not the daemon is connected.
async fn is_connected(&self) -> bool {
TUNNEL.status().connected()
}
/// Obtains statistics.
async fn basic_stats(&self) -> BasicStats {
todo!()
// let s = TUNNEL.get_stats().await;
// let status = TUNNEL.status();
// BasicStats {
// total_recv_bytes: s.total_recv_bytes,
// total_sent_bytes: s.total_sent_bytes,
// last_loss: s.last_loss,
// last_ping: s.last_ping,
// protocol: match &status {
// ConnectionStatus::Connected {
// protocol,
// address: _,
// } => protocol.clone().into(),
// _ => "".into(),
// },
// address: match status {
// ConnectionStatus::Connected {
// protocol: _,
// address,
// } => address.into(),
// _ => "".into(),
// },
// }
}
/// Obtains time-series statistics.
async fn timeseries_stats(&self, _series: Timeseries) -> Vec<(u64, f32)> {
todo!()
// let s = TUNNEL.get_stats().await;
// let diffify = |series: sosistab::TimeSeries| {
// let mut accum = HashMap::new();
// let mut last = 0.0f32;
// let now = SystemTime::now();
// accum.insert(now.duration_since(UNIX_EPOCH).unwrap().as_secs(), 0.0);
// accum.insert(now.duration_since(UNIX_EPOCH).unwrap().as_secs() - 1, 0.0);
// for (&time, &total) in series.iter() {
// if let Ok(dur) = now.duration_since(time) {
// if dur.as_secs() > 600 {
// continue;
// }
// }
// let bucket = time.duration_since(UNIX_EPOCH).unwrap().as_secs();
// let diff = (total - last).max(0.0);
// last = total;
// *accum.entry(bucket).or_default() += diff;
// }
// let first = accum.keys().min().copied().unwrap_or_default();
// let end = accum.keys().max().copied().unwrap_or_default();
// (first..end)
// .map(|i| (i, accum.get(&i).copied().unwrap_or_default()))
// .collect_vec()
// };
// match series {
// Timeseries::SendSpeed => {
// let series = s.sent_series;
// diffify(series)
// }
// Timeseries::RecvSpeed => {
// let series = s.recv_series;
// diffify(series)
// }
// Timeseries::Loss => (0..200)
// .rev()
// .map(|t| {
// let tstamp = SystemTime::now() - Duration::from_secs(t);
// let tt = tstamp.duration_since(UNIX_EPOCH).unwrap().as_secs();
// (
// tt,
// s.loss_series
// .get(SystemTime::now() - Duration::from_secs(t)),
// )
// })
// .collect_vec(),
// Timeseries::Ping => (0..200)
// .rev()
// .filter_map(|t| {
// let tstamp = SystemTime::now() - Duration::from_secs(t);
// let tt = tstamp.duration_since(UNIX_EPOCH).unwrap().as_secs();
// let res = s
// .ping_series
// .get(SystemTime::now() - Duration::from_secs(t));
// if res > 10.0 {
// None
// } else {
// Some((tt, res * 1000.0))
// }
// })
// .collect_vec(),
// }
}
/// Turns off the daemon.
async fn kill(&self) -> bool {
smolscale::spawn(async {
smol::Timer::after(Duration::from_millis(300)).await;
std::process::exit(0);
})
.detach();
true
}
}
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
pub enum Timeseries {
RecvSpeed,
SendSpeed,
Loss,
Ping,
}