pub struct TlsConnector { /* private fields */ }
Expand description

Connect a client to a remote server.

Examples

use async_std::prelude::*;
use async_std::net::TcpStream;
use async_native_tls::TlsConnector;

let stream = TcpStream::connect("google.com:443").await?;
let mut stream = TlsConnector::new()
    .use_sni(true)
    .connect("google.com", stream)
    .await?;
stream.write_all(b"GET / HTTP/1.0\r\n\r\n").await?;

let mut res = Vec::new();
stream.read_to_end(&mut res).await?;
println!("{}", String::from_utf8_lossy(&res));

Implementations§

source§

impl TlsConnector

source

pub fn new() -> Self

Create a new instance.

source

pub fn identity(self, identity: Identity) -> Self

Sets the identity to be used for client certificate authentication.

source

pub fn min_protocol_version(self, protocol: Option<Protocol>) -> Self

Sets the minimum supported protocol version.

A value of None enables support for the oldest protocols supported by the implementation. Defaults to Some(Protocol::Tlsv10).

source

pub fn max_protocol_version(self, protocol: Option<Protocol>) -> Self

Sets the maximum supported protocol version.

A value of None enables support for the newest protocols supported by the implementation. Defaults to None.

source

pub fn add_root_certificate(self, cert: Certificate) -> Self

Adds a certificate to the set of roots that the connector will trust.

The connector will use the system’s trust root by default. This method can be used to add to that set when communicating with servers not trusted by the system. Defaults to an empty set.

source

pub fn request_alpns(self, protocols: &[&str]) -> Self

Request specific protocols through ALPN (Application-Layer Protocol Negotiation).

Defaults to none

source

pub fn danger_accept_invalid_certs(self, accept_invalid_certs: bool) -> Self

Controls the use of certificate validation.

Defaults to false.

Warning

You should think very carefully before using this method. If invalid certificates are trusted, any certificate for any site will be trusted for use. This includes expired certificates. This introduces significant vulnerabilities, and should only be used as a last resort.

source

pub fn use_sni(self, use_sni: bool) -> Self

Controls the use of Server Name Indication (SNI).

Defaults to true.

source

pub fn danger_accept_invalid_hostnames( self, accept_invalid_hostnames: bool ) -> Self

Controls the use of hostname verification.

Defaults to false.

Warning

You should think very carefully before using this method. If invalid hostnames are trusted, any valid certificate for any site will be trusted for use. This introduces significant vulnerabilities, and should only be used as a last resort.

source

pub async fn connect<S>( &self, host: impl Into<Host>, stream: S ) -> Result<TlsStream<S>>where S: AsyncRead + AsyncWrite + Unpin,

Connect to a remote server.

Examples
use async_std::prelude::*;
use async_std::net::TcpStream;
use async_native_tls::TlsConnector;

let stream = TcpStream::connect("google.com:443").await?;
let mut stream = TlsConnector::new()
    .use_sni(true)
    .connect("google.com", stream)
    .await?;
stream.write_all(b"GET / HTTP/1.0\r\n\r\n").await?;

let mut res = Vec::new();
stream.read_to_end(&mut res).await?;
println!("{}", String::from_utf8_lossy(&res));

Trait Implementations§

source§

impl Debug for TlsConnector

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for TlsConnector

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl From<TlsConnectorBuilder> for TlsConnector

source§

fn from(builder: TlsConnectorBuilder) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.