2019-09-13 15:46:04 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-11-17 12:04:04 +00:00
|
|
|
"errors"
|
2019-09-13 15:46:04 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-11-17 12:04:04 +00:00
|
|
|
"github.com/pires/go-proxyproto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-09-13 15:46:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
2019-09-13 15:46:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func fakeRedis(t *testing.T, listener net.Listener) {
|
2020-12-29 09:54:03 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2019-09-13 15:46:04 +00:00
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
require.NoError(t, err)
|
2020-11-17 12:04:04 +00:00
|
|
|
|
2019-09-13 15:46:04 +00:00
|
|
|
for {
|
|
|
|
withErr := false
|
|
|
|
buf := make([]byte, 64)
|
|
|
|
if _, err := conn.Read(buf); err != nil {
|
|
|
|
withErr = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(buf[:4]) == "ping" {
|
2019-11-26 20:38:03 +00:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
2019-09-13 15:46:04 +00:00
|
|
|
if _, err := conn.Write([]byte("PONG")); err != nil {
|
2020-11-17 12:04:04 +00:00
|
|
|
_ = conn.Close()
|
2019-09-13 15:46:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 12:04:04 +00:00
|
|
|
|
2019-09-13 15:46:04 +00:00
|
|
|
if withErr {
|
2020-11-17 12:04:04 +00:00
|
|
|
_ = conn.Close()
|
2019-09-13 15:46:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseWrite(t *testing.T) {
|
|
|
|
backendListener, err := net.Listen("tcp", ":0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go fakeRedis(t, backendListener)
|
|
|
|
_, port, err := net.SplitHostPort(backendListener.Addr().String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-09 08:58:05 +00:00
|
|
|
dialer := tcpDialer{&net.Dialer{}, 10 * time.Millisecond}
|
|
|
|
|
|
|
|
proxy, err := NewProxy(":"+port, nil, dialer)
|
2019-09-13 15:46:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
proxyListener, err := net.Listen("tcp", ":0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := proxyListener.Accept()
|
|
|
|
require.NoError(t, err)
|
|
|
|
proxy.ServeTCP(conn.(*net.TCPConn))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, port, err = net.SplitHostPort(proxyListener.Addr().String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", ":"+port)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte("ping\n"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = conn.(*net.TCPConn).CloseWrite()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var buf []byte
|
|
|
|
buffer := bytes.NewBuffer(buf)
|
|
|
|
n, err := io.Copy(buffer, conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(4), n)
|
|
|
|
require.Equal(t, "PONG", buffer.String())
|
|
|
|
}
|
2020-11-17 12:04:04 +00:00
|
|
|
|
|
|
|
func TestProxyProtocol(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
version int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "PROXY protocol v1",
|
|
|
|
version: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "PROXY protocol v2",
|
|
|
|
version: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
backendListener, err := net.Listen("tcp", ":0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var version int
|
|
|
|
proxyBackendListener := proxyproto.Listener{
|
|
|
|
Listener: backendListener,
|
|
|
|
ValidateHeader: func(h *proxyproto.Header) error {
|
|
|
|
version = int(h.Version)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Policy: func(upstream net.Addr) (proxyproto.Policy, error) {
|
|
|
|
switch test.version {
|
|
|
|
case 1, 2:
|
|
|
|
return proxyproto.USE, nil
|
|
|
|
default:
|
|
|
|
return proxyproto.REQUIRE, errors.New("unsupported version")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
defer proxyBackendListener.Close()
|
|
|
|
|
|
|
|
go fakeRedis(t, &proxyBackendListener)
|
|
|
|
|
|
|
|
_, port, err := net.SplitHostPort(proxyBackendListener.Addr().String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-09 08:58:05 +00:00
|
|
|
dialer := tcpDialer{&net.Dialer{}, 10 * time.Millisecond}
|
|
|
|
|
|
|
|
proxy, err := NewProxy(":"+port, &dynamic.ProxyProtocol{Version: test.version}, dialer)
|
2020-11-17 12:04:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
proxyListener, err := net.Listen("tcp", ":0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := proxyListener.Accept()
|
|
|
|
require.NoError(t, err)
|
|
|
|
proxy.ServeTCP(conn.(*net.TCPConn))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, port, err = net.SplitHostPort(proxyListener.Addr().String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", ":"+port)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte("ping\n"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = conn.(*net.TCPConn).CloseWrite()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var buf []byte
|
|
|
|
buffer := bytes.NewBuffer(buf)
|
|
|
|
n, err := io.Copy(buffer, conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(4), n)
|
|
|
|
assert.Equal(t, "PONG", buffer.String())
|
|
|
|
|
|
|
|
assert.Equal(t, test.version, version)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|