-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanda.go
261 lines (229 loc) · 6.45 KB
/
panda.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
package panda
import (
"crypto/tls"
"crypto/x509"
"net/http"
"os"
"time"
"github.com/gorilla/websocket"
"github.com/techerfan/panda/logger"
)
const (
DefaultWebSocketPath = "/ws"
DefaultLogsHeader = "Panda"
DefaultServerAddress = ":8000"
)
type CommunicationType int
const (
JSON CommunicationType = iota
// not implemented yet
BINARY
XML
)
var Upgrader = websocket.Upgrader{
ReadBufferSize: 0,
WriteBufferSize: 0,
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type App struct {
config Config
clients []*Client
newConn chan *Client
// to check if app listens on new connection
isListening bool
// to stop apps from listening on new connections
stopListening chan bool
}
type Config struct {
ServerAddress string
WebSocketPath string
CommunicationType CommunicationType
IsTlSEnabled bool
TlsRootCaPath string
TLSCertPath string
TlSKeyPath string
InsecureSkipVerify bool
// to choose if module print logs or not
DoNotShowLogs bool
// a name that will be showed in logs between [] like [Panda]
Logsheader string
// this handler validates client's connection. If it was nil,
// package will consider that authentication is not needed and
// let the client to establish the connection. It takes a
// token as input and returns a boolean in order to specify whether continue
// or not and a time that shows when the connection should be destroyed.
AuthenticationHandler func(string) (*time.Time, bool)
//This handler decides what to do when a client's ticket is expired.
//If it is nil, there will be no default behavior.
TicketTokenExpirationHandler func(client *Client)
// to use a custom logger.
Logger logger.Logger
}
func NewApp(config ...Config) *App {
app := &App{
config: Config{},
newConn: make(chan *Client),
stopListening: make(chan bool),
}
if len(config) > 0 {
app.config = config[0]
}
if app.config.WebSocketPath == "" {
app.config.WebSocketPath = DefaultWebSocketPath
}
if app.config.Logsheader == "" {
app.config.Logsheader = DefaultLogsHeader
}
if app.config.ServerAddress == "" {
app.config.ServerAddress = DefaultServerAddress
}
if app.config.Logger == nil {
app.config.Logger = logger.New()
}
return app
}
func (a *App) Serve() {
http.HandleFunc(a.config.WebSocketPath, func(rw http.ResponseWriter, r *http.Request) {
var destructionTime *time.Time
var ticket string
if a.config.AuthenticationHandler != nil {
queries := r.URL.Query()
ticket = queries.Get("ticket")
if ticket == "" {
return
}
var isTicketOk bool
destructionTime, isTicketOk = a.config.AuthenticationHandler(ticket)
if !isTicketOk {
rw.WriteHeader(http.StatusUnauthorized)
return
}
}
a.serveWs(rw, r, destructionTime, ticket)
})
a.config.Logger.Info("WebSocket Server is up on: " + a.config.ServerAddress)
if a.config.IsTlSEnabled {
caPem, err := os.ReadFile(a.config.TlsRootCaPath)
if err != nil {
a.config.Logger.Error(err.Error())
}
// Read ca's cert
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(caPem) {
a.config.Logger.Error("could not append ca pem")
}
server := http.Server{
Addr: a.config.ServerAddress,
TLSConfig: &tls.Config{
RootCAs: certPool,
InsecureSkipVerify: a.config.InsecureSkipVerify,
},
}
if err := server.ListenAndServeTLS(a.config.TLSCertPath, a.config.TlSKeyPath); err != nil {
a.config.Logger.Error(err.Error())
}
} else {
if err := http.ListenAndServe(a.config.ServerAddress, nil); err != nil {
a.config.Logger.Error(err.Error())
}
}
}
func (a *App) Broadcast(channelName string, message string, checker ...func(*Client) bool) {
getChannelsInstance(a.config.Logger).getChannelByName(channelName).sendMessageToClients(message, checker...)
}
func (a *App) BroadcastWithCallback(channelName string, callback func(*Client) string, checker ...func(*Client) bool) {
getChannelsInstance(a.config.Logger).getChannelByName(channelName).sendMessageToClientsByCallback(callback, checker...)
}
func (a *App) Destroy(channelName string) {
getChannelsInstance(a.config.Logger).getChannelByName(channelName).destroy()
}
func (a *App) Send(message string) {
for _, cl := range a.clients {
go func(c *Client) {
c.lock.Lock()
defer c.lock.Unlock()
msg, err := newMessage("", message, Raw).marshal()
if err != nil {
a.config.Logger.Error(err.Error())
}
err = c.conn.WriteMessage(websocket.TextMessage, msg)
if err != nil {
a.config.Logger.Error(err.Error())
a.removeClient(c)
}
}(cl)
}
}
func (a *App) NewConnection(callback func(client *Client)) {
// it is not possible to have multiple listeners. so that we must stop
// other listeners (if any exists) and then make a new one.
if a.isListening {
a.stopListening <- true
a.isListening = true
}
go func(app *App) {
app.isListening = true
for {
select {
case newConn := <-app.newConn:
app.clients = append(app.clients, newConn)
go func() {
callback(newConn)
}()
case <-app.stopListening:
return
}
}
}(a)
}
// returns a slice of current clients
func (a *App) GetClients() []*Client {
return a.clients
}
// returns how many clients are connected to the server.
func (a *App) GetClientsCount() int {
return len(a.clients)
}
func (a *App) serveWs(rw http.ResponseWriter, r *http.Request, destructionTime *time.Time, ticket string) {
conn, err := Upgrader.Upgrade(rw, r, nil)
if err != nil {
a.config.Logger.Error(err.Error())
return
}
newCl := newClient(a, a.config.Logger, conn, ticket)
// to close client's connection after the specified time
// it is optionanl to set destruction time so that developer
// can use the package without authentication/authorization.
if destructionTime != nil {
timer := time.NewTimer(time.Until(*destructionTime))
go func() {
<-timer.C
a.removeClient(newCl)
if a.config.TicketTokenExpirationHandler != nil {
a.config.TicketTokenExpirationHandler(newCl)
}
err := newCl.Destroy()
if err != nil {
a.config.Logger.Error(err.Error())
}
}()
}
// whenever a new client joins, we will send it over newConn channel
// but app must listens on new connections.
// we did this because if nobody listens on channel, Go will exit
// the program by code 1.
if a.isListening {
a.newConn <- newCl
}
}
func (a *App) removeClient(c *Client) {
for i, cl := range a.clients {
if cl == c {
a.clients = append(a.clients[:i], a.clients[i+1:]...)
break
}
}
}