-
Notifications
You must be signed in to change notification settings - Fork 153
/
helper.go
84 lines (79 loc) · 1.56 KB
/
helper.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
package types
import (
"fmt"
"net"
terwayIP "github.com/AliyunContainerService/terway/pkg/ip"
"github.com/AliyunContainerService/terway/rpc"
)
func BuildIPNet(ip, subnet *rpc.IPSet) (*IPNetSet, error) {
ipnet := &IPNetSet{}
if ip == nil || subnet == nil {
return ipnet, nil
}
exec := func(ip, subnet string) (*net.IPNet, error) {
i, err := terwayIP.ToIP(ip)
if err != nil {
return nil, err
}
_, sub, err := net.ParseCIDR(subnet)
if err != nil {
return nil, err
}
sub.IP = i
return sub, nil
}
var err error
if ip.IPv4 != "" && subnet.IPv4 != "" {
ipnet.IPv4, err = exec(ip.IPv4, subnet.IPv4)
if err != nil {
return nil, err
}
}
if ip.IPv6 != "" && subnet.IPv6 != "" {
ipnet.IPv6, err = exec(ip.IPv6, subnet.IPv6)
if err != nil {
return nil, err
}
}
return ipnet, nil
}
func ToIPSet(ip *rpc.IPSet) (*IPSet, error) {
if ip == nil {
return nil, fmt.Errorf("ip is nil")
}
ipSet := &IPSet{}
var err error
if ip.IPv4 != "" {
ipSet.IPv4, err = terwayIP.ToIP(ip.IPv4)
if err != nil {
return nil, err
}
}
if ip.IPv6 != "" {
ipSet.IPv6, err = terwayIP.ToIP(ip.IPv6)
if err != nil {
return nil, err
}
}
return ipSet, nil
}
func ToIPNetSet(ip *rpc.IPSet) (*IPNetSet, error) {
if ip == nil {
return nil, fmt.Errorf("ip is nil")
}
ipNetSet := &IPNetSet{}
var err error
if ip.IPv4 != "" {
_, ipNetSet.IPv4, err = net.ParseCIDR(ip.IPv4)
if err != nil {
return nil, err
}
}
if ip.IPv6 != "" {
_, ipNetSet.IPv6, err = net.ParseCIDR(ip.IPv6)
if err != nil {
return nil, err
}
}
return ipNetSet, nil
}