-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (158 loc) · 4.36 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
"os"
"time"
)
const defaultConfigPath = "/etc/goTrack.yaml"
func main() {
// Define command-line flags
intervalFlag := pflag.DurationP("interval", "i", time.Duration(0), "Interval for polling USB devices")
debugFlag := pflag.BoolP("debug", "d", false, "Debug mode: Print debugging notes")
verboseFlag := pflag.BoolP("verbose", "x", false, "Print state at start")
noExecFlag := pflag.BoolP("noExec", "n", false, "Do not execute on device detection")
helpFlag := pflag.BoolP("help", "h", false, "Show help text")
commandFlag := pflag.StringP("command", "c", "", "Command (chain) to be executed on device change detection")
commandArgFlag := pflag.StringP("arguments", "a", "", "Command arguments")
configPathFlag := pflag.StringP("configPath", "p", "", "Path to config")
versionFlag := pflag.BoolP("version", "v", false, "Print version text")
// Parse command-line flags
pflag.Parse()
// Show help text if help flag is provided
if *helpFlag {
showHelp()
return
}
// Show version text
if *versionFlag {
fmt.Println("Version: goTrack 1.0")
return
}
var configFilePath string
if len(*configPathFlag) != 0 {
tempPath := *configPathFlag
if fileExists(tempPath) {
configFilePath = tempPath
}
}
if len(configFilePath) == 0 && fileExists(defaultConfigPath) {
configFilePath = defaultConfigPath
}
var config *Config
var err error
debug := *debugFlag
if len(configFilePath) != 0 {
// Load configuration from file
config, err = NewConfigFromFile(configFilePath)
if err != nil {
NewConfig().logErr(err)
return
}
//Move old log
if fileExists(config.LogFile) {
if config.OldLogs < 1 {
err := os.Remove(config.LogFile)
if err != nil {
NewConfig().logErr(err)
}
} else {
for i := config.OldLogs - 1; i >= 1; i-- {
oldName := fmt.Sprintf("%s.%d", config.LogFile, i)
if fileExists(oldName) {
newName := fmt.Sprintf("%s.%d", config.LogFile, i+1)
err := os.Rename(oldName, newName)
if err != nil {
NewConfig().logErr(err)
}
}
}
if fileExists(config.LogFile) {
newName := fmt.Sprintf("%s.%d", config.LogFile, 1)
err := os.Rename(config.LogFile, newName)
if err != nil {
NewConfig().logErr(err)
}
}
}
}
} else {
config = NewConfig()
if debug {
config.log("Using default config")
}
}
// Override interval with command-line flag if provided
if *intervalFlag != 0 {
config.Interval = *intervalFlag
}
// Overwrite command with command-line flag if provided
if len(*commandFlag) != 0 {
config.Commands = nil
config.Commands = append(config.Commands, Command{
Command: *commandFlag,
Args: nil,
})
// Overwrite command with command-line flag if provided
if len(*commandArgFlag) != 0 {
config.Commands[0].Args = append(config.Commands[0].Args, *commandArgFlag)
}
}
// Do not execute if true
noExec := *noExecFlag
// Print Config for debugging
if debug {
yamlData, err := yaml.Marshal(&config)
if err != nil {
config.logErr(err)
}
config.log(string(yamlData))
}
// Create file lock if activated
if config.FileLock {
createEmptyFileIfMissing(config.FileLockPath)
}
// Convert verboseFlag to var
verbose := *verboseFlag
config.printAndLog("Waiting for " + config.StartDelay.String() + " at: " + time.Now().Format("15:04:05.00")) // hh:mm:ss,ss
// Delay execution before start
time.Sleep(config.StartDelay)
// Start ticker
ticker := time.NewTicker(config.Interval)
defer ticker.Stop()
// Create USB tracker with loaded configuration
var usbTracker *USBTracker
if config.USBTracking {
usbTracker = NewUSBTracker(config)
usbTracker.InitUSBDevices(verbose)
}
var pingTracker *PingTracker
if config.PingTracking {
pingTracker = NewPingTracker(config)
}
var webTracker *WebTracker
if config.WebTracking {
webTracker = NewWebTracker(config)
}
// Create tracker with loaded configuration
config.log("Started tracking at: " + time.Now().Format("15:04:05.00"))
for {
select {
case <-ticker.C:
if config.USBTracking {
go usbTracker.TrackUSBDevices(noExec, debug)
}
if config.PingTracking {
go pingTracker.TrackPingTargets(noExec, debug)
}
if config.WebTracking {
go webTracker.TrackWebSources(noExec, debug)
}
}
}
}
func showHelp() {
fmt.Println("Usage: goTrack [OPTIONS]")
pflag.PrintDefaults()
}