forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsing.go
182 lines (156 loc) · 3.53 KB
/
parsing.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
// Copyright 2017 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package main
import (
"fmt"
"net"
"strconv"
"strings"
)
// Types that implement the flag.Value interface for parsing
type parseStrings []string
type parseIPs []net.IP
type parseCIDRs []*net.IPNet
type parseInts []int
// parseStrings implementation of the flag.Value interface
func (p *parseStrings) String() string {
if p == nil {
return ""
}
return strings.Join(*p, ",")
}
func (p *parseStrings) Set(s string) error {
if s == "" {
return fmt.Errorf("String parsing failed")
}
str := strings.Split(s, ",")
for _, s := range str {
*p = append(*p, strings.TrimSpace(s))
}
return nil
}
// parseInts implementation of the flag.Value interface
func (p *parseInts) String() string {
if p == nil {
return ""
}
var nums []string
for _, n := range *p {
nums = append(nums, strconv.Itoa(n))
}
return strings.Join(nums, ",")
}
func (p *parseInts) Set(s string) error {
if s == "" {
return fmt.Errorf("Integer parsing failed")
}
nums := strings.Split(s, ",")
for _, n := range nums {
i, err := strconv.Atoi(strings.TrimSpace(n))
if err != nil {
return err
}
*p = append(*p, i)
}
return nil
}
// parseIPs implementation of the flag.Value interface
func (p *parseIPs) String() string {
if p == nil {
return ""
}
var ipaddrs []string
for _, ipaddr := range *p {
ipaddrs = append(ipaddrs, ipaddr.String())
}
return strings.Join(ipaddrs, ",")
}
func (p *parseIPs) Set(s string) error {
if s == "" {
return fmt.Errorf("IP address parsing failed")
}
ips := strings.Split(s, ",")
for _, ip := range ips {
// Is this an IP range?
err := p.parseRange(ip)
if err == nil {
continue
}
addr := net.ParseIP(ip)
if addr == nil {
return fmt.Errorf("%s is not a valid IP address or range", ip)
}
*p = append(*p, addr)
}
return nil
}
func (p *parseIPs) appendIPs(addrs []net.IP) error {
for _, addr := range addrs {
*p = append(*p, addr)
}
return nil
}
func (p *parseIPs) parseRange(s string) error {
twoIPs := strings.Split(s, "-")
if twoIPs[0] == s {
// This is not an IP range
return fmt.Errorf("%s is not a valid IP range", s)
}
start := net.ParseIP(twoIPs[0])
end := net.ParseIP(twoIPs[1])
if end == nil {
num, err := strconv.Atoi(twoIPs[1])
if err == nil {
end = net.ParseIP(twoIPs[0])
end[len(end)-1] = byte(num)
}
}
if start == nil || end == nil {
// These should have parsed properly
return fmt.Errorf("%s is not a valid IP range", s)
}
return p.appendIPs(RangeHosts(start, end))
}
func RangeHosts(start, end net.IP) []net.IP {
var ips []net.IP
stop := net.ParseIP(end.String())
addrInc(stop)
for ip := net.ParseIP(start.String()); !ip.Equal(stop); addrInc(ip) {
addr := net.ParseIP(ip.String())
ips = append(ips, addr)
}
return ips
}
func addrInc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
// parseCIDRs implementation of the flag.Value interface
func (p *parseCIDRs) String() string {
if p == nil {
return ""
}
var cidrs []string
for _, ipnet := range *p {
cidrs = append(cidrs, ipnet.String())
}
return strings.Join(cidrs, ",")
}
func (p *parseCIDRs) Set(s string) error {
if s == "" {
return fmt.Errorf("%s is not a valid CIDR", s)
}
cidrs := strings.Split(s, ",")
for _, cidr := range cidrs {
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return fmt.Errorf("Failed to parse %s as a CIDR", cidr)
}
*p = append(*p, ipnet)
}
return nil
}