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
use reqwest::{
header::{HeaderMap, HeaderValue, AUTHORIZATION},
Client,
};
use serde_json::json;
pub use string_concat::{string_concat, string_concat_impl};
#[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_share_url = "https://proxy.webshare.io/api/v2/proxy/ipauthorization/";
match dotenv::var("PROXY_SHARE_PASSWORD") {
Ok(password) => {
let mut headers = HeaderMap::new();
match HeaderValue::from_str(
&string_concat!("Token ".to_string(), password).to_string(),
) {
Ok(hv) => {
headers.insert(AUTHORIZATION, hv);
}
_ => (),
}
let action = if remove {
client.delete(string_concat!(proxy_share_url, target, "/"))
} else {
client
.post(proxy_share_url)
.json(&json!({ "ip_address": 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.");
} else {
if response.status() == 400 {
if !remove {
// we need to get the existing IP from the system to get the ID.
// we first need to list all of the IPS from the proxy until we find our ID. After finding our ID we can remove the value.
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.");
} 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.", e)
}
};
}
p
}
// setup any of the proxies white listing if needed.
pub async fn get_local_ip(client: &Client) -> String {
// set the whitelist to the proxy service.
let metadata_url = "https://api.ipify.org";
let mut ip_address = String::new();
match client.get(metadata_url).send().await {
Ok(response) => {
if response.status().is_success() {
ip_address = response.text().await.unwrap_or_default();
println!("The IP address of the local server is {}", ip_address);
} else {
eprintln!(
"Failed to retrieve IP address. Status: {}",
response.status()
);
}
}
Err(e) => {
println!("{:?}", e)
}
}
ip_address
}
// Get the main local IP address
pub async fn get_ip(client: &Client) -> String {
let proxy_share_url = "https://proxy.webshare.io/api/v2/proxy/ipauthorization/whatsmyip/";
let mut ip_address = String::new();
// SETUP IP whitelisting. TODO: move this to another file.
match dotenv::var("PROXY_SHARE_PASSWORD") {
Ok(password) => {
let mut headers = HeaderMap::new();
match HeaderValue::from_str(&string_concat!("Token ".to_string(), password).to_string())
{
Ok(hv) => {
headers.insert(AUTHORIZATION, hv);
}
_ => (),
}
match client.get(proxy_share_url).headers(headers).send().await {
Ok(response) => {
if response.status().is_success() {
let text = response.text().await.unwrap_or_default();
let ip: ProxtIP = serde_json::from_str(&text).unwrap_or_default();
println!("The IP address of the server is {}", ip.ip_address);
ip_address = ip.ip_address;
} else {
eprintln!(
"Failed to retrieve IP address. Status: {}",
response.status()
);
ip_address = get_local_ip(&client).await;
}
}
Err(e) => {
println!("{:?}", e);
ip_address = get_local_ip(&client).await;
}
}
}
Err(e) => {
println!("Set the env {:?} to enable proxy whitelisting.", e)
}
};
ip_address
}