forked from nerdalert/nflow-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
77 lines (63 loc) · 1.27 KB
/
utils.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
package main
import (
"encoding/binary"
"fmt"
"math/rand"
"net"
"net/netip"
)
func FindIndex(a string, list []string) int {
for i, b := range list {
if b == a {
return i
}
}
return -1
}
func ConvertRangeToSlice(start, end int) []int {
var result []int
for i := start; i <= end; i++ {
result = append(result, i)
}
return result
}
func ConvertPortRangeToSlice(start, end int) []uint16 {
var result []uint16
for i := start; i <= end; i++ {
result = append(result, uint16(i))
}
return result
}
func GetCidrHosts(cidr string) ([]string, error) {
prefix, err := netip.ParsePrefix(cidr)
if err != nil {
return nil, fmt.Errorf("failed to parse cidr %s: %v", cidr, err)
}
var ips []string
for addr := prefix.Addr(); prefix.Contains(addr); addr = addr.Next() {
ips = append(ips, addr.String())
}
if len(ips) < 2 {
return ips, nil
}
return ips[1 : len(ips)-1], nil
}
func ConvertIntToIp(nn uint32) net.IP {
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, nn)
return ip
}
func FindHostIp(hosts []ConfigHost, name string) string {
if name == "" {
return ""
}
for _, host := range hosts {
if host.Name == name {
return host.Ip
}
}
panic("host not found: " + name)
}
func randomNum(min, max int) int {
return rand.Intn(max-min) + min
}