Fixed -debug output showing the error code and not the reason

This commit is contained in:
Dionysus 2025-01-05 04:48:27 -05:00
parent d7fc56cb79
commit ce0d30666b
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

View File

@ -151,20 +151,41 @@ type DNSResponse struct {
TTL uint32 // Add TTL field TTL uint32 // Add TTL field
} }
func lookupWithRetry(ip string, cfg *Config) (DNSResponse, error) { func translateRcode(rcode int) string {
switch rcode {
case dns.RcodeSuccess:
return "Success"
case dns.RcodeFormatError:
return "Format Error"
case dns.RcodeServerFailure:
return "Server Failure"
case dns.RcodeNameError: // NXDOMAIN
return "No Such Domain"
case dns.RcodeNotImplemented:
return "Not Implemented"
case dns.RcodeRefused:
return "Query Refused"
default:
return fmt.Sprintf("DNS Error %d", rcode)
}
}
func lookupWithRetry(ip string, cfg *Config) (DNSResponse, string, error) {
var lastErr error var lastErr error
var lastServer string
for i := 0; i < cfg.retries; i++ { for i := 0; i < cfg.retries; i++ {
server := cfg.getNextServer() server := cfg.getNextServer()
if server == "" { if server == "" {
return DNSResponse{}, fmt.Errorf("no DNS servers available") return DNSResponse{}, "", fmt.Errorf("no DNS servers available")
} }
lastServer = server
// Create DNS message // Create DNS message
m := new(dns.Msg) m := new(dns.Msg)
arpa, err := dns.ReverseAddr(ip) arpa, err := dns.ReverseAddr(ip)
if err != nil { if err != nil {
return DNSResponse{}, err return DNSResponse{}, "", err
} }
m.SetQuestion(arpa, dns.TypePTR) m.SetQuestion(arpa, dns.TypePTR)
m.RecursionDesired = true m.RecursionDesired = true
@ -181,15 +202,10 @@ func lookupWithRetry(ip string, cfg *Config) (DNSResponse, error) {
} }
if r.Rcode != dns.RcodeSuccess { if r.Rcode != dns.RcodeSuccess {
lastErr = fmt.Errorf("DNS query failed with code: %d", r.Rcode) lastErr = fmt.Errorf("%s", translateRcode(r.Rcode))
continue continue
} }
logServer := server
if idx := strings.Index(server, ":"); idx != -1 {
logServer = server[:idx]
}
// Process the response // Process the response
if len(r.Answer) > 0 { if len(r.Answer) > 0 {
var names []string var names []string
@ -214,25 +230,25 @@ func lookupWithRetry(ip string, cfg *Config) (DNSResponse, error) {
if isCNAME { if isCNAME {
return DNSResponse{ return DNSResponse{
Names: names, Names: names,
Server: logServer, Server: server,
RecordType: "CNAME", RecordType: "CNAME",
Target: strings.TrimSuffix(target, "."), Target: strings.TrimSuffix(target, "."),
TTL: ttl, TTL: ttl,
}, nil }, server, nil
} }
return DNSResponse{ return DNSResponse{
Names: names, Names: names,
Server: logServer, Server: server,
RecordType: "PTR", RecordType: "PTR",
TTL: ttl, TTL: ttl,
}, nil }, server, nil
} }
} }
lastErr = fmt.Errorf("no PTR records found") lastErr = fmt.Errorf("no PTR records found")
} }
return DNSResponse{}, lastErr return DNSResponse{}, lastServer, lastErr
} }
func reverse(ss []string) []string { func reverse(ss []string) []string {
@ -324,9 +340,13 @@ func worker(jobs <-chan string, wg *sync.WaitGroup, cfg *Config, stats *Stats, t
timestamp := time.Now() timestamp := time.Now()
var response DNSResponse var response DNSResponse
var err error var err error
var server string
if len(cfg.dnsServers) > 0 { if len(cfg.dnsServers) > 0 {
response, err = lookupWithRetry(ip, cfg) response, server, err = lookupWithRetry(ip, cfg)
if idx := strings.Index(server, ":"); idx != -1 {
server = server[:idx]
}
} else { } else {
names, err := net.LookupAddr(ip) names, err := net.LookupAddr(ip)
if err == nil { if err == nil {
@ -339,17 +359,19 @@ func worker(jobs <-chan string, wg *sync.WaitGroup, cfg *Config, stats *Stats, t
if err != nil { if err != nil {
stats.incrementFailed() stats.incrementFailed()
if cfg.debug { if cfg.debug {
timestamp := time.Now().Format("2006-01-02 15:04:05")
errMsg := err.Error() errMsg := err.Error()
if idx := strings.LastIndex(errMsg, ": "); idx != -1 { if idx := strings.LastIndex(errMsg, ": "); idx != -1 {
errMsg = errMsg[idx+2:] errMsg = errMsg[idx+2:]
} }
debugLine := fmt.Sprintf("[gray]%s[-] [purple]%15s[-] [gray]│[-] [red]%s[-]\n", timeStr := time.Now().Format("2006-01-02 15:04:05")
timestamp, line := fmt.Sprintf("[gray]%s [gray]│[-] [purple]%15s[-] [gray]│[-] [aqua]%-15s[-] [gray]│[-] [red] ERR [-] [gray]│[-] [gray]%-6s[-] [gray]│[-] [gray]%s[-]\n",
timeStr,
ip, ip,
server,
"",
errMsg) errMsg)
app.QueueUpdateDraw(func() { app.QueueUpdateDraw(func() {
fmt.Fprint(textView, debugLine) fmt.Fprint(textView, line)
textView.ScrollToEnd() textView.ScrollToEnd()
}) })
} }
@ -359,12 +381,14 @@ func worker(jobs <-chan string, wg *sync.WaitGroup, cfg *Config, stats *Stats, t
if len(response.Names) == 0 { if len(response.Names) == 0 {
stats.incrementFailed() stats.incrementFailed()
if cfg.debug { if cfg.debug {
timestamp := time.Now().Format("2006-01-02 15:04:05") timeStr := time.Now().Format("2006-01-02 15:04:05")
debugLine := fmt.Sprintf("[gray]%s[-] [purple]%15s[-] [gray]│[-] [red]No PTR record[-]\n", line := fmt.Sprintf("[gray]%s [gray]│[-] [purple]%15s[-] [gray]│[-] [aqua]%-15s[-] [gray]│[-] [red] ERR [-] [gray]│[-] [gray]%-6s[-] [gray]│[-] [red]No PTR record[-]\n",
timestamp, timeStr,
ip) ip,
server,
"")
app.QueueUpdateDraw(func() { app.QueueUpdateDraw(func() {
fmt.Fprint(textView, debugLine) fmt.Fprint(textView, line)
textView.ScrollToEnd() textView.ScrollToEnd()
}) })
} }
@ -385,7 +409,7 @@ func worker(jobs <-chan string, wg *sync.WaitGroup, cfg *Config, stats *Stats, t
continue continue
} }
writeNDJSON(cfg, timestamp, ip, response.Server, ptr, response.RecordType, response.Target, response.TTL) writeNDJSON(cfg, timestamp, ip, server, ptr, response.RecordType, response.Target, response.TTL)
timeStr := time.Now().Format("2006-01-02 15:04:05") timeStr := time.Now().Format("2006-01-02 15:04:05")
recordTypeColor := "[blue] PTR [-]" recordTypeColor := "[blue] PTR [-]"
@ -400,7 +424,7 @@ func worker(jobs <-chan string, wg *sync.WaitGroup, cfg *Config, stats *Stats, t
line = fmt.Sprintf("[gray]%s [gray]│[-] [purple]%15s[-] [gray]│[-] [aqua]%-15s[-] [gray]│[-] %-5s [gray]│[-] %s [gray]│[-] %s\n", line = fmt.Sprintf("[gray]%s [gray]│[-] [purple]%15s[-] [gray]│[-] [aqua]%-15s[-] [gray]│[-] %-5s [gray]│[-] %s [gray]│[-] %s\n",
timeStr, timeStr,
ip, ip,
response.Server, server,
recordTypeColor, recordTypeColor,
colorizeTTL(response.TTL), colorizeTTL(response.TTL),
colorizeIPInPtr(ptr, ip)) colorizeIPInPtr(ptr, ip))