-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
144 lines (133 loc) · 3.23 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
package main
import (
"log"
"net"
"os"
"path/filepath"
"time"
"github.com/rjeczalik/notify"
flag "github.com/spf13/pflag"
)
func isDir(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
fi, err := f.Stat()
if err != nil {
return false
}
return fi.IsDir()
}
// Creates a TCP server which watches a particular file system path provided by
// the user via command line argument. That server accepts any connection and
// whenever it receives any file system event for the directory it's watching,
// it writes a single null byte across all connections.
func main() {
var host = flag.String("host", "0.0.0.0", "Host to attempt to connect to")
var port = flag.String("port", "6754", "Port to attempt to make connection on")
flag.Parse()
// Boilerplate for keeping track of client connections
cons := []net.Conn{}
_addcon := func(c net.Conn) {
cons = append(cons, c)
}
_rmcon := func(c net.Conn) {
idx := -1
// Find index of item in collection
for i, v := range cons {
if c == v {
idx = i
}
}
// If item not found, ignore
if idx == -1 {
return
}
// Remove item from collection
cons = append(cons[:idx], cons[idx+1:]...)
}
rm := make(chan net.Conn)
add := make(chan net.Conn)
go func() {
for {
select {
case c := <-add:
_addcon(c)
case c := <-rm:
_rmcon(c)
}
}
}()
addr := *host + ":" + *port
server, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
// Listen for new connections
go func() {
for {
c, err := server.Accept()
if c == nil || err != nil {
log.Printf("Failed to accept connection %v for reason %q\n", c, err)
continue
}
log.Printf("Recieved a new connection: %v\n", c.RemoteAddr())
// Create a goroutine which sends heartbeat informantion. If
// heartbeat fails, close the connection and stop trying to send
// heartbeat so the connection will be cleaned up by the fs notify
// loop.
go func() {
for {
bw, err := c.Write([]byte{'\x01'})
if err != nil || bw != 1 {
c.Close()
return
}
time.Sleep(10 * time.Second)
}
}()
add <- c
}
}()
// Set up the filesystem monitoring
event_chan := make(chan notify.EventInfo, 1)
path, err := filepath.Abs(os.Args[1])
if err != nil {
log.Fatal("Could not find absolute path for %q: %v", os.Args[1], err)
}
if isDir(path) {
path = path + "/..."
}
if err = notify.Watch(path, event_chan, notify.All); err != nil {
log.Fatalf("Could not register notify on location %q: %v", os.Args[1], err)
}
done := make(chan bool)
// Listen for fs events forever
go func() {
for {
select {
case event := <-event_chan:
badcons := []net.Conn{}
// If we got an event, write a null byte across each connection
for _, c := range cons {
bw, err := c.Write([]byte{'\x00'})
// If we couldn't do either of those, then the connection
// is bad and we should remove that connection from our
// connection list
if err != nil || bw != 1 {
badcons = append(badcons, c)
log.Println("Marking connection as bad:", c.LocalAddr())
}
}
// Remove the bad connections
for _, bc := range badcons {
bc.Close()
rm <- bc
}
log.Println("event:", event)
}
}
}()
<-done
}