-
Notifications
You must be signed in to change notification settings - Fork 5
/
metrics_options.go
59 lines (53 loc) · 1.4 KB
/
metrics_options.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
package azugo
import (
"net"
"strings"
)
const DefaultMetricPath string = "/metrics"
type MetricsOptions struct {
// TrustAll option sets to trust all IP addresses.
TrustAll bool
// TrustedIPs represents list of trusted IP addresses.
TrustedIPs []net.IP
// TrustedNetworks represents addresses of trusted networks.
TrustedNetworks []*net.IPNet
// SkipPaths represents paths to bypass metrics handler
SkipPaths []string
}
var defaultMetricsOptions = MetricsOptions{
TrustedIPs: []net.IP{
net.IPv4(127, 0, 0, 1),
},
}
// Clear trusted metrics client list.
func (opts *MetricsOptions) Clear() *MetricsOptions {
opts.TrustAll = false
opts.TrustedIPs = make([]net.IP, 0)
opts.TrustedNetworks = make([]*net.IPNet, 0)
return opts
}
// Add IP or network in CIDR format to trusted metrics client list.
// Specify "*" to trust all sources.
func (opts *MetricsOptions) Add(ipnet string) *MetricsOptions {
// Special option to trust all sources if IP address is set as wildcard
if ipnet == "*" {
opts.TrustAll = true
return opts
}
// CIDR format
if strings.ContainsRune(ipnet, '/') {
_, netmask, err := net.ParseCIDR(ipnet)
if err != nil || netmask == nil {
return opts
}
opts.TrustedNetworks = append(opts.TrustedNetworks, netmask)
return opts
}
// Single IP address
ipaddr := net.ParseIP(ipnet)
if ipaddr == nil {
return opts
}
opts.TrustedIPs = append(opts.TrustedIPs, ipaddr)
return opts
}