-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontext.rs
145 lines (125 loc) · 3.66 KB
/
context.rs
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
use chrono::{DateTime, Utc};
use crate::data_collection::payload::Event;
#[derive(Clone)]
pub struct EventContext {
pub from: String,
pub ip: String,
pub ip_anonymized: String,
pub consent: String,
pub uuid: String,
pub timestamp: DateTime<Utc>,
pub edgee_id: String,
pub proxy_type: String,
pub proxy_desc: String,
pub as_name: String,
pub project_id: String,
pub proxy_host: String,
}
#[allow(dead_code)]
impl EventContext {
pub fn new(events: &[Event], project_id: &str, proxy_host: &str) -> Self {
let mut ctx = Self {
from: "-".to_string(),
ip: "".to_string(),
ip_anonymized: "".to_string(),
consent: "default".to_string(),
uuid: "".to_string(),
timestamp: chrono::Utc::now(),
edgee_id: "".to_string(),
proxy_type: "".to_string(),
proxy_desc: "".to_string(),
as_name: "".to_string(),
project_id: project_id.to_string(),
proxy_host: proxy_host.to_string(),
};
if let Some(event) = events.first() {
// set request_info from the first event
ctx.from = event.from.clone().unwrap_or("-".to_string());
ctx.ip = event.context.client.ip.clone();
ctx.ip_anonymized = anonymize_ip(ctx.ip.clone());
if event.consent.is_some() {
ctx.consent = event.consent.as_ref().unwrap().to_string();
}
ctx.uuid = event.uuid.clone();
ctx.timestamp = event.timestamp;
ctx.edgee_id = event.context.user.edgee_id.clone();
ctx.proxy_type = event
.context
.client
.proxy_type
.clone()
.unwrap_or("-".to_string());
ctx.proxy_desc = event
.context
.client
.proxy_desc
.clone()
.unwrap_or("-".to_string());
ctx.as_name = event
.context
.client
.as_name
.clone()
.unwrap_or("-".to_string());
}
ctx
}
pub fn get_from(&self) -> &String {
&self.from
}
pub fn get_ip(&self) -> &String {
&self.ip
}
pub fn get_ip_anonymized(&self) -> &String {
&self.ip_anonymized
}
pub fn get_consent(&self) -> &String {
&self.consent
}
pub fn get_uuid(&self) -> &String {
&self.uuid
}
pub fn get_timestamp(&self) -> &DateTime<Utc> {
&self.timestamp
}
pub fn get_edgee_id(&self) -> &String {
&self.edgee_id
}
pub fn get_proxy_type(&self) -> &String {
&self.proxy_type
}
pub fn get_proxy_desc(&self) -> &String {
&self.proxy_desc
}
pub fn get_as_name(&self) -> &String {
&self.as_name
}
pub fn get_project_id(&self) -> &String {
&self.project_id
}
pub fn get_proxy_host(&self) -> &String {
&self.proxy_host
}
}
fn anonymize_ip(ip: String) -> String {
if ip.is_empty() {
return ip;
}
use std::net::IpAddr;
const KEEP_IPV4_BYTES: usize = 3;
const KEEP_IPV6_BYTES: usize = 6;
let ip: IpAddr = ip.clone().parse().unwrap();
let anonymized_ip = match ip {
IpAddr::V4(ip) => {
let mut data = ip.octets();
data[KEEP_IPV4_BYTES..].fill(0);
IpAddr::V4(data.into())
}
IpAddr::V6(ip) => {
let mut data = ip.octets();
data[KEEP_IPV6_BYTES..].fill(0);
IpAddr::V6(data.into())
}
};
anonymized_ip.to_string()
}