-
Notifications
You must be signed in to change notification settings - Fork 53
/
remote_addresss.go
46 lines (38 loc) · 971 Bytes
/
remote_addresss.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
package strategies
import (
"github.com/Unleash/unleash-client-go/v4/context"
"github.com/Unleash/unleash-client-go/v4/strategy"
"net"
"strings"
)
type remoteAddressStrategy struct {
}
func NewRemoteAddressStrategy() *remoteAddressStrategy {
return &remoteAddressStrategy{}
}
func (s remoteAddressStrategy) Name() string {
return "remoteAddress"
}
func (s remoteAddressStrategy) IsEnabled(params map[string]interface{}, ctx *context.Context) bool {
value, found := params[strategy.ParamIps]
if !found {
return false
}
remoteAddress := net.ParseIP(strings.TrimSpace(ctx.RemoteAddress))
if remoteAddress == nil {
return false
}
ips, ok := value.(string)
if !ok {
return false
}
for _, ip := range strings.Split(ips, ",") {
ip = strings.TrimSpace(ip)
if remoteAddress.Equal(net.ParseIP(ip)) {
return true
} else if _, ipNet, _ := net.ParseCIDR(ip); ipNet != nil && ipNet.Contains(remoteAddress) {
return true
}
}
return false
}