package cwhois import ( "net" "strings" "time" "github.com/glebtv/whois" "github.com/weppos/publicsuffix-go/publicsuffix" "rocketgit.ru/rsmon/worker/app/models" ) // Perform provides functionality. func Perform(c *models.Check) *Result { result := &Result{} result.State = "FAIL" host := c.Monitor.Host if host == "localhost" || strings.HasPrefix(host, "localhost:") { result.Warnings = append(result.Warnings, "WHOIS check not possible for localhost, please disable") return result } addr := net.ParseIP(host) if addr != nil { result.Warnings = append(result.Warnings, "WHOIS check not possible for ip address, please disable") return result } zname, err := publicsuffix.Domain(host) if err != nil { result.Warnings = append(result.Warnings, "Failed to get public suffix: "+err.Error()) zname = host } if zname != host { result.Infos = append(result.Infos, "not top level domain, running WHOIS check for "+zname) } start := time.Now() response := whois.Whois(zname) result.Raw = *response result.Error = result.Raw.Error if result.Error == nil { result.State = "OK" } result.Duration = time.Since(start) if result.Error != nil { result.State = "ERR" } if response.Expires.IsZero() { if result.State == "OK" { result.State = "WARN" } result.Warnings = append(result.Warnings, "unable to get whois expiration for "+host) return result } result.Expires = &response.Expires exp := time.Until(*result.Expires).Hours() / 24 if exp < 3 { result.State = "WARN" result.Warnings = append(result.Warnings, "Domain expires in a few days") } return result }