metasploit/
client.rs

1#[path="./connect.rs"] mod connect;
2#[path="./structs/mod.rs"] mod structs;
3use structs::request::auth::login;
4use rmp_serde::{Serializer,Deserializer,decode::Error};
5use serde::{Serialize,Deserialize};
6#[derive(Deserialize)]
7struct Reslogin {
8    result:String,
9    token:String,
10}
11#[derive(Debug,Clone)]
12pub struct Client {
13    pub url:String,
14    pub token:Option<String>,
15}
16impl Client {
17    pub fn new(host:&str,port:i32,user:&str,password:&str,ssl:bool) -> Self {
18        let new_user=String::from(user);
19        let url:String;
20        let new_pass=String::from(password);
21        if ssl {
22            url=format!("https://{}:{}/api",host,port).to_string()
23        } else {
24            url=format!("http://{}:{}/api",host,port).to_string()
25        };
26        let mut body=Vec::new();
27        let mut buf=vec![];
28        let mut serializer=Serializer::new(&mut body);
29        let byte=login("auth.login".to_string(),new_user.clone(),new_pass);
30        byte.serialize(&mut serializer).unwrap();
31        let con=connect::connect(url.clone(),body,&mut buf);
32        let mut de=Deserializer::new(buf.as_slice());
33        let mut token=String::new();
34        match con {
35			Ok(_) => {
36				let de_ret:Result<Reslogin,Error>=Deserialize::deserialize(&mut de);
37				if let Ok(ref val) = de_ret {
38					if val.result=="success".to_string() {
39						token=val.token.clone();
40					} else {
41						panic!("Not authorised.Username or password is wrong");
42					}
43				}
44				if let Err(_) = de_ret {
45					panic!("Not authorised.Username or password is wrong");
46				}
47			},
48			Err(_) => {
49				panic!("Couldn't connect to the metasploit RPC Server at {}:{}",host,port);
50			},
51		}
52		Client {
53			url:url.clone(),
54			token:Some(token.clone()),
55		}
56    }
57    pub fn gettoken(&self) -> String {
58        self.token.as_ref().unwrap().to_string().clone()
59    }
60    pub fn geturl(&self) -> String {
61    	self.url.clone()
62    }
63}