From dbf990eb881116754554270e7b691b5db8e9ee34 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Fri, 2 Apr 2021 12:56:13 +0300 Subject: [PATCH] home: imp names --- internal/home/clients.go | 78 ++++++++++++++++++----------------- internal/home/clients_test.go | 10 ++--- internal/home/clientshttp.go | 38 ++++++++--------- internal/home/rdns_test.go | 4 +- internal/home/whois.go | 4 +- 5 files changed, 68 insertions(+), 66 deletions(-) diff --git a/internal/home/clients.go b/internal/home/clients.go index 92efc0befcb..ad119b002f1 100644 --- a/internal/home/clients.go +++ b/internal/home/clients.go @@ -61,8 +61,8 @@ const ( ClientSourceHostsFile ) -// ClientHost information -type ClientHost struct { +// RuntimeClient information +type RuntimeClient struct { Host string Source clientSource WhoisInfo *RuntimeClientWhoisInfo @@ -78,9 +78,9 @@ type RuntimeClientWhoisInfo struct { type clientsContainer struct { // TODO(a.garipov): Perhaps use a number of separate indices for // different types (string, net.IP, and so on). - list map[string]*Client // name -> client - idIndex map[string]*Client // ID -> client - ipHost map[string]*ClientHost // IP -> Hostname + list map[string]*Client // name -> client + idIndex map[string]*Client // ID -> client + ipToRC map[string]*RuntimeClient // IP -> runtime client lock sync.Mutex allTags map[string]bool @@ -105,7 +105,7 @@ func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd. } clients.list = make(map[string]*Client) clients.idIndex = make(map[string]*Client) - clients.ipHost = make(map[string]*ClientHost) + clients.ipToRC = make(map[string]*RuntimeClient) clients.allTags = make(map[string]bool) for _, t := range clientTags { @@ -136,7 +136,7 @@ func (clients *clientsContainer) Start() { } } -// Reload - reload auto-clients +// Reload reloads runtime clients. func (clients *clientsContainer) Reload() { clients.addFromSystemARP() } @@ -256,14 +256,14 @@ func (clients *clientsContainer) Exists(id string, source clientSource) (ok bool return true } - var ch *ClientHost - ch, ok = clients.ipHost[id] + var rc *RuntimeClient + rc, ok = clients.ipToRC[id] if !ok { return false } // Return false if the new source has higher priority. - return source <= ch.Source + return source <= rc.Source } func copyStrings(a []string) (b []string) { @@ -295,14 +295,14 @@ func (clients *clientsContainer) findMultiple(ids []string) (c *querylog.Client, name = c.Name foundIDs = c.IDs } else { - var ac ClientHost - ac, ok = clients.FindAutoClient(id) + var rc RuntimeClient + rc, ok = clients.FindRuntimeClient(id) if !ok { continue } - foundIDs = []string{ac.Host} - whois = toQueryLogWhois(ac.WhoisInfo) + foundIDs = []string{rc.Host} + whois = toQueryLogWhois(rc.WhoisInfo) } ip := net.ParseIP(id) @@ -418,21 +418,22 @@ func (clients *clientsContainer) findLocked(id string) (c *Client, ok bool) { return nil, false } -// FindAutoClient - search for an auto-client by IP -func (clients *clientsContainer) FindAutoClient(ip string) (ClientHost, bool) { +// FindRuntimeClient finds a runtime client by their IP. +func (clients *clientsContainer) FindRuntimeClient(ip string) (RuntimeClient, bool) { ipAddr := net.ParseIP(ip) if ipAddr == nil { - return ClientHost{}, false + return RuntimeClient{}, false } clients.lock.Lock() defer clients.lock.Unlock() - ch, ok := clients.ipHost[ip] + rc, ok := clients.ipToRC[ip] if ok { - return *ch, true + return *rc, true } - return ClientHost{}, false + + return RuntimeClient{}, false } // check validates the client. @@ -625,21 +626,24 @@ func (clients *clientsContainer) SetWhoisInfo(ip string, wi *RuntimeClientWhoisI return } - ch, ok := clients.ipHost[ip] + rc, ok := clients.ipToRC[ip] if ok { - ch.WhoisInfo = wi - log.Debug("clients: set whois info for auto-client %s: %+v", ch.Host, wi) + rc.WhoisInfo = wi + log.Debug("clients: set whois info for runtime client %s: %+v", rc.Host, wi) return } - // Create a ClientHost implicitly so that we don't do this check again - ch = &ClientHost{ + // Create a RuntimeClient implicitly so that we don't do this check + // again. + rc = &RuntimeClient{ Source: ClientSourceWHOIS, } - ch.WhoisInfo = wi - clients.ipHost[ip] = ch - log.Debug("clients: set whois info for auto-client with IP %s: %+v", ip, wi) + + rc.WhoisInfo = wi + clients.ipToRC[ip] = rc + + log.Debug("clients: set whois info for runtime client with ip %s: %+v", ip, wi) } // AddHost adds a new IP-hostname pairing. The priorities of the sources is @@ -655,25 +659,25 @@ func (clients *clientsContainer) AddHost(ip, host string, src clientSource) (ok // addHostLocked adds a new IP-hostname pairing. For internal use only. func (clients *clientsContainer) addHostLocked(ip, host string, src clientSource) (ok bool) { - var ch *ClientHost - ch, ok = clients.ipHost[ip] + var rc *RuntimeClient + rc, ok = clients.ipToRC[ip] if ok { - if ch.Source > src { + if rc.Source > src { return false } - ch.Source = src + rc.Source = src } else { - ch = &ClientHost{ + rc = &RuntimeClient{ Host: host, Source: src, WhoisInfo: &RuntimeClientWhoisInfo{}, } - clients.ipHost[ip] = ch + clients.ipToRC[ip] = rc } - log.Debug("clients: added %q -> %q [%d]", ip, host, len(clients.ipHost)) + log.Debug("clients: added %q -> %q [%d]", ip, host, len(clients.ipToRC)) return true } @@ -681,9 +685,9 @@ func (clients *clientsContainer) addHostLocked(ip, host string, src clientSource // rmHostsBySrc removes all entries that match the specified source. func (clients *clientsContainer) rmHostsBySrc(src clientSource) { n := 0 - for k, v := range clients.ipHost { + for k, v := range clients.ipToRC { if v.Source == src { - delete(clients.ipHost, k) + delete(clients.ipToRC, k) n++ } } diff --git a/internal/home/clients_test.go b/internal/home/clients_test.go index 1ac45e5fb97..aa040a58272 100644 --- a/internal/home/clients_test.go +++ b/internal/home/clients_test.go @@ -171,9 +171,9 @@ func TestClientsWhois(t *testing.T) { t.Run("new_client", func(t *testing.T) { clients.SetWhoisInfo("1.1.1.255", whois) - require.NotNil(t, clients.ipHost["1.1.1.255"]) + require.NotNil(t, clients.ipToRC["1.1.1.255"]) - h := clients.ipHost["1.1.1.255"] + h := clients.ipToRC["1.1.1.255"] require.NotNil(t, h) assert.Equal(t, h.WhoisInfo, whois) @@ -186,8 +186,8 @@ func TestClientsWhois(t *testing.T) { clients.SetWhoisInfo("1.1.1.1", whois) - require.NotNil(t, clients.ipHost["1.1.1.1"]) - h := clients.ipHost["1.1.1.1"] + require.NotNil(t, clients.ipToRC["1.1.1.1"]) + h := clients.ipToRC["1.1.1.1"] require.NotNil(t, h) assert.Equal(t, h.WhoisInfo, whois) @@ -202,7 +202,7 @@ func TestClientsWhois(t *testing.T) { assert.True(t, ok) clients.SetWhoisInfo("1.1.1.2", whois) - require.Nil(t, clients.ipHost["1.1.1.2"]) + require.Nil(t, clients.ipToRC["1.1.1.2"]) assert.True(t, clients.Del("client1")) }) } diff --git a/internal/home/clientshttp.go b/internal/home/clientshttp.go index 7cfa0a0a71b..681e9c5550b 100644 --- a/internal/home/clientshttp.go +++ b/internal/home/clientshttp.go @@ -34,18 +34,18 @@ type clientJSON struct { DisallowedRule string `json:"disallowed_rule"` } -type clientHostJSON struct { +type runtimeClientJSON struct { + WhoisInfo *RuntimeClientWhoisInfo `json:"whois_info"` + IP string `json:"ip"` Name string `json:"name"` Source string `json:"source"` - - WhoisInfo *RuntimeClientWhoisInfo `json:"whois_info"` } type clientListJSON struct { - Clients []clientJSON `json:"clients"` - AutoClients []clientHostJSON `json:"auto_clients"` - Tags []string `json:"supported_tags"` + Clients []clientJSON `json:"clients"` + RuntimeClients []runtimeClientJSON `json:"auto_clients"` + Tags []string `json:"supported_tags"` } // respond with information about configured clients @@ -59,15 +59,15 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, _ *http cj := clientToJSON(c) data.Clients = append(data.Clients, cj) } - for ip, ch := range clients.ipHost { - cj := clientHostJSON{ + for ip, rc := range clients.ipToRC { + cj := runtimeClientJSON{ IP: ip, - Name: ch.Host, - WhoisInfo: ch.WhoisInfo, + Name: rc.Host, + WhoisInfo: rc.WhoisInfo, } cj.Source = "etc/hosts" - switch ch.Source { + switch rc.Source { case ClientSourceDHCP: cj.Source = "DHCP" case ClientSourceRDNS: @@ -78,7 +78,7 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, _ *http cj.Source = "WHOIS" } - data.AutoClients = append(data.AutoClients, cj) + data.RuntimeClients = append(data.RuntimeClients, cj) } data.Tags = clientTags @@ -133,12 +133,12 @@ func clientToJSON(c *Client) clientJSON { return cj } -// Convert ClientHost object to JSON -func clientHostToJSON(ip string, ch ClientHost) clientJSON { - cj := clientJSON{ - Name: ch.Host, +// runtimeClientToJSON converts a RuntimeClient into a JSON struct. +func runtimeClientToJSON(ip string, rc RuntimeClient) (cj clientJSON) { + cj = clientJSON{ + Name: rc.Host, IDs: []string{ip}, - WhoisInfo: ch.WhoisInfo, + WhoisInfo: rc.WhoisInfo, } return cj @@ -265,7 +265,7 @@ func (clients *clientsContainer) findTemporary(ip net.IP, idStr string) (cj clie return cj, false } - ch, ok := clients.FindAutoClient(idStr) + rc, ok := clients.FindRuntimeClient(idStr) 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 @@ -287,7 +287,7 @@ func (clients *clientsContainer) findTemporary(ip net.IP, idStr string) (cj clie return cj, true } - cj = clientHostToJSON(idStr, ch) + cj = runtimeClientToJSON(idStr, rc) cj.Disallowed, cj.DisallowedRule = clients.dnsServer.IsBlockedIP(ip) return cj, true diff --git a/internal/home/rdns_test.go b/internal/home/rdns_test.go index 0e313ef615d..d89c5b119cc 100644 --- a/internal/home/rdns_test.go +++ b/internal/home/rdns_test.go @@ -84,7 +84,7 @@ func TestRDNS_Begin(t *testing.T) { clients: &clientsContainer{ list: map[string]*Client{}, idIndex: tc.cliIDIndex, - ipHost: map[string]*ClientHost{}, + ipToRC: map[string]*RuntimeClient{}, allTags: map[string]bool{}, }, } @@ -229,7 +229,7 @@ func TestRDNS_WorkerLoop(t *testing.T) { cc := &clientsContainer{ list: map[string]*Client{}, idIndex: map[string]*Client{}, - ipHost: map[string]*ClientHost{}, + ipToRC: map[string]*RuntimeClient{}, allTags: map[string]bool{}, } ch := make(chan net.IP) diff --git a/internal/home/whois.go b/internal/home/whois.go index ae7b6df80ac..f2923815765 100644 --- a/internal/home/whois.go +++ b/internal/home/whois.go @@ -236,9 +236,7 @@ func (w *Whois) Begin(ip net.IP) { // workerLoop processes the IP addresses it got from the channel and associates // the retrieving WHOIS info with a client. func (w *Whois) workerLoop() { - for { - ip := <-w.ipChan - + for ip := range w.ipChan { info := w.process(context.Background(), ip) if info == nil { continue