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
use serde::{Deserialize, Serialize};
use crate::model::{RuleType, TimeType};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
#[serde(rename = "type")]
pub connection_type: String,
#[serde(rename = "sourceIP")]
pub source_ip: String,
pub source_port: String,
#[serde(rename = "destinationIP")]
pub destination_ip: String,
pub destination_port: String,
pub host: String,
pub network: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct Connection {
pub id: String,
pub upload: u64,
pub download: u64,
pub metadata: Metadata,
pub rule: RuleType,
pub rule_payload: String,
pub start: TimeType,
pub chains: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct Connections {
pub connections: Vec<Connection>,
pub download_total: u64,
pub upload_total: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConnectionWithSpeed {
pub connection: Connection,
pub upload: Option<u64>,
pub download: Option<u64>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConnectionsWithSpeed {
pub connections: Vec<ConnectionWithSpeed>,
pub download_total: u64,
pub upload_total: u64,
}
#[cfg(feature = "deserialize")]
pub use deserialize::*;
#[cfg(feature = "deserialize")]
mod deserialize {
use chrono::Utc;
use crate::model::{Connection, ConnectionWithSpeed, Connections, ConnectionsWithSpeed};
impl Connection {
pub fn up_speed(&self) -> Option<u64> {
let elapsed = (Utc::now() - self.start).num_seconds();
if elapsed <= 0 {
None
} else {
Some(self.upload / elapsed as u64)
}
}
pub fn down_speed(&self) -> Option<u64> {
let elapsed = (Utc::now() - self.start).num_seconds();
if elapsed <= 0 {
None
} else {
Some(self.download / elapsed as u64)
}
}
}
impl From<Connections> for ConnectionsWithSpeed {
fn from(val: Connections) -> Self {
Self {
connections: val.connections.into_iter().map(Into::into).collect(),
download_total: val.download_total,
upload_total: val.upload_total,
}
}
}
impl From<ConnectionsWithSpeed> for Connections {
fn from(val: ConnectionsWithSpeed) -> Self {
Self {
connections: val.connections.into_iter().map(Into::into).collect(),
download_total: val.download_total,
upload_total: val.upload_total,
}
}
}
impl From<Connection> for ConnectionWithSpeed {
fn from(val: Connection) -> Self {
let elapsed = (Utc::now() - val.start).num_seconds();
if elapsed <= 0 {
Self {
connection: val,
upload: None,
download: None,
}
} else {
Self {
download: Some(val.download / elapsed as u64),
upload: Some(val.upload / elapsed as u64),
connection: val,
}
}
}
}
impl From<ConnectionWithSpeed> for Connection {
fn from(val: ConnectionWithSpeed) -> Self {
val.connection
}
}
}