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
/
Copy pathwrite.go
169 lines (135 loc) · 3.73 KB
/
write.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
package whatsapp
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/Rhymen/go-whatsapp/binary"
"github.com/Rhymen/go-whatsapp/crypto/cbc"
)
type websocketWrapper struct {
sync.Mutex
sync.WaitGroup
conn *websocket.Conn
ctx context.Context
cancel func()
pingInKeepalive int
keepAliveErrorCount int
keepAliveShortCircuit chan struct{}
}
func (wsw *websocketWrapper) countTimeout() {
if wsw.pingInKeepalive < 10 {
wsw.pingInKeepalive++
}
if wsw.pingInKeepalive == 1 {
// For the first timeout, short-circuit the keepalive loop so a ping is sent immediately
select {
case wsw.keepAliveShortCircuit <- struct{}{}:
default:
}
}
}
func newWebsocketWrapper(conn *websocket.Conn) *websocketWrapper {
ctx, cancel := context.WithCancel(context.Background())
return &websocketWrapper{
conn: conn,
ctx: ctx,
cancel: cancel,
keepAliveShortCircuit: make(chan struct{}),
}
}
func (wsw *websocketWrapper) write(messageType int, data []byte) error {
if wsw == nil || wsw.conn == nil {
return ErrInvalidWebsocket
}
wsw.Lock()
err := wsw.conn.WriteMessage(messageType, data)
wsw.Unlock()
if err != nil {
return fmt.Errorf("error writing to websocket: %w", err)
}
return nil
}
func (wac *Conn) writeJSON(data []interface{}) (<-chan string, error) {
ch, _, err := wac.writeJSONRetry(data, false)
return ch, err
}
//writeJSON enqueues a json message into the writeChan
func (wac *Conn) writeJSONRetry(data []interface{}, isResendable bool) (<-chan string, ResendFunc, error) {
ch := make(chan string, 1)
d, err := json.Marshal(data)
if err != nil {
close(ch)
return ch, nil, err
}
ts := time.Now().Unix()
messageTag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
bytes := []byte(fmt.Sprintf("%s,%s", messageTag, d))
if wac.timeTag == "" {
tss := fmt.Sprintf("%d", ts)
wac.timeTag = tss[len(tss)-3:]
}
resend := func() error {
return wac.ws.write(websocket.TextMessage, bytes)
}
wac.listener.add(ch, resend, isResendable, messageTag)
err = resend()
if err != nil {
wac.listener.pop(messageTag)
close(ch)
return ch, nil, err
}
wac.msgCount++
return ch, resend, nil
}
func (wac *Conn) writeBinary(node binary.Node, metric metric, flag flag, messageTag string) (<-chan string, error) {
ch, _, err := wac.writeBinaryRetry(node, metric, flag, messageTag, false)
return ch, err
}
func (wac *Conn) writeBinaryRetry(node binary.Node, metric metric, flag flag, messageTag string, isResendable bool) (<-chan string, ResendFunc, error) {
ch := make(chan string, 1)
if len(messageTag) < 2 {
close(ch)
return ch, nil, ErrMissingMessageTag
}
data, err := wac.encryptBinaryMessage(node)
if err != nil {
close(ch)
return ch, nil, fmt.Errorf("encryptBinaryMessage(node) failed: %w", err)
}
bytes := []byte(messageTag + ",")
bytes = append(bytes, byte(metric), byte(flag))
bytes = append(bytes, data...)
resend := func() error {
return wac.ws.write(websocket.BinaryMessage, bytes)
}
wac.listener.add(ch, resend, isResendable, messageTag)
err = resend()
if err != nil {
wac.listener.pop(messageTag)
close(ch)
return ch, nil, fmt.Errorf("failed to write message: %w", err)
}
wac.msgCount++
return ch, resend, nil
}
func (wac *Conn) encryptBinaryMessage(node binary.Node) (data []byte, err error) {
b, err := binary.Marshal(node)
if err != nil {
return nil, fmt.Errorf("binary node marshal failed: %w", err)
}
cipher, err := cbc.Encrypt(wac.session.EncKey, nil, b)
if err != nil {
return nil, fmt.Errorf("encrypt failed: %w", err)
}
h := hmac.New(sha256.New, wac.session.MacKey)
h.Write(cipher)
hash := h.Sum(nil)
data = append(data, hash[:32]...)
data = append(data, cipher...)
return data, nil
}