forked from blind-oracle/dnstap-bgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
222 lines (174 loc) · 4.11 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"flag"
"log"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/BurntSushi/toml"
)
type cfgRoot struct {
Domains string
Cache string
TTL string
IPv6 bool
DNSTap *dnstapCfg
BGP *bgpCfg
Syncer *syncerCfg
}
var (
version string
)
func main() {
var (
bgp *bgpServer
ipDB *db
syncer *syncer
err error
shutdown = make(chan struct{})
)
log.Printf("dnstap-bgp v%s", version)
config := flag.String("config", "", "Path to a config file")
flag.Parse()
if *config == "" {
log.Fatal("You need to specify path to a config file")
}
cfg := &cfgRoot{}
if _, err = toml.DecodeFile(*config, &cfg); err != nil {
log.Fatalf("Unable to parse config file '%s': %s", *config, err)
}
cfg.DNSTap.IPv6 = cfg.IPv6
cfg.BGP.IPv6 = cfg.IPv6
if cfg.Domains == "" {
log.Fatal("You need to specify path to a domain list")
}
ttl := 24 * time.Hour
if cfg.TTL != "" {
if ttl, err = time.ParseDuration(cfg.TTL); err != nil {
log.Printf("Unable to parse TTL: %s", err)
}
}
expireCb := func(e *cacheEntry) {
log.Printf("%s (%s) expired", e.IP, e.Domain)
bgp.delHost(e.IP)
if ipDB != nil {
ipDB.del(e.IP)
}
}
ipCache := newCache(ttl, expireCb)
dTree := newDomainTree()
cnt, skip, err := dTree.loadFile(cfg.Domains)
if err != nil {
log.Fatalf("Unable to load domain list: %s", err)
}
log.Printf("Domains loaded: %d, skipped: %d", cnt, skip)
if cfg.Cache != "" {
if ipDB, err = newDB(cfg.Cache); err != nil {
log.Fatalf("Unable to init DB '%s': %s", cfg.Cache, err)
}
es, err := ipDB.fetchAll()
if err != nil {
log.Fatalf("Unable to load entries from DB: %s", err)
}
now := time.Now()
i, j, k := 0, 0, 0
for _, e := range es {
if now.Sub(e.TS) >= ttl {
ipDB.del(e.IP)
j++
continue
}
if !dTree.has(e.Domain) {
ipDB.del(e.IP)
k++
continue
}
ipCache.add(e)
i++
}
log.Printf("Loaded from DB: %d, expired: %d, vanished: %d", i, j, k)
}
if bgp, err = newBgp(cfg.BGP); err != nil {
log.Fatalf("Unable to init BGP: %s", err)
}
ipDBPut := func(e *cacheEntry) {
if ipDB == nil {
return
}
if err := ipDB.add(e); err != nil {
log.Printf("Unable to add (%s, %s) to DB: %s", e.IP, e.Domain, err)
}
}
addEntry := func(e *cacheEntry, touch bool) bool {
if ipCache.exists(e.IP, touch) {
if touch {
ipDBPut(e)
}
return false
}
log.Printf("%s: %s (from peer: %t)", e.Domain, e.IP, !touch)
bgp.addHost(e.IP)
ipCache.add(e)
ipDBPut(e)
return true
}
if cfg.Syncer.Listen != "" || len(cfg.Syncer.Peers) > 0 {
syncerCb := func(peer string, new int, err error) {
log.Printf("Syncer: Peer %s: synced: %d error: %v", peer, new, err)
}
if syncer, err = newSyncer(cfg.Syncer, ipCache.getAll, addEntry, syncerCb); err != nil {
log.Fatalf("Unable to init syncer: %s", err)
}
}
addHostCb := func(ip net.IP, domain string) {
if !dTree.has(domain) {
return
}
e := &cacheEntry{
IP: ip,
Domain: domain,
TS: time.Now(),
}
addEntry(e, true)
if syncer != nil {
if err := syncer.broadcast(e); err != nil {
log.Printf("Unable to broadcast [%s %s]: %s", ip, domain, err)
}
}
}
dnsTapErrorCb := func(err error) {
log.Printf("DNSTap error: %s", err)
}
if _, err = newDnstapServer(cfg.DNSTap, addHostCb, dnsTapErrorCb); err != nil {
log.Fatalf("Unable to init DNSTap: %s", err)
}
log.Printf("Listening for DNSTap on: %s", cfg.DNSTap.Listen)
go func() {
sigchannel := make(chan os.Signal, 1)
signal.Notify(sigchannel, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGUSR1, os.Interrupt)
for sig := range sigchannel {
switch sig {
case syscall.SIGHUP:
if i, s, err := dTree.loadFile(cfg.Domains); err != nil {
log.Printf("Unable to load file: %s", err)
} else {
log.Printf("Domains loaded: %d, skipped: %d", i, s)
}
case os.Interrupt, syscall.SIGTERM:
close(shutdown)
case syscall.SIGUSR1:
log.Printf("IPs exported: %d, domains loaded: %d", ipCache.count(), dTree.count())
}
}
}()
<-shutdown
bgp.close()
if syncer != nil {
syncer.close()
}
if ipDB != nil {
ipDB.close()
}
}