Documentation ¶
Overview ¶
Package proxyproto implements Proxy Protocol (v1 and v2) parser and writer, as per specification: http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt
Index ¶
- Constants
- Variables
- func ValidateHeader(v Validator) func(*Conn)
- func WithPolicy(p Policy) func(*Conn)
- 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
- type Conn
- func (p *Conn) Close() error
- func (p *Conn) Header() *Header
- 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) SetNoDelay(value bool) error
- func (p *Conn) SetReadDeadline(t time.Time) error
- func (p *Conn) SetWriteDeadline(t time.Time) error
- func (p *Conn) Write(b []byte) (int, error)
- type Header
- func (header *Header) EqualTo(otherHeader *Header) bool
- func (header *Header) EqualsTo(otherHeader *Header) bool
- func (header *Header) Format() ([]byte, error)
- func (header *Header) LocalAddr() net.Addr
- func (header *Header) RemoteAddr() net.Addr
- func (header *Header) TLVs() ([]TLV, error)
- func (header *Header) WriteTo(w io.Writer) (int64, error)
- type Listener
- type PP2Type
- type Policy
- type PolicyFunc
- type ProtocolVersionAndCommand
- type TLV
- type Validator
Constants ¶
const ( UNSPEC = '\x00' TCPv4 = '\x11' UDPv4 = '\x12' TCPv6 = '\x21' UDPv6 = '\x22' UnixStream = '\x31' UnixDatagram = '\x32' )
const ( // Section 2.2 PP2_TYPE_ALPN PP2Type = 0x01 PP2_TYPE_AUTHORITY = 0x02 PP2_TYPE_CRC32C = 0x03 PP2_TYPE_NOOP = 0x04 PP2_TYPE_SSL = 0x20 PP2_SUBTYPE_SSL_VERSION = 0x21 PP2_SUBTYPE_SSL_CN = 0x22 PP2_SUBTYPE_SSL_CIPHER = 0x23 PP2_SUBTYPE_SSL_SIG_ALG = 0x24 PP2_SUBTYPE_SSL_KEY_ALG = 0x25 PP2_TYPE_NETNS = 0x30 // Section 2.2.7, reserved types PP2_TYPE_MIN_CUSTOM = 0xE0 PP2_TYPE_MAX_CUSTOM = 0xEF PP2_TYPE_MIN_EXPERIMENT = 0xF0 PP2_TYPE_MAX_EXPERIMENT = 0xF7 PP2_TYPE_MIN_FUTURE = 0xF8 PP2_TYPE_MAX_FUTURE = 0xFF )
const ( CRLF = "\r\n" SEPARATOR = " " )
const ( LOCAL = '\x20' PROXY = '\x21' )
Variables ¶
var ( // Protocol SIGV1 = []byte{'\x50', '\x52', '\x4F', '\x58', '\x59'} SIGV2 = []byte{'\x0D', '\x0A', '\x0D', '\x0A', '\x00', '\x0D', '\x0A', '\x51', '\x55', '\x49', '\x54', '\x0A'} 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") ErrInvalidAddress = errors.New("Invalid address") ErrInvalidPortNumber = errors.New("Invalid port number") ErrSuperfluousProxyHeader = errors.New("Upstream connection sent PROXY header but isn't allowed to send one") )
Functions ¶
func ValidateHeader ¶
ValidateHeader adds given validator for proxy headers to a connection when passed as option to NewConn()
func WithPolicy ¶
WithPolicy adds given policy to a connection when passed as option to NewConn()
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.
type Conn ¶
type Conn struct { ProxyHeaderPolicy Policy Validate Validator // contains filtered or unexported fields }
Conn is used to wrap and 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 NewConn ¶
NewConn is used to wrap a net.Conn that may be speaking the proxy protocol into a proxyproto.Conn
func (*Conn) LocalAddr ¶
LocalAddr returns the address of the server if the proxy protocol is being used, otherwise just returns the address of the socket server. In case an error happens on reading the proxy header the original LocalAddr is returned, not the one from the proxy header even if the proxy header itself is syntactically correct.
func (*Conn) Read ¶
Read is check for the proxy protocol header when doing the initial scan. 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. In case an error happens on reading the proxy header the original RemoteAddr is returned, not the one from the proxy header even if the proxy header itself is syntactically correct.
func (*Conn) SetDeadline ¶
SetDeadline wraps original conn.SetDeadline
func (*Conn) SetNoDelay ¶
SetNoDelay wraps original conn.SetNoDelay
func (*Conn) SetReadDeadline ¶
SetReadDeadline wraps original conn.SetReadDeadline
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline wraps original conn.SetWriteDeadline
type Header ¶
type Header struct { Version byte Command ProtocolVersionAndCommand TransportProtocol AddressFamilyAndProtocol SourceAddress net.IP DestinationAddress net.IP SourcePort uint16 DestinationPort uint16 // contains filtered or unexported fields }
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.
func (*Header) EqualTo ¶
EqualTo returns true if headers are equivalent, false otherwise. Deprecated: use EqualsTo instead. This method will eventually be removed.
func (*Header) RemoteAddr ¶
RemoteAddr returns the address of the remote endpoint of the connection.
type Listener ¶
type Listener struct { Listener net.Listener Policy PolicyFunc ValidateHeader Validator }
Listener is used to wrap an underlying listener, whose connections may be using the HAProxy Proxy Protocol. If the connection is using the protocol, the RemoteAddr() will return the correct client address.
type PP2Type ¶
type PP2Type byte
PP2Type is the proxy protocol v2 type
func (PP2Type) App ¶
App is true if the type is reserved for application specific data, see section 2.2.7
func (PP2Type) Experiment ¶
Experiment is true if the type is reserved for temporary experimental use by application developers, see section 2.2.7
func (PP2Type) Registered ¶
Registered is true if the type is registered in the spec, see section 2.2
type Policy ¶
type Policy int
Policy defines how a connection with a PROXY header address is treated.
const ( // USE address from PROXY header USE Policy = iota // IGNORE address from PROXY header, but accept connection IGNORE // REJECT connection when PROXY header is sent // Note: even though the first read on the connection returns an error if // a PROXY header is present, subsequent reads do not. It is the task of // the code using the connection to handle that case properly. REJECT // REQUIRE connection to send PROXY header, reject if not present // Note: even though the first read on the connection returns an error if // a PROXY header is not present, subsequent reads do not. It is the task // of the code using the connection to handle that case properly. REQUIRE )
type PolicyFunc ¶
PolicyFunc can be used to decide whether to trust the PROXY info from upstream. If set, the connecting address is passed in as an argument.
See below for the different policies.
In case an error is returned the connection is denied.
func LaxWhiteListPolicy ¶
func LaxWhiteListPolicy(allowed []string) (PolicyFunc, error)
LaxWhiteListPolicy returns a PolicyFunc which decides whether the upstream ip is allowed to send a proxy header based on a list of allowed IP addresses and IP ranges. In case upstream IP is not in list the proxy header will be ignored. If one of the provided IP addresses or IP ranges is invalid it will return an error instead of a PolicyFunc.
func MustLaxWhiteListPolicy ¶
func MustLaxWhiteListPolicy(allowed []string) PolicyFunc
MustLaxWhiteListPolicy returns a LaxWhiteListPolicy but will panic if one of the provided IP addresses or IP ranges is invalid.
func MustStrictWhiteListPolicy ¶
func MustStrictWhiteListPolicy(allowed []string) PolicyFunc
MustStrictWhiteListPolicy returns a StrictWhiteListPolicy but will panic if one of the provided IP addresses or IP ranges is invalid.
func StrictWhiteListPolicy ¶
func StrictWhiteListPolicy(allowed []string) (PolicyFunc, error)
StrictWhiteListPolicy returns a PolicyFunc which decides whether the upstream ip is allowed to send a proxy header based on a list of allowed IP addresses and IP ranges. In case upstream IP is not in list reading on the connection will be refused on the first read. Please note: subsequent reads do not error. It is the task of the code using the connection to handle that case properly. If one of the provided IP addresses or IP ranges is invalid it will return an error instead of a PolicyFunc.
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.