- whois: couldn't set info on existing auto-clients

This commit is contained in:
Simon Zolin 2019-10-11 16:58:10 +03:00
parent b9462e72a4
commit c299753b67
2 changed files with 42 additions and 14 deletions

View File

@ -73,9 +73,9 @@ type ClientHost struct {
} }
type clientsContainer struct { type clientsContainer struct {
list map[string]*Client // name -> client list map[string]*Client // name -> client
ipIndex map[string]*Client // IP -> client ipIndex map[string]*Client // IP -> client
ipHost map[string]ClientHost // IP -> Hostname ipHost map[string]*ClientHost // IP -> Hostname
lock sync.Mutex lock sync.Mutex
} }
@ -87,7 +87,7 @@ func (clients *clientsContainer) Init() {
} }
clients.list = make(map[string]*Client) clients.list = make(map[string]*Client)
clients.ipIndex = make(map[string]*Client) clients.ipIndex = make(map[string]*Client)
clients.ipHost = make(map[string]ClientHost) clients.ipHost = make(map[string]*ClientHost)
go clients.periodicUpdate() go clients.periodicUpdate()
} }
@ -303,7 +303,7 @@ func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) {
return return
} }
ch = ClientHost{ ch = &ClientHost{
Source: ClientSourceWHOIS, Source: ClientSourceWHOIS,
} }
ch.WhoisInfo = info ch.WhoisInfo = info
@ -324,16 +324,18 @@ func (clients *clientsContainer) AddHost(ip, host string, source clientSource) (
return false, nil return false, nil
} }
// check index // check auto-clients index
c, ok := clients.ipHost[ip] ch, ok := clients.ipHost[ip]
if ok && c.Source > source { if ok && ch.Source > source {
return false, nil return false, nil
} } else if ok {
ch.Source = source
clients.ipHost[ip] = ClientHost{ } else {
Host: host, ch = &ClientHost{
Source: source, Host: host,
WhoisInfo: c.WhoisInfo, Source: source,
}
clients.ipHost[ip] = ch
} }
log.Tracef("'%s' -> '%s' [%d]", ip, host, len(clients.ipHost)) log.Tracef("'%s' -> '%s' [%d]", ip, host, len(clients.ipHost))
return true, nil return true, nil

View File

@ -135,3 +135,29 @@ func TestClients(t *testing.T) {
// get // get
assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile)) assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile))
} }
func TestClientsWhois(t *testing.T) {
var c Client
clients := clientsContainer{}
clients.Init()
whois := [][]string{{"orgname", "orgname-val"}, {"country", "country-val"}}
// set whois info on new client
clients.SetWhoisInfo("1.1.1.255", whois)
assert.True(t, clients.ipHost["1.1.1.255"].WhoisInfo[0][1] == "orgname-val")
// set whois info on existing auto-client
_, _ = clients.AddHost("1.1.1.1", "host", ClientSourceRDNS)
clients.SetWhoisInfo("1.1.1.1", whois)
assert.True(t, clients.ipHost["1.1.1.1"].WhoisInfo[0][1] == "orgname-val")
// set whois info on existing client
c = Client{
IP: "1.1.1.2",
Name: "client1",
}
_, _ = clients.Add(c)
clients.SetWhoisInfo("1.1.1.2", whois)
assert.True(t, clients.ipIndex["1.1.1.2"].WhoisInfo[0][1] == "orgname-val")
_ = clients.Del("client1")
}