2022-08-21 13:20:43 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 12:20:29 -06:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-08-21 13:20:43 -05:00
|
|
|
|
|
|
|
package proxyprotocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Listener is used to wrap an underlying listener,
|
|
|
|
// whose connections may be using the HAProxy Proxy Protocol (version 1 or 2).
|
|
|
|
// If the connection is using the protocol, the RemoteAddr() will return
|
|
|
|
// the correct client address.
|
|
|
|
//
|
|
|
|
// Optionally define ProxyHeaderTimeout to set a maximum time to
|
|
|
|
// receive the Proxy Protocol Header. Zero means no timeout.
|
|
|
|
type Listener struct {
|
|
|
|
Listener net.Listener
|
|
|
|
ProxyHeaderTimeout time.Duration
|
|
|
|
AcceptUnknown bool // allow PROXY UNKNOWN
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept implements the Accept method in the Listener interface
|
|
|
|
// it waits for the next call and returns a wrapped Conn.
|
|
|
|
func (p *Listener) Accept() (net.Conn, error) {
|
|
|
|
// Get the underlying connection
|
|
|
|
conn, err := p.Listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newConn := NewConn(conn, p.ProxyHeaderTimeout)
|
|
|
|
newConn.acceptUnknown = p.AcceptUnknown
|
|
|
|
return newConn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the underlying listener.
|
|
|
|
func (p *Listener) Close() error {
|
|
|
|
return p.Listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the underlying listener's network address.
|
|
|
|
func (p *Listener) Addr() net.Addr {
|
|
|
|
return p.Listener.Addr()
|
|
|
|
}
|