all: imp log msg

This commit is contained in:
Stanislav Chzhen 2023-11-30 14:46:53 +03:00
parent ce0a9454df
commit e09f8a0bd9
4 changed files with 41 additions and 25 deletions

View File

@ -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
@ -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{}
@ -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:
@ -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:
@ -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
}

View File

@ -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...)
@ -400,7 +400,7 @@ func (clients *clientsContainer) clientOrArtificial(
return &querylog.Client{
Name: host,
WHOIS: rc.WHOIS,
WHOIS: rc.WHOIS(),
}, false
}
@ -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})
@ -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
@ -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
}
@ -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++

View File

@ -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) {
@ -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) {

View File

@ -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,
@ -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)