summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/whoisquery/provider.go
blob: 032f979f4cc6ddce0ebf34c2fc281ef277730fd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: GPL-3.0-or-later

package whoisquery

import (
	"strings"
	"time"

	"github.com/araddon/dateparse"
	"github.com/likexian/whois"
	whoisparser "github.com/likexian/whois-parser"
)

type provider interface {
	remainingTime() (float64, error)
}

type fromNet struct {
	domainAddress string
	client        *whois.Client
}

func newProvider(config Config) (provider, error) {
	domain := config.Source
	client := whois.NewClient()
	client.SetTimeout(config.Timeout.Duration())

	return &fromNet{
		domainAddress: domain,
		client:        client,
	}, nil
}

func (f *fromNet) remainingTime() (float64, error) {
	raw, err := f.client.Whois(f.domainAddress)
	if err != nil {
		return 0, err
	}

	result, err := whoisparser.Parse(raw)
	if err != nil {
		return 0, err
	}

	// https://community.netdata.cloud/t/whois-query-monitor-cannot-parse-expiration-time/3485
	if strings.Contains(result.Domain.ExpirationDate, " ") {
		if v, err := time.Parse("2006.01.02 15:04:05", result.Domain.ExpirationDate); err == nil {
			return time.Until(v).Seconds(), nil
		}
	}

	expire, err := dateparse.ParseAny(result.Domain.ExpirationDate)
	if err != nil {
		return 0, err
	}

	return time.Until(expire).Seconds(), nil
}