proxyprotocol

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: Apache-2.0 Imports: 9 Imported by: 141

README

go-proxyprotocol

GoDoc

PROXY protocol implementation in Go.

Usage

import

import (
	proxyprotocol "github.com/blacktear23/go-proxyprotocol"
)

basic usage

// Create listener
l, err := net.Listen("tcp", "...")

// Wrap listener as PROXY protocol listener
ppl, err := proxyprotocol.NewListener(l, "*", 5, false)

for {
    conn, err := ppl.Accept()
    if err != nil {
        // PROXY protocol related errors can be output by log and
        // continue accept next one.
        if proxyprotocol.IsProxyProtocolError(err) {
            log.Errorf("PROXY protocol error: %s", err.Error())
            continue
        }
        panic(err)
    }
    go processConn(conn)
}

Notice For AWS NLB

If using AWS NLB, as default NLB will not send ProxyProtocol v2 header to server until client send data. This will cause read timeout error if your server send data first. For example: SMTP, FTP, SSH, MySQL etc.

The default value for NLB target group attribute proxy_protocol_v2.client_to_server.header_placement is on_first_ack_with_payload. User need to contact AWS support to change it to on_first_ack.

Lazy Mode

go-proxyprotocol support lazy mode for ProxyProtocol header parse. Using this mode the header parse step will postpone to first Conn.Read function call. This will handle AWS NLB problem. And user must ensure that the client IP address must be get after a Conn.Read call.

Using lazy mode is simple:

// Create listener
l, err := net.Listener("tcp", "...")


// Wrap listener as PROXY protocol listener and enable lazy mode.
ppl, err := proxyprotocol.NewLazyListener(l, "*", 5, false)

...

Fallback-able

go-proxyprotocol support fallback-able mode for ProxyProtocol header process. When multiple client with different system connect to the server and some using PROXY protocol some not and it's hard to determine the allowed IP range, just set fallbackable parameter to true, it can handle this.

// Create listener
l, err := net.Listener("tcp", "...")


// Wrap listener as PROXY protocol listener and enable lazy mode and fallback-able
ppl, err := proxyprotocol.NewLazyListener(l, "*", 5, true)

...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrProxyProtocolV1HeaderInvalid = errors.New("PROXY Protocol v1 header is invalid")
	ErrProxyProtocolV2HeaderInvalid = errors.New("PROXY Protocol v2 header is invalid")
	ErrProxyProtocolInvalid         = errors.New("Invalid PROXY Protocol Header")
	ErrHeaderReadTimeout            = errors.New("Header read timeout")
)

Functions

func IsProxyProtocolError

func IsProxyProtocolError(err error) bool

func NewLazyListener added in v1.0.1

func NewLazyListener(listener net.Listener, allowedIPs string, headerReadTimeout int, fallbackable bool) (net.Listener, error)

Create new PROXY protocol listener for lazy mode. Lazy mode means PROXY protocol header will be processed at first `Read` call. * listener is basic listener for TCP * allowedIPs is protocol allowed addresses or CIDRs split by `,` if use '*' means allow any address * headerReadTimeout is timeout for PROXY protocol header read

func NewListener

func NewListener(listener net.Listener, allowedIPs string, headerReadTimeout int, fallbackable bool) (net.Listener, error)

Create new PROXY protocol listener * listener is basic listener for TCP * allowedIPs is protocol allowed addresses or CIDRs split by `,` if use '*' means allow any address * headerReadTimeout is timeout for PROXY protocol header read

Types

This section is empty.

Jump to

Keyboard shortcuts

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