forked from dmachard/DNS-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_tail.go
275 lines (229 loc) · 6.38 KB
/
file_tail.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package collectors
import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"time"
"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/transformers"
"github.com/dmachard/go-logger"
"github.com/hpcloud/tail"
"github.com/miekg/dns"
)
type Tail struct {
done chan bool
tailf *tail.Tail
loggers []dnsutils.Worker
config *dnsutils.Config
logger *logger.Logger
name string
}
func NewTail(loggers []dnsutils.Worker, config *dnsutils.Config, logger *logger.Logger, name string) *Tail {
logger.Info("[%s] tail collector - enabled", name)
s := &Tail{
done: make(chan bool),
config: config,
loggers: loggers,
logger: logger,
name: name,
}
s.ReadConfig()
return s
}
func (c *Tail) GetName() string { return c.name }
func (c *Tail) SetLoggers(loggers []dnsutils.Worker) {
c.loggers = loggers
}
func (c *Tail) Loggers() []chan dnsutils.DnsMessage {
channels := []chan dnsutils.DnsMessage{}
for _, p := range c.loggers {
channels = append(channels, p.Channel())
}
return channels
}
func (c *Tail) ReadConfig() {
//tbc
}
func (c *Tail) LogInfo(msg string, v ...interface{}) {
c.logger.Info("["+c.name+"] tail collector - "+msg, v...)
}
func (c *Tail) LogError(msg string, v ...interface{}) {
c.logger.Error("["+c.name+"] tail collector - "+msg, v...)
}
func (c *Tail) Channel() chan dnsutils.DnsMessage {
return nil
}
func (c *Tail) Stop() {
c.LogInfo("stopping...")
// Stop to follow file
c.LogInfo("stop following file...")
c.tailf.Stop()
// read done channel and block until run is terminated
<-c.done
close(c.done)
}
func (c *Tail) Follow() error {
var err error
location := tail.SeekInfo{Offset: 0, Whence: io.SeekEnd}
config := tail.Config{Location: &location, ReOpen: true, Follow: true, Logger: tail.DiscardingLogger, Poll: true, MustExist: true}
c.tailf, err = tail.TailFile(c.config.Collectors.Tail.FilePath, config)
if err != nil {
return err
}
return nil
}
func (c *Tail) Run() {
c.LogInfo("starting collector...")
err := c.Follow()
if err != nil {
c.logger.Fatal("collector tail - unable to follow file: ", err)
}
// prepare enabled transformers
subprocessors := transformers.NewTransforms(&c.config.IngoingTransformers, c.logger, c.name, c.Loggers())
// init dns message
dm := dnsutils.DnsMessage{}
dm.Init()
// init dns message with additionnals parts
subprocessors.InitDnsMessageFormat(&dm)
hostname, err := os.Hostname()
if err == nil {
dm.DnsTap.Identity = hostname
} else {
dm.DnsTap.Identity = "undefined"
}
for line := range c.tailf.Lines {
var matches []string
var re *regexp.Regexp
if len(c.config.Collectors.Tail.PatternQuery) > 0 {
re = regexp.MustCompile(c.config.Collectors.Tail.PatternQuery)
matches = re.FindStringSubmatch(line.Text)
dm.DNS.Type = dnsutils.DnsQuery
dm.DnsTap.Operation = dnsutils.DNSTAP_OPERATION_QUERY
}
if len(c.config.Collectors.Tail.PatternReply) > 0 && len(matches) == 0 {
re = regexp.MustCompile(c.config.Collectors.Tail.PatternReply)
matches = re.FindStringSubmatch(line.Text)
dm.DNS.Type = dnsutils.DnsReply
dm.DnsTap.Operation = dnsutils.DNSTAP_OPERATION_REPLY
}
if len(matches) == 0 {
continue
}
qrIndex := re.SubexpIndex("qr")
if qrIndex != -1 {
dm.DnsTap.Operation = matches[qrIndex]
}
var t time.Time
timestampIndex := re.SubexpIndex("timestamp")
if timestampIndex != -1 {
t, err = time.Parse(c.config.Collectors.Tail.TimeLayout, matches[timestampIndex])
if err != nil {
continue
}
} else {
t = time.Now()
}
dm.DnsTap.TimeSec = int(t.Unix())
dm.DnsTap.TimeNsec = int(t.UnixNano() - t.Unix()*1e9)
identityIndex := re.SubexpIndex("identity")
if identityIndex != -1 {
dm.DnsTap.Identity = matches[identityIndex]
}
rcodeIndex := re.SubexpIndex("rcode")
if rcodeIndex != -1 {
dm.DNS.Rcode = matches[rcodeIndex]
}
queryipIndex := re.SubexpIndex("queryip")
if queryipIndex != -1 {
dm.NetworkInfo.QueryIp = matches[queryipIndex]
}
queryportIndex := re.SubexpIndex("queryport")
if queryportIndex != -1 {
dm.NetworkInfo.QueryPort = matches[queryportIndex]
} else {
dm.NetworkInfo.ResponsePort = "0"
}
responseipIndex := re.SubexpIndex("responseip")
if responseipIndex != -1 {
dm.NetworkInfo.ResponseIp = matches[responseipIndex]
}
responseportIndex := re.SubexpIndex("responseport")
if responseportIndex != -1 {
dm.NetworkInfo.ResponsePort = matches[responseportIndex]
} else {
dm.NetworkInfo.ResponsePort = "0"
}
familyIndex := re.SubexpIndex("family")
if familyIndex != -1 {
dm.NetworkInfo.Family = matches[familyIndex]
} else {
dm.NetworkInfo.Family = dnsutils.PROTO_IPV4
}
protocolIndex := re.SubexpIndex("protocol")
if protocolIndex != -1 {
dm.NetworkInfo.Protocol = matches[protocolIndex]
} else {
dm.NetworkInfo.Protocol = dnsutils.PROTO_UDP
}
lengthIndex := re.SubexpIndex("length")
if lengthIndex != -1 {
length, err := strconv.Atoi(matches[lengthIndex])
if err == nil {
dm.DNS.Length = length
}
}
domainIndex := re.SubexpIndex("domain")
if domainIndex != -1 {
dm.DNS.Qname = matches[domainIndex]
}
qtypeIndex := re.SubexpIndex("qtype")
if qtypeIndex != -1 {
dm.DNS.Qtype = matches[qtypeIndex]
}
latencyIndex := re.SubexpIndex("latency")
if latencyIndex != -1 {
dm.DnsTap.LatencySec = matches[latencyIndex]
}
// compute timestamp
ts := time.Unix(int64(dm.DnsTap.TimeSec), int64(dm.DnsTap.TimeNsec))
dm.DnsTap.Timestamp = ts.UnixNano()
dm.DnsTap.TimestampRFC3339 = ts.UTC().Format(time.RFC3339Nano)
// fake dns packet
dnspkt := new(dns.Msg)
var dnstype uint16
dnstype = dns.TypeA
if dm.DNS.Qtype == "AAAA" {
dnstype = dns.TypeAAAA
}
dnspkt.SetQuestion(dm.DNS.Qname, dnstype)
if dm.DNS.Type == dnsutils.DnsReply {
rr, _ := dns.NewRR(fmt.Sprintf("%s %s 0.0.0.0", dm.DNS.Qname, dm.DNS.Qtype))
if err == nil {
dnspkt.Answer = append(dnspkt.Answer, rr)
}
var rcode int
rcode = 0
if dm.DNS.Rcode == "NXDOMAIN" {
rcode = 3
}
dnspkt.Rcode = rcode
}
dm.DNS.Payload, _ = dnspkt.Pack()
dm.DNS.Length = len(dm.DNS.Payload)
// apply all enabled transformers
if subprocessors.ProcessMessage(&dm) == transformers.RETURN_DROP {
continue
}
// send to loggers
chanLoggers := c.Loggers()
for i := range chanLoggers {
chanLoggers[i] <- dm
}
}
// cleanup transformers
subprocessors.Reset()
c.LogInfo("run terminated")
c.done <- true
}