Skip to content

Commit

Permalink
home: accept ids in clients find
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Jan 26, 2021
1 parent 5b72595 commit f79876e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
18 changes: 9 additions & 9 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,25 @@ func (clients *clientsContainer) FindUpstreams(ip string) *proxy.UpstreamConfig
}

// findLocked searches for a client by its ID. For internal use only.
func (clients *clientsContainer) findLocked(ip string) (c *Client, ok bool) {
ipAddr := net.ParseIP(ip)
if ipAddr == nil {
return nil, false
}

c, ok = clients.idIndex[ip]
func (clients *clientsContainer) findLocked(id string) (c *Client, ok bool) {
c, ok = clients.idIndex[id]
if ok {
return c, true
}

ip := net.ParseIP(id)
if ip == nil {
return nil, false
}

for _, c = range clients.list {
for _, id := range c.IDs {
_, ipnet, err := net.ParseCIDR(id)
if err != nil {
continue
}

if ipnet.Contains(ipAddr) {
if ipnet.Contains(ip) {
return c, true
}
}
Expand All @@ -333,7 +333,7 @@ func (clients *clientsContainer) findLocked(ip string) (c *Client, ok bool) {
return nil, false
}

macFound := clients.dhcpServer.FindMACbyIP(ipAddr)
macFound := clients.dhcpServer.FindMACbyIP(ip)
if macFound == nil {
return nil, false
}
Expand Down
28 changes: 15 additions & 13 deletions internal/home/clientshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,18 @@ func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *ht
func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
data := []map[string]clientJSON{}
for i := 0; ; i++ {
ipStr := q.Get(fmt.Sprintf("ip%d", i))
ip := net.ParseIP(ipStr)
if ip == nil {
for i := 0; i < len(q); i++ {
idStr := q.Get(fmt.Sprintf("ip%d", i))
if idStr == "" {
break
}

c, ok := clients.Find(ipStr)
ip := net.ParseIP(idStr)
c, ok := clients.Find(idStr)
var cj clientJSON
if !ok {
var found bool
cj, found = clients.findTemporary(ip, ipStr)
cj, found = clients.findTemporary(ip, idStr)
if !found {
continue
}
Expand All @@ -250,7 +250,7 @@ func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http
}

data = append(data, map[string]clientJSON{
ipStr: cj,
idStr: cj,
})
}

Expand All @@ -263,9 +263,9 @@ 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 net.IP, ipStr string) (cj clientJSON, found bool) {
ch, ok := clients.FindAutoClient(ipStr)
if !ok {
func (clients *clientsContainer) findTemporary(ip net.IP, idStr string) (cj clientJSON, found bool) {
ch, ok := clients.FindAutoClient(idStr)
if !ok && ip != nil {
// 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.
Expand All @@ -277,16 +277,18 @@ func (clients *clientsContainer) findTemporary(ip net.IP, ipStr string) (cj clie
}

cj = clientJSON{
IDs: []string{ipStr},
IDs: []string{idStr},
Disallowed: disallowed,
DisallowedRule: rule,
}

return cj, true
}

cj = clientHostToJSON(ipStr, ch)
cj.Disallowed, cj.DisallowedRule = clients.dnsServer.IsBlockedIP(ip)
cj = clientHostToJSON(idStr, ch)
if ip != nil {
cj.Disallowed, cj.DisallowedRule = clients.dnsServer.IsBlockedIP(ip)
}

return cj, true
}
Expand Down

0 comments on commit f79876e

Please sign in to comment.