goipgrep: refactor into module; pure-Go ping/resolve; cache+CI; drop binary

This commit is contained in:
tobias
2026-02-17 09:26:30 +01:00
parent 27760b0bf1
commit a931be4707
20 changed files with 1214 additions and 376 deletions

View File

@@ -0,0 +1,75 @@
//go:build linux
package resolve
import (
"bufio"
"io"
"os"
"strings"
"git.ktf.ninja/tabledevil/gists/projects/go-tools/go/goipgrep/internal/normalize"
)
type NeighborTable struct {
ByMAC map[string][]string
}
func LoadNeighborTable() (*NeighborTable, error) {
f, err := os.Open("/proc/net/arp")
if err != nil {
return nil, err
}
defer f.Close()
return parseProcNetARP(f), nil
}
func ResolveMACs(macs []string) ([]MACResolution, error) {
tab, err := LoadNeighborTable()
if err != nil {
return nil, err
}
var res []MACResolution
for _, mac := range macs {
res = append(res, MACResolution{MAC: mac, IPs: append([]string(nil), tab.ByMAC[mac]...)})
}
return res, nil
}
type MACResolution struct {
MAC string `json:"mac"`
IPs []string `json:"ips,omitempty"`
}
func parseProcNetARP(r io.Reader) *NeighborTable {
nt := &NeighborTable{ByMAC: make(map[string][]string)}
sc := bufio.NewScanner(r)
first := true
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if line == "" {
continue
}
if first {
// header
first = false
continue
}
fields := strings.Fields(line)
if len(fields) < 4 {
continue
}
ip := fields[0]
macRaw := fields[3]
mac, ok := normalize.NormalizeMAC(macRaw)
if !ok {
continue
}
// Skip incomplete/placeholder entries.
if mac == "00:00:00:00:00:00" {
continue
}
nt.ByMAC[mac] = append(nt.ByMAC[mac], ip)
}
return nt
}

View File

@@ -0,0 +1,23 @@
//go:build !linux
package resolve
import "fmt"
type NeighborTable struct {
ByMAC map[string][]string
}
type MACResolution struct {
MAC string `json:"mac"`
IPs []string `json:"ips,omitempty"`
}
func LoadNeighborTable() (*NeighborTable, error) {
return nil, fmt.Errorf("MAC neighbor/ARP resolution is only supported on Linux without external tools")
}
func ResolveMACs(macs []string) ([]MACResolution, error) {
_ = macs
return nil, fmt.Errorf("MAC resolution is only supported on Linux without external tools")
}

View File

@@ -0,0 +1,74 @@
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
}