/
websocket.go
273 lines (246 loc) · 7.37 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
// Copyright (c) 2021 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package appservice
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"path/filepath"
"sync"
"sync/atomic"
"github.com/gorilla/websocket"
)
type WebsocketRequest struct {
ReqID int `json:"id,omitempty"`
Command string `json:"command"`
Data interface{} `json:"data"`
}
type WebsocketCommand struct {
ReqID int `json:"id,omitempty"`
Command string `json:"command"`
Data json.RawMessage `json:"data"`
}
func (wsc *WebsocketCommand) MakeResponse(data interface{}) *WebsocketRequest {
return &WebsocketRequest{
ReqID: wsc.ReqID,
Command: "response",
Data: data,
}
}
type WebsocketTransaction struct {
Status string `json:"status"`
TxnID string `json:"txn_id"`
Transaction
}
type WebsocketMessage struct {
WebsocketTransaction
WebsocketCommand
}
type MeowWebsocketCloseCode string
const (
MeowServerShuttingDown MeowWebsocketCloseCode = "server_shutting_down"
MeowConnectionReplaced MeowWebsocketCloseCode = "conn_replaced"
)
var (
WebsocketManualStop = errors.New("the websocket was disconnected manually")
WebsocketOverridden = errors.New("a new call to StartWebsocket overrode the previous connection")
WebsocketUnknownError = errors.New("an unknown error occurred")
)
func (mwcc MeowWebsocketCloseCode) String() string {
switch mwcc {
case MeowServerShuttingDown:
return "the server is shutting down"
case MeowConnectionReplaced:
return "the connection was replaced by another client"
default:
return string(mwcc)
}
}
type CloseCommand struct {
Code int `json:"-"`
Command string `json:"command"`
Status MeowWebsocketCloseCode `json:"status"`
}
func (cc CloseCommand) Error() string {
return fmt.Sprintf("websocket: close %d: %s", cc.Code, cc.Status.String())
}
func parseCloseError(err error) error {
closeError := &websocket.CloseError{}
if !errors.As(err, &closeError) {
return err
}
var closeCommand CloseCommand
closeCommand.Code = closeError.Code
closeCommand.Command = "disconnect"
if len(closeError.Text) > 0 {
jsonErr := json.Unmarshal([]byte(closeError.Text), &closeCommand)
if jsonErr != nil {
return err
}
}
if len(closeCommand.Status) == 0 {
if closeCommand.Code == 4001 {
closeCommand.Status = MeowConnectionReplaced
} else if closeCommand.Code == websocket.CloseServiceRestart {
closeCommand.Status = MeowServerShuttingDown
}
}
return &closeCommand
}
func (as *AppService) SendWebsocket(cmd *WebsocketRequest) error {
if as.ws == nil {
return errors.New("websocket not connected")
}
as.wsWriteLock.Lock()
defer as.wsWriteLock.Unlock()
return as.ws.WriteJSON(cmd)
}
func (as *AppService) addWebsocketResponseWaiter(reqID int, waiter chan<- *WebsocketCommand) {
as.websocketRequestsLock.Lock()
as.websocketRequests[reqID] = waiter
as.websocketRequestsLock.Unlock()
}
func (as *AppService) removeWebsocketResponseWaiter(reqID int, waiter chan<- *WebsocketCommand) {
as.websocketRequestsLock.Lock()
existingWaiter, ok := as.websocketRequests[reqID]
if ok && existingWaiter == waiter {
delete(as.websocketRequests, reqID)
}
close(waiter)
as.websocketRequestsLock.Unlock()
}
type ErrorResponse struct {
Code string `json:"code"`
Message string `json:"message"`
}
func (er *ErrorResponse) Error() string {
return fmt.Sprintf("%s: %s", er.Code, er.Message)
}
func (as *AppService) RequestWebsocket(ctx context.Context, cmd *WebsocketRequest, response interface{}) error {
cmd.ReqID = int(atomic.AddInt32(&as.websocketRequestID, 1))
respChan := make(chan *WebsocketCommand, 1)
as.addWebsocketResponseWaiter(cmd.ReqID, respChan)
defer as.removeWebsocketResponseWaiter(cmd.ReqID, respChan)
err := as.SendWebsocket(cmd)
if err != nil {
return err
}
select {
case resp := <-respChan:
if resp.Command == "error" {
var respErr ErrorResponse
err = json.Unmarshal(resp.Data, &respErr)
if err != nil {
return fmt.Errorf("failed to parse error JSON: %w", err)
}
return &respErr
} else if response != nil {
err = json.Unmarshal(resp.Data, &response)
if err != nil {
return fmt.Errorf("failed to parse response JSON: %w", err)
}
return nil
} else {
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
func (as *AppService) consumeWebsocket(stopFunc func(error), ws *websocket.Conn) {
defer stopFunc(WebsocketUnknownError)
for {
var msg WebsocketMessage
err := ws.ReadJSON(&msg)
if err != nil {
as.Log.Debugln("Error reading from websocket:", err)
stopFunc(parseCloseError(err))
return
}
if msg.Command == "" || msg.Command == "transaction" {
as.handleTransaction(&msg.Transaction)
} else if msg.Command == "connect" {
as.Log.Debugln("Websocket connect confirmation received")
} else if msg.Command == "response" || msg.Command == "error" {
as.websocketRequestsLock.RLock()
respChan, ok := as.websocketRequests[msg.ReqID]
if ok {
select {
case respChan <- &msg.WebsocketCommand:
default:
as.Log.Warnfln("Failed to handle response to %d: channel didn't accept response", msg.ReqID)
}
} else {
as.Log.Warnfln("Dropping response to %d: unknown request ID", msg.ReqID)
}
as.websocketRequestsLock.RUnlock()
} else {
select {
case as.WebsocketCommands <- msg.WebsocketCommand:
default:
as.Log.Warnfln("Dropping websocket command %s %d / %s", msg.Command, msg.ReqID, msg.Data)
}
}
}
}
func (as *AppService) StartWebsocket(baseURL string, onConnect func()) error {
parsed, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("failed to parse URL: %w", err)
}
parsed.Path = filepath.Join(parsed.Path, "_matrix/client/unstable/fi.mau.as_sync")
if parsed.Scheme == "http" {
parsed.Scheme = "ws"
} else if parsed.Scheme == "https" {
parsed.Scheme = "wss"
}
ws, resp, err := websocket.DefaultDialer.Dial(parsed.String(), http.Header{
"Authorization": []string{fmt.Sprintf("Bearer %s", as.Registration.AppToken)},
"User-Agent": []string{as.BotClient().UserAgent},
})
if resp != nil && resp.StatusCode >= 400 {
var errResp Error
err = json.NewDecoder(resp.Body).Decode(&errResp)
if err != nil {
return fmt.Errorf("websocket request returned HTTP %d with non-JSON body", resp.StatusCode)
} else {
return fmt.Errorf("websocket request returned %s (HTTP %d): %s", errResp.ErrorCode, resp.StatusCode, errResp.Message)
}
} else if err != nil {
return fmt.Errorf("failed to open websocket: %w", err)
}
if as.StopWebsocket != nil {
as.StopWebsocket(WebsocketOverridden)
}
closeChan := make(chan error)
closeChanSync := sync.Once{}
stopFunc := func(err error) {
closeChanSync.Do(func() {
closeChan <- err
})
}
as.ws = ws
as.StopWebsocket = stopFunc
as.PrepareWebsocket()
as.Log.Debugln("Appservice transaction websocket connected")
go as.consumeWebsocket(stopFunc, ws)
if onConnect != nil {
onConnect()
}
closeErr := <-closeChan
err = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""))
if err != nil && err != websocket.ErrCloseSent {
as.Log.Warnln("Error writing close message to websocket:", err)
}
err = ws.Close()
if err != nil {
as.Log.Warnln("Error closing websocket:", err)
}
return closeErr
}