Documentation
¶
Overview ¶
Package proxyprotocol implements version 1 and 2 of the PROXY protocol.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
Conn wraps a net.Conn using the PROXY protocol to determin LocalAddr() and RemoteAddr().
func (*Conn) ProxyHeader ¶
ProxyHeader will return the PROXY header received on the current connection.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address provided by the PROXY header.
func (*Conn) SetDeadline ¶
SetDeadline calls SetDeadline on the underlying net.Conn.
type Header ¶
type Header interface {
Version() int
SrcAddr() net.Addr
DestAddr() net.Addr
WriteTo(io.Writer) (int64, error)
}
Header provides information decoded from a PROXY header.
type HeaderV1 ¶
HeaderV1 contains information relayed by the PROXY protocol version 1 (human-readable) header.
Example (Proxy) ¶
l, err := net.Listen("tcp", ":8080")
if err != nil {
log.Println("ERROR: listen:", err)
return
}
defer l.Close()
var hdr HeaderV1
c, err := l.Accept()
if err != nil {
log.Println("ERROR: accept:", err)
return
}
defer c.Close()
// Populate hdr from the new incomming connection.
hdr.FromConn(c, false)
// Example target
//
// This server will be sent a PROXY header.
dst, err := net.Dial("tcp", "192.168.0.2:12345")
if err != nil {
log.Println("ERROR: connect:", err)
return
}
defer dst.Close()
// This will write the PROXY header to the backend server.
_, err = hdr.WriteTo(dst)
if err != nil {
log.Println("ERROR: write header:", err)
return
}
func (*HeaderV1) FromConn ¶
FromConn will populate header data from the given net.Conn.
The RemoteAddr of the Conn will be considered the Source address/port and the LocalAddr of the Conn will be considered the Destination address/port for the purposes of the PROXY header if outgoing is false, if outgoing is true, the inverse is true.
type HeaderV2 ¶
HeaderV2 contains information relayed by the PROXY protocol version 2 (binary) header.
Example (Proxy) ¶
l, err := net.Listen("tcp", ":8080")
if err != nil {
log.Println("ERROR: listen:", err)
return
}
defer l.Close()
var hdr HeaderV2
c, err := l.Accept()
if err != nil {
log.Println("ERROR: accept:", err)
return
}
defer c.Close()
// Populate hdr from the new incomming connection.
hdr.FromConn(c, false)
// Example target
//
// This server will be sent a PROXY header.
dst, err := net.Dial("tcp", "192.168.0.2:12345")
if err != nil {
log.Println("ERROR: connect:", err)
return
}
defer dst.Close()
// This will write the PROXY header to the backend server.
_, err = hdr.WriteTo(dst)
if err != nil {
log.Println("ERROR: write header:", err)
return
}
func (HeaderV2) DestAddr ¶
DestAddr returns the destination address as TCP, UDP, Unix, or nil depending on Protocol and Family.
func (*HeaderV2) FromConn ¶
FromConn will populate header data from the given net.Conn.
The RemoteAddr of the Conn will be considered the Source address/port and the LocalAddr of the Conn will be considered the Destination address/port for the purposes of the PROXY header if outgoing is false, if outgoing is true, the inverse is true.
type InvalidHeaderErr ¶
type InvalidHeaderErr struct {
Read []byte
// contains filtered or unexported fields
}
InvalidHeaderErr contains the parsing error as well as all data read from the reader.
type Listener ¶
Listener wraps a net.Listener automatically wrapping new connections with PROXY protocol support.
func NewListener ¶
NewListener will wrap nl, automatically handling PROXY headers for all connections. To expect PROXY headers only from certain addresses/subnets, use SetFilter.
By default, all connections must provide a PROXY header within the specified timeout.
Example ¶
nl, err := net.Listen("tcp", ":80")
if err != nil {
log.Println("ERROR: listen:", err)
return
}
defer nl.Close()
// Wrap listener with 3 second timeout for PROXY header
l := NewListener(nl, 3*time.Second)
for {
c, err := l.Accept()
if err != nil {
log.Println("ERROR: accept:", err)
return
}
// RemoteAddr will be the source address of the PROXY header
log.Println("New connection from:", c.RemoteAddr().String())
}
func (*Listener) Accept ¶
Accept waits for and returns the next connection to the listener, wrapping it with NewConn if the RemoteAddr matches any registered rules.
func (*Listener) Filter ¶
Filter returns the current set of filter rules.
Filter is safe to call from multiple goroutines while the listener is in use.
func (*Listener) SetDefaultTimeout ¶
SetDefaultTimeout sets the default timeout, used when the subnet filter is nil.
SetDefaultTimeout is safe to call from multiple goroutines while the listener is in use.
func (*Listener) SetFilter ¶
SetFilter allows limiting PROXY header requirements to matching Subnets with an optional timeout. If filter is nil, all connections will be required to provide a PROXY header (the default).
Connections not matching any rule will be returned directly without reading a PROXY header.
Duplicate subnet rules will automatically be removed and the lowest non-zero timeout will be used.
SetFilter is safe to call from multiple goroutines while the listener is in use.
type Rule ¶
type Rule struct {
// Subnet is used to match incomming IP addresses against this rule.
Subnet *net.IPNet
// Timeout indicates the max amount of time to receive the PROXY header before
// terminating the connection.
Timeout time.Duration
}
Rule contains configuration for a single subnet.