Skip to content

Commit

Permalink
Merge pull request #4 from jamesbcook/parse-dash-network
Browse files Browse the repository at this point in the history
Add the ability to parse dashed networks
  • Loading branch information
tomsteele authored May 2, 2017
2 parents 076efd1 + 03cd7d6 commit 74053a5
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"strconv"
"strings"
"time"

"github.com/docopt/docopt-go"
Expand Down Expand Up @@ -48,7 +49,7 @@ type O struct {
}

func parse() *O {
args, err := docopt.Parse(usage, nil, true, "cookiescan 2.2.0", false)
args, err := docopt.Parse(usage, nil, true, "cookiescan 2.3.0", false)
if err != nil {
log.Fatalf("Error parsing usage. Error: %s\n", err.Error())
}
Expand Down Expand Up @@ -123,7 +124,7 @@ func parse() *O {
return o
}

// linesToIPList processes a list of IP addresses or networks in CIDR format.
// linesToIPList processes a list of IP addresses or networks in CIDR or dash format.
// Returning a list of all possible IP addresses.
func linesToIPList(lines []string) ([]string, error) {
ipList := []string{}
Expand All @@ -134,13 +135,39 @@ func linesToIPList(lines []string) ([]string, error) {
for ip := ip.Mask(network.Mask); network.Contains(ip); increaseIP(ip) {
ipList = append(ipList, ip.String())
}
} else if strings.Contains(line, "-") {
splitIP := strings.SplitN(line, "-", 2)
ip := net.ParseIP(splitIP[0])
endIP := net.ParseIP(splitIP[1])
if endIP != nil {
if !isStartingIPLower(ip, endIP) {
return ipList, fmt.Errorf("%s is greater than %s", ip.String(), endIP.String())
}
ipList = append(ipList, ip.String())
for !ip.Equal(endIP) {
increaseIP(ip)
ipList = append(ipList, ip.String())
}
} else {
ipOct := strings.SplitN(ip.String(), ".", 4)
endIP := net.ParseIP(ipOct[0] + "." + ipOct[1] + "." + ipOct[2] + "." + splitIP[1])
if endIP != nil {
if !isStartingIPLower(ip, endIP) {
return ipList, fmt.Errorf("%s is greater than %s", ip.String(), endIP.String())
}
ipList = append(ipList, ip.String())
for !ip.Equal(endIP) {
increaseIP(ip)
ipList = append(ipList, ip.String())
}
} else {
return ipList, fmt.Errorf("%s is not an IP Address or CIDR Network", line)
}
}
} else if ip, err := net.LookupIP(line); err == nil {
ipList = append(ipList, ip[0].String())
} else {
return ipList, fmt.Errorf("%s is not an IP Address or CIDR Network", line)
ips, err := net.LookupIP(line)
if err != nil {
return ipList, fmt.Errorf("%s is not a valid hostname", line)
}
ipList = append(ipList, ips[0].String())
}
}
return ipList, nil
Expand All @@ -156,6 +183,18 @@ func increaseIP(ip net.IP) {
}
}

func isStartingIPLower(start, end net.IP) bool {
if len(start) != len(end) {
return false
}
for i := range start {
if start[i] > end[i] {
return false
}
}
return true
}

// readFileLines returns all the lines in a file.
func readFileLines(path string) ([]string, error) {
file, err := os.Open(path)
Expand Down

0 comments on commit 74053a5

Please sign in to comment.