Made error displaying optional, fixing cidr splitting (again)

This commit is contained in:
Dionysus 2023-09-21 15:16:54 -04:00
parent ee006d166d
commit c95e0f15f7
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
2 changed files with 25 additions and 15 deletions

BIN
massrdns Executable file

Binary file not shown.

View File

@ -16,6 +16,7 @@ import (
var dnsServers []string var dnsServers []string
var failureCounts = make(map[string]int) var failureCounts = make(map[string]int)
var showErrors bool
func loadDNSServersFromFile(filePath string) ([]string, error) { func loadDNSServersFromFile(filePath string) ([]string, error) {
var servers []string var servers []string
@ -61,7 +62,7 @@ func reverseDNSLookup(ip string, server string) (string, error) {
PreferGo: true, PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{} d := net.Dialer{}
return d.DialContext(ctx, network, server) return d.DialContext(ctx, "udp", server)
}, },
} }
@ -70,13 +71,13 @@ func reverseDNSLookup(ip string, server string) (string, error) {
if isNetworkError(err) { if isNetworkError(err) {
return "", err return "", err
} }
return fmt.Sprintf("%s | %s | Error: %s", time.Now().Format("03:04:05 PM"), server, err), nil return "", err
} }
if len(names) == 0 { if len(names) == 0 {
return fmt.Sprintf("%s | %s | No PTR records", time.Now().Format("03:04:05 PM"), server), nil return fmt.Sprintf("%s | %-18s | No PTR records", time.Now().Format("03:04:05 PM"), server), nil
} }
return fmt.Sprintf("%s | %s | %s", time.Now().Format("03:04:05 PM"), server, names[0]), nil return fmt.Sprintf("%s | %s | %-18s", time.Now().Format("03:04:05 PM"), server, names[0]), nil
} }
func isNetworkError(err error) bool { func isNetworkError(err error) bool {
@ -108,6 +109,8 @@ func splitCIDR(cidr string, parts int) ([]*net.IPNet, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
startIP := make(net.IP, len(ip))
copy(startIP, ip)
maskSize, _ := ipNet.Mask.Size() maskSize, _ := ipNet.Mask.Size()
@ -126,10 +129,11 @@ func splitCIDR(cidr string, parts int) ([]*net.IPNet, error) {
var subnets []*net.IPNet var subnets []*net.IPNet
for i := 0; i < parts; i++ { for i := 0; i < parts; i++ {
subnets = append(subnets, &net.IPNet{ subnets = append(subnets, &net.IPNet{
IP: ip, IP: make(net.IP, len(startIP)),
Mask: net.CIDRMask(newMaskSize, 32), Mask: net.CIDRMask(newMaskSize, 32),
}) })
incrementIPBy(ip, 1<<uint(32-newMaskSize)) copy(subnets[i].IP, startIP)
incrementIPBy(startIP, 1<<uint(32-newMaskSize))
} }
return subnets, nil return subnets, nil
@ -153,26 +157,31 @@ func worker(cidr *net.IPNet, resultsChan chan string) {
result, err := reverseDNSLookup(ip.String(), randomServer) result, err := reverseDNSLookup(ip.String(), randomServer)
// Check for network errors if err != nil {
if err != nil && isNetworkError(err) { if showErrors {
failureCounts[randomServer]++ resultsChan <- fmt.Sprintf("%s | %-18s | Error: %s", time.Now().Format("03:04:05 PM"), randomServer, err)
if failureCounts[randomServer] > 10 { }
dnsServers = removeFromList(dnsServers, randomServer)
delete(failureCounts, randomServer) if isNetworkError(err) {
failureCounts[randomServer]++
if failureCounts[randomServer] > 10 {
dnsServers = removeFromList(dnsServers, randomServer)
delete(failureCounts, randomServer)
}
} }
triedServers[randomServer] = true triedServers[randomServer] = true
retries-- retries--
continue continue
} else if err == nil { } else {
resultsChan <- result resultsChan <- result
success = true success = true
break break
} }
} }
if !success { if !success && showErrors {
resultsChan <- fmt.Sprintf("%s | %s | Max retries reached", time.Now().Format("03:04:05 PM"), ip) resultsChan <- fmt.Sprintf("%s | %-18s | Max retries reached", time.Now().Format("03:04:05 PM"), ip)
} }
} }
} }
@ -185,6 +194,7 @@ func main() {
flag.StringVar(&cidr, "cidr", "", "IP address CIDR to perform reverse DNS lookup") flag.StringVar(&cidr, "cidr", "", "IP address CIDR to perform reverse DNS lookup")
flag.IntVar(&concurrency, "concurrency", 10, "Number of concurrent workers for reverse DNS lookup") flag.IntVar(&concurrency, "concurrency", 10, "Number of concurrent workers for reverse DNS lookup")
flag.StringVar(&dnsFile, "dnsfile", "", "Path to the file containing DNS servers (one per line)") flag.StringVar(&dnsFile, "dnsfile", "", "Path to the file containing DNS servers (one per line)")
flag.BoolVar(&showErrors, "errors", false, "Display errors in the output") // New flag
flag.Parse() flag.Parse()
if cidr == "" || dnsFile == "" { if cidr == "" || dnsFile == "" {