added comment about why socket always returns None
This commit is contained in:
committed by
Martin Algesten
parent
034981f535
commit
d75643b478
@@ -1,5 +1,5 @@
|
|||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use std::sync::{Arc};
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{env, error, fmt, result};
|
use std::{env, error, fmt, result};
|
||||||
|
|
||||||
@@ -54,7 +54,9 @@ fn main() -> Result<()> {
|
|||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let agent = ureq::builder()
|
let agent = ureq::builder()
|
||||||
.tls_connector(Arc::new(mbedtls_connector::MbedTlsConnector::new(mbedtls::ssl::config::AuthMode::None)))
|
.tls_connector(Arc::new(mbedtls_connector::MbedTlsConnector::new(
|
||||||
|
mbedtls::ssl::config::AuthMode::None,
|
||||||
|
)))
|
||||||
.timeout_connect(Duration::from_secs(5))
|
.timeout_connect(Duration::from_secs(5))
|
||||||
.timeout(Duration::from_secs(20))
|
.timeout(Duration::from_secs(20))
|
||||||
.build();
|
.build();
|
||||||
|
|||||||
@@ -5,16 +5,16 @@ use ureq::{Error, ReadWrite, TlsConnector};
|
|||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use mbedtls::rng::CtrDrbg;
|
||||||
use mbedtls::ssl::config::{Endpoint, Preset, Transport};
|
use mbedtls::ssl::config::{Endpoint, Preset, Transport};
|
||||||
use mbedtls::ssl::{Config, Context};
|
use mbedtls::ssl::{Config, Context};
|
||||||
use mbedtls::rng::CtrDrbg;
|
|
||||||
|
|
||||||
fn entropy_new() -> mbedtls::rng::OsEntropy {
|
fn entropy_new() -> mbedtls::rng::OsEntropy {
|
||||||
mbedtls::rng::OsEntropy::new()
|
mbedtls::rng::OsEntropy::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MbedTlsConnector {
|
pub struct MbedTlsConnector {
|
||||||
context: Arc<Mutex<Context>>
|
context: Arc<Mutex<Context>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -33,7 +33,9 @@ impl std::error::Error for MbedTlsError {
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub(crate) fn default_tls_config() -> std::sync::Arc<dyn TlsConnector> {
|
pub(crate) fn default_tls_config() -> std::sync::Arc<dyn TlsConnector> {
|
||||||
Arc::new(MbedTlsConnector::new(mbedtls::ssl::config::AuthMode::Required))
|
Arc::new(MbedTlsConnector::new(
|
||||||
|
mbedtls::ssl::config::AuthMode::Required,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MbedTlsConnector {
|
impl MbedTlsConnector {
|
||||||
@@ -45,50 +47,46 @@ impl MbedTlsConnector {
|
|||||||
config.set_authmode(mode);
|
config.set_authmode(mode);
|
||||||
let ctx = Context::new(Arc::new(config));
|
let ctx = Context::new(Arc::new(config));
|
||||||
MbedTlsConnector {
|
MbedTlsConnector {
|
||||||
context: Arc::new(Mutex::new(ctx))
|
context: Arc::new(Mutex::new(ctx)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TlsConnector for MbedTlsConnector {
|
impl TlsConnector for MbedTlsConnector {
|
||||||
fn connect(
|
fn connect(&self, _dns_name: &str, tcp_stream: TcpStream) -> Result<Box<dyn ReadWrite>, Error> {
|
||||||
&self,
|
|
||||||
_dns_name: &str,
|
|
||||||
tcp_stream: TcpStream,
|
|
||||||
) -> Result<Box<dyn ReadWrite>, Error> {
|
|
||||||
|
|
||||||
let mut ctx = self.context.lock().unwrap();
|
let mut ctx = self.context.lock().unwrap();
|
||||||
match ctx.establish(tcp_stream, None) {
|
match ctx.establish(tcp_stream, None) {
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let io_err = io::Error::new(io::ErrorKind::InvalidData, MbedTlsError);
|
let io_err = io::Error::new(io::ErrorKind::InvalidData, MbedTlsError);
|
||||||
return Err(io_err.into());
|
return Err(io_err.into());
|
||||||
}
|
}
|
||||||
Ok(()) => Ok(MbedTlsStream::new(self))
|
Ok(()) => Ok(MbedTlsStream::new(self)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MbedTlsStream {
|
struct MbedTlsStream {
|
||||||
context: Arc<Mutex<Context>>
|
context: Arc<Mutex<Context>>, //tcp_stream: TcpStream,
|
||||||
//tcp_stream: TcpStream,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MbedTlsStream {
|
impl MbedTlsStream {
|
||||||
pub fn new(mtc: &MbedTlsConnector) -> Box<MbedTlsStream> {
|
pub fn new(mtc: &MbedTlsConnector) -> Box<MbedTlsStream> {
|
||||||
Box::new(MbedTlsStream {
|
Box::new(MbedTlsStream {
|
||||||
context: mtc.context.clone()
|
context: mtc.context.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl ReadWrite for MbedTlsStream {
|
impl ReadWrite for MbedTlsStream {
|
||||||
|
// no obvious way to get socket back out of mbedtls context
|
||||||
|
// context.io() returns Any, which is hard to turn back into
|
||||||
|
// TcpStream reference, and what is lifetime of reference?
|
||||||
fn socket(&self) -> Option<&TcpStream> {
|
fn socket(&self) -> Option<&TcpStream> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl io::Read for MbedTlsStream {
|
impl io::Read for MbedTlsStream {
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
let mut ctx = self.context.lock().unwrap();
|
let mut ctx = self.context.lock().unwrap();
|
||||||
ctx.read(buf)
|
ctx.read(buf)
|
||||||
@@ -107,7 +105,6 @@ impl io::Write for MbedTlsStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "cd ../.. && cargo build --example mbedtls-req --features=\"mbedtls\""
|
* compile-command: "cd ../.. && cargo build --example mbedtls-req --features=\"mbedtls\""
|
||||||
|
|||||||
Reference in New Issue
Block a user