-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipranger.go
240 lines (202 loc) · 5.25 KB
/
ipranger.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package ipranger
import (
"errors"
"net"
"strings"
"sync"
"sync/atomic"
"github.com/khulnasoft-labs/hmap/store/hybrid"
"github.com/khulnasoft-labs/mapcidr"
"github.com/khulnasoft-labs/networkpolicy"
iputil "github.com/khulnasoft-labs/utils/ip"
stringsutil "github.com/khulnasoft-labs/utils/strings"
"github.com/yl2chen/cidranger"
)
type IPRanger struct {
sync.RWMutex
Np *networkpolicy.NetworkPolicy
iprangerop cidranger.Ranger
Hosts *hybrid.HybridMap
Stats Stats
CoalescedIPV4 []*net.IPNet
CoalescedIPV6 []*net.IPNet
}
func New() (*IPRanger, error) {
hm, err := hybrid.New(hybrid.DefaultDiskOptions)
if err != nil {
return nil, err
}
var np networkpolicy.NetworkPolicy
return &IPRanger{Np: &np, iprangerop: cidranger.NewPCTrieRanger(), Hosts: hm}, nil
}
func (ir *IPRanger) ContainsAll(hosts ...string) bool {
for _, host := range hosts {
if !ir.Contains(host) {
return false
}
}
return true
}
func (ir *IPRanger) ContainsAny(hosts ...string) bool {
for _, host := range hosts {
if ir.Contains(host) {
return true
}
}
return false
}
func (ir *IPRanger) Contains(host string) bool {
ir.RLock()
defer ir.RUnlock()
// not valid => not contained
if !ir.Np.Validate(host) {
return false
}
// ip => check internal ip ranger
if iputil.IsIP(host) {
if ok, err := ir.iprangerop.Contains(net.ParseIP(host)); err == nil {
return ok
}
}
// fqdn, cidr => check hmap
_, ok := ir.Hosts.Get(host)
return ok
}
func (ir *IPRanger) Add(host string) error {
// skip invalid
if !ir.Np.Validate(host) {
return errors.New("invalid host")
}
// skip already contained
if ir.Contains(host) {
return errors.New("host already added")
}
// ips: valid + new => add
if iputil.IsIP(host) {
if ir.Np.Validate(host) {
return ir.add(host)
}
return errors.New("invalid ip")
}
// cidrs => add
if iputil.IsCIDR(host) {
return ir.add(host)
}
return errors.New("only ip/cidr can be added")
}
func (ir *IPRanger) asIPNet(host string) (*net.IPNet, error) {
var (
network *net.IPNet
err error
)
switch {
case iputil.IsCIDR(host):
_, network, err = net.ParseCIDR(host)
case iputil.IsIPv4(host):
network = iputil.AsIPV4IpNet(host)
case iputil.IsIPv6(host):
network = iputil.AsIPV6IpNet(host)
default:
err = errors.New("unsupported ip/cidr type")
}
return network, err
}
func (ir *IPRanger) add(host string) error {
ir.Lock()
defer ir.Unlock()
network, err := ir.asIPNet(host)
if err != nil {
return err
}
atomic.AddUint64(&ir.Stats.IPS, mapcidr.AddressCountIpnet(network))
return ir.iprangerop.Insert(cidranger.NewBasicRangerEntry(*network))
}
func (ir *IPRanger) IsValid(host string) bool {
return ir.Np.Validate(host)
}
func (ir *IPRanger) Delete(host string) error {
// if it's an ip convert it to cidr representation
if iputil.IsIP(host) || iputil.IsCIDR(host) {
return ir.delete(host)
}
return errors.New("only ip or cidr supported")
}
func (ir *IPRanger) delete(host string) error {
ir.Lock()
defer ir.Unlock()
network, err := ir.asIPNet(host)
if err != nil {
return err
}
atomic.AddUint64(&ir.Stats.IPS, -mapcidr.AddressCountIpnet(network))
_, err = ir.iprangerop.Remove(*network)
return err
}
func (ir *IPRanger) AddHostWithMetadata(host, metadata string) error {
if !ir.IsValid(host) {
return errors.New("invalid host with metadata")
}
// cache ip/cidr
_ = ir.Add(host)
// dedupe all the hosts and also keep track of ip => host for the output - just append new hostname
if data, ok := ir.Hosts.Get(host); ok {
// check if fqdn not contained
datas := string(data)
if datas != metadata && !stringsutil.ContainsAny(datas, metadata+",", ","+metadata+",", ","+metadata) {
hosts := strings.Split(string(data), ",")
hosts = append(hosts, metadata)
atomic.AddUint64(&ir.Stats.Hosts, 1)
return ir.Hosts.Set(host, []byte(strings.Join(hosts, ",")))
}
// host already contained
return nil
}
atomic.AddUint64(&ir.Stats.Hosts, 1)
return ir.Hosts.Set(host, []byte(metadata))
}
func (ir *IPRanger) HasIP(IP string) bool {
_, ok := ir.Hosts.Get(IP)
return ok
}
func (ir *IPRanger) GetHostsByIP(IP string) ([]string, error) {
dt, ok := ir.Hosts.Get(IP)
if ok {
return strings.Split(string(dt), ","), nil
}
// if not found return the ip
return []string{IP}, nil
}
func (ir *IPRanger) Close() error {
return ir.Hosts.Close()
}
func (ir *IPRanger) Shrink() error {
// shrink all the cidrs and ips (ipv4)
var items []*net.IPNet
ir.Hosts.Scan(func(item, _ []byte) error {
ipnet, err := ir.asIPNet(string(item))
if err != nil {
return err
}
items = append(items, ipnet)
return nil
})
ir.CoalescedIPV4, ir.CoalescedIPV6 = mapcidr.CoalesceCIDRs(items)
// reset the internal ranger with the new data
ir.iprangerop = cidranger.NewPCTrieRanger()
atomic.StoreUint64(&ir.Stats.IPS, 0)
return ir.addToTcpTrie(ir.CoalescedIPV4, ir.CoalescedIPV6)
}
func (ir *IPRanger) addToTcpTrie(coalescedIpGroups ...[]*net.IPNet) error {
ir.Lock()
defer ir.Unlock()
for _, coalescedIpGroup := range coalescedIpGroups {
for _, coalescedIP := range coalescedIpGroup {
err := ir.iprangerop.Insert(cidranger.NewBasicRangerEntry(*coalescedIP))
if err != nil {
return err
}
atomic.AddUint64(&ir.Stats.IPS, mapcidr.AddressCountIpnet(coalescedIP))
}
}
return nil
}