File: aws.go

package info (click to toggle)
golang-github-pires-go-proxyproto 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 332 kB
  • sloc: makefile: 3
file content (43 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Amazon's application extension to TLVs for NLB VPC endpoint services
// https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-target-groups.html#proxy-protocol

package tlvparse

import (
	"regexp"

	"github.com/pires/go-proxyproto"
)

const (
	// Amazon's extension
	PP2_TYPE_AWS            = 0xEA
	PP2_SUBTYPE_AWS_VPCE_ID = 0x01
)

var vpceRe = regexp.MustCompile("^[A-Za-z0-9-]*$")

func IsAWSVPCEndpointID(tlv proxyproto.TLV) bool {
	return tlv.Type == PP2_TYPE_AWS && len(tlv.Value) > 0 && tlv.Value[0] == PP2_SUBTYPE_AWS_VPCE_ID
}

func AWSVPCEndpointID(tlv proxyproto.TLV) (string, error) {
	if !IsAWSVPCEndpointID(tlv) {
		return "", proxyproto.ErrIncompatibleTLV
	}
	vpce := string(tlv.Value[1:])
	if !vpceRe.MatchString(vpce) {
		return "", proxyproto.ErrMalformedTLV
	}
	return vpce, nil
}

// FindAWSVPCEndpointID returns the first AWS VPC ID in the TLV if it exists and is well-formed.
func FindAWSVPCEndpointID(tlvs []proxyproto.TLV) string {
	for _, tlv := range tlvs {
		if vpc, err := AWSVPCEndpointID(tlv); err == nil && vpc != "" {
			return vpc
		}
	}
	return ""
}