From bc3df7ac5cc9e3fdf37062071ff6a9c25afbdcbe Mon Sep 17 00:00:00 2001 From: Nicholas Westerhausen <2317381+nwesterhausen@users.noreply.github.com> Date: Tue, 9 Apr 2024 17:04:36 +0000 Subject: [PATCH] fix: refresh WHOIS for new domains --- configuration/whois-cache.configuration.go | 4 +++- service/domain.service.go | 8 ++++---- service/whois.service.go | 11 ++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/configuration/whois-cache.configuration.go b/configuration/whois-cache.configuration.go index d4db769..3ea50cb 100644 --- a/configuration/whois-cache.configuration.go +++ b/configuration/whois-cache.configuration.go @@ -74,11 +74,13 @@ func (w *WhoisCacheStorage) Add(fqdn string) { LastUpdated: time.Time{}, } - // Perform the whois query + // Perform the whois query for the new domain newEntry.Refresh() // Add the entry to the list w.FileContents.Entries = append(w.FileContents.Entries, newEntry) + // Flush the cache to disk + w.Flush() } func (w *WhoisCacheStorage) Refresh() { diff --git a/service/domain.service.go b/service/domain.service.go index b9f33fd..71d6d2a 100644 --- a/service/domain.service.go +++ b/service/domain.service.go @@ -24,7 +24,7 @@ func (s *ServicesDomain) CreateDomain(domain configuration.Domain) (int, error) } } // This should never happen.. but just in case return -1 and an error - return -1, errors.New("Failed to add domain") + return -1, errors.New("failed to add domain") } func (s *ServicesDomain) GetDomain(fqdn string) (configuration.Domain, error) { @@ -33,7 +33,7 @@ func (s *ServicesDomain) GetDomain(fqdn string) (configuration.Domain, error) { return d, nil } } - return configuration.Domain{}, errors.New("Domain not found") + return configuration.Domain{}, errors.New("domain not found") } func (s *ServicesDomain) GetDomains() ([]configuration.Domain, error) { @@ -52,7 +52,7 @@ func (s *ServicesDomain) UpdateDomain(domain configuration.Domain) error { } } // This should never happen.. but just in case return an error - return errors.New("Failed to update domain") + return errors.New("failed to update domain") } func (s *ServicesDomain) DeleteDomain(fqdn string) error { @@ -65,7 +65,7 @@ func (s *ServicesDomain) DeleteDomain(fqdn string) error { // Return nil to indicate success (we can confirm the domain was deleted by checking the list) for _, d := range s.store.DomainFile.Domains { if d.FQDN == fqdn { - return errors.New("Failed to delete domain") + return errors.New("failed to delete domain") } } return nil diff --git a/service/whois.service.go b/service/whois.service.go index bf13e9b..f7d676b 100644 --- a/service/whois.service.go +++ b/service/whois.service.go @@ -23,7 +23,16 @@ func (s *ServicesWhois) GetWhois(fqdn string) (configuration.WhoisCache, error) } log.Println("🙅 WHOIS entry cache miss for", fqdn) - return configuration.WhoisCache{}, errors.New("WHOIS entry not found") + // Since we cache missed, let's try to fetch the WHOIS entry instead + s.store.Add(fqdn) + // Try to get the entry again + for _, entry := range s.store.FileContents.Entries { + if entry.FQDN == fqdn { + return entry, nil + } + } + + return configuration.WhoisCache{}, errors.New("entry missing") } func (s *ServicesWhois) MarkAlertSent(fqdn string, alert configuration.Alert) bool {