-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (98 loc) · 2.61 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/fsnotify/fsnotify"
)
var backoffPlan = []time.Duration{0, 250, 500, 1_000, 2_000, 4_000}
type config struct {
watcher *fsnotify.Watcher
tokenUrl string
linger time.Duration
}
func main() {
var path, tokenUrl string
var linger time.Duration
flag.StringVar(&path, "path", "/tmp", "File or directory to monitor for changes")
flag.StringVar(&tokenUrl, "token-url", "", "Canary token url generated from canarytokens.org to be pinged on events")
flag.DurationVar(&linger, "linger", 1*time.Second, "Time to wait for new events to arrive before pinging the token url")
flag.Parse()
if _, err := os.Stat(path); os.IsNotExist(err) {
flag.Usage()
log.Fatalf("path %s does not exist", path)
}
if _, err := url.ParseRequestURI(tokenUrl); err != nil {
flag.Usage()
log.Fatalf("url %s is invalid: %v", tokenUrl, flag.ErrHelp)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Panicf("could not create watcher: %v", err)
}
defer watcher.Close()
err = watcher.Add(path)
if err != nil {
log.Panicf("could not monitor path: %v", err)
}
c := config{watcher, tokenUrl, linger}
if err := c.startWatchLoop(watcher); err != nil {
log.Panicf("watch loop failed: %v", err)
}
}
func (c *config) startWatchLoop(watcher *fsnotify.Watcher) error {
timers := map[string]*time.Timer{}
for {
select {
case err, ok := <-watcher.Errors:
// Indicates that the Errors channel was closed
if !ok {
return nil
}
return err
case e, ok := <-watcher.Events:
// Indicates that the Events channel was closed
if !ok {
return nil
}
// Ignore CHMOD events as they are too frequent
if e.Has(fsnotify.Chmod) {
continue
}
if t, ok := timers[e.Name]; ok {
t.Stop()
}
timers[e.Name] = time.AfterFunc(c.linger, func() { c.pingWithRetry(e) })
}
}
}
func (c *config) pingWithRetry(event fsnotify.Event) {
req, _ := http.NewRequest("HEAD", c.tokenUrl, nil)
req.Header.Add("X-Canary-Path-Name", event.Name)
req.Header.Add("X-Canary-Path-Op", event.Op.String())
for i, d := range backoffPlan {
time.Sleep(d * time.Millisecond)
err := c.ping(req)
if err == nil {
log.Printf("ping successful for %s", event.Name)
return
}
log.Printf("ping failed on attempt %d: %v", i, err)
}
log.Printf("ping skipped due to earlier failure")
}
func (c *config) ping(req *http.Request) error {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("unexpected status %s", resp.Status)
}
return nil
}