-
Notifications
You must be signed in to change notification settings - Fork 2
/
cap.go
173 lines (148 loc) · 3.59 KB
/
cap.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
package main
import (
"context"
"log"
"regexp"
"strings"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/tcpassembly"
"github.com/google/gopacket/tcpassembly/tcpreader"
"github.com/urfave/cli/v2"
)
var conf *Config
type Config struct {
PathFilter *regexp.Regexp
ForceHttp2 bool
Verbose bool
HideNoPath bool
ProtoInc []string
ProtoFile []string
}
func newConfig(ctx *cli.Context) (c *Config, err error) {
c = &Config{
ForceHttp2: ctx.Bool("force"),
Verbose: ctx.Bool("verbose"),
HideNoPath: ctx.Bool("hide-no-path"),
ProtoInc: ctx.StringSlice("proto-include"),
ProtoFile: ctx.StringSlice("proto-file"),
}
if tmp := ctx.String("path-regex"); tmp != "" {
if c.PathFilter, err = regexp.Compile(tmp); err != nil {
return
}
}
// if ctx.Bool("verbose") {
// flag.Set("assembly_debug_log", "true")
// }
return
}
func capAction(ctx *cli.Context) (err error) {
if conf, err = newConfig(ctx); err != nil {
return
}
if pbMgr, err = newPbManager(ctx.Context); err != nil {
return
}
handle, err := pcap.OpenLive(ctx.String("interface"), int32(ctx.Int("snapshot-length")), false, pcap.BlockForever)
if err != nil {
return
}
defer handle.Close()
if ctx.Args().Present() {
if err = handle.SetBPFFilter(strings.Join(ctx.Args().Slice(), " ")); err != nil {
return
}
}
source := gopacket.NewPacketSource(handle, handle.LinkType())
sf := newStreamFactory(ctx)
defer sf.Close()
streamPool := tcpassembly.NewStreamPool(sf)
assembler := tcpassembly.NewAssembler(streamPool)
// use 'flush' to force tcpassembly trigger callback
// if on-the-fly(tcp has been connected)
tick := time.NewTicker(2 * time.Second)
defer tick.Stop()
lastFlush := time.Now()
flush := func() {
if now := time.Now(); now.Sub(lastFlush) >= 3*time.Second {
assembler.FlushWithOptions(tcpassembly.FlushOptions{T: lastFlush})
lastFlush = now
}
}
for {
select {
case <-ctx.Done():
assembler.FlushAll()
if conf.Verbose {
log.Println("got ctx.Done")
}
return
case <-tick.C:
flush()
case packet, ok := <-source.Packets():
flush()
if !ok {
return
}
// ignore other protocol
if packet.NetworkLayer() == nil || packet.TransportLayer() == nil ||
packet.TransportLayer().LayerType() != layers.LayerTypeTCP {
continue
}
netFlow := packet.NetworkLayer().NetworkFlow()
tcp := packet.TransportLayer().(*layers.TCP)
seen := packet.Metadata().Timestamp
// use meeting-room to link two way
tuple := newTcp4Tuple(netFlow, tcp.TransportFlow())
if tcp.RST || tcp.FIN {
sf.rooms.Exit(tuple)
} else {
sf.rooms.Enter(tuple, seen)
}
assembler.AssembleWithTimestamp(netFlow, tcp, seen)
}
}
}
type streamFactory struct {
rooms meetingRooms
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
}
func newStreamFactory(ctx *cli.Context) (sf *streamFactory) {
sf = &streamFactory{
rooms: make(meetingRooms),
}
sf.ctx, sf.cancel = context.WithCancel(ctx.Context)
return
}
func (sf *streamFactory) Close() {
sf.cancel()
sf.rooms.Wait()
sf.wg.Wait()
}
func (sf *streamFactory) New(netFlow, tcpFlow gopacket.Flow) tcpassembly.Stream {
tuple := newTcp4Tuple(netFlow, tcpFlow)
room, ok := sf.rooms[tuple.Key()]
if !ok {
if conf.Verbose {
log.Println(tuple, "no meeting room")
}
// ignore this stream
ret := tcpreader.NewReaderStream()
sf.wg.Add(1)
go func() {
defer sf.wg.Done()
ret.Close()
}()
return &ret
}
s := newTcpStream(tuple, room)
sf.wg.Add(1)
go s.Process(sf.wg.Done)
return s
}