protocolmux

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: BSD-3-Clause Imports: 5 Imported by: 0

README

protocolmux

protocolmux allows to peek the first bytes of a connection and decided to which handler forward the connection.

l, _ := net.Listen("tcp", addr)

// Initialize the muuxer
mux := &Mux{L: l}

// Create listeners just by setting the prefix
helloListener := mux.ListenTo([][]byte{[]byte("ping")})

for {
	conn, _ := helloListener.Accept()
	conn.Write("pong")
	conn.Close();
}

// Or use one of the prefixes
go http.Serve(mux.ListenTo(HttpPrefix), nil) // Handle HTTP
go http.ServeTLS(mux.ListenTo(TLSPrefix), nil,...) // Handle HTTPS

Variables

var (
    HTTPPrefix = [][]byte{
        []byte("GET"),
        []byte("HEAD"),
        []byte("POST"),
        []byte("PUT"),
        []byte("DELETE"),
        []byte("CONNECT"),
        []byte("OPTIONS"),
        []byte("TRACE"),
        []byte("PATCH"),
    }
    TLSPrefix = [][]byte{
        {22, 3, 0},
        {22, 3, 1},
        {22, 3, 2},
        {22, 3, 3},
    }
)

Types

type Mux

type Mux struct { ... }

func (*Mux) Listen

func (m *Mux) Listen() error

func (*Mux) ListenTo

func (m *Mux) ListenTo(prefixes [][]byte) net.Listener


Readme created from Go doc with goreadme

Documentation

Overview

protocolmux allows to peek the first bytes of a connection and decided to which handler forward the connection.

l, _ := net.Listen("tcp", addr)

// Initialize the muuxer
mux := &Mux{L: l}

// Create listeners just by setting the prefix
helloListener := mux.ListenTo([][]byte{[]byte("ping")})

for {
	conn, _ := helloListener.Accept()
	conn.Write("pong")
	conn.Close();
}

// Or use one of the prefixes
go http.Serve(mux.ListenTo(HttpPrefix), nil) // Handle HTTP
go http.ServeTLS(mux.ListenTo(TLSPrefix), nil,...) // Handle HTTPS

Index

Constants

This section is empty.

Variables

View Source
var (
	HTTPPrefix = [][]byte{
		[]byte("GET"),
		[]byte("HEAD"),
		[]byte("POST"),
		[]byte("PUT"),
		[]byte("DELETE"),
		[]byte("CONNECT"),
		[]byte("OPTIONS"),
		[]byte("TRACE"),
		[]byte("PATCH"),
	}
	TLSPrefix = [][]byte{
		{22, 3, 0},
		{22, 3, 1},
		{22, 3, 2},
		{22, 3, 3},
	}
)

Functions

This section is empty.

Types

type Mux

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

func (*Mux) Listen

func (m *Mux) Listen() error

func (*Mux) ListenTo

func (m *Mux) ListenTo(prefixes [][]byte) net.Listener

Jump to

Keyboard shortcuts

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