forked from sipcapture/heplify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (128 loc) · 5.25 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"flag"
"fmt"
"os"
"strconv"
"sync"
"github.com/negbie/logp"
"github.com/sipcapture/heplify/config"
"github.com/sipcapture/heplify/sniffer"
)
const version = "heplify 1.63"
func createFlags() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Use %s like: %s [option]\n", version, os.Args[0])
flag.PrintDefaults()
}
var (
err error
ifaceConfig config.InterfacesConfig
logging logp.Logging
fileRotator logp.FileRotator
dbg string
std bool
sys bool
fNum int
fSize uint64
)
flag.StringVar(&ifaceConfig.Device, "i", "any", "Listen on interface")
flag.StringVar(&ifaceConfig.Type, "t", "pcap", "Capture types are [pcap, af_packet]")
flag.UintVar(&ifaceConfig.FanoutID, "fg", 0, "Fanout group ID for af_packet")
flag.IntVar(&ifaceConfig.FanoutWorker, "fw", 4, "Fanout worker count for af_packet")
flag.StringVar(&ifaceConfig.ReadFile, "rf", "", "Read pcap file")
flag.StringVar(&ifaceConfig.WriteFile, "wf", "", "Path to write pcap file")
flag.IntVar(&ifaceConfig.RotationTime, "rt", 60, "Pcap rotation time in minutes")
flag.BoolVar(&config.Cfg.Zip, "zf", false, "Enable pcap compression")
flag.IntVar(&ifaceConfig.Loop, "lp", 1, "Loop count over ReadFile. Use 0 to loop forever")
flag.BoolVar(&ifaceConfig.ReadSpeed, "rs", false, "Use packet timestamps with maximum pcap read speed")
flag.IntVar(&ifaceConfig.Snaplen, "s", 8192, "Snaplength")
flag.StringVar(&ifaceConfig.PortRange, "pr", "5060-5090", "Portrange to capture SIP")
flag.StringVar(&ifaceConfig.CustomBPF, "bpf", "", "Custom BPF to capture packets")
flag.BoolVar(&ifaceConfig.WithVlan, "vlan", false, "vlan")
flag.BoolVar(&ifaceConfig.WithErspan, "erspan", false, "erspan")
flag.IntVar(&ifaceConfig.BufferSizeMb, "b", 32, "Interface buffersize (MB)")
flag.StringVar(&dbg, "d", "", "Enable certain debug selectors [defrag,layer,payload,rtp,rtcp,sdp]")
flag.BoolVar(&std, "e", false, "Log to stderr and disable syslog/file output")
flag.BoolVar(&sys, "sl", false, "Log to syslog")
flag.StringVar(&logging.Level, "l", "info", "Log level [debug, info, warning, error]")
flag.BoolVar(&ifaceConfig.OneAtATime, "o", false, "Read packet for packet")
flag.StringVar(&fileRotator.Path, "p", "./", "Log filepath")
flag.StringVar(&fileRotator.Name, "n", "heplify.log", "Log filename")
flag.IntVar(&fNum, "fnum", 7, "The total num of log files to keep")
flag.Uint64Var(&fSize, "fsize", 10*1024*1024, "The rotate size per log file based on byte")
flag.StringVar(&config.Cfg.Mode, "m", "SIPRTCP", "Capture modes [SIP, SIPDNS, SIPLOG, SIPRTCP]")
flag.BoolVar(&config.Cfg.Dedup, "dd", false, "Deduplicate packets")
flag.StringVar(&config.Cfg.Discard, "di", "", "Discard uninteresting packets by any string")
flag.StringVar(&config.Cfg.DiscardMethod, "dim", "", "Discard uninteresting SIP packets by CSeq [OPTIONS,NOTIFY]")
flag.StringVar(&config.Cfg.DiscardSrcIP, "disip", "", "Discard uninteresting SIP packets by Source IP(s)")
flag.StringVar(&config.Cfg.Filter, "fi", "", "Filter interesting packets by any string")
flag.StringVar(&config.Cfg.HepCollector, "hin", "", "HEP collector address [udp:127.0.0.1:9093]")
flag.BoolVar(&config.Cfg.CollectOnlySip, "collectonlysip", false, "collect only sip")
flag.StringVar(&config.Cfg.HepServer, "hs", "127.0.0.1:9060", "HEP server address")
flag.StringVar(&config.Cfg.HepNodePW, "hp", "", "HEP node PW")
flag.UintVar(&config.Cfg.HepNodeID, "hi", 2002, "HEP node ID")
flag.StringVar(&config.Cfg.HepNodeName, "hn", "", "HEP node Name")
flag.StringVar(&config.Cfg.Network, "nt", "udp", "Network types are [udp, tcp, tls]")
flag.BoolVar(&config.Cfg.Protobuf, "protobuf", false, "Use Protobuf on wire")
flag.BoolVar(&config.Cfg.Reassembly, "tcpassembly", false, "If true, tcpassembly will be enabled")
flag.UintVar(&config.Cfg.SendRetries, "tcpsendretries", 64, "Number of retries for sending before giving up and reconnecting")
flag.BoolVar(&config.Cfg.Version, "version", false, "Show heplify version")
flag.Parse()
config.Cfg.Iface = &ifaceConfig
logp.ToStderr = &std
logging.ToSyslog = &sys
logp.DebugSelectorsStr = &dbg
fileRotator.KeepFiles = &fNum
fileRotator.RotateEveryBytes = &fSize
logging.Files = &fileRotator
config.Cfg.Logging = &logging
if config.Cfg.HepNodeID > 0xFFFFFFFE {
config.Cfg.HepNodeID = 0xFFFFFFFE
}
config.Cfg.Discard, err = strconv.Unquote(`"` + config.Cfg.Discard + `"`)
checkErr(err)
config.Cfg.Filter, err = strconv.Unquote(`"` + config.Cfg.Filter + `"`)
checkErr(err)
}
func checkErr(err error) {
if err != nil {
fmt.Printf("\nError: %v\n\n", err)
}
}
func checkCritErr(err error) {
if err != nil {
fmt.Printf("\nCritical: %v\n\n", err)
os.Exit(1)
}
}
func main() {
createFlags()
if config.Cfg.Version {
fmt.Println(version)
os.Exit(0)
}
err := logp.Init("heplify", config.Cfg.Logging)
checkCritErr(err)
worker := 1
if config.Cfg.Iface.Type == "af_packet" &&
config.Cfg.Iface.FanoutID > 0 && config.Cfg.Iface.FanoutWorker > 1 {
worker = config.Cfg.Iface.FanoutWorker
}
var wg sync.WaitGroup
for i := 0; i < worker; i++ {
capture, err := sniffer.New(&config.Cfg)
checkCritErr(err)
defer func() {
err = capture.Close()
checkCritErr(err)
}()
wg.Add(1)
go func() {
err = capture.Run()
checkCritErr(err)
wg.Done()
}()
}
wg.Wait()
}