code.gitea.io/gitea@v1.19.3/modules/proxyprotocol/listener.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package proxyprotocol
     5  
     6  import (
     7  	"net"
     8  	"time"
     9  )
    10  
    11  // Listener is used to wrap an underlying listener,
    12  // whose connections may be using the HAProxy Proxy Protocol (version 1 or 2).
    13  // If the connection is using the protocol, the RemoteAddr() will return
    14  // the correct client address.
    15  //
    16  // Optionally define ProxyHeaderTimeout to set a maximum time to
    17  // receive the Proxy Protocol Header. Zero means no timeout.
    18  type Listener struct {
    19  	Listener           net.Listener
    20  	ProxyHeaderTimeout time.Duration
    21  	AcceptUnknown      bool // allow PROXY UNKNOWN
    22  }
    23  
    24  // Accept implements the Accept method in the Listener interface
    25  // it waits for the next call and returns a wrapped Conn.
    26  func (p *Listener) Accept() (net.Conn, error) {
    27  	// Get the underlying connection
    28  	conn, err := p.Listener.Accept()
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	newConn := NewConn(conn, p.ProxyHeaderTimeout)
    34  	newConn.acceptUnknown = p.AcceptUnknown
    35  	return newConn, nil
    36  }
    37  
    38  // Close closes the underlying listener.
    39  func (p *Listener) Close() error {
    40  	return p.Listener.Close()
    41  }
    42  
    43  // Addr returns the underlying listener's network address.
    44  func (p *Listener) Addr() net.Addr {
    45  	return p.Listener.Addr()
    46  }