-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
validator.go
87 lines (73 loc) · 2.1 KB
/
validator.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
package ipfailover
import (
"fmt"
"net"
"strconv"
"strings"
)
// ValidateIPAddress validates IP address.
func ValidateIPAddress(ip string) error {
ipaddr := strings.TrimSpace(ip)
if net.ParseIP(ipaddr) == nil {
return fmt.Errorf("Invalid IP address: %s", ip)
}
return nil
}
// ValidateIPAddressRange validates an IP address range or single IP address.
func ValidateIPAddressRange(iprange string) error {
iprange = strings.TrimSpace(iprange)
if strings.Count(iprange, "-") < 1 {
return ValidateIPAddress(iprange)
}
if strings.Count(iprange, "-") > 1 {
return fmt.Errorf("invalid IP range format: %s", iprange)
}
// Its an IP range of the form: n.n.n.n-n
rangeLimits := strings.Split(iprange, "-")
startIP := rangeLimits[0]
parts := strings.Split(startIP, ".")
if len(parts) < 4 {
return fmt.Errorf("invalid IP range start fomat: %s", startIP)
}
rangeStart := parts[3]
rangeEnd := rangeLimits[1]
if err := ValidateIPAddress(startIP); err != nil {
return err
}
// Manufacture ending IP address for the range.
parts[3] = rangeEnd
endIP := strings.Join(parts, ".")
if ValidateIPAddress(endIP) != nil {
return fmt.Errorf("invalid IP range end: %s [%s]", rangeEnd, endIP)
}
// Lastly, ensure start <= end
start, err := strconv.Atoi(rangeStart)
if err != nil {
return fmt.Errorf("invalid IP range start: %s [%s]", rangeStart, startIP)
}
end, err := strconv.Atoi(rangeEnd)
if err != nil {
return fmt.Errorf("invalid IP range end: %s [%s]", rangeEnd, endIP)
}
if start > end {
return fmt.Errorf("invalid IP range %s-%s: start=%v > end=%v", startIP, endIP, start, end)
}
return nil
}
// ValidateVirtualIPs validates virtual IP range/addresses.
func ValidateVirtualIPs(vips string) error {
virtualIPs := strings.TrimSpace(vips)
if len(virtualIPs) < 1 {
return nil
}
for _, ip := range strings.Split(virtualIPs, ",") {
if err := ValidateIPAddressRange(ip); err != nil {
return err
}
}
return nil
}
// ValidateCmdOptions validates command line operations.
func ValidateCmdOptions(options *IPFailoverConfigCmdOptions) error {
return ValidateVirtualIPs(options.VirtualIPs)
}