Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Make sure the resolver is running before using this command.`,
}

fmt.Printf("Last %d DNS requests:\n\n", len(queries[start:]))
fmt.Printf("%-40s %-10s %-20s %s\n", "Domain", "Status", "Time", "Blocked")
fmt.Printf("%-40s %-27s %-10s %-20s %s\n", "Domain", "Client", "Status", "Time", "Blocked")
fmt.Println(string(make([]byte, 80)))

for _, query := range queries[start:] {
Expand All @@ -68,7 +68,13 @@ Make sure the resolver is running before using this command.`,
domain = domain[:35] + "..."
}

fmt.Printf("%-40s %-10s %-20s %s\n", domain, status, timeStr, blockedStr)
// Truncate hostname if too long
dnsClient := query.Client
if len(dnsClient) > 25 {
dnsClient = dnsClient[:22] + "..."
}

fmt.Printf("%-40s %-27s %-10s %-20s %s\n", domain, dnsClient, status, timeStr, blockedStr)
}

fmt.Printf("\nTotal queries: %d\n", len(queries))
Expand Down
1 change: 1 addition & 0 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type DNSQuery struct {
Client string `json:"client"`
Domain string `json:"domain"`
Timestamp time.Time `json:"timestamp"`
Blocked bool `json:"blocked"`
Expand Down
22 changes: 22 additions & 0 deletions internal/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"log"
"net"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -70,6 +71,25 @@ func NewServerWithPort(cfg *config.Config, apiServer *api.Server, port string) *
}
}

func resolveHost(addr string) string {
// Extract host part from "ip:port"
host, _, err := net.SplitHostPort(addr)
if err != nil {
// addr might just be an IP without port
host = addr
}

// Do reverse DNS lookup
names, err := net.LookupAddr(host)
if err != nil || len(names) == 0 {
return host // fallback to IP
}

// Remove trailing dot from hostname
hostname := strings.TrimSuffix(names[0], ".")
return hostname
}

// wildcardToRegex converts a wildcard pattern to a regex pattern
// Examples:
//
Expand Down Expand Up @@ -325,7 +345,9 @@ func (s *Server) handleRequest(w dns.ResponseWriter, r *dns.Msg) {

// Add to API server if available
if s.apiServer != nil {
clientHostname := resolveHost(w.RemoteAddr().String())
query := api.DNSQuery{
Client: clientHostname,
Domain: domain,
Timestamp: time.Now(),
Blocked: blocked,
Expand Down
16 changes: 11 additions & 5 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,8 @@ Make sure the resolver is running with 'sinkzone resolver'`
}

// Header
header := fmt.Sprintf("%-40s %-20s %-10s\n", "Domain", "Time", "Status")
header += strings.Repeat("-", 70) + "\n"
header := fmt.Sprintf("%-40s %-27s %-20s %-10s\n", "Domain", "Client", "Time", "Status")
header += strings.Repeat("-", 97) + "\n"

// Table rows
var rows []string
Expand All @@ -814,12 +814,18 @@ Make sure the resolver is running with 'sinkzone resolver'`
domain = domain[:35] + "..."
}

// Truncate hostname if too long
dnsClient := query.Client
if len(dnsClient) > 25 {
dnsClient = dnsClient[:22] + "..."
}

// Check if this row is selected
// Since we display newest first (reversed), map cursor position
isSelected := i == m.monitoring.tableCursor
recentlyChanged := query.Domain == m.lastChangedDomain && time.Since(m.lastChangeTime) < 2*time.Second

row := formatTableRow(domain, query.Timestamp, status, isSelected, recentlyChanged)
row := formatTableRow(domain, dnsClient, query.Timestamp, status, isSelected, recentlyChanged)
rows = append(rows, row)
}

Expand Down Expand Up @@ -903,8 +909,8 @@ func formatAllowlistRow(domain string, domainType string, status string, isSelec
return row
}

func formatTableRow(domain string, timestamp time.Time, status string, isSelected bool, recentlyChanged bool) string {
row := fmt.Sprintf("%-40s %-20s %-10s", domain, timestamp.Format("15:04:05"), status)
func formatTableRow(domain string, dnsClient string, timestamp time.Time, status string, isSelected bool, recentlyChanged bool) string {
row := fmt.Sprintf("%-40s %-27s %-20s %-10s", domain, dnsClient, timestamp.Format("15:04:05"), status)

if isSelected && recentlyChanged {
// Combined state: selected and recently changed - use a distinct color
Expand Down