-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn.go
More file actions
93 lines (83 loc) · 2.21 KB
/
Copy pathconn.go
File metadata and controls
93 lines (83 loc) · 2.21 KB
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
// Supports Windows, Linux, Mac, and Raspberry Pi
// Going to add SSL
package main
import (
"log"
"net/http"
"strings"
"github.com/3devo/dvconnector/models"
"github.com/3devo/dvconnector/utils"
"github.com/gorilla/websocket"
"github.com/julienschmidt/httprouter"
)
type connection struct {
// The websocket connection.
ws *websocket.Conn
// Buffered channel of outbound messages.
send chan []byte
authenticated bool
}
func (c *connection) reader(env *utils.Env) {
for {
_, message, err := c.ws.ReadMessage()
if err != nil {
break
}
if c.authenticated {
h.broadcast <- message
} else {
auth := strings.SplitN(string(message), " ", 2)
if len(auth) == 2 && auth[0] == "login" && !c.authenticated {
_, err := utils.ValidateJWTToken(auth[1])
if err != nil {
c.ws.WriteMessage(websocket.TextMessage, []byte("unauthorized"))
c.ws.Close()
return
}
blacklist := models.BlackListedToken{}
if err := env.Db.One("Token", auth[1], &blacklist); err == nil {
c.ws.WriteMessage(websocket.TextMessage, []byte("unauthorized"))
c.ws.Close()
return
}
c.ws.WriteMessage(websocket.TextMessage, []byte("Access granted"))
c.authenticated = true
} else if !c.authenticated {
c.ws.WriteMessage(websocket.TextMessage, []byte("unauthorized"))
c.ws.Close()
}
}
}
c.ws.Close()
}
func (c *connection) writer(env *utils.Env) {
for message := range c.send {
if c.authenticated {
err := c.ws.WriteMessage(websocket.TextMessage, message)
if err != nil {
break
}
} else {
c.ws.WriteMessage(websocket.TextMessage, []byte("unauthorized"))
}
}
c.ws.Close()
}
func wsHandle(env *utils.Env) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.Print("Started a new websocket handler")
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(w, "Not a websocket handshake", 400)
return
} else if err != nil {
return
}
//c := &connection{send: make(chan []byte, 256), ws: ws}
c := &connection{send: make(chan []byte, 256*10), ws: ws}
h.register <- c
defer func() { h.unregister <- c }()
go c.writer(env)
c.reader(env)
}
}