github.com/netdata/go.d.plugin@v0.58.1/modules/whoisquery/provider.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package whoisquery
     4  
     5  import (
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/araddon/dateparse"
    10  	"github.com/likexian/whois"
    11  	whoisparser "github.com/likexian/whois-parser"
    12  )
    13  
    14  type provider interface {
    15  	remainingTime() (float64, error)
    16  }
    17  
    18  type fromNet struct {
    19  	domainAddress string
    20  	client        *whois.Client
    21  }
    22  
    23  func newProvider(config Config) (provider, error) {
    24  	domain := config.Source
    25  	client := whois.NewClient()
    26  	client.SetTimeout(config.Timeout.Duration)
    27  
    28  	return &fromNet{
    29  		domainAddress: domain,
    30  		client:        client,
    31  	}, nil
    32  }
    33  
    34  func (f *fromNet) remainingTime() (float64, error) {
    35  	raw, err := f.client.Whois(f.domainAddress)
    36  	if err != nil {
    37  		return 0, err
    38  	}
    39  
    40  	result, err := whoisparser.Parse(raw)
    41  	if err != nil {
    42  		return 0, err
    43  	}
    44  
    45  	// https://community.netdata.cloud/t/whois-query-monitor-cannot-parse-expiration-time/3485
    46  	if strings.Contains(result.Domain.ExpirationDate, " ") {
    47  		if v, err := time.Parse("2006.01.02 15:04:05", result.Domain.ExpirationDate); err == nil {
    48  			return time.Until(v).Seconds(), nil
    49  		}
    50  	}
    51  
    52  	expire, err := dateparse.ParseAny(result.Domain.ExpirationDate)
    53  	if err != nil {
    54  		return 0, err
    55  	}
    56  
    57  	return time.Until(expire).Seconds(), nil
    58  }