forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
349 lines (307 loc) · 9.07 KB
/
websocket.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
package main
import (
"errors"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency"
)
// Const vars for websocket
const (
WebsocketResponseSuccess = "OK"
)
// WebsocketClient stores information related to the websocket client
type WebsocketClient struct {
ID int
Conn *websocket.Conn
LastRecv time.Time
Authenticated bool
}
// WebsocketEvent is the struct used for websocket events
type WebsocketEvent struct {
Exchange string `json:"exchange,omitempty"`
AssetType string `json:"assetType,omitempty"`
Event string
Data interface{}
}
// WebsocketEventResponse is the struct used for websocket event responses
type WebsocketEventResponse struct {
Event string `json:"event"`
Data interface{} `json:"data"`
Error string `json:"error"`
}
// WebsocketOrderbookTickerRequest is a struct used for ticker and orderbook
// requests
type WebsocketOrderbookTickerRequest struct {
Exchange string `json:"exchangeName"`
Currency string `json:"currency"`
AssetType string `json:"assetType"`
}
// WebsocketClientHub stores an array of websocket clients
var WebsocketClientHub []WebsocketClient
// WebsocketClientHandler upgrades the HTTP connection to a websocket
// compatible one
func WebsocketClientHandler(w http.ResponseWriter, r *http.Request) {
connectionLimit := bot.config.Webserver.WebsocketConnectionLimit
numClients := len(WebsocketClientHub)
if numClients >= connectionLimit {
log.Printf("Websocket client rejected due to websocket client limit reached. Number of clients %d. Limit %d.",
numClients, connectionLimit)
w.WriteHeader(http.StatusForbidden)
return
}
upgrader := websocket.Upgrader{
WriteBufferSize: 1024,
ReadBufferSize: 1024,
}
// Allow insecure origin if the Origin request header is present and not
// equal to the Host request header. Default to false
if bot.config.Webserver.WebsocketAllowInsecureOrigin {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
}
newClient := WebsocketClient{
ID: len(WebsocketClientHub),
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
newClient.Conn = conn
WebsocketClientHub = append(WebsocketClientHub, newClient)
numClients++
log.Printf("New websocket client connected. Connected clients: %d. Limit %d.",
numClients, connectionLimit)
}
// DisconnectWebsocketClient disconnects a websocket client
func DisconnectWebsocketClient(id int, err error) {
for i := range WebsocketClientHub {
if WebsocketClientHub[i].ID == id {
WebsocketClientHub[i].Conn.Close()
WebsocketClientHub = append(WebsocketClientHub[:i], WebsocketClientHub[i+1:]...)
log.Printf("Disconnected Websocket client, error: %s", err)
return
}
}
}
// SendWebsocketMessage sends a websocket message to a specific client
func SendWebsocketMessage(id int, data interface{}) error {
for _, x := range WebsocketClientHub {
if x.ID == id {
return x.Conn.WriteJSON(data)
}
}
return nil
}
// BroadcastWebsocketMessage broadcasts a websocket event message to all
// websocket clients
func BroadcastWebsocketMessage(evt WebsocketEvent) error {
for _, x := range WebsocketClientHub {
x.Conn.WriteJSON(evt)
}
return nil
}
// WebsocketAuth is a struct used for
type WebsocketAuth struct {
Username string `json:"username"`
Password string `json:"password"`
}
type wsCommandHandler func(wsClient *websocket.Conn, data interface{}) error
var wsHandlers = map[string]wsCommandHandler{
"getconfig": wsGetConfig,
"saveconfig": wsSaveConfig,
"getaccountinfo": wsGetAccountInfo,
"gettickers": wsGetTickers,
"getticker": wsGetTicker,
"getorderbooks": wsGetOrderbooks,
"getorderbook": wsGetOrderbook,
"getexchangerates": wsGetExchangeRates,
"getportfolio": wsGetPortfolio,
}
func wsGetConfig(wsClient *websocket.Conn, data interface{}) error {
wsResp := WebsocketEventResponse{
Event: "GetConfig",
Data: bot.config,
}
return wsClient.WriteJSON(wsResp)
}
func wsSaveConfig(wsClient *websocket.Conn, data interface{}) error {
wsResp := WebsocketEventResponse{
Event: "SaveConfig",
}
var cfg config.Config
err := common.JSONDecode(data.([]byte), &cfg)
if err != nil {
wsResp.Error = err.Error()
err = wsClient.WriteJSON(wsResp)
if err != nil {
return err
}
}
err = bot.config.UpdateConfig(bot.configFile, cfg)
if err != nil {
wsResp.Error = err.Error()
err = wsClient.WriteJSON(wsResp)
if err != nil {
return err
}
}
setupBotExchanges()
wsResp.Data = WebsocketResponseSuccess
return wsClient.WriteJSON(wsResp)
}
func wsGetAccountInfo(wsClient *websocket.Conn, data interface{}) error {
accountInfo := GetAllEnabledExchangeAccountInfo()
wsResp := WebsocketEventResponse{
Event: "GetAccountInfo",
Data: accountInfo,
}
return wsClient.WriteJSON(wsResp)
}
func wsGetTickers(wsClient *websocket.Conn, data interface{}) error {
wsResp := WebsocketEventResponse{
Event: "GetTickers",
}
wsResp.Data = GetAllActiveTickers()
return wsClient.WriteJSON(wsResp)
}
func wsGetTicker(wsClient *websocket.Conn, data interface{}) error {
wsResp := WebsocketEventResponse{
Event: "GetTicker",
}
var tickerReq WebsocketOrderbookTickerRequest
err := common.JSONDecode(data.([]byte), &tickerReq)
if err != nil {
wsResp.Error = err.Error()
wsClient.WriteJSON(wsResp)
return err
}
result, err := GetSpecificTicker(tickerReq.Currency,
tickerReq.Exchange, tickerReq.AssetType)
if err != nil {
wsResp.Error = err.Error()
wsClient.WriteJSON(wsResp)
return err
}
wsResp.Data = result
return wsClient.WriteJSON(wsResp)
}
func wsGetOrderbooks(wsClient *websocket.Conn, data interface{}) error {
wsResp := WebsocketEventResponse{
Event: "GetOrderbooks",
}
wsResp.Data = GetAllActiveOrderbooks()
return wsClient.WriteJSON(wsResp)
}
func wsGetOrderbook(wsClient *websocket.Conn, data interface{}) error {
wsResp := WebsocketEventResponse{
Event: "GetOrderbook",
}
var orderbookReq WebsocketOrderbookTickerRequest
err := common.JSONDecode(data.([]byte), &orderbookReq)
if err != nil {
wsResp.Error = err.Error()
wsClient.WriteJSON(wsResp)
return err
}
result, err := GetSpecificOrderbook(orderbookReq.Currency,
orderbookReq.Exchange, orderbookReq.AssetType)
if err != nil {
wsResp.Error = err.Error()
wsClient.WriteJSON(wsResp)
return err
}
wsResp.Data = result
return wsClient.WriteJSON(wsResp)
}
func wsGetExchangeRates(wsClient *websocket.Conn, data interface{}) error {
wsResp := WebsocketEventResponse{
Event: "GetExchangeRates",
}
if currency.YahooEnabled {
wsResp.Data = currency.CurrencyStore
} else {
wsResp.Data = currency.CurrencyStoreFixer
}
return wsClient.WriteJSON(wsResp)
}
func wsGetPortfolio(wsClient *websocket.Conn, data interface{}) error {
wsResp := WebsocketEventResponse{
Event: "GetPortfolio",
}
wsResp.Data = bot.portfolio.GetPortfolioSummary()
return wsClient.WriteJSON(wsResp)
}
// WebsocketHandler Handles websocket client requests
func WebsocketHandler() {
for {
for x := range WebsocketClientHub {
var evt WebsocketEvent
err := WebsocketClientHub[x].Conn.ReadJSON(&evt)
if err != nil {
DisconnectWebsocketClient(x, err)
continue
}
if evt.Event == "" {
DisconnectWebsocketClient(x, errors.New("Websocket client sent data we did not understand"))
continue
}
dataJSON, err := common.JSONEncode(evt.Data)
if err != nil {
log.Println(err)
continue
}
req := common.StringToLower(evt.Event)
log.Printf("Websocket req: %s", req)
if !WebsocketClientHub[x].Authenticated && evt.Event != "auth" {
wsResp := WebsocketEventResponse{
Event: "auth",
Error: "you must authenticate first",
}
SendWebsocketMessage(x, wsResp)
DisconnectWebsocketClient(x, errors.New("Websocket client did not auth"))
continue
} else if !WebsocketClientHub[x].Authenticated && evt.Event == "auth" {
var auth WebsocketAuth
err = common.JSONDecode(dataJSON, &auth)
if err != nil {
log.Println(err)
continue
}
hashPW := common.HexEncodeToString(common.GetSHA256([]byte(bot.config.Webserver.AdminPassword)))
if auth.Username == bot.config.Webserver.AdminUsername && auth.Password == hashPW {
WebsocketClientHub[x].Authenticated = true
wsResp := WebsocketEventResponse{
Event: "auth",
Data: WebsocketResponseSuccess,
}
SendWebsocketMessage(x, wsResp)
log.Println("Websocket client authenticated successfully")
continue
} else {
wsResp := WebsocketEventResponse{
Event: "auth",
Error: "invalid username/password",
}
SendWebsocketMessage(x, wsResp)
DisconnectWebsocketClient(x, errors.New("Websocket client sent wrong username/password"))
continue
}
}
result, ok := wsHandlers[req]
if !ok {
log.Printf("Websocket unsupported event")
continue
}
err = result(WebsocketClientHub[x].Conn, dataJSON)
if err != nil {
log.Printf("Websocket request %s failed. Error %s", evt.Event, err)
continue
}
}
time.Sleep(time.Millisecond)
}
}