-
Notifications
You must be signed in to change notification settings - Fork 6
/
tls_poller.go
183 lines (147 loc) · 4.21 KB
/
tls_poller.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
"github.com/cilium/ebpf/perf"
"github.com/go-errors/errors"
"github.com/hashicorp/golang-lru/simplelru"
"github.com/kubeshark/tracer/misc"
"github.com/rs/zerolog/log"
)
const (
fdCachedItemAvgSize = 40
fdCacheMaxItems = 500000 / fdCachedItemAvgSize
)
type tlsPoller struct {
tls *Tracer
streams map[string]*tlsStream
closeStreams chan string
chunksReader *perf.Reader
procfs string
fdCache *simplelru.LRU // Actual type is map[string]addressPair
evictedCounter int
sorter *PacketSorter
}
func newTlsPoller(
tls *Tracer,
procfs string,
sorter *PacketSorter,
) (*tlsPoller, error) {
poller := &tlsPoller{
tls: tls,
streams: make(map[string]*tlsStream),
closeStreams: make(chan string, misc.TlsCloseChannelBufferSize),
chunksReader: nil,
procfs: procfs,
sorter: sorter,
}
fdCache, err := simplelru.NewLRU(fdCacheMaxItems, poller.fdCacheEvictCallback)
if err != nil {
return nil, errors.Wrap(err, 0)
}
poller.fdCache = fdCache
return poller, nil
}
func (p *tlsPoller) init(bpfObjects *tracerObjects, bufferSize int) error {
var err error
p.chunksReader, err = perf.NewReader(bpfObjects.ChunksBuffer, bufferSize)
if err != nil {
return errors.Wrap(err, 0)
}
return nil
}
func (p *tlsPoller) close() error {
return p.chunksReader.Close()
}
func (p *tlsPoller) poll(streamsMap *TcpStreamMap) {
// tracerTlsChunk is generated by bpf2go.
chunks := make(chan *tracerTlsChunk)
go p.pollChunksPerfBuffer(chunks)
for {
select {
case chunk, ok := <-chunks:
if !ok {
return
}
if err := p.handleTlsChunk(chunk, streamsMap); err != nil {
logError(err)
}
case key := <-p.closeStreams:
delete(p.streams, key)
}
}
}
func (p *tlsPoller) pollChunksPerfBuffer(chunks chan<- *tracerTlsChunk) {
log.Info().Msg("Start polling for tls events")
for {
record, err := p.chunksReader.Read()
if err != nil {
close(chunks)
if errors.Is(err, perf.ErrClosed) {
return
}
logError(errors.Errorf("Error reading chunks from tls perf, aborting TLS! %v", err))
return
}
if record.LostSamples != 0 {
log.Info().Msg(fmt.Sprintf("Buffer is full, dropped %d chunks", record.LostSamples))
continue
}
buffer := bytes.NewReader(record.RawSample)
var chunk tracerTlsChunk
if err := binary.Read(buffer, binary.LittleEndian, &chunk); err != nil {
logError(errors.Errorf("Error parsing chunk %v", err))
continue
}
chunks <- &chunk
}
}
func (p *tlsPoller) handleTlsChunk(chunk *tracerTlsChunk, streamsMap *TcpStreamMap) error {
address := chunk.getAddressPair()
// Creates one *tlsStream per TCP stream
key := buildTlsKey(address, chunk.isRequest())
stream, streamExists := p.streams[key]
if !streamExists {
stream = NewTlsStream(p, key)
stream.setId(streamsMap.NextId())
streamsMap.Store(stream.getId(), stream)
p.streams[key] = stream
stream.client = NewTlsReader(p.buildTcpId(address, true), stream, true)
stream.server = NewTlsReader(p.buildTcpId(address, false), stream, false)
}
reader := chunk.getReader(stream)
reader.newChunk(chunk)
return nil
}
func buildTlsKey(address *addressPair, isRequest bool) string {
if isRequest {
return fmt.Sprintf("%s:%d>%s:%d", address.srcIp, address.srcPort, address.dstIp, address.dstPort)
} else {
return fmt.Sprintf("%s:%d>%s:%d", address.dstIp, address.dstPort, address.srcIp, address.srcPort)
}
}
func (p *tlsPoller) buildTcpId(address *addressPair, isRequest bool) *TcpID {
if isRequest {
return &TcpID{
SrcIP: address.srcIp.String(),
DstIP: address.dstIp.String(),
SrcPort: strconv.FormatUint(uint64(address.srcPort), 10),
DstPort: strconv.FormatUint(uint64(address.dstPort), 10),
}
} else {
return &TcpID{
SrcIP: address.dstIp.String(),
DstIP: address.srcIp.String(),
SrcPort: strconv.FormatUint(uint64(address.dstPort), 10),
DstPort: strconv.FormatUint(uint64(address.srcPort), 10),
}
}
}
func (p *tlsPoller) fdCacheEvictCallback(key interface{}, value interface{}) {
p.evictedCounter = p.evictedCounter + 1
if p.evictedCounter%1000000 == 0 {
log.Info().Msg(fmt.Sprintf("Tls fdCache evicted %d items", p.evictedCounter))
}
}