You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

listener.go 1.2KB

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