Documentation
¶
Overview ¶
Package bfe_proxy implements Proxy Protocol (v1 and v2) parser and writer, as per specification: http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
Index ¶
- Constants
- Variables
- type AddressFamilyAndProtocol
- func (ap AddressFamilyAndProtocol) IsDatagram() bool
- func (ap AddressFamilyAndProtocol) IsIPv4() bool
- func (ap AddressFamilyAndProtocol) IsIPv6() bool
- func (ap AddressFamilyAndProtocol) IsStream() bool
- func (ap AddressFamilyAndProtocol) IsUnix() bool
- func (ap AddressFamilyAndProtocol) IsUnspec() bool
- func (ap AddressFamilyAndProtocol) String() string
- type Conn
- func (p *Conn) BalancerAddr() net.Addr
- func (p *Conn) Close() error
- func (p *Conn) GetNetConn() net.Conn
- func (p *Conn) LocalAddr() net.Addr
- func (p *Conn) Read(b []byte) (int, error)
- func (p *Conn) RemoteAddr() net.Addr
- func (p *Conn) SetDeadline(t time.Time) error
- func (p *Conn) SetReadDeadline(t time.Time) error
- func (p *Conn) SetWriteDeadline(t time.Time) error
- func (p *Conn) VirtualAddr() net.Addr
- func (p *Conn) Write(b []byte) (int, error)
- type Header
- type ProtocolVersionAndCommand
- type ProxyState
Constants ¶
const ( UNSPEC = '\x00' TCPv4 = '\x11' UDPv4 = '\x12' TCPv6 = '\x21' UDPv6 = '\x22' UnixStream = '\x31' UnixDatagram = '\x32' )
const ( CRLF = "\r\n" SEPARATOR = " " )
const ( LOCAL = '\x20' PROXY = '\x21' )
Variables ¶
var ( SIGV1 = []byte{'\x50', '\x52', '\x4F', '\x58', '\x59'} SIGV2 = []byte{'\x0D', '\x0A', '\x0D', '\x0A', '\x00', '\x0D', '\x0A', '\x51', '\x55', '\x49', '\x54', '\x0A'} )
Protocol signature
var ( ErrCantReadProtocolVersionAndCommand = errors.New("Can't read proxy protocol version and command") ErrCantReadAddressFamilyAndProtocol = errors.New("Can't read address family or protocol") ErrCantReadLength = errors.New("Can't read length") ErrCantResolveSourceUnixAddress = errors.New("Can't resolve source Unix address") ErrCantResolveDestinationUnixAddress = errors.New("Can't resolve destination Unix address") ErrNoProxyProtocol = errors.New("Proxy protocol signature not present") ErrUnknownProxyProtocolVersion = errors.New("Unknown proxy protocol version") ErrUnsupportedProtocolVersionAndCommand = errors.New("Unsupported proxy protocol version and command") ErrUnsupportedAddressFamilyAndProtocol = errors.New("Unsupported address family and protocol") ErrInvalidLength = errors.New("Invalid length") ErrLengthExceeded = errors.New("Length Exceeded") ErrInvalidAddress = errors.New("Invalid address") ErrInvalidPortNumber = errors.New("Invalid port number") )
Functions ¶
This section is empty.
Types ¶
type AddressFamilyAndProtocol ¶
type AddressFamilyAndProtocol byte
AddressFamilyAndProtocol represents address family and transport protocol.
func (AddressFamilyAndProtocol) IsDatagram ¶
func (ap AddressFamilyAndProtocol) IsDatagram() bool
IsDatagram returns true if the transport protocol is UDP or DGRAM (SOCK_DGRAM), false otherwise.
func (AddressFamilyAndProtocol) IsIPv4 ¶
func (ap AddressFamilyAndProtocol) IsIPv4() bool
IsIPv4 returns true if the address family is IPv4 (AF_INET4), false otherwise.
func (AddressFamilyAndProtocol) IsIPv6 ¶
func (ap AddressFamilyAndProtocol) IsIPv6() bool
IsIPv6 returns true if the address family is IPv6 (AF_INET6), false otherwise.
func (AddressFamilyAndProtocol) IsStream ¶
func (ap AddressFamilyAndProtocol) IsStream() bool
IsStream returns true if the transport protocol is TCP or STREAM (SOCK_STREAM), false otherwise.
func (AddressFamilyAndProtocol) IsUnix ¶
func (ap AddressFamilyAndProtocol) IsUnix() bool
IsUnix returns true if the address family is UNIX (AF_UNIX), false otherwise.
func (AddressFamilyAndProtocol) IsUnspec ¶
func (ap AddressFamilyAndProtocol) IsUnspec() bool
IsUnspec returns true if the transport protocol or address family is unspecified, false otherwise.
func (AddressFamilyAndProtocol) String ¶
func (ap AddressFamilyAndProtocol) String() string
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is used to wrap an underlying connection which may be speaking the Proxy Protocol. If it is, the RemoteAddr() will return the address of the client instead of the proxy address.
func (*Conn) BalancerAddr ¶
BalancerAddr returns the address of balancer
func (*Conn) GetNetConn ¶
GetNetConn returns the underlying connection
func (*Conn) Read ¶
Read reads data from the connection. It check for the proxy protocol header when doing the initial read. If there is an error parsing the header, it is returned and the socket is closed.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the address of the client if the proxy protocol is being used, otherwise just returns the address of the socket peer. If there is an error parsing the header, the address of the client is not returned, and the socket is closed.
func (*Conn) SetDeadline ¶
SetDeadline implements the Conn.SetDeadline method
func (*Conn) SetReadDeadline ¶
SetReadDeadline implements the Conn.SetReadDeadline method
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline implements the Conn.SetWriteDeadline method
func (*Conn) VirtualAddr ¶
VirtualAddr returns the virtual address
type Header ¶
type Header struct { Version byte Command ProtocolVersionAndCommand TransportProtocol AddressFamilyAndProtocol SourceAddress net.IP DestinationAddress net.IP SourcePort uint16 DestinationPort uint16 }
Header is the placeholder for proxy protocol header.
func Read ¶
Read identifies the proxy protocol version and reads the remaining of the header, accordingly.
If proxy protocol header signature is not present, the reader buffer remains untouched and is safe for reading outside of this code.
If proxy protocol header signature is present but an error is raised while processing the remaining header, assume the reader buffer to be in a corrupt state. Also, this operation will block until enough bytes are available for peeking.
func ReadTimeout ¶
ReadTimeout acts as Read but takes a timeout. If that timeout is reached, it's assumed there's no proxy protocol header.
type ProtocolVersionAndCommand ¶
type ProtocolVersionAndCommand byte
ProtocolVersionAndCommand represents proxy protocol version and command.
func (ProtocolVersionAndCommand) IsLocal ¶
func (pvc ProtocolVersionAndCommand) IsLocal() bool
IsLocal returns true if the protocol version is \x2 and command is LOCAL, false otherwise.
func (ProtocolVersionAndCommand) IsProxy ¶
func (pvc ProtocolVersionAndCommand) IsProxy() bool
IsProxy returns true if the protocol version is \x2 and command is PROXY, false otherwise.
func (ProtocolVersionAndCommand) IsUnspec ¶
func (pvc ProtocolVersionAndCommand) IsUnspec() bool
IsUnspec returns true if the protocol version or command is unspecified, false otherwise.
type ProxyState ¶
type ProxyState struct { ProxyErrReadHeader *metrics.Counter // connection with io err while read header ProxyErrNoProxyProtocol *metrics.Counter // connection with signature unmatched ProxyMatchedV1Signature *metrics.Counter // connection with signature v1 matched ProxyMatchedV2Signature *metrics.Counter // connection with signature v1 matched ProxyErrInvalidHeader *metrics.Counter // connection with invalid header ProxyNormalV1Header *metrics.Counter // connection with normal v1 header ProxyNormalV2Header *metrics.Counter // connection with normal v2 header }
ProxyState is State for Proxy
func GetProxyState ¶
func GetProxyState() *ProxyState