Add TCP Servers Transports support
Co-authored-by: Romain <rtribotte@users.noreply.github.com>
This commit is contained in:
parent
c2dac39da1
commit
3eeea2bb2b
101 changed files with 5956 additions and 1669 deletions
|
@ -15,40 +15,30 @@ import (
|
|||
|
||||
// Proxy forwards a TCP request to a TCP service.
|
||||
type Proxy struct {
|
||||
address string
|
||||
tcpAddr *net.TCPAddr
|
||||
terminationDelay time.Duration
|
||||
proxyProtocol *dynamic.ProxyProtocol
|
||||
address string
|
||||
proxyProtocol *dynamic.ProxyProtocol
|
||||
dialer Dialer
|
||||
}
|
||||
|
||||
// NewProxy creates a new Proxy.
|
||||
func NewProxy(address string, terminationDelay time.Duration, proxyProtocol *dynamic.ProxyProtocol) (*Proxy, error) {
|
||||
func NewProxy(address string, proxyProtocol *dynamic.ProxyProtocol, dialer Dialer) (*Proxy, error) {
|
||||
if proxyProtocol != nil && (proxyProtocol.Version < 1 || proxyProtocol.Version > 2) {
|
||||
return nil, fmt.Errorf("unknown proxyProtocol version: %d", proxyProtocol.Version)
|
||||
}
|
||||
|
||||
// Creates the tcpAddr only for IP based addresses,
|
||||
// because there is no need to resolve the name on every new connection,
|
||||
// and building it should happen once.
|
||||
var tcpAddr *net.TCPAddr
|
||||
if host, _, err := net.SplitHostPort(address); err == nil && net.ParseIP(host) != nil {
|
||||
tcpAddr, err = net.ResolveTCPAddr("tcp", address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &Proxy{
|
||||
address: address,
|
||||
tcpAddr: tcpAddr,
|
||||
terminationDelay: terminationDelay,
|
||||
proxyProtocol: proxyProtocol,
|
||||
address: address,
|
||||
proxyProtocol: proxyProtocol,
|
||||
dialer: dialer,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ServeTCP forwards the connection to a service.
|
||||
func (p *Proxy) ServeTCP(conn WriteCloser) {
|
||||
log.Debug().Msgf("Handling connection from %s to %s", conn.RemoteAddr(), p.address)
|
||||
log.Debug().
|
||||
Str("address", p.address).
|
||||
Str("remoteAddr", conn.RemoteAddr().String()).
|
||||
Msg("Handling connection")
|
||||
|
||||
// needed because of e.g. server.trackedConnection
|
||||
defer conn.Close()
|
||||
|
@ -89,21 +79,13 @@ func (p *Proxy) ServeTCP(conn WriteCloser) {
|
|||
<-errChan
|
||||
}
|
||||
|
||||
func (p Proxy) dialBackend() (*net.TCPConn, error) {
|
||||
// Dial using directly the TCPAddr for IP based addresses.
|
||||
if p.tcpAddr != nil {
|
||||
return net.DialTCP("tcp", nil, p.tcpAddr)
|
||||
}
|
||||
|
||||
log.Debug().Msgf("Dial with lookup to address %s", p.address)
|
||||
|
||||
// Dial with DNS lookup for host based addresses.
|
||||
conn, err := net.Dial("tcp", p.address)
|
||||
func (p Proxy) dialBackend() (WriteCloser, error) {
|
||||
conn, err := p.dialer.Dial("tcp", p.address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn.(*net.TCPConn), nil
|
||||
return conn.(WriteCloser), nil
|
||||
}
|
||||
|
||||
func (p Proxy) connCopy(dst, src WriteCloser, errCh chan error) {
|
||||
|
@ -125,8 +107,8 @@ func (p Proxy) connCopy(dst, src WriteCloser, errCh chan error) {
|
|||
return
|
||||
}
|
||||
|
||||
if p.terminationDelay >= 0 {
|
||||
err := dst.SetReadDeadline(time.Now().Add(p.terminationDelay))
|
||||
if p.dialer.TerminationDelay() >= 0 {
|
||||
err := dst.SetReadDeadline(time.Now().Add(p.dialer.TerminationDelay()))
|
||||
if err != nil {
|
||||
log.Debug().Err(err).Msg("Error while setting deadline")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue