Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Latest tonic #9

Open
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "tonic-openssl"
version = "0.2.0"
version = "0.3.0"
authors = ["Lucio Franco <[email protected]>"]
edition = "2018"
edition = "2021"
license = "MIT"
documentation = "https://docs.rs/tonic-openssl/0.2.0/tonic_openssl"
repository = "https://github.com/LucioFranco/tonic-openssl"
Expand All @@ -19,7 +19,7 @@ members = [

[dependencies]
tokio = "1"
tonic = "0.8"
tonic = "0.12"
async-stream = "0.3"
tokio-openssl = "0.6"
openssl = "0.10"
Expand Down
17 changes: 9 additions & 8 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example"
version = "0.1.0"
authors = ["Lucio Franco <[email protected]>"]
edition = "2018"
edition = "2021"
publish = false

[[bin]]
Expand All @@ -18,16 +18,17 @@ name = "server"
path = "src/server.rs"

[dependencies]
tonic = "0.8"
tonic-openssl = { version = "0.2", path = ".." }
hyper = "0.14"
hyper-openssl = "0.9"
prost = "0.11"
tonic = "0.12"
tonic-openssl = { version = "0.3", path = ".." }
hyper = { version = "1.0", features = ["http1", "http2"] }
hyper-openssl = { version = "0.10", features = ["client-legacy"] }
prost = "0.13"
tokio = { version = "1", features = ["full"] }
tokio-stream = { version = "0.1", features = ["net"] }
openssl = "0.10"
tower = "0.4"
tower = "0.5"
pretty_env_logger = "*"
hyper-util = { version = "0.1.9", features = ["client-legacy", "http1", "http2"] }

[build-dependencies]
tonic-build = "0.8"
tonic-build = "0.12"
7 changes: 4 additions & 3 deletions example/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use hyper::{client::connect::HttpConnector, Client, Uri};
use hyper_openssl::HttpsConnector;
use hyper::Uri;
use hyper_util::{client::legacy::{connect::HttpConnector, Client}, rt::TokioExecutor};
use hyper_openssl::client::legacy::HttpsConnector;
use openssl::{
ssl::{SslConnector, SslMethod},
x509::X509,
Expand Down Expand Up @@ -37,7 +38,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// Configure hyper's client to be h2 only and build with the
// correct https connector.
let hyper = Client::builder().http2_only(true).build(https);
let hyper = Client::builder(TokioExecutor::new()).http2_only(true).build(https);

let uri = Uri::from_static("https://[::1]:50051");

Expand Down
16 changes: 7 additions & 9 deletions example/src/client2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
//! This makes it easier to store the resulting GreeterClient inside a struct.
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use hyper::{
client::{HttpConnector, ResponseFuture},
Body, Client, Request, Response, Uri,
};
use hyper_openssl::HttpsConnector;
use hyper::{Request, Response, Uri};
use hyper_util::{client::legacy::{connect::HttpConnector, Client, ResponseFuture}, rt::TokioExecutor};
use hyper_openssl::client::legacy::HttpsConnector;
use openssl::{
ssl::{SslConnector, SslMethod},
x509::X509,
Expand Down Expand Up @@ -55,7 +53,7 @@ impl MyChannel {
let mut http = HttpConnector::new();
http.enforce_http(false);
let client = match certificate {
None => MyClient::ClearText(Client::builder().http2_only(true).build(http)),
None => MyClient::ClearText(Client::builder(TokioExecutor::new()).http2_only(true).build(http)),
Some(pem) => {
let ca = X509::from_pem(&pem[..])?;
let mut connector = SslConnector::builder(SslMethod::tls())?;
Expand All @@ -66,7 +64,7 @@ impl MyChannel {
c.set_verify_hostname(false);
Ok(())
});
MyClient::Tls(Client::builder().http2_only(true).build(https))
MyClient::Tls(Client::builder(TokioExecutor::new()).http2_only(true).build(https))
}
};

Expand All @@ -77,8 +75,8 @@ impl MyChannel {
// Check out this blog post for an introduction to Tower:
// https://tokio.rs/blog/2021-05-14-inventing-the-service-trait
impl Service<Request<BoxBody>> for MyChannel {
type Response = Response<Body>;
type Error = hyper::Error;
type Response = Response<hyper::body::Incoming>;
type Error = hyper_util::client::legacy::Error;
type Future = ResponseFuture;

fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Expand Down
42 changes: 42 additions & 0 deletions src/certificate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// Represents a X509 certificate.
#[derive(Debug, Clone)]
pub struct Certificate {
pub(crate) pem: Vec<u8>,
}

impl Certificate {
/// Parse a PEM encoded X509 Certificate.
///
/// The provided PEM should include at least one PEM encoded certificate.
pub fn from_pem(pem: impl AsRef<[u8]>) -> Self {
let pem = pem.as_ref().into();
Self { pem }
}

/// Get a immutable reference to underlying certificate
pub fn get_ref(&self) -> &[u8] {
self.pem.as_slice()
}

/// Get a mutable reference to underlying certificate
pub fn get_mut(&mut self) -> &mut [u8] {
self.pem.as_mut()
}

/// Consumes `self`, returning the underlying certificate
pub fn into_inner(self) -> Vec<u8> {
self.pem
}
}

impl AsRef<[u8]> for Certificate {
fn as_ref(&self) -> &[u8] {
self.pem.as_ref()
}
}

impl AsMut<[u8]> for Certificate {
fn as_mut(&mut self) -> &mut [u8] {
self.pem.as_mut()
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use std::{
task::{Context, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tonic::transport::{server::Connected, Certificate};
use tonic::transport::server::Connected;
mod certificate;
pub use certificate::Certificate;

/// Wrapper error type.
pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
Expand Down