-
Notifications
You must be signed in to change notification settings - Fork 26
/
websocket.go
230 lines (194 loc) · 4.31 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
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package textsecure
import (
"errors"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/golang/protobuf/proto"
"github.com/janimo/textsecure/protobuf"
"golang.org/x/net/websocket"
"crypto/tls"
log "github.com/Sirupsen/logrus"
)
type wsConn struct {
conn *websocket.Conn
id uint64
closing bool
}
var wsconn *wsConn
// set up a tunnel via HTTP CONNECT
// see https://gist.github.com/madmo/8548738
func httpConnect(proxy string, wsConfig *websocket.Config) (net.Conn, error) {
p, err := net.Dial("tcp", proxy)
if err != nil {
return nil, err
}
req := http.Request{
Method: "CONNECT",
URL: &url.URL{},
Host: wsConfig.Location.Host,
}
cc := httputil.NewClientConn(p, nil)
cc.Do(&req)
if err != nil && err != httputil.ErrPersistEOF {
return nil, err
}
conn, _ := cc.Hijack()
return conn, nil
}
func newWSConn(originURL, user, pass string) (*wsConn, error) {
v := url.Values{}
v.Set("login", user)
v.Set("password", pass)
params := v.Encode()
wsURL := strings.Replace(originURL, "http", "ws", 1) + "?" + params
wsConfig, err := websocket.NewConfig(wsURL, originURL)
if err != nil {
return nil, err
}
wsConfig.TlsConfig = &tls.Config{RootCAs: rootCA}
var wsc *websocket.Conn
req := http.Request{
URL: &url.URL{},
}
proxyURL, err := getProxy(&req)
if err != nil {
return nil, err
}
if proxyURL == nil {
wsc, err = websocket.DialConfig(wsConfig)
if err != nil {
return nil, err
}
} else {
conn, err := httpConnect(proxyURL.Host, wsConfig)
if err != nil {
return nil, err
}
if wsConfig.Location.Scheme == "wss" {
conn = tls.Client(conn, wsConfig.TlsConfig)
}
wsc, err = websocket.NewClient(wsConfig, conn)
if err != nil {
return nil, err
}
}
return &wsConn{conn: wsc}, nil
}
func (wsc *wsConn) send(b []byte) error {
return websocket.Message.Send(wsc.conn, b)
}
func (wsc *wsConn) receive() ([]byte, error) {
var b []byte
err := websocket.Message.Receive(wsc.conn, &b)
if err != nil {
return nil, err
}
return b, nil
}
func (wsc *wsConn) close() error {
return wsc.conn.Close()
}
func (wsc *wsConn) sendRequest(verb, path string, body []byte, id *uint64) error {
typ := textsecure.WebSocketMessage_REQUEST
wsm := &textsecure.WebSocketMessage{
Type: &typ,
Request: &textsecure.WebSocketRequestMessage{
Verb: &verb,
Path: &path,
Body: body,
Id: id,
},
}
b, err := proto.Marshal(wsm)
if err != nil {
return err
}
return wsc.send(b)
}
func (wsc *wsConn) keepAlive() {
for {
if wsc.closing {
return
}
err := wsc.sendRequest("GET", "/v1/keepalive", nil, nil)
if err != nil {
log.Error(err)
return
}
time.Sleep(time.Second * 15)
}
}
func (wsc *wsConn) sendAck(id uint64) error {
typ := textsecure.WebSocketMessage_RESPONSE
message := "OK"
status := uint32(200)
wsm := &textsecure.WebSocketMessage{
Type: &typ,
Response: &textsecure.WebSocketResponseMessage{
Id: &id,
Status: &status,
Message: &message,
},
}
b, err := proto.Marshal(wsm)
if err != nil {
return err
}
return wsc.send(b)
}
// StartListening connects to the server and handles incoming websocket messages.
func StartListening() error {
var err error
wsconn, err = newWSConn(config.Server+"/v1/websocket/", config.Tel, registrationInfo.password)
if err != nil {
return err
}
go wsconn.keepAlive()
for {
bmsg, err := wsconn.receive()
if err != nil {
log.Error(err)
break
}
//do not handle and ack the message if closing
if wsconn.closing {
break
}
wsm := &textsecure.WebSocketMessage{}
err = proto.Unmarshal(bmsg, wsm)
if err != nil {
log.Error(err)
continue
}
m := wsm.GetRequest().GetBody()
err = handleReceivedMessage(m)
if err != nil {
log.Error(err)
continue
}
err = wsconn.sendAck(wsm.GetRequest().GetId())
if err != nil {
log.Error(err)
break
}
}
wsconn.close()
return err
}
// ErrNotListening is returned when trying to stop listening when there's no
// valid listening connection set up
var ErrNotListening = errors.New("there is no listening connection to stop")
// StopListening disables the receiving of messages.
func StopListening() error {
if wsconn == nil {
return ErrNotListening
}
wsconn.closing = true
return nil
}