-
Notifications
You must be signed in to change notification settings - Fork 0
/
columbus.go
82 lines (58 loc) · 1.66 KB
/
columbus.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
71
72
73
74
75
76
77
78
79
80
81
82
package columbus
import (
"context"
"net/http"
"sync/atomic"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/elmasy-com/elnet/validator"
"github.com/miekg/dns"
eldns "github.com/elmasy-com/elnet/dns"
)
var (
log = clog.NewWithPlugin("columbus")
DomainsChan chan *string = nil
InsertWorkers *atomic.Bool = nil // Indicate whether insertWokers started
)
type Columbus struct {
Next plugin.Handler
}
func (e Columbus) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
di := NewDomainInserter(w)
return plugin.NextOrFailure(e.Name(), e.Next, ctx, di, r)
}
func (e Columbus) Name() string { return "columbus" }
type DomainInserter struct {
dns.ResponseWriter
}
func NewDomainInserter(w dns.ResponseWriter) *DomainInserter {
return &DomainInserter{ResponseWriter: w}
}
func (r *DomainInserter) WriteMsg(res *dns.Msg) error {
if len(DomainsChan) == cap(DomainsChan) {
log.Warningf("DomainChannel is full!")
}
if res.Rcode == 0 && len(res.Answer) > 0 && len(res.Question) > 0 {
DomainsChan <- &res.Question[0].Name
}
return r.ResponseWriter.WriteMsg(res)
}
func insertWorker() {
for d := range DomainsChan {
if !validator.Domain(*d) {
continue
}
req, err := http.NewRequest("PUT", "https://columbus.elmasy.com/api/insert/"+eldns.Clean(*d), nil)
if err != nil {
log.Errorf("Failed to create request for %s: %s", *d, err)
continue
}
req.Header.Add("User-Agent", "coredns-columbus")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorf("Failed to PUT for %s: %s", *d, err)
continue
}
resp.Body.Close()
}
}