MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / ParseIPRangeList

Function ParseIPRangeList

internal/waf/values/ip_range.go:53–123  ·  view source on GitHub ↗
(value string)

Source from the content-addressed store, hash-verified

51}
52
53func ParseIPRangeList(value string) *IPRangeList {
54 var list = NewIPRangeList()
55
56 if len(value) == 0 {
57 return list
58 }
59
60 var lines = strings.Split(value, "\n")
61 for _, line := range lines {
62 line = strings.TrimSpace(line)
63 if len(line) == 0 {
64 continue
65 }
66
67 if strings.Contains(line, ",") { // IPFrom,IPTo
68 var pieces = strings.SplitN(line, ",", 2)
69 if len(pieces) == 2 {
70 var ipFromString = strings.TrimSpace(pieces[0])
71 if len(ipFromString) == 0 {
72 ipFromString = "0.0.0.0"
73 }
74 var ipFrom = net.ParseIP(ipFromString)
75 var ipTo = net.ParseIP(strings.TrimSpace(pieces[1]))
76 if ipFrom != nil && ipTo != nil {
77 if bytes.Compare(ipFrom, ipTo) > 0 {
78 ipFrom, ipTo = ipTo, ipFrom
79 }
80 list.Ranges = append(list.Ranges, &IPRange{
81 Type: IPRangeTypeRange,
82 IPFrom: ipFrom,
83 IPTo: ipTo,
84 })
85 }
86 }
87 } else if strings.Contains(line, "-") { // IPFrom-IPTo
88 var pieces = strings.SplitN(line, "-", 2)
89 if len(pieces) == 2 {
90 var ipFrom = net.ParseIP(strings.TrimSpace(pieces[0]))
91 var ipTo = net.ParseIP(strings.TrimSpace(pieces[1]))
92 if ipFrom != nil && ipTo != nil {
93 if bytes.Compare(ipFrom, ipTo) > 0 {
94 ipFrom, ipTo = ipTo, ipFrom
95 }
96 list.Ranges = append(list.Ranges, &IPRange{
97 Type: IPRangeTypeRange,
98 IPFrom: ipFrom,
99 IPTo: ipTo,
100 })
101 }
102 }
103 } else if strings.Contains(line, "/") { // CIDR
104 _, cidr, _ := net.ParseCIDR(line)
105 if cidr != nil {
106 list.Ranges = append(list.Ranges, &IPRange{
107 Type: IPRangeTypeCIDR,
108 CIDR: cidr,
109 })
110 }

Callers 3

InitMethod · 0.92
TestIPRange_ContainsFunction · 0.92

Calls 3

NewIPRangeListFunction · 0.85
ParseIPMethod · 0.80
ContainsMethod · 0.45

Tested by 2

TestIPRange_ContainsFunction · 0.74