2019-03-14 08:30:04 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2020-11-17 12:04:04 +00:00
|
|
|
"fmt"
|
2019-03-14 08:30:04 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
2019-09-13 15:46:04 +00:00
|
|
|
"time"
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2020-11-17 12:04:04 +00:00
|
|
|
"github.com/pires/go-proxyproto"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2019-03-14 08:30:04 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Proxy forwards a TCP request to a TCP service.
|
2019-03-14 08:30:04 +00:00
|
|
|
type Proxy struct {
|
2020-11-13 11:48:04 +00:00
|
|
|
address string
|
2022-05-19 14:40:09 +00:00
|
|
|
tcpAddr *net.TCPAddr
|
2019-09-13 15:46:04 +00:00
|
|
|
terminationDelay time.Duration
|
2020-11-17 12:04:04 +00:00
|
|
|
proxyProtocol *dynamic.ProxyProtocol
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// NewProxy creates a new Proxy.
|
2020-11-17 12:04:04 +00:00
|
|
|
func NewProxy(address string, terminationDelay time.Duration, proxyProtocol *dynamic.ProxyProtocol) (*Proxy, error) {
|
|
|
|
if proxyProtocol != nil && (proxyProtocol.Version < 1 || proxyProtocol.Version > 2) {
|
|
|
|
return nil, fmt.Errorf("unknown proxyProtocol version: %d", proxyProtocol.Version)
|
|
|
|
}
|
|
|
|
|
2022-05-19 14:40:09 +00:00
|
|
|
// 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
|
|
|
|
}
|
2020-11-13 11:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Proxy{
|
|
|
|
address: address,
|
2022-05-19 14:40:09 +00:00
|
|
|
tcpAddr: tcpAddr,
|
2020-11-13 11:48:04 +00:00
|
|
|
terminationDelay: terminationDelay,
|
2020-11-20 10:30:07 +00:00
|
|
|
proxyProtocol: proxyProtocol,
|
2020-11-13 11:48:04 +00:00
|
|
|
}, nil
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// ServeTCP forwards the connection to a service.
|
2019-09-13 15:46:04 +00:00
|
|
|
func (p *Proxy) ServeTCP(conn WriteCloser) {
|
2022-05-30 09:14:09 +00:00
|
|
|
log.WithoutContext().Debugf("Handling connection from %s to %s", conn.RemoteAddr(), p.address)
|
2019-09-13 15:46:04 +00:00
|
|
|
|
|
|
|
// needed because of e.g. server.trackedConnection
|
2019-03-14 08:30:04 +00:00
|
|
|
defer conn.Close()
|
2019-05-09 12:30:06 +00:00
|
|
|
|
2021-03-23 10:24:03 +00:00
|
|
|
connBackend, err := p.dialBackend()
|
2019-03-14 08:30:04 +00:00
|
|
|
if err != nil {
|
2021-03-23 10:24:03 +00:00
|
|
|
log.WithoutContext().Errorf("Error while connecting to backend: %v", err)
|
2019-03-14 08:30:04 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-13 15:46:04 +00:00
|
|
|
|
|
|
|
// maybe not needed, but just in case
|
2019-03-14 08:30:04 +00:00
|
|
|
defer connBackend.Close()
|
2019-09-13 15:46:04 +00:00
|
|
|
errChan := make(chan error)
|
2020-11-17 12:04:04 +00:00
|
|
|
|
|
|
|
if p.proxyProtocol != nil && p.proxyProtocol.Version > 0 && p.proxyProtocol.Version < 3 {
|
|
|
|
header := proxyproto.HeaderProxyFromAddrs(byte(p.proxyProtocol.Version), conn.RemoteAddr(), conn.LocalAddr())
|
|
|
|
if _, err := header.WriteTo(connBackend); err != nil {
|
|
|
|
log.WithoutContext().Errorf("Error while writing proxy protocol headers to backend connection: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 15:46:04 +00:00
|
|
|
go p.connCopy(conn, connBackend, errChan)
|
|
|
|
go p.connCopy(connBackend, conn, errChan)
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
err = <-errChan
|
|
|
|
if err != nil {
|
2019-09-13 15:46:04 +00:00
|
|
|
log.WithoutContext().Errorf("Error during connection: %v", err)
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2019-09-13 15:46:04 +00:00
|
|
|
|
|
|
|
<-errChan
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 10:24:03 +00:00
|
|
|
func (p Proxy) dialBackend() (*net.TCPConn, error) {
|
2022-05-19 14:40:09 +00:00
|
|
|
// Dial using directly the TCPAddr for IP based addresses.
|
|
|
|
if p.tcpAddr != nil {
|
|
|
|
return net.DialTCP("tcp", nil, p.tcpAddr)
|
2021-03-23 10:24:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 14:40:09 +00:00
|
|
|
log.WithoutContext().Debugf("Dial with lookup to address %s", p.address)
|
|
|
|
|
|
|
|
// Dial with DNS lookup for host based addresses.
|
2021-03-23 10:24:03 +00:00
|
|
|
conn, err := net.Dial("tcp", p.address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn.(*net.TCPConn), nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 15:46:04 +00:00
|
|
|
func (p Proxy) connCopy(dst, src WriteCloser, errCh chan error) {
|
2019-03-14 08:30:04 +00:00
|
|
|
_, err := io.Copy(dst, src)
|
|
|
|
errCh <- err
|
2019-09-13 15:46:04 +00:00
|
|
|
|
|
|
|
errClose := dst.CloseWrite()
|
|
|
|
if errClose != nil {
|
2019-09-13 18:00:06 +00:00
|
|
|
log.WithoutContext().Debugf("Error while terminating connection: %v", errClose)
|
|
|
|
return
|
2019-09-13 15:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.terminationDelay >= 0 {
|
|
|
|
err := dst.SetReadDeadline(time.Now().Add(p.terminationDelay))
|
|
|
|
if err != nil {
|
2019-09-13 18:00:06 +00:00
|
|
|
log.WithoutContext().Debugf("Error while setting deadline: %v", err)
|
2019-09-13 15:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|