listener

package
v0.0.0-...-4f56531 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2020 License: BSD-3-Clause Imports: 9 Imported by: 37

Documentation

Overview

Package listener provides a TCP listener on roids.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/fiorix/go-listener/listener"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "hello, world")
	})
	ln, err := listener.New(":80", listener.FastOpen())
	if err != nil {
		panic(err)
	}
	defer ln.Close()
	http.Serve(ln, nil)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*config) error

Option type for listener options.

func FastOpen

func FastOpen() Option

FastOpen enables TCP fast open.

func HTTP2

func HTTP2() Option

HTTP2 configures TLS listeners to set the "h2" option for net/http to to serve HTTP/2 over TLS. This option can only be used when combined with other TLS settings, either using local key/cert or LetsEncrypt.

func LetsEncrypt

func LetsEncrypt(cacheDir, email string, hosts ...string) Option

LetsEncrypt configures automatic TLS certificates using letsencrypt.org.

The cache dir is used to store certificates retrieved from LetsEncrypt and reuse on server restarts. If not specified, "." is used.

The email parameter is optionally used for registration with LetsEncrypt to notify about certificate problems. If not set, certificates are obtained anonymously.

The hosts parameter must define a list of allowed hostnames to obtain certificates for with LetsEncrypt.

By calling this function you are accepting LetsEncrypt's TOS. https://letsencrypt.org/repository/

Example
package main

import (
	"github.com/fiorix/go-listener/listener"
)

func main() {
	opts := []listener.Option{
		listener.FastOpen(),
		listener.LetsEncrypt(
			"letsencrypt.cache", // cache file
			"me@example.com",    // optional email for registration
			"example.com",       // hosts...
			"api.example.com",
			"foobar.example.com",
		),
	}
	ln, err := listener.New(":443", opts...)
	if err != nil {
		panic(err)
	}
	defer ln.Close()
	// http.Serve(ln, nil)...
}
Output:

func Naggle

func Naggle() Option

Naggle enables Naggle's algorithm - effectively setting nodelay=false. This might be useful when combined with fast open, to allow data on ack.

func TLS

func TLS(certFile, keyFile string) Option

TLS configures TLS with certificate and key files.

Example
package main

import (
	"crypto/tls"

	"github.com/fiorix/go-listener/listener"
)

func main() {
	opts := []listener.Option{
		listener.FastOpen(),
		listener.TLS("cert.pem", "key.pem"),
		listener.TLSClientAuth("cacert.pem", tls.VerifyClientCertIfGiven),
	}
	ln, err := listener.New(":443", opts...)
	if err != nil {
		panic(err)
	}
	defer ln.Close()
	// http.Serve(ln, nil)...
}
Output:

func TLSClientAuth

func TLSClientAuth(cacertFile string, authType tls.ClientAuthType) Option

TLSClientAuth configures TLS client certificate authentication.

type TCPListener

type TCPListener struct {
	net.Listener
	// contains filtered or unexported fields
}

TCPListener implements the net.Listener interface. Listens on a TCP port that is configured with options from this package. e.g. fast-open, etc.

func New

func New(addr string, opts ...Option) (*TCPListener, error)

New creates and initializes a new TCP listener that is ready to accept client connections.

func (*TCPListener) TLSConfig

func (ln *TCPListener) TLSConfig() *tls.Config

TLSConfig returns the TLS configuration for the TCP listener.

Directories

Path Synopsis
Package fastopen provides TCP fast-open configuration for TCP listeners.
Package fastopen provides TCP fast-open configuration for TCP listeners.
Package listenercmd provides env and flag configuration for go-listener.
Package listenercmd provides env and flag configuration for go-listener.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL