-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathremote.go
More file actions
80 lines (69 loc) · 1.72 KB
/
Copy pathremote.go
File metadata and controls
80 lines (69 loc) · 1.72 KB
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
71
72
73
74
75
76
77
78
79
80
// Encoding: UTF-8
package main
import (
"context"
"errors"
"net"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/jellydator/ttlcache/v3"
)
var remotelyDisabled = ttlcache.New(
ttlcache.WithTTL[string, string](5*time.Minute),
ttlcache.WithDisableTouchOnHit[string, string](),
)
func Remote(ctx context.Context, dnsRecords []string) {
for _, r := range dnsRecords {
go RemoteFetcher(ctx, r)
}
}
func RemoteFetcher(ctx context.Context, dnsRecord string) {
for {
select {
case <-ctx.Done():
return
default:
}
lookupCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
txtrecords, err := net.DefaultResolver.LookupTXT(lookupCtx, dnsRecord)
cancel()
if err != nil {
if ctx.Err() != nil {
return // parent context cancelled, exit
}
if dnsErr, ok := err.(*net.DNSError); ok && dnsErr.IsNotFound {
log.Debug(dnsErr)
} else if !errors.Is(err, context.DeadlineExceeded) {
log.Error(err)
} else {
log.Errorln("DNS lookup timed out:", dnsRecord)
}
} else {
for _, txt := range txtrecords {
for _, entry := range strings.Split(txt, ",") {
entry := strings.SplitN(entry, "=", 2)
if len(entry) != 2 {
log.Debugln("Invalid DNS entry:", dnsRecord, entry)
continue
}
if strings.EqualFold(strings.TrimSpace(entry[1]), "disabled") {
remotelyDisabled.Set(strings.TrimSpace(entry[0]), dnsRecord, ttlcache.DefaultTTL)
}
}
}
}
select {
case <-time.After(2 * time.Minute):
case <-ctx.Done():
return
}
}
}
func RemotelyDisabled(check string) (dnsRecord string, disabled bool) {
// Check if disabled remotely via SRV Record
if remotelyDisabled.Has(check) {
return remotelyDisabled.Get(check).Value(), true
}
return
}