Skip to content

Commit

Permalink
enable checking by name.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Feb 5, 2019
1 parent f845920 commit 4f9ec4d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
13 changes: 10 additions & 3 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ func filterRanges(input filterRangesInput) filterRangesOutput {
output.Doc.SyncToken = input.Doc.SyncToken
output.Doc.CreateDate = input.Doc.CreateDate
for _, r := range input.Doc.Prefixes {
if input.noAmazon && strings.ToLower(r.Service) == "amazon" {
continue
}
var match bool
if input.region != "" {
if strings.EqualFold(r.Region, input.region) {
Expand All @@ -27,6 +30,9 @@ func filterRanges(input filterRangesInput) filterRangesOutput {
}
}
for _, ip6r := range input.Doc.IPv6Prefixes {
if input.noAmazon && strings.ToLower(ip6r.Service) == "amazon" {
continue
}
var match bool
if input.region != "" {
if strings.EqualFold(ip6r.Region, input.region) {
Expand All @@ -51,9 +57,10 @@ func filterRanges(input filterRangesInput) filterRangesOutput {
}

type filterRangesInput struct {
Doc IPRangeDoc
region string
service string
Doc IPRangeDoc
region string
service string
noAmazon bool
}

type filterRangesOutput struct {
Expand Down
11 changes: 9 additions & 2 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ func lookupPrefix(input lookupPrefixInput) (output lookupPrefixOutput, err error
if netIP != nil {
if strings.Contains(netIP.String(), ":") {
for _, r := range input.doc.IPv6Prefixes {
if input.noAmazon && strings.ToLower(r.Service) == "amazon" {
continue
}
var netPrefix *net.IPNet
_, netPrefix, err = net.ParseCIDR(r.IPv6Prefix)
if netPrefix.Contains(netIP) {
Expand All @@ -20,6 +23,9 @@ func lookupPrefix(input lookupPrefixInput) (output lookupPrefixOutput, err error
}
} else {
for _, r := range input.doc.Prefixes {
if input.noAmazon && strings.ToLower(r.Service) == "amazon" {
continue
}
var netPrefix *net.IPNet
_, netPrefix, err = net.ParseCIDR(r.IPPrefix)
if netPrefix.Contains(netIP) {
Expand All @@ -33,8 +39,9 @@ func lookupPrefix(input lookupPrefixInput) (output lookupPrefixOutput, err error
}

type lookupPrefixInput struct {
doc IPRangeDoc
ip string
doc IPRangeDoc
ip string
noAmazon bool
}

type lookupPrefixOutput struct {
Expand Down
44 changes: 44 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func startCLI(args []string) (msg string, display bool, err error) {
Name: "ip",
Usage: "find prefix by ip",
},
cli.StringFlag{
Name: "name",
Usage: "find prefix by name",
},
cli.BoolFlag{
Name: "4only",
Usage: "only output IPv4",
Expand All @@ -106,6 +110,10 @@ func startCLI(args []string) (msg string, display bool, err error) {
Name: "6only",
Usage: "only output IPv6",
},
cli.BoolFlag{
Name: "no-amazon",
Usage: "exclude matches with service AMAZON",
},
cli.StringFlag{
Name: "fields",
Value: "all",
Expand Down Expand Up @@ -135,6 +143,8 @@ func startCLI(args []string) (msg string, display bool, err 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")
Expand All @@ -143,6 +153,10 @@ func startCLI(args []string) (msg string, display bool, err error) {
} 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")
Expand All @@ -157,6 +171,13 @@ func startCLI(args []string) (msg string, display bool, err error) {
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)
}
// TODO: check name is valid (format and lookup)
}

for _, f := range strings.Split(fields, ",") {
if !stringInSlice(f, validFields) {
Expand Down Expand Up @@ -192,11 +213,34 @@ func startCLI(args []string) (msg string, display bool, err error) {
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
Expand Down

0 comments on commit 4f9ec4d

Please sign in to comment.