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
use reqwest::header::{CONTENT_TYPE, COOKIE};
use reqwest::{
header::{HeaderMap, HeaderValue},
Client,
};
use serde_json::json;
use std::env;
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
struct ProxtIP {
/// The local ip address of the server.
ip_address: String,
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
pub struct ProxyIP {
/// The ip address of the request
pub ip_address: String,
/// The id of the whitelist
pub id: u128,
#[serde(default)]
/// The created at date
pub created_at: Option<String>,
#[serde(default)]
/// The last used data.
pub last_used_at: Option<String>,
}
// setup any of the proxies white listing if needed.
pub async fn setup_proxy(client: &Client, target: &str, remove: bool) -> ProxyIP {
let mut p = ProxyIP::default();
if !target.is_empty() {
let proxy_url = "https://api.evomi.com/products/ip_whitelist";
match env::var("EVOMI_API_TOKEN") {
Ok(password) => {
let mut headers = HeaderMap::new();
match HeaderValue::from_str(&format!("Authorization={}", password).to_string()) {
Ok(hv) => {
headers.insert(COOKIE, hv);
headers.insert(
CONTENT_TYPE,
HeaderValue::from_static("application/json; charset=utf-8"),
);
}
_ => (),
}
let action = if remove {
client.delete(proxy_url).json(&json!({ "ip": target }))
} else {
client.post(proxy_url).json(&json!({ "ip": target }))
};
match action.headers(headers).send().await {
Ok(response) => {
if response.status().is_success() {
if !remove {
let ip: ProxyIP = serde_json::from_str(
&response.text().await.unwrap_or_default(),
)
.unwrap_or_default();
p.clone_from(&ip);
}
println!("Successfully updated the IP authorization for Evomi.");
} else {
if response.status() == 400 {
if !remove {
let res_text = response.text().await;
let ip: ProxyIP =
serde_json::from_str(&res_text.unwrap_or_default())
.unwrap_or_default();
p.clone_from(&ip);
}
println!("IP already whitelisted for Evomi.");
} else {
if !remove && response.status() == 500 {
println!(
"IP Already whitelisted for evomi. Status: {:?} - Removed:{:?}",
response.status(),
remove,
);
} else {
println!(
"Failed to update the IP authorization. Status: {:?} - Removed:{:?}",
response.status(),
remove,
);
}
}
}
}
Err(err) => {
println!("{:?}", err)
}
}
}
Err(e) => {
println!("Set the env {:?} to enable proxy whitelisting Evomi.", e)
}
};
}
p
}