-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
111 lines (90 loc) · 2.16 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
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"sort"
"strings"
"sync"
"syscall"
"github.com/Sirupsen/logrus"
)
const (
InterfacesFlag = "ifs"
PortFlag = "p"
)
var (
Log = logrus.New()
stop = make(chan struct{})
IfaceList = NewInterfaceList()
ifs = flag.String(InterfacesFlag, "", "Comma separated list of interfaces to watch.")
port = flag.Int(PortFlag, 8001, "HTTP server listening port.")
)
func withLogging(f func()) {
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("Recovered from panic(%+v)", r)
Log.WithField("error", err).Panicf("Stopped with panic: %s", err.Error())
}
}()
f()
}
func main() {
flag.Parse()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
for sig := range c {
Log.Infof("Signalled (%s). Shutting down.", sig)
Log.WithField("signal", sig).Infof("Signalled. Shutting down.")
shutdown(0)
}
}()
splitIfs := strings.Split(*ifs, ",")
if *ifs == "" || len(splitIfs) == 0 {
Log.Infof("At least one interface to watch must be provided via the -%s command line argument.", InterfacesFlag)
os.Exit(1)
}
// Get a list of all interfaces.
ifaces, err := net.Interfaces()
if err != nil {
Log.WithField("error", err).Fatalf("Error getting the list of interfaces.")
}
sort.Strings(splitIfs)
var wg sync.WaitGroup
iCount := 0
for _, iface := range ifaces {
i := sort.SearchStrings(splitIfs, iface.Name)
if i < len(splitIfs) && splitIfs[i] == iface.Name {
wg.Add(1)
iCount++
// Start up a watch on each interface.
go func(iface net.Interface) {
defer wg.Done()
if err := watch(iface); err != nil {
Log.WithFields(logrus.Fields{
"error": err,
"interface": iface.Name,
}).Errorf("Error watching interface.")
}
}(iface)
}
}
if iCount == 0 {
Log.Infof("Exited. No valid interfaces provided.")
os.Exit(1)
}
go func() {
if err = <-StartHTTPServer(*port); err != nil {
Log.WithField("error", err).Fatal("Error starting HTTP server.")
}
}()
wg.Wait()
Log.Infof("Exited.")
}
func shutdown(code int) {
Log.WithField("code", code).Infof("Stopping.")
close(stop)
}