UDP support
Co-authored-by: Julien Salleyron <julien.salleyron@gmail.com>
This commit is contained in:
parent
8988c8f9af
commit
115d42e0f0
72 changed files with 4730 additions and 321 deletions
|
@ -1,5 +1,10 @@
|
|||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// EntryPoint holds the entry point configuration.
|
||||
type EntryPoint struct {
|
||||
Address string `description:"Entry point address." json:"address,omitempty" toml:"address,omitempty" yaml:"address,omitempty"`
|
||||
|
@ -8,11 +13,34 @@ type EntryPoint struct {
|
|||
ForwardedHeaders *ForwardedHeaders `description:"Trust client forwarding headers." json:"forwardedHeaders,omitempty" toml:"forwardedHeaders,omitempty" yaml:"forwardedHeaders,omitempty"`
|
||||
}
|
||||
|
||||
// GetAddress strips any potential protocol part of the address field of the
|
||||
// entry point, in order to return the actual address.
|
||||
func (ep EntryPoint) GetAddress() string {
|
||||
splitN := strings.SplitN(ep.Address, "/", 2)
|
||||
return splitN[0]
|
||||
}
|
||||
|
||||
// GetProtocol returns the protocol part of the address field of the entry point.
|
||||
// If none is specified, it defaults to "tcp".
|
||||
func (ep EntryPoint) GetProtocol() (string, error) {
|
||||
splitN := strings.SplitN(ep.Address, "/", 2)
|
||||
if len(splitN) < 2 {
|
||||
return "tcp", nil
|
||||
}
|
||||
|
||||
protocol := strings.ToLower(splitN[1])
|
||||
if protocol == "tcp" || protocol == "udp" {
|
||||
return protocol, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("invalid protocol: %s", splitN[1])
|
||||
}
|
||||
|
||||
// SetDefaults sets the default values.
|
||||
func (e *EntryPoint) SetDefaults() {
|
||||
e.Transport = &EntryPointsTransport{}
|
||||
e.Transport.SetDefaults()
|
||||
e.ForwardedHeaders = &ForwardedHeaders{}
|
||||
func (ep *EntryPoint) SetDefaults() {
|
||||
ep.Transport = &EntryPointsTransport{}
|
||||
ep.Transport.SetDefaults()
|
||||
ep.ForwardedHeaders = &ForwardedHeaders{}
|
||||
}
|
||||
|
||||
// ForwardedHeaders Trust client forwarding headers.
|
||||
|
|
67
pkg/config/static/entrypoints_test.go
Normal file
67
pkg/config/static/entrypoints_test.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package static
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEntryPointProtocol(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
address string
|
||||
expectedAddress string
|
||||
expectedProtocol string
|
||||
expectedError bool
|
||||
}{
|
||||
{
|
||||
name: "Without protocol",
|
||||
address: "127.0.0.1:8080",
|
||||
expectedAddress: "127.0.0.1:8080",
|
||||
expectedProtocol: "tcp",
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "With TCP protocol in upper case",
|
||||
address: "127.0.0.1:8080/TCP",
|
||||
expectedAddress: "127.0.0.1:8080",
|
||||
expectedProtocol: "tcp",
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "With UDP protocol in upper case",
|
||||
address: "127.0.0.1:8080/UDP",
|
||||
expectedAddress: "127.0.0.1:8080",
|
||||
expectedProtocol: "udp",
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "With UDP protocol in weird case",
|
||||
address: "127.0.0.1:8080/uDp",
|
||||
expectedAddress: "127.0.0.1:8080",
|
||||
expectedProtocol: "udp",
|
||||
expectedError: false,
|
||||
},
|
||||
|
||||
{
|
||||
name: "With invalid protocol",
|
||||
address: "127.0.0.1:8080/toto/tata",
|
||||
expectedError: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ep := EntryPoint{
|
||||
Address: tt.address,
|
||||
}
|
||||
protocol, err := ep.GetProtocol()
|
||||
if tt.expectedError {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expectedProtocol, protocol)
|
||||
require.Equal(t, tt.expectedAddress, ep.GetAddress())
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue