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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Fetcher adaptor for [Frebbox](https://dev.freebox.fr/sdk/os/#) french internet provider box
use std::{collections::HashMap, convert::Infallible};
use bytes::{Buf as _, Bytes};
use hmac::Hmac;
use http::{Method, Request, Response, StatusCode};
use http_body_util::{BodyExt as _, Full, combinators::BoxBody};
use hyper::body::Incoming;
use opentelemetry::KeyValue;
use prosa::core::{adaptor::Adaptor, proc::ProcConfig};
use prosa_fetcher::{
adaptor::FetcherAdaptor,
proc::{FetchAction, FetcherError, FetcherProc, FetcherSettings},
};
use serde::Deserialize;
use serde_json::{Map, Value};
use tokio::sync::watch;
use tracing::{debug, warn};
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub enum FreeboxFetchState {
#[default]
Connection,
System,
SwitchStatus,
SwitchPort(u8),
End,
}
impl FreeboxFetchState {
/// Getter of the URI for the current Freebox call to do
pub fn call(&self) -> Option<(Method, hyper::Uri)> {
match self {
FreeboxFetchState::Connection => Some((
Method::GET,
"/api/v4/connection/".parse::<hyper::Uri>().unwrap(),
)),
FreeboxFetchState::System => Some((
Method::GET,
"/api/v4/system/".parse::<hyper::Uri>().unwrap(),
)),
FreeboxFetchState::SwitchStatus => Some((
Method::GET,
"/api/v4/switch/status/".parse::<hyper::Uri>().unwrap(),
)),
FreeboxFetchState::SwitchPort(port) => Some((
Method::GET,
format!("/api/v4/switch/port/{port}/stats")
.parse::<hyper::Uri>()
.unwrap(),
)),
_ => None,
}
}
pub fn next_state(&self, number_ports: u8) -> FreeboxFetchState {
match self {
FreeboxFetchState::Connection => FreeboxFetchState::System,
FreeboxFetchState::System => FreeboxFetchState::SwitchStatus,
FreeboxFetchState::SwitchStatus => FreeboxFetchState::SwitchPort(number_ports),
FreeboxFetchState::SwitchPort(port) => {
// Port ID start at 1
if *port > 1 {
FreeboxFetchState::SwitchPort(port - 1)
} else {
FreeboxFetchState::End
}
}
_ => FreeboxFetchState::End,
}
}
}
#[derive(Default, Debug, Clone, Deserialize)]
pub struct FreeboxApiResponse {
success: bool,
result: Option<HashMap<String, Value>>,
}
impl FreeboxApiResponse {
pub fn get_string(&self, key: &str) -> Option<String> {
self.result
.as_ref()
.and_then(|m| m.get(key).and_then(|v| v.as_str().map(|s| s.to_owned())))
}
pub fn get_u64(&self, key: &str) -> Option<u64> {
self.result.as_ref().and_then(|m| {
m.get(key).and_then(|v| match v {
Value::Number(n) => n.as_u64(),
Value::String(s) => s.parse().ok(),
_ => None,
})
})
}
}
/// Adaptor for [Freebox](https://dev.freebox.fr/sdk/os/#) french internet provider box
#[derive(Adaptor)]
pub struct FetcherFreeboxAdaptor {
settings: FetcherSettings,
challenge_freebox: Option<String>,
session_token: Option<String>,
state: FreeboxFetchState,
number_ports: u8,
// Observability
meter_conn: watch::Sender<FreeboxApiResponse>,
meter_system: watch::Sender<FreeboxApiResponse>,
meter_switch: watch::Sender<Vec<Map<String, Value>>>,
meter_eth: watch::Sender<Vec<FreeboxApiResponse>>,
}
impl<M> FetcherAdaptor<M> for FetcherFreeboxAdaptor
where
M: 'static
+ std::marker::Send
+ std::marker::Sync
+ std::marker::Sized
+ std::clone::Clone
+ std::fmt::Debug
+ prosa::core::msg::Tvf
+ std::default::Default,
{
fn new(proc: &FetcherProc<M>) -> Result<Self, FetcherError<M>>
where
Self: std::marker::Sized,
{
let (meter_conn, watch_conn) = watch::channel(FreeboxApiResponse::default());
let watch_conn_byte = watch_conn.clone();
let _observable_byte = proc
.get_proc_param()
.meter("freebox")
.u64_observable_counter("prosa_freebox_byte")
.with_description("Freebox byte counter")
.with_callback(move |observer| {
let conn = watch_conn_byte.borrow();
if let (Some(conn_type), Some(up)) =
(conn.get_string("type"), conn.get_u64("bytes_up"))
{
observer.observe(
up,
&[
KeyValue::new("conn", conn_type),
KeyValue::new("flow", "send"),
],
);
}
if let (Some(conn_type), Some(down)) =
(conn.get_string("type"), conn.get_u64("bytes_down"))
{
observer.observe(
down,
&[
KeyValue::new("conn", conn_type),
KeyValue::new("flow", "recv"),
],
);
}
})
.build();
let watch_conn_rate = watch_conn.clone();
let _observable_rate = proc
.get_proc_param()
.meter("freebox")
.u64_observable_gauge("prosa_freebox_rate")
.with_description("Freebox rate gauge")
.with_callback(move |observer| {
let conn = watch_conn_rate.borrow();
if let (Some(conn_type), Some(up)) =
(conn.get_string("type"), conn.get_u64("rate_up"))
{
observer.observe(
up,
&[
KeyValue::new("conn", conn_type),
KeyValue::new("type", "rate"),
KeyValue::new("flow", "send"),
],
);
}
if let (Some(conn_type), Some(down)) =
(conn.get_string("type"), conn.get_u64("rate_down"))
{
observer.observe(
down,
&[
KeyValue::new("conn", conn_type),
KeyValue::new("type", "rate"),
KeyValue::new("flow", "recv"),
],
);
}
if let (Some(conn_type), Some(up)) =
(conn.get_string("type"), conn.get_u64("bandwidth_up"))
{
observer.observe(
up,
&[
KeyValue::new("conn", conn_type),
KeyValue::new("type", "bandwidth"),
KeyValue::new("flow", "send"),
],
);
}
if let (Some(conn_type), Some(down)) =
(conn.get_string("type"), conn.get_u64("bandwidth_down"))
{
observer.observe(
down,
&[
KeyValue::new("conn", conn_type),
KeyValue::new("type", "bandwidth"),
KeyValue::new("flow", "recv"),
],
);
}
})
.build();
let _observable_state = proc
.get_proc_param()
.meter("freebox")
.u64_observable_gauge("prosa_freebox_state")
.with_description(
"State of the Freebox (0 for down, 1 for going down, 2 for going up, 3 for up)",
)
.with_callback(move |observer| {
let conn = watch_conn.borrow();
if let (Some(conn_type), Some(state)) =
(conn.get_string("type"), conn.get_string("state"))
{
let state_enum = match state.as_str() {
"up" => 3,
"going_up" => 2,
"going_down" => 1,
_ => 0,
};
observer.observe(state_enum, &[KeyValue::new("conn", conn_type)]);
}
})
.build();
let (meter_system, watch_system) = watch::channel(FreeboxApiResponse::default());
let _observable_temp = proc
.get_proc_param()
.meter("freebox")
.u64_observable_gauge("prosa_freebox_temp")
.with_description("Temperatures of the Freebox")
.with_callback(move |observer| {
let conn = watch_system.borrow();
if let (Some(board_name), Some(temp)) =
(conn.get_string("board_name"), conn.get_u64("temp_cpum"))
{
observer.observe(
temp,
&[
KeyValue::new("board", board_name),
KeyValue::new("type", "cpum"),
],
);
}
if let (Some(board_name), Some(temp)) =
(conn.get_string("board_name"), conn.get_u64("temp_sw"))
{
observer.observe(
temp,
&[
KeyValue::new("board", board_name),
KeyValue::new("type", "sw"),
],
);
}
if let (Some(board_name), Some(temp)) =
(conn.get_string("board_name"), conn.get_u64("temp_cpub"))
{
observer.observe(
temp,
&[
KeyValue::new("board", board_name),
KeyValue::new("type", "cpub"),
],
);
}
if let (Some(board_name), Some(temp)) =
(conn.get_string("board_name"), conn.get_u64("temp_t1"))
{
observer.observe(
temp,
&[
KeyValue::new("board", board_name),
KeyValue::new("type", "t1"),
],
);
}
if let (Some(board_name), Some(temp)) =
(conn.get_string("board_name"), conn.get_u64("temp_t2"))
{
observer.observe(
temp,
&[
KeyValue::new("board", board_name),
KeyValue::new("type", "t2"),
],
);
}
if let (Some(board_name), Some(rpm)) =
(conn.get_string("board_name"), conn.get_u64("fan_rpm"))
{
observer.observe(
rpm,
&[
KeyValue::new("board", board_name),
KeyValue::new("type", "fan"),
],
);
}
})
.build();
let (meter_switch, watch_switch) = watch::channel(Vec::<Map<String, Value>>::new());
let watch_switch_status = watch_switch.clone();
let _observable_switch_status =
proc.get_proc_param()
.meter("freebox")
.u64_observable_gauge("prosa_freebox_switch_status")
.with_description("Status of the Freebox switch port")
.with_callback(move |observer| {
let switch = watch_switch_status.borrow();
for port in switch.iter() {
if let Some(port_id) = port.get("id").and_then(|i| i.as_u64()) {
match port.get("link").and_then(|s| s.as_str()) {
Some("up") => observer
.observe(1, &[KeyValue::new("port", port_id.to_string())]),
Some("down") => observer
.observe(0, &[KeyValue::new("port", port_id.to_string())]),
_ => {}
}
}
}
})
.build();
let _observable_switch_speed = proc
.get_proc_param()
.meter("freebox")
.u64_observable_gauge("prosa_freebox_switch_speed")
.with_description("Negociate speed of the Freebox switch port")
.with_callback(move |observer| {
let switch = watch_switch.borrow();
for port in switch.iter() {
if let (Some(port_id), Some(speed)) = (
port.get("id").and_then(|i| i.as_u64()),
port.get("speed")
.and_then(|i| i.as_str().and_then(|s| s.parse().ok())),
) {
observer.observe(speed, &[KeyValue::new("port", port_id.to_string())]);
}
}
})
.build();
let (meter_eth, watch_eth) = watch::channel(Vec::<FreeboxApiResponse>::new());
let watch_eth_byte = watch_eth.clone();
let _observable_eth_byte = proc
.get_proc_param()
.meter("freebox")
.u64_observable_counter("prosa_freebox_switch_bytes")
.with_description("Bytes process by the Freebox switch port")
.with_callback(move |observer| {
let eth = watch_eth_byte.borrow();
for (port_id, port) in eth.iter().enumerate() {
if let Some(rx_bytes) = port.get_u64("rx_good_bytes") {
observer.observe(
rx_bytes,
&[
KeyValue::new("port", port_id.to_string()),
KeyValue::new("flow", "recv"),
],
);
}
if let Some(tx_bytes) = port.get_u64("tx_bytes") {
observer.observe(
tx_bytes,
&[
KeyValue::new("port", port_id.to_string()),
KeyValue::new("flow", "send"),
],
);
}
}
})
.build();
let _observable_eth_packet = proc
.get_proc_param()
.meter("freebox")
.u64_observable_counter("prosa_freebox_switch_packets")
.with_description("Packets process by the Freebox switch port")
.with_callback(move |observer| {
let eth = watch_eth.borrow();
for (port_id, port) in eth.iter().enumerate() {
if let Some(rx_packets) = port.get_u64("rx_good_packets") {
observer.observe(
rx_packets,
&[
KeyValue::new("port", port_id.to_string()),
KeyValue::new("flow", "recv"),
],
);
}
if let Some(tx_packets) = port.get_u64("tx_packets") {
observer.observe(
tx_packets,
&[
KeyValue::new("port", port_id.to_string()),
KeyValue::new("flow", "send"),
],
);
}
if let Some(rx_packets) = port.get_u64("rx_err_packets") {
observer.observe(
rx_packets,
&[
KeyValue::new("port", port_id.to_string()),
KeyValue::new("flow", "recv_err"),
],
);
}
if let Some(tx_packets) = port.get_u64("tx_collisions") {
observer.observe(
tx_packets,
&[
KeyValue::new("port", port_id.to_string()),
KeyValue::new("flow", "send_err"),
],
);
}
}
})
.build();
Ok(Self {
settings: proc.settings.clone(),
challenge_freebox: None,
session_token: None,
state: FreeboxFetchState::End,
number_ports: 0,
meter_conn,
meter_system,
meter_switch,
meter_eth,
})
}
fn fetch(&mut self) -> Result<FetchAction<M>, FetcherError<M>> {
// Call HTTP to retrieve statistics with first state
self.state = FreeboxFetchState::default();
Ok(FetchAction::Http)
}
fn create_http_request(
&self,
mut request_builder: http::request::Builder,
) -> Result<Request<BoxBody<hyper::body::Bytes, Infallible>>, FetcherError<M>> {
if self.challenge_freebox.is_none() {
// Get a challenge to login after
request_builder = request_builder
.method(Method::GET)
.uri("/api/v4/login/".parse::<hyper::Uri>().unwrap())
.header(hyper::header::CONNECTION, "keep-alive")
.header(hyper::header::ACCEPT, "application/json");
let request = request_builder.body(BoxBody::default())?;
Ok(request)
} else if let Some(challenge_freebox) = &self.challenge_freebox
&& self.session_token.is_none()
{
// Get a session token to login
if let (Some(username), Some(challenge)) = (
self.settings.username(),
self.settings
.challenge_password::<Hmac<sha1::Sha1>, M>(challenge_freebox.as_bytes())?,
) {
let json_data =
format!("{{\"app_id\":\"{username}\",\"password\":\"{challenge:02x}\"}}");
request_builder = request_builder
.method(Method::POST)
.uri("/api/v4/login/session/".parse::<hyper::Uri>().unwrap())
.header(hyper::header::CONNECTION, "keep-alive")
.header(hyper::header::ACCEPT, "application/json")
.header(hyper::header::CONTENT_TYPE, "application/json")
.header(hyper::header::CONTENT_LENGTH, json_data.len().to_string());
let request =
request_builder.body(BoxBody::new(Full::new(Bytes::from(json_data))))?;
Ok(request)
} else {
Err(FetcherError::Other(
"Can't retrieve `challenge` from remote".to_string(),
))
}
} else if let Some((method, uri)) = self.state.call() {
// Send request depending of the state
request_builder = request_builder
.method(method)
.uri(uri)
.header(hyper::header::CONNECTION, "keep-alive")
.header(hyper::header::ACCEPT, "application/json")
.header("X-Fbx-App-Auth", self.session_token.as_ref().unwrap());
let request = request_builder.body(BoxBody::default())?;
Ok(request)
} else {
Err(FetcherError::Other(
"Can't get URI for remote API call".to_string(),
))
}
}
async fn process_http_response(
&mut self,
response: Result<Response<Incoming>, FetcherError<M>>,
) -> Result<FetchAction<M>, FetcherError<M>> {
match response {
Ok(response) => {
if self.challenge_freebox.is_none() {
match response.status() {
StatusCode::OK => {
let server = response
.headers()
.get(http::header::SERVER)
.and_then(|s| s.to_str().ok().map(|h| h.to_string()));
let body = response
.collect()
.await
.map_err(|e| FetcherError::Hyper(e, server.unwrap_or_default()))?
.aggregate();
// Parse the login return to get the challenge value
let login_json: FreeboxApiResponse =
serde_json::from_reader(body.reader())
.map_err(|e| FetcherError::Io(e.into()))?;
if let Some(challenge) = login_json.get_string("challenge") {
self.challenge_freebox = Some(challenge.to_string());
// Go for next call to retrieve `session_token`
Ok(FetchAction::Http)
} else {
Err(FetcherError::Other(
"Can't retrieve `challenge` from remote".to_string(),
))
}
}
code => Err(FetcherError::Other(format!(
"Receive error from HTTP remote for challenge: {code}"
))),
}
} else if self.session_token.is_none() {
match response.status() {
StatusCode::OK => {
let server = response
.headers()
.get(http::header::SERVER)
.and_then(|s| s.to_str().ok().map(|h| h.to_string()));
let body = response
.collect()
.await
.map_err(|e| FetcherError::Hyper(e, server.unwrap_or_default()))?
.aggregate();
// Parse the login return to get the challenge value
let login_json: FreeboxApiResponse =
serde_json::from_reader(body.reader())
.map_err(|e| FetcherError::Io(e.into()))?;
if let Some(token) = login_json.get_string("session_token") {
self.session_token = Some(token.to_string());
// Go for next call to get all statistics
Ok(FetchAction::Http)
} else {
Err(FetcherError::Other(
"Can't retrieve `session_token` from remote for session"
.to_string(),
))
}
}
code => Err(FetcherError::Other(format!(
"Receive error from HTTP remote: {code}"
))),
}
} else {
match response.status() {
StatusCode::OK => {
let server = response
.headers()
.get(http::header::SERVER)
.and_then(|s| s.to_str().ok().map(|h| h.to_string()));
let body = response
.collect()
.await
.map_err(|e| FetcherError::Hyper(e, server.unwrap_or_default()))?
.aggregate();
if self.state == FreeboxFetchState::SwitchStatus {
let switch_status_resp: Value =
serde_json::from_reader(body.reader())
.map_err(|e| FetcherError::Io(e.into()))?;
if let Some(switch_status_array) =
switch_status_resp.get("result").and_then(|r| r.as_array())
{
self.number_ports = switch_status_array.len() as u8;
let _ = self.meter_switch.send(
switch_status_array
.iter()
.map(|v| {
if let Some(value) = v.as_object() {
value.clone()
} else {
Map::new()
}
})
.collect(),
);
self.state = self.state.next_state(self.number_ports);
Ok(FetchAction::Http)
} else {
Ok(FetchAction::None)
}
} else {
// Parse the API response return to get the data
let api_resp: FreeboxApiResponse =
serde_json::from_reader(body.reader())
.map_err(|e| FetcherError::Io(e.into()))?;
if api_resp.success {
match self.state {
FreeboxFetchState::Connection => {
let _ = self.meter_conn.send(api_resp);
}
FreeboxFetchState::System => {
let _ = self.meter_system.send(api_resp);
}
FreeboxFetchState::SwitchPort(port_id) => {
let mut eth = self.meter_eth.borrow().clone();
if !eth.is_empty() {
eth[(port_id - 1) as usize] = api_resp;
} else {
eth =
Vec::with_capacity(self.number_ports as usize);
for _ in 0..self.number_ports {
eth.push(FreeboxApiResponse::default());
}
eth[(port_id - 1) as usize] = api_resp;
}
let _ = self.meter_eth.send(eth);
}
_ => {}
}
} else {
warn!(
"API[{:?}] respond with an error: {api_resp:?}",
self.state
);
return Ok(FetchAction::None);
}
self.state = self.state.next_state(self.number_ports);
if self.state != FreeboxFetchState::End {
// Call for next state
Ok(FetchAction::Http)
} else {
// Every call have been made
Ok(FetchAction::None)
}
}
}
code => Err(FetcherError::Other(format!(
"Receive error from HTTP remote: {code}"
))),
}
}
}
Err(FetcherError::Hyper(he, addr)) => {
if he.is_canceled() {
debug!(addr = addr, "HTTP error {:?}", he);
Ok(FetchAction::None)
} else {
warn!(addr = addr, "HTTP error {:?}", he);
Err(FetcherError::Hyper(he, addr))
}
}
Err(e) => Err(e),
}
}
}