refactor: Move whois to client package and implement caching

This commit is contained in:
TwiN
2022-11-15 21:35:22 -05:00
parent c172e733be
commit d24ff5bd07
3 changed files with 77 additions and 19 deletions

View File

@ -16,7 +16,6 @@ import (
"github.com/TwiN/gatus/v4/client"
"github.com/TwiN/gatus/v4/core/ui"
"github.com/TwiN/gatus/v4/util"
"github.com/TwiN/whois"
)
type EndpointType string
@ -66,8 +65,6 @@ var (
// This is because the free whois service we are using should not be abused, especially considering the fact that
// the data takes a while to be updated.
ErrInvalidEndpointIntervalForDomainExpirationPlaceholder = errors.New("the minimum interval for an endpoint with a condition using the " + DomainExpirationPlaceholder + " placeholder is 300s (5m)")
whoisClient = whois.NewClient().WithReferralCache(true)
)
// Endpoint is the configuration of a monitored
@ -257,11 +254,20 @@ func (endpoint *Endpoint) EvaluateHealth() *Result {
if endpoint.needsToRetrieveIP() {
endpoint.getIP(result)
}
// Retrieve domain expiration if necessary
if endpoint.needsToRetrieveDomainExpiration() && len(result.Hostname) > 0 {
var err error
if result.DomainExpiration, err = client.GetDomainExpiration(result.Hostname); err != nil {
result.AddError(err.Error())
}
}
// Call the endpoint (if there's no errors)
if len(result.Errors) == 0 {
endpoint.call(result)
} else {
result.Success = false
}
// Evaluate the conditions
for _, condition := range endpoint.Conditions {
success := condition.evaluate(result, endpoint.UIConfig.DontResolveFailedConditions)
if !success {
@ -269,10 +275,6 @@ func (endpoint *Endpoint) EvaluateHealth() *Result {
}
}
result.Timestamp = time.Now()
// Retrieve domain expiration if necessary
if endpoint.needsToRetrieveDomainExpiration() && len(result.Hostname) > 0 {
endpoint.getDomainExpiration(result)
}
// No need to keep the body after the endpoint has been evaluated
result.body = nil
// Clean up parameters that we don't need to keep in the results
@ -291,19 +293,11 @@ func (endpoint *Endpoint) EvaluateHealth() *Result {
}
func (endpoint *Endpoint) getIP(result *Result) {
ips, err := net.LookupIP(result.Hostname)
if err != nil {
if ips, err := net.LookupIP(result.Hostname); err != nil {
result.AddError(err.Error())
return
}
result.IP = ips[0].String()
}
func (endpoint *Endpoint) getDomainExpiration(result *Result) {
if whoisResponse, err := whoisClient.QueryAndParse(result.Hostname); err != nil {
result.AddError("error querying and parsing hostname using whois client: " + err.Error())
} else {
result.DomainExpiration = time.Until(whoisResponse.ExpirationDate)
result.IP = ips[0].String()
}
}