From fc033fdbfae1931c0c9fdb570da979406639248d Mon Sep 17 00:00:00 2001 From: root Date: Mon, 14 Mar 2022 15:06:19 +0000 Subject: [PATCH] fix: properly reverse ip address --- backend/api_v1/rdns.go | 3 ++- backend/dns/dns_resolve.go | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/backend/api_v1/rdns.go b/backend/api_v1/rdns.go index 10ede28..ff74b18 100644 --- a/backend/api_v1/rdns.go +++ b/backend/api_v1/rdns.go @@ -54,8 +54,9 @@ func rdns(g group, log *zap.Logger, dnsServer string) { return } + ipAddr := net.ParseIP(ip) result, err := dnsLib.LookupRDNS( - log, ip, dnsServer, + log, ipAddr, dnsServer, ) if err != nil { ctx.Error(&gin.Error{ diff --git a/backend/dns/dns_resolve.go b/backend/dns/dns_resolve.go index 437eab2..ae7fee6 100644 --- a/backend/dns/dns_resolve.go +++ b/backend/dns/dns_resolve.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math/rand" + "net" "reflect" "strings" "sync" @@ -427,16 +428,20 @@ func Lookup(log *zap.Logger, dnsServer, recordType, hostname string, fullTrace b return recursiveQuery(log, dnsServer, recordType, hostname) } -func reverseString(s string) string { - chars := []rune(s) - for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 { - chars[i], chars[j] = chars[j], chars[i] +func reverseIP(ip net.IP) string { + addressParts := strings.Split(ip.String(), ".") + reversed := []string{} + + for i := len(addressParts) - 1; i >= 0; i-- { + octet := addressParts[i] + reversed = append(reversed, octet) } - return string(chars) + + return strings.Join(reversed, ".") } -func LookupRDNS(log *zap.Logger, ip, dnsServer string) (RecordType, error) { - hostname := reverseString(ip) + ".in-addr.arpa." +func LookupRDNS(log *zap.Logger, ip net.IP, dnsServer string) (RecordType, error) { + hostname := reverseIP(ip) + ".in-addr.arpa." resp, err := traceQuery(log, dnsServer, "PTR", hostname) if err != nil { return nil, err