Files
gists/projects/go-tools/goipgrep/internal/resolve/reverse.go
T
tobias 1cbf8afb4a chore: cleanup — untrack binaries, consolidate Go dirs, dedupe tools
- Untrack and delete compiled binaries (tarsum, gosoft.exe, rust uniq/uniq2);
  ignore build outputs (dist/, bin/, *.exe, *.test, .ruff_cache/)
- Merge tools/go/ and projects/go-tools/go/ into projects/go-tools/<name>/
- Fix goipgrep .gitignore: bare 'ipgrep' pattern was ignoring cmd/ipgrep/,
  so the main entrypoint was never tracked; now anchored to /ipgrep
- Archive duplicate implementations to archive/experimental/{rust,go}/
  (uniq, between, tarsum rewrites); canonical versions stay in tools/
- Update README tool catalog to match new layout

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 13:42:45 +02:00

75 lines
1.2 KiB
Go

package resolve
import (
"context"
"net"
"runtime"
"sort"
"strings"
"sync"
"time"
)
type ReverseResult struct {
IP string `json:"ip"`
Hostname string `json:"hostname,omitempty"`
}
func ReverseLookupAll(ctx context.Context, ips []string, timeout time.Duration, jobs int) []ReverseResult {
if jobs <= 0 {
jobs = runtime.GOMAXPROCS(0)
}
in := make(chan string)
out := make(chan ReverseResult)
var wg sync.WaitGroup
worker := func() {
defer wg.Done()
for ip := range in {
r := ReverseResult{IP: ip}
cctx, cancel := context.WithTimeout(ctx, timeout)
names, err := net.DefaultResolver.LookupAddr(cctx, ip)
cancel()
if err == nil && len(names) > 0 {
// trim trailing dot for readability
r.Hostname = strings.TrimSuffix(names[0], ".")
}
select {
case out <- r:
case <-ctx.Done():
return
}
}
}
wg.Add(jobs)
for i := 0; i < jobs; i++ {
go worker()
}
go func() {
defer close(in)
for _, ip := range ips {
select {
case in <- ip:
case <-ctx.Done():
return
}
}
}()
go func() {
wg.Wait()
close(out)
}()
var res []ReverseResult
for r := range out {
res = append(res, r)
}
sort.Slice(res, func(i, j int) bool { return res[i].IP < res[j].IP })
return res
}