-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.go
58 lines (52 loc) · 1.19 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
package gourmet
import (
"errors"
"log"
"net"
"os"
"strconv"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
func checkIfInterfaceExists(iface string) error {
devices, err := pcap.FindAllDevs()
if err != nil {
log.Fatal(err)
}
for _, device := range devices {
if device.Name == iface {
return nil
}
}
return errors.New("specified network interface does not exist")
}
func getInterfaceAddresses(interfaceName string) (addresses []string) {
i, err := net.InterfaceByName(interfaceName)
if err != nil {
// this should never happen. If it does, our sensor is broken and needs to die...
panic("interface invalid")
}
addrs, err := i.Addrs()
if err != nil {
panic("interface invalid")
}
for _, addr := range addrs {
addresses = append(addresses, addr.String())
}
return addresses
}
func processPorts(transport gopacket.Flow) (srcPort, dstPort int) {
srcPort, _ = strconv.Atoi(transport.Src().String())
dstPort, _ = strconv.Atoi(transport.Dst().String())
return srcPort, dstPort
}
func dirExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}