diff --git a/Cargo.toml b/Cargo.toml index fa1feda..583e470 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rumqtt" description = "Mqtt client for your IOT needs" -version = "0.31.0" +version = "0.31.1" authors = ["raviteja , - expiry: i64 + expiry: i64, } pub struct NetworkStreamBuilder { @@ -96,7 +103,7 @@ use crate::client::network::{generate_httpproxy_auth, resolve}; proxy_host: proxy_host.to_owned(), proxy_port: proxy_port, key: key.to_owned(), - expiry + expiry, }); self @@ -133,6 +140,7 @@ use crate::client::network::{generate_httpproxy_auth, resolve}; } #[allow(clippy::too_many_arguments)] + #[cfg(feature = "jwt")] pub fn http_connect( &self, id: &str, @@ -179,9 +187,7 @@ use crate::client::network::{generate_httpproxy_auth, resolve}; let addr = resolve(host, port); let addr = future::result(addr); - addr.and_then(|addr| { - TcpStream::connect(&addr) - }) + addr.and_then(|addr| TcpStream::connect(&addr)) } pub fn connect( @@ -193,9 +199,24 @@ use crate::client::network::{generate_httpproxy_auth, resolve}; let host_tcp = host.to_owned(); let http_proxy = self.http_proxy.clone(); let stream = match http_proxy { - Some(HttpProxy{id, proxy_host, proxy_port, key, expiry}) => { - let s = self.http_connect(&id, &proxy_host, proxy_port, &host_tcp, port, &key, expiry); - Either::A(s) + Some(HttpProxy { + id, + proxy_host, + proxy_port, + key, + expiry, + }) => { + // http connect requires jwt + #[cfg(feature = "jwt")] + { + let s = self.http_connect(&id, &proxy_host, proxy_port, &host_tcp, port, &key, expiry); + Either::A(s) + } + #[cfg(not(feature = "jwt"))] + { + let s = self.tcp_connect(host, port); + Either::A(s) + } } None => { let s = self.tcp_connect(host, port); @@ -230,25 +251,27 @@ use crate::client::network::{generate_httpproxy_auth, resolve}; } } - fn resolve(host: &str, port: u16) -> Result { use std::net::ToSocketAddrs; - (host, port).to_socket_addrs() - .and_then(|mut addrs| { - addrs.next().ok_or_else(|| { - let err_msg = format!("invalid hostname '{}'", host); - io::Error::new(io::ErrorKind::Other, err_msg) - }) + (host, port).to_socket_addrs().and_then(|mut addrs| { + addrs.next().ok_or_else(|| { + let err_msg = format!("invalid hostname '{}'", host); + io::Error::new(io::ErrorKind::Other, err_msg) }) + }) } +/// Json WebToken helper functions +#[cfg(feature = "jwt")] fn generate_httpproxy_auth(id: &str, key: &[u8], expiry: i64) -> String { use chrono::{Duration, Utc}; + use jsonwebtoken::{encode, Algorithm, Header}; use uuid::Uuid; - #[derive(Debug, Serialize, Deserialize)] + #[derive(Debug)] + #[cfg_attr(feature = "jwt", derive(Serialize, Deserialize))] struct Claims { iat: i64, exp: i64, @@ -309,7 +332,6 @@ impl AsyncWrite for NetworkStream { } } - mod test { #[test] fn resolve() {