Skip to content

Commit

Permalink
all: imp log msg
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Nov 30, 2023
1 parent ce0a945 commit e09f8a0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
36 changes: 28 additions & 8 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (cs Source) MarshalText() (text []byte, err error) {

// Runtime is a client information from different sources.
type Runtime struct {
// WHOIS is the filtered WHOIS information of a client.
WHOIS *whois.Info
// whois is the filtered WHOIS information of a client.
whois *whois.Info

// arp is the ARP information of a client.
arp []string
Expand All @@ -74,7 +74,7 @@ type Runtime struct {
hostsFile []string
}

// Info returns client information with highest priority.
// Info returns a client information with highest priority.
func (r *Runtime) Info() (cs Source, host string) {
info := []string{}

Expand All @@ -92,7 +92,7 @@ func (r *Runtime) Info() (cs Source, host string) {
return cs, strings.Join(info, " ")
}

// SetInfo sets client information. info must be non-nil.
// SetInfo sets a client information. info must be non-nil.
func (r *Runtime) SetInfo(cs Source, info []string) {
switch cs {
case SourceARP:
Expand All @@ -106,11 +106,31 @@ func (r *Runtime) SetInfo(cs Source, info []string) {
}
}

// Clear clears a cs information.
func (r *Runtime) Clear(cs Source) {
// WHOIS returns a WHOIS client information.
func (r *Runtime) WHOIS() (info *whois.Info) {
return r.whois
}

// WHOISOrEmpty returns a WHOIS client information or a pointer to an empty
// struct. Frontend expects non-nil value.
func (r *Runtime) WHOISOrEmpty() (info *whois.Info) {
if r.whois != nil {
return r.whois
}

return &whois.Info{}
}

// SetWHOIS sets a WHOIS client information. info must be non-nil.
func (r *Runtime) SetWHOIS(info *whois.Info) {
r.whois = info
}

// Unset clears a cs information.
func (r *Runtime) Unset(cs Source) {
switch cs {
case SourceWHOIS:
r.WHOIS = nil
r.whois = nil
case SourceARP:
r.arp = nil
case SourceRDNS:
Expand All @@ -124,7 +144,7 @@ func (r *Runtime) Clear(cs Source) {

// IsEmpty returns true if there is no information from any source.
func (r *Runtime) IsEmpty() (ok bool) {
if r.WHOIS == nil && r.arp == nil && r.rdns == nil && r.dhcp == nil && r.hostsFile == nil {
if r.whois == nil && r.arp == nil && r.rdns == nil && r.dhcp == nil && r.hostsFile == nil {
return true
}

Expand Down
22 changes: 9 additions & 13 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func (clients *clientsContainer) Init(
log.Fatal("clients.list != nil")
}

clients.list = make(map[string]*Client)
clients.idIndex = make(map[string]*Client)
clients.ipToRC = make(map[netip.Addr]*client.Runtime)
clients.list = map[string]*Client{}
clients.idIndex = map[string]*Client{}
clients.ipToRC = map[netip.Addr]*client.Runtime{}

clients.allTags = stringutil.NewSet(clientTags...)

Expand Down Expand Up @@ -400,7 +400,7 @@ func (clients *clientsContainer) clientOrArtificial(

return &querylog.Client{
Name: host,
WHOIS: rc.WHOIS,
WHOIS: rc.WHOIS(),
}, false
}

Expand Down Expand Up @@ -576,9 +576,7 @@ func (clients *clientsContainer) findRuntimeClient(ip netip.Addr) (rc *client.Ru
}

if !ok {
rc = &client.Runtime{
WHOIS: &whois.Info{},
}
rc = &client.Runtime{}
}

rc.SetInfo(client.SourceDHCP, []string{host})
Expand Down Expand Up @@ -789,7 +787,7 @@ func (clients *clientsContainer) setWHOISInfo(ip netip.Addr, wi *whois.Info) {
log.Debug("clients: set whois info for runtime client %s: %+v", host, wi)
}

rc.WHOIS = wi
rc.SetWHOIS(wi)
}

// addHost adds a new IP-hostname pairing. The priorities of the sources are
Expand Down Expand Up @@ -848,15 +846,13 @@ func (clients *clientsContainer) addHostLocked(
}
}

rc = &client.Runtime{
WHOIS: &whois.Info{},
}
rc = &client.Runtime{}
clients.ipToRC[ip] = rc
}

rc.SetInfo(src, []string{host})

log.Debug("clients: added %s -> %q [%d]", ip, host, len(clients.ipToRC))
log.Debug("clients: adding client info %s -> %q %q [%d]", ip, src, host, len(clients.ipToRC))

return true
}
Expand All @@ -865,7 +861,7 @@ func (clients *clientsContainer) addHostLocked(
func (clients *clientsContainer) rmHostsBySrc(src client.Source) {
n := 0
for ip, rc := range clients.ipToRC {
rc.Clear(src)
rc.Unset(src)
if rc.IsEmpty() {
delete(clients.ipToRC, ip)
n++
Expand Down
4 changes: 2 additions & 2 deletions internal/home/clients_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestClientsWHOIS(t *testing.T) {
rc := clients.ipToRC[ip]
require.NotNil(t, rc)

assert.Equal(t, rc.WHOIS, whois)
assert.Equal(t, rc.WHOIS(), whois)
})

t.Run("existing_auto-client", func(t *testing.T) {
Expand All @@ -249,7 +249,7 @@ func TestClientsWHOIS(t *testing.T) {
rc := clients.ipToRC[ip]
require.NotNil(t, rc)

assert.Equal(t, rc.WHOIS, whois)
assert.Equal(t, rc.WHOIS(), whois)
})

t.Run("can't_set_manually-added", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/home/clientshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http
for ip, rc := range clients.ipToRC {
src, host := rc.Info()
cj := runtimeClientJSON{
WHOIS: rc.WHOIS,
WHOIS: rc.WHOISOrEmpty(),
Name: host,
Source: src,
IP: ip,
Expand Down Expand Up @@ -413,7 +413,7 @@ func (clients *clientsContainer) findRuntime(ip netip.Addr, idStr string) (cj *c
cj = &clientJSON{
Name: host,
IDs: []string{idStr},
WHOIS: rc.WHOIS,
WHOIS: rc.WHOISOrEmpty(),
}

disallowed, rule := clients.dnsServer.IsBlockedClient(ip, idStr)
Expand Down

0 comments on commit e09f8a0

Please sign in to comment.