-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.go
64 lines (52 loc) · 1.21 KB
/
utils.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
//go:build !ja3_disable_gopacket
package ja3
import (
"fmt"
"os"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
)
// PacketSource means we can read Packets.
type PacketSource interface {
ReadPacketData() ([]byte, gopacket.CaptureInfo, error)
}
func openPcap(file string) (PacketSource, *os.File, layers.LinkType, error) {
// get file handle
f, err := os.Open(file)
if err != nil {
panic(err)
}
var (
reader PacketSource
linkType layers.LinkType
)
// try to create pcap reader
pcapReader, errPcap := pcapgo.NewReader(f)
if errPcap != nil {
// maybe its a PCAPNG
// reopen file, otherwise offsets will be wrong
err = f.Close()
if err != nil {
panic(err)
}
f, err = os.Open(file)
if err != nil {
panic(err)
}
// try to create pcapng reader
ngReader, errPcapNg := pcapgo.NewNgReader(f, pcapgo.DefaultNgReaderOptions)
if errPcapNg != nil {
// nope
fmt.Println("pcap error:", errPcap)
fmt.Println("pcap-ng error:", errPcapNg)
panic("cannot open PCAP file")
}
linkType = ngReader.LinkType()
reader = ngReader
} else {
linkType = pcapReader.LinkType()
reader = pcapReader
}
return reader, f, linkType, err
}