This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
forked from Rhymen/go-whatsapp
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
keepalive.go
218 lines (195 loc) · 5.12 KB
/
keepalive.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
package whatsapp
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"strconv"
"sync"
"time"
"github.com/gorilla/websocket"
)
func (wac *Conn) keepAlive(ws *websocketWrapper, minIntervalMs int, maxIntervalMs int) {
wac.log.Debugfln("Websocket keepalive loop starting %p", ws)
defer func() {
wac.log.Debugfln("Websocket keepalive loop exiting %p", ws)
ws.Done()
}()
ws.keepAliveErrorCount = 0
for {
if ws.pingInKeepalive > 0 {
go wac.keepAliveAdminTest(ws)
}
err := wac.sendKeepAlive(ws)
if err != nil {
ws.keepAliveErrorCount += 1
wac.log.Errorfln("Websocket keepalive for %p failed (error #%d): %v", ws, ws.keepAliveErrorCount, err)
if errors.Is(err, ErrConnectionTimeout) {
if ws.keepAliveErrorCount > 4 {
go wac.handle(ErrWebsocketKeepaliveFailed)
return
}
continue
} else if errors.Is(err, websocket.ErrCloseSent) {
return
}
} else if ws.keepAliveErrorCount > 0 {
wac.log.Debugfln("Websocket keepalive for %p is working again after %d errors", ws, ws.keepAliveErrorCount)
ws.keepAliveErrorCount = 0
}
interval := rand.Intn(maxIntervalMs-minIntervalMs) + minIntervalMs
select {
case <-time.After(time.Duration(interval) * time.Millisecond):
case <-ws.keepAliveShortCircuit:
case <-ws.ctx.Done():
return
}
}
}
func (wac *Conn) keepAliveAdminTest(ws *websocketWrapper) {
if wac.ws != ws {
wac.log.Warnln("keepAliveAdminTest was called with wrong websocket wrapper (got %p, current is %p)", ws, wac.ws)
return
}
err := wac.AdminTest()
if err != nil {
wac.log.Warnln("Keepalive admin test failed:", err)
if errors.Is(err, ErrPingFalse) {
wac.handle(err)
}
} else {
wac.ws.pingInKeepalive--
if wac.ws.pingInKeepalive <= 0 {
wac.log.Infoln("Keepalive admin test successful, not pinging anymore")
} else {
wac.log.Infofln("Keepalive admin test successful, stopping pings after %d more successes", wac.ws.pingInKeepalive)
}
}
}
func (wac *Conn) sendKeepAlive(ws *websocketWrapper) error {
respChan := make(chan string, 1)
wac.listener.add(respChan, nil, false, "!")
bytes := []byte("?,,")
err := ws.write(websocket.TextMessage, bytes)
if err != nil {
wac.listener.pop("!")
close(respChan)
return fmt.Errorf("error sending keepAlive: %w", err)
}
select {
case resp := <-respChan:
msecs, err := strconv.ParseInt(resp, 10, 64)
if err != nil {
return fmt.Errorf("Error converting time string to uint: %w", err)
}
wac.ServerLastSeen = time.Unix(msecs/1000, (msecs%1000)*int64(time.Millisecond))
case <-time.After(wac.msgTimeout):
return ErrConnectionTimeout
case <-ws.ctx.Done():
return nil
}
return nil
}
func (wac *Conn) AdminTest() error {
return wac.AdminTestWithSuppress(false)
}
func (wac *Conn) AdminTestWithSuppress(suppressHook bool) error {
if !wac.connected {
return ErrNotConnected
}
if !wac.loggedIn {
return ErrNotLoggedIn
}
err := wac.sendAdminTest()
if !suppressHook && wac.AdminTestHook != nil {
wac.AdminTestHook(err)
}
return err
}
type adminTestWait struct {
sync.Mutex
input *inputWaiter
output []chan error
done bool
result error
}
func newAdminTestWait() *adminTestWait {
input := make(chan string, 1)
atw := &adminTestWait{
output: make([]chan error, 0),
input: &inputWaiter{ch: input, resend: nil},
}
go atw.wait(input)
return atw
}
func (atw *adminTestWait) wait(input <-chan string) {
atw.result = atw.handleResp(<-input)
atw.done = true
atw.Lock()
for _, ch := range atw.output {
ch <- atw.result
}
atw.output = nil
atw.Unlock()
}
func (atw *adminTestWait) handleResp(resp string) error {
var response interface{}
if err := json.Unmarshal([]byte(resp), &response); err != nil {
return fmt.Errorf("error decoding response message: %w", err)
}
if respArr, ok := response.([]interface{}); ok && len(respArr) == 2 && respArr[0].(string) == "Pong" {
if respArr[1].(bool) == true {
return nil
} else {
return ErrPingFalse
}
}
return fmt.Errorf("unexpected ping response: %s", resp)
}
func (atw *adminTestWait) Listen() <-chan error {
atw.Lock()
ch := make(chan error, 1)
if atw.done {
ch <- atw.result
} else {
atw.output = append(atw.output, ch)
}
atw.Unlock()
return ch
}
func (wac *Conn) CountTimeout() {
if wac.ws != nil {
wac.ws.countTimeout()
if wac.CountTimeoutHook != nil {
wac.CountTimeoutHook(wac.ws.keepAliveErrorCount)
}
}
}
const adminTest = `["admin","test"]`
func (wac *Conn) sendAdminTest() error {
wac.atwLock.Lock()
if wac.atw == nil || wac.atw.done {
wac.atw = newAdminTestWait()
}
atw := wac.atw
wac.atwLock.Unlock()
messageTag := fmt.Sprintf("%d.--%d", time.Now().Unix(), wac.msgCount)
wac.listener.addWaiter(atw.input, messageTag, true)
wac.log.Debugln("Sending admin test request with tag", messageTag)
bytes := []byte(fmt.Sprintf("%s,%s", messageTag, adminTest))
err := wac.ws.write(websocket.TextMessage, bytes)
if err != nil {
return fmt.Errorf("error sending admin test: %w", err)
}
wac.msgCount++
select {
case err = <-atw.Listen():
return err
case <-time.After(wac.msgTimeout):
wac.CountTimeout()
if wac.ws.keepAliveErrorCount > 0 {
return ErrWebsocketKeepaliveFailed
}
return ErrConnectionTimeout
}
}