noxious_client/proxy.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 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
use crate::client::get_error_body;
use crate::client::Result;
use noxious::{
proxy::{ProxyConfig, ProxyWithToxics},
toxic::{StreamDirection, Toxic, ToxicKind},
};
use reqwest::{Client as HttpClient, StatusCode};
/// A proxy object returned by the [`Client`](Client).
/// To manipulate this proxy and manipulate the toxics, you can call methods on
/// this object.
#[derive(Debug, Clone, PartialEq)]
pub struct Proxy {
base_url: String,
created: bool,
/// Contains the proxy listen and upstream address, name. You can mutate them
/// and call `.save()` to update the proxy.
pub config: ProxyConfig,
toxics: Vec<Toxic>,
}
impl Proxy {
/// Save saves changes to a proxy such as its enabled status or upstream port.
/// Note: this does not update the toxics
pub async fn save(&mut self) -> Result<()> {
let request = if self.created {
HttpClient::new()
.post(self.base_url.clone() + "/proxies/" + &self.config.name)
.json(&self.config)
} else {
HttpClient::new()
.post(self.base_url.clone() + "/proxies")
.json(&self.config)
};
let response = request.send().await?;
if response.status().is_success() {
self.created = true;
Ok(())
} else {
let expected_status = if self.created {
StatusCode::OK
} else {
StatusCode::CREATED
};
Err(get_error_body(response, expected_status).await)
}
}
/// Enable a proxy again after it has been disabled
pub async fn enable(&mut self) -> Result<()> {
self.config.enabled = true;
self.save().await
}
/// Disable a proxy so that no connections can pass through. This will drop all active connections.
pub async fn disable(&mut self) -> Result<()> {
self.config.enabled = false;
self.save().await
}
/// Returns whether this proxy is enabled or not
pub fn is_enabled(&self) -> bool {
self.config.enabled
}
/// Give this proxy a new name, save it.
pub async fn change_name(&mut self, new_name: &str) -> Result<()> {
let old_name = self.config.name.clone();
self.config.name = new_name.to_owned();
let res = HttpClient::new()
.post(self.base_url.clone() + "/proxies/" + &old_name)
.json(&self.config)
.send()
.await?;
if res.status().is_success() {
Ok(())
} else {
Err(get_error_body(res, StatusCode::OK).await)
}
}
/// Delete a proxy complete and close all existing connections through it. All information about
/// the proxy such as listen port and active toxics will be deleted as well. If you just wish to
/// stop and later enable a proxy, use `enable()` and `disable()`.
pub async fn delete(self) -> Result<()> {
let res = HttpClient::new()
.delete(self.base_url.clone() + "/proxies/" + &self.config.name)
.send()
.await?;
if res.status().is_success() {
Ok(())
} else {
Err(get_error_body(res, StatusCode::NO_CONTENT).await)
}
}
/// Returns a map of all active toxics and their attributes
pub async fn toxics(&self) -> Result<Vec<Toxic>> {
let res = HttpClient::new()
.get(self.base_url.clone() + "/proxies/" + &self.config.name + "/toxics")
.send()
.await?;
if res.status().is_success() {
Ok(res.json::<Vec<Toxic>>().await?)
} else {
Err(get_error_body(res, StatusCode::OK).await)
}
}
/// Add a new toxic to this proxy.
/// # Example
/// ```ignore
/// use noxious_client::{Client, Toxic, ToxicKind, StreamDirection};
///
/// #[tokio::main]
/// async fn main() {
/// let toxic = Toxic {
/// kind: ToxicKind::Latency { latency: 40, jitter: 5 },
/// name: "myProxy_latency".to_owned(),
/// toxicity: 0.9,
/// direction: StreamDirection::Upstream,
/// };
///
/// let client = Client::new("127.0.0.1:8474");
/// let result = client.add_toxic(&toxic).await;
/// }
/// ```
///
pub async fn add_toxic(&self, toxic: &Toxic) -> Result<Toxic> {
let res = HttpClient::new()
.post(self.base_url.clone() + "/proxies/" + &self.config.name + "/toxics")
.json(toxic)
.send()
.await?;
if res.status().is_success() {
Ok(res.json::<Toxic>().await?)
} else {
Err(get_error_body(res, StatusCode::OK).await)
}
}
/// Updates a toxic with the given name
/// If toxicity is below zero, it will be sent as 0
pub async fn update_toxic(
&self,
name: &str,
toxicity: f32,
kind: ToxicKind,
direction: StreamDirection,
) -> Result<Toxic> {
let toxicity: f32 = if toxicity < 0.0 { 0.0 } else { toxicity };
let toxic = Toxic {
kind,
name: name.to_owned(),
toxicity,
direction,
};
let res = HttpClient::new()
.post(self.base_url.clone() + "/proxies/" + &self.config.name + "/toxics/" + name)
.json(&toxic)
.send()
.await?;
if res.status().is_success() {
Ok(res.json::<Toxic>().await?)
} else {
Err(get_error_body(res, StatusCode::OK).await)
}
}
/// Removes a toxic with the given name
pub async fn remove_toxic(&self, name: &str) -> Result<()> {
let res = HttpClient::new()
.delete(self.base_url.clone() + "/proxies/" + &self.config.name + "/toxics/" + name)
.send()
.await?;
if res.status().is_success() {
Ok(())
} else {
Err(get_error_body(res, StatusCode::NO_CONTENT).await)
}
}
pub(crate) fn new(base_url: &str, name: &str, listen: &str, upstream: &str) -> Proxy {
Proxy {
base_url: base_url.to_owned(),
created: false,
config: ProxyConfig {
name: name.to_owned(),
listen: listen.to_owned(),
upstream: upstream.to_owned(),
enabled: true,
rand_seed: None,
},
toxics: Vec::new(),
}
}
#[doc(hidden)]
pub fn from_proxy_with_toxics(base_url: &str, obj: ProxyWithToxics) -> Proxy {
Proxy {
base_url: base_url.to_owned(),
created: true,
config: obj.proxy,
toxics: obj.toxics,
}
}
}