-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (109 loc) · 3.13 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"syscall"
"time"
"github.com/mholt/archiver"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/pcapgo"
)
var (
deviceName string
outputFile string
filter string
pidFile string
durationMinutes int
snapshotLen uint32 = 4096
promiscuous bool = false
err error
timeout time.Duration = -1 * time.Second
handle *pcap.Handle
f *os.File
zip bool
debug bool
)
func init() {
flag.StringVar(&deviceName, "i", "eth0", "interface")
flag.StringVar(&outputFile, "o", "out.pcap", "out file")
flag.StringVar(&filter, "f", "udp and port 5060", "filter")
flag.IntVar(&durationMinutes, "r", 1, "rotate out file every minutes")
flag.BoolVar(&zip, "z", false, "zip rotated file")
flag.BoolVar(&debug, "debug", false, "debug")
flag.StringVar(&pidFile, "p", "/var/run/sniff.pid", "pid file")
}
func copy(src string, dst string) {
data, _ := ioutil.ReadFile(src)
ioutil.WriteFile(src, nil, 0644)
go func() {
ioutil.WriteFile(dst, data, 0644)
archiver.Zip.Make(dst+".zip", []string{dst})
os.Remove(dst)
}()
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
flag.Parse()
writePidFile()
for {
f, _ := os.Create(outputFile)
writer := pcapgo.NewWriter(f)
writer.WriteFileHeader(snapshotLen, layers.LinkTypeEthernet)
// Open the device for capturing
handle, err = pcap.OpenLive(deviceName, int32(snapshotLen), promiscuous, timeout)
if err != nil {
fmt.Printf("Error opening device %s: %v", deviceName, err)
os.Exit(1)
}
err = handle.SetBPFFilter(filter)
if err != nil {
log.Fatal(err)
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
start := time.Now()
fmt.Printf("sniff on %s", deviceName)
for packet := range packetSource.Packets() {
if debug {
fmt.Println(packet)
}
writer.WritePacket(packet.Metadata().CaptureInfo, packet.Data())
if time.Since(start) > time.Minute*time.Duration(durationMinutes) {
break
}
}
now := time.Now().Add(-1 * time.Minute * time.Duration(durationMinutes))
unixTimestamp := now.Format(time.RFC3339)
f.Close()
handle.Close()
copy(outputFile, outputFile+"."+unixTimestamp)
}
}
func writePidFile() error {
// Read in the pid file as a slice of bytes.
if piddata, err := ioutil.ReadFile(pidFile); err == nil {
// Convert the file contents to an integer.
if pid, err := strconv.Atoi(string(piddata)); err == nil {
// Look for the pid in the process list.
if process, err := os.FindProcess(pid); err == nil {
// Send the process a signal zero kill.
if err := process.Signal(syscall.Signal(0)); err == nil {
// We only get an error if the pid isn't running, or it's not ours.
return fmt.Errorf("pid already running: %d", pid)
}
}
}
}
// If we get here, then the pidfile didn't exist,
// or the pid in it doesn't belong to the user running this app.
return ioutil.WriteFile(pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0664)
}