This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
connhandler.go
229 lines (214 loc) · 5.6 KB
/
connhandler.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
package main
import (
"fmt"
"runtime/debug"
"sync"
"github.com/apex/log"
"github.com/gorilla/websocket"
)
// ConnInfo - stores some info about a connection
type ConnInfo struct {
mel *Melodious
connection *websocket.Conn
messageStream chan<- BaseMessage
subscriptions *sync.Map
loggedIn bool
username string
}
// HasFlag - checks if the given connection has the given flag
func (connInfo *ConnInfo) HasFlag(channel string, flag string) (bool, error) {
if !connInfo.loggedIn {
return false, nil
}
has, err := connInfo.mel.Database.HasFlag(connInfo.username, channel, flag)
return has, err
}
// HasPerm - checks if the given connection has the given permission
func (connInfo *ConnInfo) HasPerm(channel string, flag string) (bool, error) {
if !connInfo.loggedIn {
return false, nil
}
owner, err := connInfo.mel.Database.IsUserOwner(connInfo.username)
if err != nil {
return false, err
}
if owner {
return true, nil
}
has, err := connInfo.mel.Database.HasFlag(connInfo.username, channel, flag)
return has, err
}
// HasPermChID - checks if the given connection has the given permission
func (connInfo *ConnInfo) HasPermChID(channel int, flag string) (bool, error) {
if !connInfo.loggedIn {
return false, nil
}
owner, err := connInfo.mel.Database.IsUserOwner(connInfo.username)
if err != nil {
return false, err
}
if owner {
return true, nil
}
has, err := connInfo.mel.Database.HasFlagChID(connInfo.username, channel, flag)
return has, err
}
// handleConnection Handles users which are connected to Melodious
func handleConnection(mel *Melodious, conn *websocket.Conn) {
messageStream := make(chan BaseMessage)
connInfo := &ConnInfo{
mel: mel,
connection: conn,
messageStream: messageStream,
subscriptions: &sync.Map{},
loggedIn: false,
username: "<unknown>",
}
mh := wrapMessageHandler(mel, connInfo, messageHandler)
connDead := make(chan bool)
conn.SetCloseHandler(func(code int, text string) error {
connDead <- true
if connInfo.loggedIn {
mel.RemoveConnection(connInfo.username, connInfo)
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"username": connInfo.username,
}).Info("somebody has disconnected")
}
log.WithFields(log.Fields{
"code": code,
"text": text,
"addr": conn.RemoteAddr().String(),
}).Info("one of my connections is closed now")
event := &MessageUserQuit{Username: connInfo.username}
mel.IterateOverAllConnections(func(connInfo *ConnInfo) {
connInfo.messageStream <- event
})
return nil
})
running := true
// receiver
go func() {
for running {
func() {
defer func() {
if err := recover(); err != nil {
messageStream <- &MessageFatal{Message: fmt.Sprintf("%v", err)}
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
"err": err,
}).Error("panic while receiving a message")
debug.PrintStack()
running = false
}
}()
if !running {
return
}
var iface map[string]interface{}
err := conn.ReadJSON(&iface)
if err != nil {
messageStream <- &MessageFatal{Message: "invalid JSON received"}
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
"err": err,
}).Error("cannot read a JSON message")
}
if !running {
return
}
msg, err := LoadMessage(iface)
if err != nil {
messageStream <- &MessageFatal{Message: err.Error()}
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
"err": err,
}).Error("cannot process a JSON message")
return
}
switch msg.(type) {
case *MessageQuit:
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
}).Info("somebody wants to disconnect")
running = false
conn.Close()
}
go mh(msg)
}()
}
}()
// sender
go func() {
// server info
conn.WriteJSON(map[string]interface{}{
"type": "server-info",
"server-name": mel.Config.ServerName,
"version": "indev",
})
for running {
select {
case _ = <-connDead:
running = false
case msg := <-messageStream:
func() {
defer func() {
if err := recover(); err != nil {
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
"err": err,
}).Error("panic while sending a message")
debug.PrintStack()
running = false
conn.Close()
}
}()
if !running {
return
}
iface, err := MessageToIface(msg)
if err != nil {
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
"err": err,
}).Error("cannot convert message to a map[string]interface{}")
return
}
if !running {
return
}
err = conn.WriteJSON(iface)
if err != nil {
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
"err": err,
}).Error("unable to write JSON message")
}
switch msg.(type) {
case *MessageQuit:
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
}).Info("disconnecting somebody")
running = false
conn.Close()
case *MessageFatal:
log.WithFields(log.Fields{
"addr": conn.RemoteAddr().String(),
"name": connInfo.username,
}).Info("fatal error")
running = false
conn.Close()
}
}()
}
}
}()
}