Skip to content

Commit

Permalink
home: imp names
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Apr 2, 2021
1 parent c2cfdef commit dbf990e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 66 deletions.
78 changes: 41 additions & 37 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ const (
ClientSourceHostsFile
)

// ClientHost information
type ClientHost struct {
// RuntimeClient information
type RuntimeClient struct {
Host string
Source clientSource
WhoisInfo *RuntimeClientWhoisInfo
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -136,7 +136,7 @@ func (clients *clientsContainer) Start() {
}
}

// Reload - reload auto-clients
// Reload reloads runtime clients.
func (clients *clientsContainer) Reload() {
clients.addFromSystemARP()
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -655,35 +659,35 @@ 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
}

// 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++
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/home/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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"))
})
}
Expand Down
38 changes: 19 additions & 19 deletions internal/home/clientshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/home/rdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions internal/home/whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dbf990e

Please sign in to comment.