Pull request: home: don't miss blocked clients in client search api

Merge in DNS/adguard-home from 2428-blocked-runtime-fix to master

Updates #2428.

Squashed commit of the following:

commit 8aaa3e22a894f0335ced93339655771989846c94
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Jan 15 16:32:53 2021 +0300

    home: don't miss blocked clients in client search api
This commit is contained in:
Ainar Garipov
2021-01-15 20:30:48 +03:00
parent 56cb8a4dde
commit 679bbcdc26
5 changed files with 75 additions and 17 deletions

View File

@@ -233,24 +233,22 @@ func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http
if len(ip) == 0 {
break
}
el := map[string]interface{}{}
c, ok := clients.Find(ip)
var cj clientJSON
if !ok {
ch, ok := clients.FindAutoClient(ip)
if !ok {
continue // a client with this IP isn't found
var found bool
cj, found = clients.findTemporary(ip)
if !found {
continue
}
cj := clientHostToJSON(ip, ch)
cj.Disallowed, cj.DisallowedRule = clients.dnsServer.IsBlockedIP(ip)
el[ip] = cj
} else {
cj := clientToJSON(&c)
cj = clientToJSON(&c)
cj.Disallowed, cj.DisallowedRule = clients.dnsServer.IsBlockedIP(ip)
el[ip] = cj
}
el[ip] = cj
data = append(data, el)
}
@@ -267,6 +265,36 @@ func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http
}
}
// findTemporary looks up the IP in temporary storages, like autohosts or
// blocklists.
func (clients *clientsContainer) findTemporary(ip string) (cj clientJSON, found bool) {
ch, ok := clients.FindAutoClient(ip)
if !ok {
// It is still possible that the IP used to be in the runtime
// clients list, but then the server was reloaded. So, check
// the DNS server's blocked IP list.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2428.
disallowed, rule := clients.dnsServer.IsBlockedIP(ip)
if rule == "" {
return clientJSON{}, false
}
cj = clientJSON{
IDs: []string{ip},
Disallowed: disallowed,
DisallowedRule: rule,
}
return cj, true
}
cj = clientHostToJSON(ip, ch)
cj.Disallowed, cj.DisallowedRule = clients.dnsServer.IsBlockedIP(ip)
return cj, true
}
// RegisterClientsHandlers registers HTTP handlers
func (clients *clientsContainer) registerWebHandlers() {
httpRegister("GET", "/control/clients", clients.handleGetClients)