Skip to content

Commit

Permalink
Merge pull request #63 from projectdiscovery/issue-62-rev-ptr
Browse files Browse the repository at this point in the history
Adding reverse PTR helper
  • Loading branch information
Mzack9999 authored Feb 9, 2023
2 parents 49ee8d1 + 4e83c22 commit 1cba687
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ip/iputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,23 @@ func GetBindableAddress(port int, ips ...string) (string, error) {

return "", errs
}

// ToFQDN performs a reverse PTR using default system resolvers
func ToFQDN(target string) ([]string, error) {
if !IsIP(target) {
return []string{target}, fmt.Errorf("%s is not an IP", target)
}
names, err := net.LookupAddr(target)
if err != nil {
return nil, err
}
if len(names) == 0 {
return names, fmt.Errorf("no names found for ip: %s", target)
}

for i, name := range names {
names[i] = stringsutil.TrimSuffixAny(name, ".")
}

return names, nil
}
15 changes: 15 additions & 0 deletions ip/iputil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,18 @@ func TestAsIPV6IpNet(t *testing.T) {
}
}
}

func TestWhatsMyIP(t *testing.T) {
// we can't compare the ip with local interfaces as it might be the external gateway one
// so we just verify we can contact the api endpoint
_, err := WhatsMyIP()
require.Nil(t, err, "couldn't retrieve ip")
}

func TestToFQDN(t *testing.T) {
// we can't compare the ip with local interfaces as it might be the external gateway one
// so we just verify we can contact the api endpoint
fqdns, err := ToFQDN("1.1.1.1")
require.Nil(t, err, "couldn't retrieve ip")
require.NotNil(t, fqdns)
}

0 comments on commit 1cba687

Please sign in to comment.