Giter Club home page Giter Club logo

easy-tokio-rustls's Introduction

Easy Tokio Rustls

This library provides convenient abstractions for creating simple TLS sockets with tokio-rustls.

Example client usage

use anyhow::Result;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use easy_tokio_rustls::TlsClient;

const BUFFER_SIZE: usize = 8 * 1024;
const REQUEST: &[u8] = b"GET / HTTP/1.1\r\nHost: suchprogramming.com\r\n\r\n";

#[tokio::main]
async fn main() -> Result<()> {
    let client = TlsClient::new("suchprogramming.com:443").await?;
    let mut connection = client.connect().await?;

    connection.write_all(REQUEST).await?;
    let mut buffer = [0; BUFFER_SIZE];
    loop {
        let read_size = connection.read(&mut buffer).await?;
        if read_size == 0 {
            connection.shutdown().await?;
            return Ok(());
        }
        let html = std::str::from_utf8(&buffer[0..read_size]).unwrap();
        print!("{}", html);
        if html.contains("</html>") {
            connection.shutdown().await?;
            return Ok(());
        }
    }
}

Example server usage

use anyhow::Result;
use std::str;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use easy_tokio_rustls::TlsServer;

const BUFFER_SIZE: usize = 8 * 1024;
const RESPONSE: &[u8] = b"HTTP/1.1 200 OK\r\nServer: a very great server\r\n\r\n";

#[tokio::main]
pub async fn main() -> Result<()> {
    let interface = "0.0.0.0:8443";
    let cert_file = "cert.pem";
    let key_file = "privkey.pem";

    let server = TlsServer::new(interface, cert_file, key_file).await?;
    let listener = server.listen().await?;
    println!("Listening on {}", interface);

    // This is a simplified server, handling 1 connection at a time certainly isn't recommended
    let (stream, addr) = listener.stream_accept().await?;
    println!("Client connected from {}", addr);

    let mut client = stream.tls_accept().await?;
    println!("TLS connection accepted");

    let mut buffer = [0; BUFFER_SIZE];
    let read_size = client.read(&mut buffer).await?;
    let request = str::from_utf8(&buffer[..read_size])?;
    println!("Client sent:\n{}", request);

    client.write_all(RESPONSE).await?;
    client.flush().await?;
    println!("Reply sent, shutting down...");

    client.shutdown().await?;

    Ok(())
}

Future features

Things I'd try to add to this project:

  • mTLS Auth
  • Certificate Pinning

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.