-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
229 lines (208 loc) · 7.26 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
MIT License
Copyright (c) 2018 Phillip Smith
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"fmt"
"flag"
"log"
"os"
"path"
"time"
"github.com/rjeczalik/notify"
"github.com/gobwas/glob"
"gopkg.in/gomail.v2"
)
/*
* STRUCTS
*/
type watchPath struct {
title string // name of the watch
path []string // path(s) to watch
pattern []string // pattern(s) to match
exclude []string // pattern(s) to exclude
notify bool // should we notify for files that match?
qtine bool // should we quarantine files that match?
qdest string // destination to quarantine files to
}
type notification struct {
dest string // recipient of notification
subject string // subject of notificaton
msg string // notification text
ts int64 // timestamp notification was created
}
/*
* MAIN
*/
var gconf global_config
var watches []watchPath
var version string // set at build time by -ldflags
var pending_notifs []notification
func main() {
// process command line arguments
var conf_fname string
var show_version bool
flag.StringVar(&conf_fname, "config", default_conf_fname, "configuration file to load")
flag.BoolVar(&show_version, "version", false, "show version information")
flag.Parse()
if show_version {
fmt.Println("fscanary", version)
fmt.Println("Copyright 2018 Phillip Smith. Licensed under MIT license")
os.Exit(0)
}
// load configuration file
gconf, watches = load_config(conf_fname)
// Validate configuration
for _,watch := range watches {
if watch.qtine {
// must have a valid destination to quarantine to
if len(watch.qdest) == 0 {
// dest dir no configured!
log.Fatal(fmt.Sprintf(
"'%s' has quarantine enabled but no destination directory configured",
watch.title))
}
if x,_ := path_is_dir(watch.qdest); x != true {
// dest dir does not exist
log.Fatal(fmt.Sprintf(
"'%s' quarantine destination '%s' does not exist",
watch.title, watch.qdest))
}
}
}
/*
* Make the channel buffered to ensure no event is dropped. Notify will drop
* an event if the receiver is not able to keep up the sending pace.
*/
chNotify := make(chan notify.EventInfo, 16)
chQuit := make(chan int)
logmsg(1, "Creating watches. This may take a long time for large directory trees. Please wait for Ready message.")
for _,watch := range watches {
logmsg(2, fmt.Sprintln("Adding watch: ", watch.title))
for _,path := range watch.path {
logmsg(2, fmt.Sprintln(" Path: ", path))
addWatch(chNotify, path)
}
}
defer notify.Stop(chNotify)
// Block until an event is received.
logmsg(0,"Ready")
for {
select {
case ei := <-chNotify:
logmsg(5, fmt.Sprintln("Got event:", ei))
handle_event(ei)
case <-chQuit:
logmsg(1, "QUIT")
os.Exit(0)
}
// async sending of pending_notifs
go send_pending_notifs()
}
}
/******************************************************************************
* FUNCTIONS
*****************************************************************************/
func addWatch(ch chan notify.EventInfo, path string) {
if err := notify.Watch(path + string(os.PathSeparator) + "...", ch, notify.Create|notify.Write|notify.Rename); err != nil {
log.Fatal(err)
}
}
/*
* examines a notify.EventInfo and compares it against our
* watch configuration for matching files.
*/
func handle_event(ei notify.EventInfo) {
var event_verb string
switch ei.Event() {
case notify.Create: event_verb = "Created"
case notify.Write: event_verb = "Modified"
case notify.Rename: event_verb = "Renamed"
default: event_verb = "Unknown"
}
_ = event_verb
// abort if file is actually a directory
if x,_ := path_is_dir(ei.Path()); x == true {
logmsg(5, fmt.Sprintf("Skipping Event (is directory): %s", ei.Path()))
return
}
// strip leading path fom ei.Path() so we can compare patterns against the
// filename only rather than the full path
fname := path.Base(ei.Path())
for _,watch := range watches {
// check for matching patterns
compare_paths:
for _,pattern := range watch.pattern {
logmsg(2, fmt.Sprintf("Comparing '%s' to '%s'", fname, pattern))
var g_path glob.Glob
g_path = glob.MustCompile(pattern)
if g_path.Match(fname) {
for _,exclude := range watch.exclude {
var g_exclude glob.Glob
g_exclude = glob.MustCompile(exclude)
if g_exclude.Match(fname) {
logmsg(5, fmt.Sprintf("File '%s' match exclude pattern '%s'; skipping", fname, exclude))
continue compare_paths
}
}
logmsg(4, "Match found")
if watch.notify {
// generate a notification
myhostname,_ := os.Hostname()
logmsg(1, fmt.Sprintf("Generating notification for %s event on file '%s'",
event_verb, ei.Path()))
var n notification
n.dest = gconf.email
n.subject = fmt.Sprintf("File Matching '%s'", watch.title)
n.msg = fmt.Sprintf("The following file matching pattern '%s' was saved on host %s\n\n%s",
pattern, myhostname, ei.Path())
t := time.Now().UTC()
n.ts = t.Unix()
pending_notifs = append(pending_notifs, n)
}
if watch.qtine {
// quarantine the file
logmsg(1, fmt.Sprintf("Quarantining '%s' to '%s'",
ei.Path(), watch.qdest))
err := moveFile(ei.Path(), watch.qdest + ei.Path())
if err != nil {
fmt.Println(err)
return
}
}
break
}
}
}
logmsg(3, fmt.Sprintf("Finished examining '%s'", ei.Path()))
}
func send_pending_notifs() {
curr_notifs := pending_notifs // copy pending_notifs to a new slice
pending_notifs = nil // erase the global pending_notifs slice
for _, n := range curr_notifs {
m := gomail.NewMessage()
m.SetHeader("From", gconf.smtp_from)
m.SetHeader("To", n.dest)
m.SetHeader("Subject", n.subject)
m.SetBody("text/plain", n.msg)
logmsg(1, fmt.Sprintf("Sending notification '%s' to '%s'", n.subject, n.dest))
d := gomail.Dialer{Host: gconf.smtp_server, Port: gconf.smtp_port}
if err := d.DialAndSend(m); err != nil { panic(err) }
}
}