-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
138 lines (127 loc) · 2.78 KB
/
manager.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
)
var (
webSocketUpgrader = websocket.Upgrader{
CheckOrigin: checkOrigin,
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
type Manager struct {
client ClientList
sync.RWMutex
otps RetentionMap
handlers map[string]EventHandler
}
func NewManager(ctx context.Context) *Manager {
return &Manager{
client: make(ClientList),
handlers: make(map[string]EventHandler),
otps: NewRetentionMap(ctx, 5*time.Second),
}
}
func (m *Manager) setupEventHandlers() {
m.handlers[EventSendMessage] = SendMessage
}
func SendMessage(event Event, c *Client) error {
fmt.Println(event)
message, err := event.Payload.MarshalJSON()
if err != nil {
log.Println(err)
}
c.connection.WriteMessage(websocket.TextMessage, message)
return nil
}
func (m *Manager) routeEvent(event Event, c *Client) error {
if handler, ok := m.handlers[event.Type]; ok {
if err := handler(event, c); err != nil {
return err
}
return nil
} else {
return errors.New("there is no such event")
}
}
func (m *Manager) serveWs(w http.ResponseWriter, r *http.Request) {
otp := r.URL.Query().Get("otp")
if otp == "" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if !m.otps.VerifyOTP(otp) {
w.WriteHeader(http.StatusUnauthorized)
return
}
log.Println("New Connection")
//Upgrade Regular HTTP to Websocket
conn, err := webSocketUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
client := NewClient(conn, m)
m.addClient(client)
//start client messages
go client.readMessages()
go client.writeMessages()
}
func (m *Manager) loginHandler(w http.ResponseWriter, r *http.Request) {
type userLoginRequest struct {
UserName string `json:"username"`
Password string `json:"password"`
}
var req userLoginRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
if req.UserName == "vacaramin" && req.Password == "password" {
type response struct {
OTP string `json:"otp"`
}
otp := m.otps.NewOTP()
resp := response{
OTP: otp.Key,
}
data, err := json.Marshal(resp)
if err != nil {
log.Println(err)
}
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
w.WriteHeader(http.StatusUnauthorized)
}
func (m *Manager) addClient(client *Client) {
m.Lock()
defer m.Unlock()
m.client[client] = true
}
func (m *Manager) removeClient(client *Client) {
m.Lock()
defer m.Unlock()
if _, ok := m.client[client]; ok {
client.connection.Close()
delete(m.client, client)
}
m.client[client] = false
}
func checkOrigin(r *http.Request) bool {
origin := r.Header.Get("Origin")
switch origin {
case "http://localhost:8080":
return true
default:
return false
}
}