forked from qdm12/ddns-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
70 lines (61 loc) · 1.78 KB
/
check.go
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
59
60
61
62
63
64
65
66
67
68
69
70
package health
import (
"context"
"errors"
"fmt"
"net/netip"
"strings"
"github.com/qdm12/ddns-updater/internal/constants"
)
func MakeIsHealthy(db AllSelecter, resolver LookupIPer) func(ctx context.Context) error {
return func(ctx context.Context) (err error) {
return isHealthy(ctx, db, resolver)
}
}
var (
ErrRecordUpdateFailed = errors.New("record update failed")
ErrRecordIPNotSet = errors.New("record IP not set")
ErrLookupMismatch = errors.New("lookup IP addresses do not match")
)
// isHealthy checks all the records were updated successfully and returns an error if not.
func isHealthy(ctx context.Context, db AllSelecter, resolver LookupIPer) (err error) {
records := db.SelectAll()
for _, record := range records {
if record.Status == constants.FAIL {
return fmt.Errorf("%w: %s", ErrRecordUpdateFailed, record.String())
} else if record.Provider.Proxied() {
continue
}
hostname := record.Provider.BuildDomainName()
currentIP := record.History.GetCurrentIP()
if !currentIP.IsValid() {
return fmt.Errorf("%w: for hostname %s", ErrRecordIPNotSet, hostname)
}
lookedUpNetIPs, err := resolver.LookupIP(ctx, "ip", hostname)
if err != nil {
return err
}
found := false
lookedUpIPsString := make([]string, len(lookedUpNetIPs))
for i, netIP := range lookedUpNetIPs {
var ip netip.Addr
switch {
case netIP == nil:
case netIP.To4() != nil:
ip = netip.AddrFrom4([4]byte(netIP.To4()))
default: // IPv6
ip = netip.AddrFrom16([16]byte(netIP.To16()))
}
if ip.Compare(currentIP) == 0 {
found = true
break
}
lookedUpIPsString[i] = ip.String()
}
if !found {
return fmt.Errorf("%w: %s instead of %s for %s",
ErrLookupMismatch, strings.Join(lookedUpIPsString, ","), currentIP, hostname)
}
}
return nil
}