|
| 1 | +use crate::action::Action as RedirectionioAction; |
| 2 | +use crate::api::Log; |
| 3 | +use crate::filter::FilterBodyAction; |
| 4 | +use crate::http::{Header, PathAndQueryWithSkipped, Request as RedirectionioRequest, TrustedProxies}; |
| 5 | +use crate::RouterConfig; |
| 6 | +use chrono::Utc; |
| 7 | +use serde_json::{from_str as json_decode, to_string as json_encode}; |
| 8 | +use std::collections::hash_map::DefaultHasher; |
| 9 | +use std::hash::{Hash, Hasher}; |
| 10 | +use wasm_bindgen::prelude::*; |
| 11 | + |
| 12 | +#[wasm_bindgen()] |
| 13 | +pub struct Request { |
| 14 | + #[wasm_bindgen(skip)] |
| 15 | + pub request: RedirectionioRequest, |
| 16 | +} |
| 17 | + |
| 18 | +#[wasm_bindgen()] |
| 19 | +pub struct HeaderMap { |
| 20 | + #[wasm_bindgen(skip)] |
| 21 | + pub headers: Vec<Header>, |
| 22 | +} |
| 23 | + |
| 24 | +#[wasm_bindgen()] |
| 25 | +pub struct Action { |
| 26 | + #[wasm_bindgen(skip)] |
| 27 | + pub action: Option<RedirectionioAction>, |
| 28 | +} |
| 29 | + |
| 30 | +#[wasm_bindgen()] |
| 31 | +pub struct BodyFilter { |
| 32 | + #[wasm_bindgen(skip)] |
| 33 | + pub filter: Option<FilterBodyAction>, |
| 34 | +} |
| 35 | + |
| 36 | +#[wasm_bindgen()] |
| 37 | +impl Request { |
| 38 | + #[wasm_bindgen(constructor)] |
| 39 | + pub fn new(uri: String, host: String, scheme: String, method: String) -> Request { |
| 40 | + let config = RouterConfig::default(); |
| 41 | + |
| 42 | + Request { |
| 43 | + request: RedirectionioRequest { |
| 44 | + headers: Vec::new(), |
| 45 | + host: Some(host), |
| 46 | + method: Some(method), |
| 47 | + scheme: Some(scheme), |
| 48 | + path_and_query_skipped: PathAndQueryWithSkipped::from_config(&config, uri.as_str()), |
| 49 | + path_and_query: Some(uri), |
| 50 | + remote_addr: None, |
| 51 | + created_at: Some(Utc::now()), |
| 52 | + sampling_override: None, |
| 53 | + }, |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + pub fn set_remote_ip(&mut self, remote_addr_str: String) { |
| 58 | + self.request.set_remote_ip(remote_addr_str, &TrustedProxies::default()); |
| 59 | + } |
| 60 | + |
| 61 | + pub fn add_header(&mut self, name: String, value: String) { |
| 62 | + self.request.add_header(name, value, false) |
| 63 | + } |
| 64 | + |
| 65 | + pub fn serialize(&self) -> String { |
| 66 | + match json_encode(&self.request) { |
| 67 | + Err(_) => "".to_string(), |
| 68 | + Ok(request_serialized) => request_serialized, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + pub fn get_hash(&self) -> u64 { |
| 73 | + let mut hasher = DefaultHasher::new(); |
| 74 | + self.request.hash(&mut hasher); |
| 75 | + |
| 76 | + hasher.finish() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[wasm_bindgen()] |
| 81 | +impl HeaderMap { |
| 82 | + #[allow(clippy::new_without_default)] |
| 83 | + #[wasm_bindgen(constructor)] |
| 84 | + pub fn new() -> HeaderMap { |
| 85 | + HeaderMap { headers: Vec::new() } |
| 86 | + } |
| 87 | + |
| 88 | + pub fn add_header(&mut self, name: String, value: String) { |
| 89 | + self.headers.push(Header { name, value }) |
| 90 | + } |
| 91 | + |
| 92 | + pub fn remove_header(&mut self, name: String) { |
| 93 | + self.headers.retain(|header| header.name != name) |
| 94 | + } |
| 95 | + |
| 96 | + pub fn len(&self) -> usize { |
| 97 | + self.headers.len() |
| 98 | + } |
| 99 | + |
| 100 | + pub fn is_empty(&self) -> bool { |
| 101 | + self.headers.is_empty() |
| 102 | + } |
| 103 | + |
| 104 | + pub fn get_header_name(&self, index: usize) -> String { |
| 105 | + match self.headers.get(index) { |
| 106 | + None => "".to_string(), |
| 107 | + Some(header) => header.name.clone(), |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + pub fn get_header_value(&self, index: usize) -> String { |
| 112 | + match self.headers.get(index) { |
| 113 | + None => "".to_string(), |
| 114 | + Some(header) => header.value.clone(), |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[wasm_bindgen()] |
| 120 | +impl Action { |
| 121 | + #[wasm_bindgen(constructor)] |
| 122 | + pub fn new(action_serialized: String) -> Action { |
| 123 | + let action = match json_decode(action_serialized.as_str()) { |
| 124 | + Err(error) => { |
| 125 | + log::error!("Unable to deserialize \"{}\" to action: {}", action_serialized, error,); |
| 126 | + |
| 127 | + None |
| 128 | + } |
| 129 | + Ok(action) => Some(action), |
| 130 | + }; |
| 131 | + |
| 132 | + Action { action } |
| 133 | + } |
| 134 | + |
| 135 | + pub fn empty() -> Action { |
| 136 | + Action { action: None } |
| 137 | + } |
| 138 | + |
| 139 | + pub fn get_status_code(&mut self, response_status_code: u16) -> u16 { |
| 140 | + if let Some(action) = self.action.as_mut() { |
| 141 | + return action.get_status_code(response_status_code, None); |
| 142 | + } |
| 143 | + |
| 144 | + 0 |
| 145 | + } |
| 146 | + |
| 147 | + pub fn filter_headers(&mut self, headers: HeaderMap, response_status_code: u16, add_rule_ids_header: bool) -> HeaderMap { |
| 148 | + if self.action.is_none() { |
| 149 | + return headers; |
| 150 | + } |
| 151 | + |
| 152 | + let action = self.action.as_mut().unwrap(); |
| 153 | + let new_headers = action.filter_headers(headers.headers, response_status_code, add_rule_ids_header, None); |
| 154 | + |
| 155 | + HeaderMap { headers: new_headers } |
| 156 | + } |
| 157 | + |
| 158 | + pub fn create_body_filter(&mut self, response_status_code: u16, headers: &HeaderMap) -> BodyFilter { |
| 159 | + if self.action.is_none() { |
| 160 | + return BodyFilter { filter: None }; |
| 161 | + } |
| 162 | + |
| 163 | + let action = self.action.as_mut().unwrap(); |
| 164 | + let filter = action.create_filter_body(response_status_code, &headers.headers); |
| 165 | + |
| 166 | + BodyFilter { filter } |
| 167 | + } |
| 168 | + |
| 169 | + pub fn should_log_request(&mut self, response_status_code: u16) -> bool { |
| 170 | + if self.action.is_none() { |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + let action = self.action.as_mut().unwrap(); |
| 175 | + |
| 176 | + action.should_log_request(true, response_status_code, None) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +#[wasm_bindgen()] |
| 181 | +impl BodyFilter { |
| 182 | + pub fn is_null(&self) -> bool { |
| 183 | + self.filter.is_none() |
| 184 | + } |
| 185 | + |
| 186 | + pub fn filter(&mut self, data: Vec<u8>) -> Vec<u8> { |
| 187 | + match self.filter.as_mut() { |
| 188 | + None => data, |
| 189 | + Some(filter) => filter.filter(data, None), |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + pub fn end(&mut self) -> Vec<u8> { |
| 194 | + match self.filter.as_mut() { |
| 195 | + None => Vec::new(), |
| 196 | + Some(filter) => filter.end(None), |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +#[wasm_bindgen()] |
| 202 | +pub fn init_log() { |
| 203 | + wasm_logger::init(wasm_logger::Config::default()); |
| 204 | +} |
| 205 | + |
| 206 | +#[wasm_bindgen()] |
| 207 | +pub fn create_log_in_json( |
| 208 | + request: Request, |
| 209 | + status_code: u16, |
| 210 | + response_headers: HeaderMap, |
| 211 | + action: &Action, |
| 212 | + proxy: String, |
| 213 | + time: u64, |
| 214 | + client_ip: String, |
| 215 | +) -> String { |
| 216 | + let log = Log::from_proxy( |
| 217 | + &request.request, |
| 218 | + status_code, |
| 219 | + &response_headers.headers, |
| 220 | + action.action.as_ref(), |
| 221 | + proxy.as_str(), |
| 222 | + time.into(), |
| 223 | + client_ip.as_str(), |
| 224 | + None, |
| 225 | + ); |
| 226 | + |
| 227 | + match json_encode(&log) { |
| 228 | + Err(_) => "".to_string(), |
| 229 | + Ok(s) => s, |
| 230 | + } |
| 231 | +} |
0 commit comments