Giter VIP home page Giter VIP logo

tlsmux's Introduction

tlsmux

build GoDoc

Go package providing an implementation of a net.Conn multiplexer based on the TLS SNI (Server Name Indication) sent by a client.

Installation

Install using go get github.com/kevinpollet/tlsmux.

Usage

Mux

The Mux struct allows registering handlers which will be called when the muxer serve a net.Conn with a matching server name.

mux := tlsmux.Mux{}

l, err := net.Listen("tcp", "127.0.0.1:8080")
if err != nil {
    log.Fatal(err)
}

if err := mux.Serve(l); err != nil {
    log.Fatal(err)
}

Handler

The Handler interface is used to handle an incoming net.Conn without decrypting the underlying TLS communication (Pass Through). Implementations are responsible for closing the connection.

The HandlerFunc type is an adapter to allow the use of ordinary functions as a Handler.

mux.Handle("server.name", tlsmux.HandlerFunc(func(conn net.Conn) error {
    defer conn.Close()

    // Handle the encrypted TLS connection.
}))

TLSHandler

The TLSHandler struct is a Handler implementation allowing to terminate the TLS connection with the configured tls.Config. Thus, the net.Conn parameter of a TLSHandler if of type tls.Conn.
Implementations are responsible for closing the connection.

The TLSHandlerFunc type is an adapter to allow the use of ordinary functions as a TLSHandler.

cfg := &tls.Config{
    MinVersion: tls.VersionTLS13,
    Certificates: []tls.Certificate{cert},
}

mux.Handle("foo.localhost", tlsmux.TLSHandlerFunc(cfg, func(conn net.Conn) error {
    defer conn.Close()

    // Handle the decrypted TLS connection.
}))

ProxyHandler

The ProxyHandler struct is a Handler implementation forwarding the connection bytes to the configured Address. The ProxyHandlerFunc is an adapter allowing the use of a ProxyHandler as a HandlerFunc.

// Forward the encrypted connection bytes.
mux.Handle("foo.localhost", tlsmux.ProxyHandler{Addr: "127.0.0.1:443"})

// Forward the decrypted connection bytes.
mux.Handle("foo.localhost", tlsmux.TLSHandlerFunc(tlsConfig, tlsmux.ProxyHandlerFunc("127.0.0.1:80"))

License

MIT

tlsmux's People

Contributors

kevinpollet avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.