forked from signal-golang/textsecure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
199 lines (169 loc) · 3.78 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
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package textsecure
import (
"fmt"
"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
}
// 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{InsecureSkipVerify: true}
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) {
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) 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
}
wsc.send(b)
return nil
}
func (wsc *wsConn) keepAlive() {
for {
err := wsc.sendRequest("GET", "/v1/keepalive", nil, nil)
if err != nil {
log.Error(err)
}
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
}
wsc.send(b)
return nil
}
// ListenForMessages connects to the server and handles incoming websocket messages.
func ListenForMessages() error {
wsc, err := newWSConn(config.Server+"/v1/websocket/", config.Tel, registrationInfo.password)
if err != nil {
return fmt.Errorf("Could not establish websocket connection: %s\n", err)
}
go wsc.keepAlive()
for {
bmsg, err := wsc.receive()
if err != nil {
log.Error(err)
time.Sleep(3 * time.Second)
continue
}
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 = wsc.sendAck(wsm.GetRequest().GetId())
if err != nil {
log.Error(err)
}
}
}