-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
285 lines (265 loc) · 6.49 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"fmt"
"net"
"os"
"sort"
"strings"
"time"
"gopkg.in/urfave/cli.v1"
)
const (
ipURL = "https://ip-ranges.amazonaws.com/ip-ranges.json"
strCIDR = "cidr"
strPrefix = "prefix"
strJSON = "json"
strYAML = "yaml"
strText = "text"
)
var (
validFields = []string{"cidr", "service", "region", "all"}
validEncodings = []string{strJSON, strYAML, strText}
)
type Prefix struct {
IPPrefix string `json:"ip_prefix" yaml:"ip_prefix"`
Region string `json:"region" yaml:"region"`
Service string `json:"service" yaml:"service"`
}
type IPv6Prefix struct {
IPv6Prefix string `json:"ipv6_prefix" yaml:"ipv6_prefix"`
Region string `json:"region" yaml:"region"`
Service string `json:"service" yaml:"service"`
}
type IPRangeDoc struct {
SyncToken string `json:"syncToken"`
CreateDate string `json:"createDate"`
Prefixes []Prefix `json:"prefixes" yaml:"prefixes"`
IPv6Prefixes []IPv6Prefix `json:"ipv6_prefixes" yaml:"ipv6_prefixes"`
}
// overwritten at build time
var (
version, versionOutput, tag, sha, buildDate string
msg string
rCode int
)
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func main() {
display, err := startCLI(os.Args)
if err != nil {
fmt.Printf("error: %+v\n", err)
os.Exit(1)
}
if display && msg != "" {
fmt.Print(msg)
}
os.Exit(rCode)
}
func startCLI(args []string) (display bool, err error) {
app := cli.NewApp()
app.EnableBashCompletion = true
app.Name = "aws-ips"
app.Version = versionOutput
app.Compiled = time.Now()
app.Authors = []cli.Author{
{
Name: "Jon Hadfield",
Email: "[email protected]",
},
}
app.HelpName = "-"
app.Usage = "aws-ips"
app.Description = ""
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "region, r",
Usage: "AWS region",
},
cli.StringFlag{
Name: "service, s",
Usage: "AWS service",
},
cli.StringFlag{
Name: "ip",
Usage: "find prefix by ip",
},
cli.StringFlag{
Name: "name, n",
Usage: "find prefix by name",
},
cli.BoolFlag{
Name: "4only, 4",
Usage: "only output IPv4",
},
cli.BoolFlag{
Name: "6only, 6",
Usage: "only output IPv6",
},
cli.BoolFlag{
Name: "no-amazon, na",
Usage: "exclude matches with service AMAZON",
},
cli.StringFlag{
Name: "fields, f",
Value: "all",
Usage: "cidr, region, service, all",
},
cli.StringFlag{
Name: "encoding, e",
Value: "text",
Usage: "output encoding (json, yaml, text)",
},
cli.StringFlag{
Name: "separator, sep",
Value: " | ",
Usage: "field separator for text encoding",
},
cli.StringFlag{
Name: "from-file, file",
Usage: "file downloaded from https://ip-ranges.amazonaws.com/ip-ranges.json",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "no stdout",
},
}
app.Action = func(c *cli.Context) error {
region := c.String("region")
service := c.String("service")
ip := c.String("ip")
name := c.String("name")
noAmazon := c.Bool("no-amazon")
encoding := c.String("encoding")
fields := c.String("fields")
textSeparator := c.String("separator")
if c.Bool("quiet") {
display = false
} else {
display = true
}
if ip != "" && name != "" {
_, _ = fmt.Fprintf(c.App.Writer, "error: ip cannot be used in combination with name.\n")
os.Exit(1)
}
if ip != "" {
if region != "" || service != "" {
_, _ = fmt.Fprintf(c.App.Writer, "error: ip cannot be used with region or service.\n")
os.Exit(1)
}
// check ip valid
if strings.HasSuffix(ip, "/32") {
ip = ip[:len(ip)-3]
}
if net.ParseIP(ip) == nil {
_, _ = fmt.Fprintf(c.App.Writer, "error: ip is invalid.\n")
os.Exit(1)
}
}
if name != "" {
if region != "" || service != "" {
_, _ = fmt.Fprintf(c.App.Writer, "error: name cannot be used with region or service.\n")
os.Exit(1)
}
}
for _, f := range strings.Split(fields, ",") {
if !stringInSlice(f, validFields) {
_, _ = fmt.Fprintf(c.App.Writer, "error: fields must be one of: %s.\n",
strings.Join(validFields, ", "))
os.Exit(1)
}
}
if encoding != "" && !stringInSlice(encoding, validEncodings) {
_, _ = fmt.Fprintf(c.App.Writer, "error: encoding must be one of: %s.\n",
strings.Join(validEncodings, ", "))
os.Exit(1)
}
loaded, err := loadRanges(c.String("from-file"))
if err != nil {
return err
}
outputDoc := loaded
switch {
case ip != "":
var lookupPrefixInput lookupPrefixInput
lookupPrefixInput.doc = loaded
lookupPrefixInput.ip = ip
var lookupOutput lookupPrefixOutput
lookupOutput, err = lookupPrefix(lookupPrefixInput)
if err != nil {
return err
}
outputDoc = lookupOutput.doc
// default to outputting all fields
if fields == "" {
fields = "all"
}
case name != "":
// Get IP for name
ips, err := net.LookupHost(name)
if err != nil {
_, _ = fmt.Fprintf(c.App.Writer, "error: failed to resolve: %s\n", name)
os.Exit(1)
}
ip = ips[0]
var lookupPrefixInput lookupPrefixInput
lookupPrefixInput.doc = loaded
lookupPrefixInput.ip = ip
lookupPrefixInput.noAmazon = noAmazon
var lookupOutput lookupPrefixOutput
lookupOutput, err = lookupPrefix(lookupPrefixInput)
if err != nil {
return err
}
outputDoc = lookupOutput.doc
// default to outputting all fields
if fields == "" {
fields = "all"
}
case service != "" || region != "":
var frInput filterRangesInput
frInput.Doc = loaded
frInput.region = region
frInput.service = service
frInput.noAmazon = noAmazon
var filteredOutput filterRangesOutput
filteredOutput = filterRanges(frInput)
outputDoc = filteredOutput.Doc
if err != nil {
return err
}
// default to outputting CIDR
if fields == "" {
fields = strCIDR
}
default:
// default to returning all, unfiltered
if fields == "" {
fields = strPrefix
}
}
if len(outputDoc.Prefixes) == 0 && len(outputDoc.IPv6Prefixes) == 0 {
rCode = 2
}
// only output text format if there are entries to output
if encoding == strText && (len(outputDoc.Prefixes) == 0 && len(outputDoc.IPv6Prefixes) == 0) {
return nil
}
msg = output(outputInput{
fourOnly: c.Bool("4only"),
sixOnly: c.Bool("6only"),
Doc: outputDoc,
encoding: encoding,
textSeparator: textSeparator,
fields: fields,
})
return nil
}
sort.Sort(cli.FlagsByName(app.Flags))
return display, app.Run(args)
}