/
listener.go
78 lines (62 loc) · 2.13 KB
/
listener.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package bootstrap
import (
"fmt"
"net"
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_config_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
envoy_extensions_filters_network_http_connection_manager_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
"google.golang.org/protobuf/proto"
)
type Listener = envoy_config_listener_v3.Listener
type ListenerFilter = envoy_config_listener_v3.ListenerFilter
type Filter = envoy_config_listener_v3.Filter
type FilterChain = envoy_config_listener_v3.FilterChain
type FilterChainMatch = envoy_config_listener_v3.FilterChainMatch
type HTTPFilter = envoy_extensions_filters_network_http_connection_manager_v3.HttpFilter
type CidrRange = envoy_config_core_v3.CidrRange
const TCP = envoy_config_core_v3.SocketAddress_TCP
const UDP = envoy_config_core_v3.SocketAddress_UDP
const INBOUND = envoy_config_core_v3.TrafficDirection_INBOUND
const OUTBOUND = envoy_config_core_v3.TrafficDirection_OUTBOUND
func NewFilter(name string, config proto.Message) *Filter {
type TypedConfig = envoy_config_listener_v3.Filter_TypedConfig
any, err := MarshalAny(config)
if err != nil {
panic(fmt.Errorf("failed to marshall %q type to Any: %s",
config.ProtoReflect().Descriptor().FullName(), err))
}
return &Filter{
Name: name,
ConfigType: &TypedConfig{
TypedConfig: any,
},
}
}
func NewHTTPFilter(name string, config proto.Message) *HTTPFilter {
type TypedConfig = envoy_extensions_filters_network_http_connection_manager_v3.HttpFilter_TypedConfig
any, err := MarshalAny(config)
if err != nil {
panic(fmt.Errorf("failed to marshall %q type to Any: %s",
config.ProtoReflect().Descriptor().FullName(), err))
}
return &HTTPFilter{
Name: name,
ConfigType: &TypedConfig{
TypedConfig: any,
},
}
}
// NewCidrForIP returns a /32 for IPv6 and a /128 for IPv6.
func NewCidrForIP(ip net.IP) *CidrRange {
if ip != nil {
nbytes := uint32(16)
if ip.To4() != nil {
nbytes = 4
}
return &CidrRange{
AddressPrefix: ip.String(),
PrefixLen: UInt32(nbytes * 8),
}
}
return nil
}