/
windsock.go
355 lines (310 loc) · 8.36 KB
/
windsock.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/json"
"errors"
"expvar"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/kelseyhightower/envconfig"
zmq "github.com/pebbe/zmq2"
"golang.org/x/net/websocket"
)
// obviously, this should not be hard-coded in real life:
var SECRET = "6f1d916c-7761-4874-8d5b-8f8f93d20bf2"
var AUTH_WINDOW = 60 * time.Second
var startTime = time.Now().UTC()
// metrics
// messages from the broker
var numMessages = expvar.NewInt("NumZMQMessages")
// currently connected clients
var numClients = expvar.NewInt("NumClients")
// total clients that have been seen
var totalClients = expvar.NewInt("TotalClients")
// bundle a list of online users along with
// in and out channels
type room struct {
Users map[*OnlineUser]bool
Broadcast chan envelope
Incoming chan envelope
}
// how we route zmq messages around
type envelope struct {
Address string `json:"address"`
Content string `json:"content"`
}
func uptime() interface{} {
uptime := time.Since(startTime)
return int64(uptime)
}
func startswith(s, prefix string) bool {
if len(s) < len(prefix) {
return false
}
return s[:len(prefix)] == prefix
}
func (e envelope) RouteTo(u *OnlineUser) bool {
return startswith(e.Address, u.Uci.SubPrefix)
}
var runningRoom *room = &room{}
// listen for messages on a channel
// and route them out them to appropriate users
func (r *room) run() {
log.Debug("room.run()")
for e := range r.Broadcast {
for u := range r.Users {
if e.RouteTo(u) {
u.Send <- e
}
}
}
}
func (r *room) SendMessage(e envelope) {
r.Broadcast <- e
}
func InitRoom() {
log.Debug("InitRoom()")
runningRoom = &room{
Users: make(map[*OnlineUser]bool),
Broadcast: make(chan envelope),
Incoming: make(chan envelope),
}
go runningRoom.run()
}
type OnlineUser struct {
Connection *websocket.Conn
Uci userConnectionInfo
Send chan envelope
}
// loop indefinitely, taking messages on a channel
// and sending them out to the user's websocket
func (this *OnlineUser) PushToClient() {
for e := range this.Send {
err := websocket.JSON.Send(this.Connection, e)
log.Info("sent websocket message")
if err != nil {
break
}
}
}
// loop indefinitely listening for incoming
// messages from a user's websocket
func (this *OnlineUser) PullFromClient() {
for {
var content string
err := websocket.Message.Receive(this.Connection, &content)
if err != nil {
return
}
log.Info("incoming:", content)
runningRoom.Incoming <- envelope{this.Uci.PubPrefix, content}
}
}
type userConnectionInfo struct {
Uni string
SubPrefix string
PubPrefix string
}
// improvements that should be made:
// * include hash function name in the token (so we can swap it in the future)
// * include a version number in the token (to enable backwards compatability)
// * allow a mode where IP address isn't checked
func validateToken(token string, current_time time.Time,
remote_ip net.Addr, uci *userConnectionInfo) error {
// token will look something like this:
// anp8:gobot:gobot.browser.anp8:1344361884:667494:127.0.0.1:306233f64522f1f970fc62fb3cf2d7320c899851
parts := strings.Split(token, ":")
if len(parts) != 7 {
log.WithFields(
log.Fields{
"token": token,
"parts": len(parts),
}).Error("couldn't parse token")
return errors.New("invalid token")
}
// their UNI
uni := parts[0]
sub_prefix := parts[1]
pub_prefix := parts[2]
uci.Uni = uni
uci.SubPrefix = sub_prefix
uci.PubPrefix = pub_prefix
// UNIX timestamp
now, err := strconv.Atoi(parts[3])
if err != nil {
log.WithFields(log.Fields{
"token": token,
"timestamp_field": parts[3],
"now": now,
"error": err,
}).Error("invalid timestamp in token")
return errors.New("invalid timestamp in token")
}
// a random salt
salt := parts[4]
ip_address := parts[5]
// the hmac of those parts with our shared secret
hmc := parts[6]
// make sure we're within a 60 second window
token_time := time.Unix(int64(now), 0)
if current_time.Sub(token_time) > time.Duration(AUTH_WINDOW) {
log.Error("stale token")
return errors.New("stale token")
}
// TODO: check that their ip address matches
// PROBLEM: remote_ip is something like: "http://127.0.0.1:8000"
// instead of "127.0.0.1", so we still need to figure out how
// to get the IP address out of there (and make sure it is the right
// end of the connection)
// if remote_ip.String() != ip_address {
// fmt.Printf("%s %s\n",remote_ip.String(), ip_address)
// return uni, errors.New("remote address doesn't match token")
// }
// check that the HMAC matches
h := hmac.New(sha1.New, []byte(SECRET))
h.Write([]byte(fmt.Sprintf("%s:%s:%s:%d:%s:%s", uni, sub_prefix, pub_prefix, now, salt, ip_address)))
sum := fmt.Sprintf("%x", h.Sum(nil))
if sum != hmc {
log.WithFields(log.Fields{
"token": token,
"expected": hmc,
"calculated": sum,
}).Error("token HMAC doesn't match")
return errors.New("token HMAC doesn't match")
}
return nil
}
func BuildConnection(ws *websocket.Conn) {
log.Info("BuildConnection()")
token := ws.Request().URL.Query().Get("token")
log.Debug(token)
var uci userConnectionInfo
err := validateToken(token, time.Now(), ws.RemoteAddr(), &uci)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("validation error")
// how should this reply to the client?
return
}
onlineUser := &OnlineUser{
Connection: ws,
Uci: uci,
Send: make(chan envelope, 256),
}
runningRoom.Users[onlineUser] = true
numClients.Add(1)
totalClients.Add(1)
go onlineUser.PushToClient()
onlineUser.PullFromClient()
delete(runningRoom.Users, onlineUser)
numClients.Add(-1)
log.Info("tore down user connection")
}
// listen on a zmq SUB socket
// and shovel messages from it out to the websockets
func zmqToWebsocket(subsocket zmq.Socket) {
for {
address, _ := subsocket.Recv(0)
content, _ := subsocket.Recv(0)
log.WithFields(log.Fields{
"address": string(address),
"content": string(content),
}).Info("received a zmq message")
runningRoom.SendMessage(envelope{string(address), string(content)})
numMessages.Add(1)
}
}
// send a message to the zmq PUB socket
func sendMessage(reqsocket zmq.Socket, e envelope) {
serialized_envelope, _ := json.Marshal(e)
reqsocket.Send(string(serialized_envelope), 0)
// wait for a reply
reqsocket.Recv(0)
}
// take messages from the Incoming channel
// and just shovel them out to the zmq PUB socket
func websocketToZmq(reqsocket zmq.Socket) {
for msg := range runningRoom.Incoming {
// this could potentially be done async:
sendMessage(reqsocket, msg)
}
}
type ConfigData struct {
Secret string
SubSocket string
ReqSocket string
WebSocketPort string
SubKey string
Certificate string
Key string
}
type config struct {
LogLevel string `envconfig:"LOG_LEVEL"`
}
func main() {
log.SetLevel(log.InfoLevel)
var configfile string
flag.StringVar(&configfile, "config", "./windsock.json", "Windsock JSON config file")
flag.Parse()
file, err := ioutil.ReadFile(configfile)
if err != nil {
log.Fatal(err)
}
f := ConfigData{}
err = json.Unmarshal(file, &f)
if err != nil {
log.Fatal(err)
}
SECRET = f.Secret
var c config
err = envconfig.Process("windsock", &c)
if err != nil {
log.Fatal(err.Error())
}
// defaults to INFO
if c.LogLevel == "DEBUG" {
log.SetLevel(log.DebugLevel)
}
if c.LogLevel == "WARN" {
log.SetLevel(log.WarnLevel)
}
if c.LogLevel == "ERROR" {
log.SetLevel(log.ErrorLevel)
}
if c.LogLevel == "FATAL" {
log.SetLevel(log.FatalLevel)
}
subsocket, _ := zmq.NewSocket(zmq.SUB)
reqsocket, _ := zmq.NewSocket(zmq.REQ)
defer reqsocket.Close()
defer subsocket.Close()
reqsocket.Connect(f.ReqSocket)
subsocket.SetSubscribe(f.SubKey)
subsocket.Connect(f.SubSocket)
InitRoom()
// two goroutines to move messages in each direction
go websocketToZmq(*reqsocket)
go zmqToWebsocket(*subsocket)
http.Handle("/socket/", websocket.Handler(BuildConnection))
expvar.Publish("Uptime", expvar.Func(uptime))
if f.Certificate != "" && f.Key != "" {
// configured for SSL
log.Info("starting WSS on ", f.WebSocketPort)
err = http.ListenAndServeTLS(f.WebSocketPort, f.Certificate, f.Key, nil)
} else {
log.Info("starting WS on ", f.WebSocketPort)
err = http.ListenAndServe(f.WebSocketPort, nil)
}
if err != nil {
log.Fatal("ListenAndServe: " + err.Error())
}
}