Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

Fix no default features #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rumqtt"
description = "Mqtt client for your IOT needs"
version = "0.31.0"
version = "0.31.1"
authors = ["raviteja <[email protected]"]
documentation = "https://docs.rs/rumqtt"
repository = "https://github.com/AtherEnergy/rumqtt"
Expand All @@ -19,8 +19,8 @@ derive_more = "0.13"
base64 = "0.10"
uuid = {version = "0.7", features = ["serde", "v4"]}
mqtt311 = "0.2"
tokio-rustls = ">=0.8, <=0.9"
webpki = ">=0.8, <=0.19"
tokio-rustls = "^0.9"
webpki = ">=0.8, <0.20"


[dependencies.jsonwebtoken]
Expand Down Expand Up @@ -48,4 +48,11 @@ pretty_env_logger = "0.3"
[features]
default = ["jwt"]
acknotify = []
jwt = ["jsonwebtoken", "chrono", "serde", "serde_derive"]
jwt = ["jsonwebtoken", "chrono", "serde", "serde_derive"]

[[example]]
name = "gcloud"
required-features = ["jwt"]
[[example]]
name = "genproxyauth"
required-features = ["jwt"]
74 changes: 48 additions & 26 deletions src/client/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,38 @@ use std::io::{self, Read, Write};

use crate::client::network::stream::NetworkStream;
use futures::Poll;

#[cfg(feature = "jwt")]
use serde_derive::{Deserialize, Serialize};

use std::net::SocketAddr;
use tokio::io::{AsyncRead, AsyncWrite};

pub mod stream {
use crate::client::network::{generate_httpproxy_auth, resolve};
use crate::codec::MqttCodec;
use crate::error::ConnectError;

#[cfg(feature = "jwt")]
use crate::client::network::generate_httpproxy_auth;

use crate::{client::network::resolve, codec::MqttCodec, error::ConnectError};

use futures::{
future::{self, Either},
sink::Sink,
stream::Stream,
Future,
};
use std::{
io::{
self, {BufReader, Cursor},
},
io::{self, BufReader, Cursor},
sync::Arc,
};
use tokio::net::TcpStream;
use tokio::codec::{Decoder, Framed, LinesCodec};
use tokio::{
codec::{Decoder, Framed, LinesCodec},
net::TcpStream,
};
use tokio_rustls::{
rustls::{internal::pemfile, ClientConfig, ClientSession},
TlsConnector, TlsStream,
TlsConnector,
TlsStream,
};
use webpki::DNSNameRef;

Expand Down Expand Up @@ -54,7 +61,7 @@ use crate::client::network::{generate_httpproxy_auth, resolve};
proxy_host: String,
proxy_port: u16,
key: Vec<u8>,
expiry: i64
expiry: i64,
}

pub struct NetworkStreamBuilder {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
}) => {
Comment on lines +202 to +208
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the rustfmt approved those line breaks?

// 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);
Expand Down Expand Up @@ -230,25 +251,27 @@ use crate::client::network::{generate_httpproxy_auth, resolve};
}
}


fn resolve(host: &str, port: u16) -> Result<SocketAddr, io::Error> {
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,
Expand Down Expand Up @@ -309,7 +332,6 @@ impl AsyncWrite for NetworkStream {
}
}


mod test {
#[test]
fn resolve() {
Expand Down