metasploit/msf/blocking/
db.rs

1#![allow(non_camel_case_types)]
2#[path="../../connect.rs"] mod connect;
3#[path="../../structs/mod.rs"] mod structs;
4use crate::error::{MsfError,Error as E};
5use structs::request as req;
6use crate::client::Client;
7use std::fs::File;
8use std::io::Read;
9use std::collections::HashMap;
10use serde::{Serialize,de::DeserializeOwned as DOwned};
11use rmp_serde::{Serializer,decode::Error as derror,from_read};
12
13pub fn hosts<T:DOwned>(client:Client) -> Result<T,E> {
14    let mut body=Vec::new();
15    let mut serializer=Serializer::new(&mut body);
16    let byte=req::db::hosts("db.hosts".to_string(),client.token.as_ref().unwrap().to_string());
17    byte.serialize(&mut serializer).unwrap();
18    r#return(client.url,body)
19
20}
21pub fn get_host<T:DOwned>(client:Client,workspace:Option<String>,host:&str) -> Result<T,E>  {
22    let mut body=Vec::new();
23    let mut serializer=Serializer::new(&mut body);
24    let mut hash:HashMap<String,String>=HashMap::new();
25    hash.insert("host".to_string(),host.to_string());
26    match workspace {
27        Some(val) => {
28            hash.insert("workspace".to_string(),val);
29        },
30        None => {},
31    };
32    let byte=req::db::grd_host("db.get_host".to_string(),client.token.as_ref().unwrap().to_string(),hash);
33    byte.serialize(&mut serializer).unwrap();
34    r#return(client.url,body)
35}
36pub fn report_host<T:DOwned>(client:Client,workspace:Option<String>,host:&str) -> Result<T,E> {
37    let mut body=Vec::new();
38    let mut serializer=Serializer::new(&mut body);
39    let mut hash:HashMap<String,String>=HashMap::new();
40    hash.insert("host".to_string(),host.to_string());
41    match workspace {
42        Some(val) => {
43            hash.insert("workspace".to_string(),val);
44        },
45        None => {},
46    };
47    let byte=req::db::grd_host("db.report_host".to_string(),client.token.as_ref().unwrap().to_string(),hash);
48    byte.serialize(&mut serializer).unwrap();
49    r#return(client.url,body)
50}
51pub fn del_host<T:DOwned>(client:Client,workspace:Option<String>,host:&str) -> Result<T,E> {
52    let mut body=Vec::new();
53    let mut serializer=Serializer::new(&mut body);
54    let mut hash:HashMap<String,String>=HashMap::new();
55    hash.insert("host".to_string(),host.to_string());
56    match workspace {
57        Some(val) => {
58            hash.insert("workspace".to_string(),val);
59        },
60        None => {},
61    };
62    let byte=req::db::grd_host("db.del_host".to_string(),client.token.as_ref().unwrap().to_string(),hash);
63    byte.serialize(&mut serializer).unwrap();
64    r#return(client.url,body)
65}
66pub fn services<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
67    let mut body=Vec::new();
68    let mut serializer=Serializer::new(&mut body);
69    let byte=req::db::nc("db.services".to_string(),client.token.as_ref().unwrap().to_string(),hash);
70    byte.serialize(&mut serializer).unwrap();
71    r#return(client.url,body)
72}
73pub fn report_service<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
74    let mut body=Vec::new();
75    let mut serializer=Serializer::new(&mut body);
76    let byte=req::db::grd_service("db.report_service".to_string(),client.token.as_ref().unwrap().to_string(),hash);
77    byte.serialize(&mut serializer).unwrap();
78    r#return(client.url,body)
79}
80pub fn get_service<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
81    let mut body=Vec::new();
82    let mut serializer=Serializer::new(&mut body);
83    let byte=req::db::grd_service("db.get_service".to_string(),client.token.as_ref().unwrap().to_string(),hash);
84    byte.serialize(&mut serializer).unwrap();
85    r#return(client.url,body)
86}
87pub fn del_service<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
88    let mut body=Vec::new();
89    let mut serializer=Serializer::new(&mut body);
90    let byte=req::db::grd_service("db.del_service".to_string(),client.token.as_ref().unwrap().to_string(),hash);
91    byte.serialize(&mut serializer).unwrap();
92    r#return(client.url,body)
93}
94
95pub fn vulns<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
96    let mut body=Vec::new();
97    let mut serializer=Serializer::new(&mut body);
98    let byte=req::db::nc("db.vulns".to_string(),client.token.as_ref().unwrap().to_string(),hash);
99    byte.serialize(&mut serializer).unwrap();
100    r#return(client.url,body)
101}
102pub fn del_vuln<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
103    let mut body=Vec::new();
104    let mut serializer=Serializer::new(&mut body);
105    let byte=req::db::grd_vuln("db.del_vuln".to_string(),client.token.as_ref().unwrap().to_string(),hash);
106    byte.serialize(&mut serializer).unwrap();
107    r#return(client.url,body)
108}
109pub fn report_vuln<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
110    let mut body=Vec::new();
111    let mut serializer=Serializer::new(&mut body);
112    let byte=req::db::grd_vuln("db.report_vuln".to_string(),client.token.as_ref().unwrap().to_string(),hash);
113    byte.serialize(&mut serializer).unwrap();
114    r#return(client.url,body)
115}
116pub fn get_vuln<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
117    let mut body=Vec::new();
118    let mut serializer=Serializer::new(&mut body);
119    let byte=req::db::grd_vuln("db.get_vuln".to_string(),client.token.as_ref().unwrap().to_string(),hash);
120    byte.serialize(&mut serializer).unwrap();
121    r#return(client.url,body)
122}
123
124pub fn workspaces<T:DOwned>(client:Client) -> Result<T,E> {
125    let mut body=Vec::new();
126    let mut serializer=Serializer::new(&mut body);
127    let byte=req::db::workspaces("db.workspaces".to_string(),client.token.as_ref().unwrap().to_string());
128    byte.serialize(&mut serializer).unwrap();
129    r#return(client.url,body)
130}
131pub fn current_workspace<T:DOwned>(client:Client) -> Result<T,E> {
132    let mut body=Vec::new();
133    let mut serializer=Serializer::new(&mut body);
134    let byte=req::db::current_workspace("db.current_workspace".to_string(),client.token.as_ref().unwrap().to_string());
135    byte.serialize(&mut serializer).unwrap();
136    r#return(client.url,body)
137}
138pub fn get_workspace<T:DOwned>(client:Client,workspace:&str) -> Result<T,E> {
139    let mut body=Vec::new();
140    let mut serializer=Serializer::new(&mut body);
141    let byte=req::db::gsda_workspace("db.get_workspace".to_string(),client.token.as_ref().unwrap().to_string(),workspace.to_string());
142    byte.serialize(&mut serializer).unwrap();
143    r#return(client.url,body)
144}
145pub fn set_workspace<T:DOwned>(client:Client,workspace:&str) -> Result<T,E> {
146    let mut body=Vec::new();
147    let mut serializer=Serializer::new(&mut body);
148    let byte=req::db::gsda_workspace("db.set_workspace".to_string(),client.token.as_ref().unwrap().to_string(),workspace.to_string());
149    byte.serialize(&mut serializer).unwrap();
150    r#return(client.url,body)
151}
152pub fn del_workspace<T:DOwned>(client:Client,workspace:&str) -> Result<T,E> {
153    let mut body=Vec::new();
154    let mut serializer=Serializer::new(&mut body);
155    let byte=req::db::gsda_workspace("db.del_workspace".to_string(),client.token.as_ref().unwrap().to_string(),workspace.to_string());
156    byte.serialize(&mut serializer).unwrap();
157    r#return(client.url,body)
158}
159pub fn add_workspace<T:DOwned>(client:Client,workspace:&str) -> Result<T,E> {
160    let mut body=Vec::new();
161    let mut serializer=Serializer::new(&mut body);
162    let byte=req::db::gsda_workspace("db.add_workspace".to_string(),client.token.as_ref().unwrap().to_string(),workspace.to_string());
163    byte.serialize(&mut serializer).unwrap();
164    r#return(client.url,body)
165}
166
167pub fn get_note<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
168    let mut body=Vec::new();
169    let mut serializer=Serializer::new(&mut body);
170    let byte=req::db::grd_note("db.get_note".to_string(),client.token.as_ref().unwrap().to_string(),hash);
171    byte.serialize(&mut serializer).unwrap();
172    r#return(client.url,body)
173}
174pub fn report_note<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
175    let mut body=Vec::new();
176    let mut serializer=Serializer::new(&mut body);
177    let byte=req::db::grd_note("db.report_note".to_string(),client.token.as_ref().unwrap().to_string(),hash);
178    byte.serialize(&mut serializer).unwrap();
179    r#return(client.url,body)
180}
181pub fn notes<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
182    let mut body=Vec::new();
183    let mut serializer=Serializer::new(&mut body);
184    let byte=req::db::nc("db.notes".to_string(),client.token.as_ref().unwrap().to_string(),hash);
185    byte.serialize(&mut serializer).unwrap();
186    r#return(client.url,body)
187}
188pub fn del_note<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
189    let mut body=Vec::new();
190    let mut serializer=Serializer::new(&mut body);
191    let byte=req::db::grd_note("db.del_note".to_string(),client.token.as_ref().unwrap().to_string(),hash);
192    byte.serialize(&mut serializer).unwrap();
193    r#return(client.url,body)
194}
195
196pub fn get_client<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
197    let mut body=Vec::new();
198    let mut serializer=Serializer::new(&mut body);
199    let byte=req::db::grd_client("db.get_client".to_string(),client.token.as_ref().unwrap().to_string(),hash);
200    byte.serialize(&mut serializer).unwrap();
201    r#return(client.url,body)
202}
203pub fn clients<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
204    let mut body=Vec::new();
205    let mut serializer=Serializer::new(&mut body);
206    let byte=req::db::nc("db.clients".to_string(),client.token.as_ref().unwrap().to_string(),hash);
207    byte.serialize(&mut serializer).unwrap();
208    r#return(client.url,body)
209}
210pub fn del_client<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
211    let mut body=Vec::new();
212    let mut serializer=Serializer::new(&mut body);
213    let byte=req::db::grd_client("db.del_client".to_string(),client.token.as_ref().unwrap().to_string(),hash);
214    byte.serialize(&mut serializer).unwrap();
215    r#return(client.url,body)
216}
217pub fn report_client<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
218    let mut body=Vec::new();
219    let mut serializer=Serializer::new(&mut body);
220    let byte=req::db::grd_client("db.report_client".to_string(),client.token.as_ref().unwrap().to_string(),hash);
221    byte.serialize(&mut serializer).unwrap();
222    r#return(client.url,body)
223}
224
225pub fn get_ref<T:DOwned>(client:Client,ref_name:&str) -> Result<T,E> {
226    let mut body=Vec::new();
227    let mut serializer=Serializer::new(&mut body);
228    let byte=req::db::get_ref("db.get_ref".to_string(),client.token.as_ref().unwrap().to_string(),ref_name.to_string());
229    byte.serialize(&mut serializer).unwrap();
230    r#return(client.url,body)
231}
232
233pub fn events<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
234    let mut body=Vec::new();
235    let mut serializer=Serializer::new(&mut body);
236    let byte=req::db::nc("db.events".to_string(),client.token.as_ref().unwrap().to_string(),hash);
237    byte.serialize(&mut serializer).unwrap();
238    r#return(client.url,body)
239}
240pub fn report_event<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
241    let mut body=Vec::new();
242    let mut serializer=Serializer::new(&mut body);
243    let byte=req::db::report_event("db.report_event".to_string(),client.token.as_ref().unwrap().to_string(),hash);
244    byte.serialize(&mut serializer).unwrap();
245    r#return(client.url,body)
246}
247
248pub fn report_loot<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
249    let mut body=Vec::new();
250    let mut serializer=Serializer::new(&mut body);
251    let byte=req::db::report_loot("db.report_loot".to_string(),client.token.as_ref().unwrap().to_string(),hash);
252    byte.serialize(&mut serializer).unwrap();
253    r#return(client.url,body)
254}
255pub fn loots<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
256    let mut body=Vec::new();
257    let mut serializer=Serializer::new(&mut body);
258    let byte=req::db::nc("db.loots".to_string(),client.token.as_ref().unwrap().to_string(),hash);
259    byte.serialize(&mut serializer).unwrap();
260    r#return(client.url,body)
261}
262
263pub fn creds<T:DOwned>(client:Client,hash:HashMap<String,String>) -> Result<T,E> {
264    let mut body=Vec::new();
265    let mut serializer=Serializer::new(&mut body);
266    let byte=req::db::nc("db.creds".to_string(),client.token.as_ref().unwrap().to_string(),hash);
267    byte.serialize(&mut serializer).unwrap();
268    r#return(client.url,body)
269}
270
271pub fn import_data<T:DOwned>(client:Client,data:&str) -> Result<T,E> {
272    let mut body=Vec::new();
273    let mut serializer=Serializer::new(&mut body);
274    let byte=req::db::import_db("db.import_data".to_string(),client.token.as_ref().unwrap().to_string(),data.to_string());
275    byte.serialize(&mut serializer).unwrap();
276    r#return(client.url,body)
277}
278pub fn import_file<T:DOwned>(client:Client,mut file:File) -> Result<T,E> {
279    let mut body=Vec::new();
280    let mut serializer=Serializer::new(&mut body);
281    let mut data=String::new();
282    file.read_to_string(&mut data).unwrap();
283    let byte=req::db::import_db("db.import_data".to_string(),client.token.as_ref().unwrap().to_string(),data);
284    byte.serialize(&mut serializer).unwrap();
285    r#return(client.url,body)
286}
287pub fn set_driver<T:DOwned>(client:Client,driver:&str) -> Result<T,E> {
288    let mut body=Vec::new();
289    let mut hash:HashMap<String,String>=HashMap::new();
290    let mut serializer=Serializer::new(&mut body);
291    hash.insert("driver".to_string(),driver.to_string());
292    let byte=req::db::driver("db.driver".to_string(),client.token.as_ref().unwrap().to_string(),hash);
293    byte.serialize(&mut serializer).unwrap();
294    r#return(client.url,body)
295}
296pub fn get_driver<T:DOwned>(client:Client) -> Result<T,E> {
297    let mut body=Vec::new();
298    let hash:HashMap<String,String>=HashMap::new();
299    let mut serializer=Serializer::new(&mut body);
300    let byte=req::db::driver("db.driver".to_string(),client.token.as_ref().unwrap().to_string(),hash);
301    byte.serialize(&mut serializer).unwrap();
302    r#return(client.url,body)
303}
304
305pub fn dbconnect<T:DOwned>(client:Client,driver:&str,mut hash:HashMap<String,String>) -> Result<T,E> {
306    let mut body=Vec::new();
307    hash.insert("driver".to_string(),driver.to_string());
308    let mut serializer=Serializer::new(&mut body);
309    let byte=req::db::connect("db.connect".to_string(),client.token.as_ref().unwrap().to_string(),hash);
310    byte.serialize(&mut serializer).unwrap();
311    r#return(client.url,body)
312}
313
314pub fn status<T:DOwned>(client:Client) -> Result<T,E> {
315    let mut body=Vec::new();
316    let mut serializer=Serializer::new(&mut body);
317    let byte=req::db::status("db.status".to_string(),client.token.as_ref().unwrap().to_string());
318    byte.serialize(&mut serializer).unwrap();
319    r#return(client.url,body)
320}
321
322pub fn disconnect<T:DOwned>(client:Client) -> Result<T,E> {
323    let mut body=Vec::new();
324    let mut serializer=Serializer::new(&mut body);
325    let byte=req::db::disconnect("db.disconnect".to_string(),client.token.as_ref().unwrap().to_string());
326    byte.serialize(&mut serializer).unwrap();
327    r#return(client.url,body)
328}
329
330fn r#return<T:DOwned>(url:String,body:Vec<u8>) -> Result<T,E> {
331    let mut buf=vec![];
332    let con=connect::connect(url,body,&mut buf);
333    let new_buf=buf.clone();
334    match con {
335        Ok(_) => {
336            let ret:Result<T,derror>=from_read(new_buf.as_slice());
337            match ret {
338                Ok(val) => {
339                    Ok(val)
340                },
341                Err(_) => {
342                    let ret2:Result<MsfError,derror>=from_read(new_buf.as_slice());
343                    match ret2 {
344                        Ok(e) => {
345                            Err(E::MsfError(e))
346                        },
347                        Err(e) => {
348                            Err(E::DError(e))
349                        },
350                    }
351                },
352            }
353        },
354        Err(e) => {
355            Err(E::ConnectionError(e))
356        },
357    }
358}