-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (154 loc) · 3.79 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"arplogger/internal/arp"
"arplogger/internal/help"
)
var (
flagPrint2Log = flag.Bool("log", false, "Store ARP activity to log file.")
flagPrint2Console = flag.Bool("console", false, "Print to console.")
flagLogAll = flag.Bool("all", false, "Log all ARP activity.")
flagLogNewPair = flag.Bool("new", false, "Log only new ARP pairs: IP <-> MAC.")
flagIntf = flag.String("interface", "", "Network interface to listen for ARP packets.")
)
func genFilename() (string, error) {
const layout = "2006-01-02_15.04.05.999999999"
var root string
switch runtime.GOOS {
case "darwin":
u, _ := user.Current()
root = filepath.Join(u.HomeDir, "Library", "Logs")
case "linux":
root = "/var/log"
default:
root = os.TempDir()
}
logpath := filepath.Join(root, "arplogger")
if err := os.MkdirAll(logpath, os.ModePerm); err != nil {
return "", err
}
logFileName := fmt.Sprintf("%s_%s.log", "arplogger", time.Now().Format(layout))
return filepath.Join(logpath, logFileName), nil
}
func parseFlags() bool {
flag.Parse()
if flag.NFlag() == 0 {
fmt.Print(help.Usage)
return false
}
if !*flagPrint2Console && !*flagPrint2Log {
fmt.Println("No output is defined. Please, specify -console and/or -log.")
return false
}
if !*flagLogAll && !*flagLogNewPair {
fmt.Println("No logging mode is defined. Please, specify -all and/or -new")
return false
}
if len(*flagIntf) == 0 {
fmt.Println("No network interface is specified. " +
"Please, use -interface to specify source interface to read network packets.")
return false
}
return true
}
func getMode() int {
var mode int
if *flagLogAll {
mode |= arp.LogAllPackets
}
if *flagLogNewPair {
mode |= arp.LogNewPairs
}
return mode
}
func getDest() int {
var dest int
if *flagPrint2Console {
dest |= arp.Print2Console
}
if *flagPrint2Log {
dest |= arp.Print2Log
}
return dest
}
func getLogFilename(dest *int) string {
var filename string
if *dest&arp.Print2Log == arp.Print2Log {
var err error
if filename, err = genFilename(); err != nil {
fmt.Printf("Can't create log file due to error %s. Default printing to console.", err)
*dest |= arp.Print2Console
}
}
return filename
}
func openInterface(intf string) (*pcap.Handle, error) {
// max packet size: 64K
const snaplen = 64*1024 - 1
handle, err := pcap.OpenLive(intf, snaplen, true, pcap.BlockForever)
if err != nil {
return nil, err
}
if err := handle.SetBPFFilter("arp"); err != nil {
return nil, err
}
return handle, nil
}
func processPackets(ctx context.Context, handle *pcap.Handle, logger *arp.Logger) {
src := gopacket.NewPacketSource(handle, layers.LayerTypeEthernet)
in := src.Packets()
read:
for {
var packet gopacket.Packet
select {
case <-ctx.Done():
break read
case packet = <-in:
layer := packet.Layer(layers.LayerTypeARP)
if layer == nil {
continue
}
logger.Log(layer.(*layers.ARP))
}
}
}
func main() {
if !parseFlags() {
return
}
var (
mode = getMode()
dest = getDest()
)
filename := getLogFilename(&dest)
logger, err := arp.NewLogger(dest, mode, filename)
if err != nil {
fmt.Printf("Failed to create logger due to error: %s.", err)
return
}
defer logger.Close()
handle, err := openInterface(*flagIntf)
if err != nil {
fmt.Printf("Failed to open network interface due to error: %s.", err)
return
}
defer handle.Close()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
if dest&arp.Print2Log == arp.Print2Log {
fmt.Println("Saving to log file: ", filename)
}
fmt.Println("Waiting for incoming ARP packets...")
processPackets(ctx, handle, logger)
}