This commit is contained in:
bluepython508 committed 2026-10-22 12:27:19 +01:00
1 parent 3530afb854
commit 6903fcff27
2146 files changed
+112256 -190703

No files matched your search

+41 -26
View File
@@ -1,5 +1,5 @@
// Type-Length-Value splitting and parsing for proxy protocol V2
// See spec https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt sections 2.2 to 2.7 and
// Type-Length-Value splitting and parsing for proxy protocol V2.
// See spec https://www.haproxy.org/download/3.4/doc/proxy-protocol.txt sections 2.2 to 2.2.8.
package proxyproto
@@ -10,22 +10,28 @@ import (
"math"
)
// TLV type constants defined by the PROXY protocol spec.
//
//nolint:revive // Names follow the spec.
const (
// Section 2.2
PP2_TYPE_ALPN PP2Type = 0x01
PP2_TYPE_AUTHORITY PP2Type = 0x02
PP2_TYPE_CRC32C PP2Type = 0x03
PP2_TYPE_NOOP PP2Type = 0x04
PP2_TYPE_UNIQUE_ID PP2Type = 0x05
PP2_TYPE_SSL PP2Type = 0x20
PP2_SUBTYPE_SSL_VERSION PP2Type = 0x21
PP2_SUBTYPE_SSL_CN PP2Type = 0x22
PP2_SUBTYPE_SSL_CIPHER PP2Type = 0x23
PP2_SUBTYPE_SSL_SIG_ALG PP2Type = 0x24
PP2_SUBTYPE_SSL_KEY_ALG PP2Type = 0x25
PP2_TYPE_NETNS PP2Type = 0x30
// Section 2.2.
PP2_TYPE_ALPN PP2Type = 0x01
PP2_TYPE_AUTHORITY PP2Type = 0x02
PP2_TYPE_CRC32C PP2Type = 0x03
PP2_TYPE_NOOP PP2Type = 0x04
PP2_TYPE_UNIQUE_ID PP2Type = 0x05
PP2_TYPE_SSL PP2Type = 0x20
PP2_SUBTYPE_SSL_VERSION PP2Type = 0x21
PP2_SUBTYPE_SSL_CN PP2Type = 0x22
PP2_SUBTYPE_SSL_CIPHER PP2Type = 0x23
PP2_SUBTYPE_SSL_SIG_ALG PP2Type = 0x24
PP2_SUBTYPE_SSL_KEY_ALG PP2Type = 0x25
PP2_SUBTYPE_SSL_GROUP PP2Type = 0x26
PP2_SUBTYPE_SSL_SIG_SCHEME PP2Type = 0x27
PP2_SUBTYPE_SSL_CLIENT_CERT PP2Type = 0x28
PP2_TYPE_NETNS PP2Type = 0x30
// Section 2.2.7, reserved types
// Section 2.2.8, reserved types.
PP2_TYPE_MIN_CUSTOM PP2Type = 0xE0
PP2_TYPE_MAX_CUSTOM PP2Type = 0xEF
PP2_TYPE_MIN_EXPERIMENT PP2Type = 0xF0
@@ -35,15 +41,18 @@ const (
)
var (
ErrTruncatedTLV = errors.New("proxyproto: truncated TLV")
ErrMalformedTLV = errors.New("proxyproto: malformed TLV Value")
// ErrTruncatedTLV indicates a TLV was truncated.
ErrTruncatedTLV = errors.New("proxyproto: truncated TLV")
// ErrMalformedTLV indicates a TLV has malformed data.
ErrMalformedTLV = errors.New("proxyproto: malformed TLV Value")
// ErrIncompatibleTLV indicates a TLV is of an unexpected type.
ErrIncompatibleTLV = errors.New("proxyproto: incompatible TLV type")
)
// PP2Type is the proxy protocol v2 type
// PP2Type is the proxy protocol v2 type.
type PP2Type byte
// TLV is a uninterpreted Type-Length-Value for V2 protocol, see section 2.2
// TLV is a uninterpreted Type-Length-Value for V2 protocol, see section 2.2.
type TLV struct {
Type PP2Type
Value []byte
@@ -83,7 +92,9 @@ func JoinTLVs(tlvs []TLV) ([]byte, error) {
return nil, fmt.Errorf("proxyproto: cannot format TLV %v with length %d", tlv.Type, len(tlv.Value))
}
var length [2]byte
binary.BigEndian.PutUint16(length[:], uint16(len(tlv.Value)))
//nolint:gosec // lengthValue is validated above.
lengthValue := uint16(len(tlv.Value))
binary.BigEndian.PutUint16(length[:], lengthValue)
raw = append(raw, byte(tlv.Type))
raw = append(raw, length[:]...)
raw = append(raw, tlv.Value...)
@@ -91,7 +102,7 @@ func JoinTLVs(tlvs []TLV) ([]byte, error) {
return raw, nil
}
// Registered is true if the type is registered in the spec, see section 2.2
// Registered is true if the type is registered in the spec, see section 2.2.
func (p PP2Type) Registered() bool {
switch p {
case PP2_TYPE_ALPN,
@@ -105,28 +116,32 @@ func (p PP2Type) Registered() bool {
PP2_SUBTYPE_SSL_CIPHER,
PP2_SUBTYPE_SSL_SIG_ALG,
PP2_SUBTYPE_SSL_KEY_ALG,
PP2_SUBTYPE_SSL_GROUP,
PP2_SUBTYPE_SSL_SIG_SCHEME,
PP2_SUBTYPE_SSL_CLIENT_CERT,
PP2_TYPE_NETNS:
return true
}
return false
}
// App is true if the type is reserved for application specific data, see section 2.2.7
// App is true if the type is reserved for application specific data, see section 2.2.8.
func (p PP2Type) App() bool {
return p >= PP2_TYPE_MIN_CUSTOM && p <= PP2_TYPE_MAX_CUSTOM
}
// Experiment is true if the type is reserved for temporary experimental use by application developers, see section 2.2.7
// Experiment is true if the type is reserved for temporary experimental use by application
// developers, see section 2.2.8.
func (p PP2Type) Experiment() bool {
return p >= PP2_TYPE_MIN_EXPERIMENT && p <= PP2_TYPE_MAX_EXPERIMENT
}
// Future is true is the type is reserved for future use, see section 2.2.7
// Future is true is the type is reserved for future use, see section 2.2.8.
func (p PP2Type) Future() bool {
return p >= PP2_TYPE_MIN_FUTURE
}
// Spec is true if the type is covered by the spec, see section 2.2 and 2.2.7
// Spec is true if the type is covered by the spec, see section 2.2 and 2.2.8.
func (p PP2Type) Spec() bool {
return p.Registered() || p.App() || p.Experiment() || p.Future()
}