2019-05-30 10:20:56 +00:00
|
|
|
package whatsapp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-01-09 20:02:56 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
2019-05-30 10:20:56 +00:00
|
|
|
"github.com/Rhymen/go-whatsapp/binary"
|
|
|
|
"github.com/Rhymen/go-whatsapp/crypto/cbc"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-02-01 20:29:04 +00:00
|
|
|
func (wac *Conn) addListener(ch chan string, messageTag string) {
|
|
|
|
wac.listener.Lock()
|
|
|
|
wac.listener.m[messageTag] = ch
|
|
|
|
wac.listener.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wac *Conn) removeListener(answerMessageTag string) {
|
|
|
|
wac.listener.Lock()
|
|
|
|
delete(wac.listener.m, answerMessageTag)
|
|
|
|
wac.listener.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-05-30 10:20:56 +00:00
|
|
|
//writeJson enqueues a json message into the writeChan
|
|
|
|
func (wac *Conn) writeJson(data []interface{}) (<-chan string, error) {
|
2020-01-09 20:02:56 +00:00
|
|
|
|
2021-02-01 20:29:04 +00:00
|
|
|
ch := make(chan string, 1)
|
|
|
|
|
2020-01-09 20:02:56 +00:00
|
|
|
wac.writerLock.Lock()
|
|
|
|
defer wac.writerLock.Unlock()
|
|
|
|
|
2019-05-30 10:20:56 +00:00
|
|
|
d, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2021-02-01 20:29:04 +00:00
|
|
|
close(ch)
|
|
|
|
return ch, err
|
2019-05-30 10:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts := time.Now().Unix()
|
|
|
|
messageTag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
|
|
|
|
bytes := []byte(fmt.Sprintf("%s,%s", messageTag, d))
|
|
|
|
|
2020-08-24 21:35:08 +00:00
|
|
|
if wac.timeTag == "" {
|
|
|
|
tss := fmt.Sprintf("%d", ts)
|
|
|
|
wac.timeTag = tss[len(tss)-3:]
|
|
|
|
}
|
|
|
|
|
2021-02-01 20:29:04 +00:00
|
|
|
wac.addListener(ch, messageTag)
|
|
|
|
|
|
|
|
err = wac.write(websocket.TextMessage, bytes)
|
2019-05-30 10:20:56 +00:00
|
|
|
if err != nil {
|
2021-02-01 20:29:04 +00:00
|
|
|
close(ch)
|
|
|
|
wac.removeListener(messageTag)
|
|
|
|
return ch, err
|
2019-05-30 10:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wac.msgCount++
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wac *Conn) writeBinary(node binary.Node, metric metric, flag flag, messageTag string) (<-chan string, error) {
|
2021-02-01 20:29:04 +00:00
|
|
|
|
|
|
|
ch := make(chan string, 1)
|
|
|
|
|
2019-05-30 10:20:56 +00:00
|
|
|
if len(messageTag) < 2 {
|
2021-02-01 20:29:04 +00:00
|
|
|
close(ch)
|
|
|
|
return ch, ErrMissingMessageTag
|
2019-05-30 10:20:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 20:02:56 +00:00
|
|
|
wac.writerLock.Lock()
|
|
|
|
defer wac.writerLock.Unlock()
|
|
|
|
|
2019-05-30 10:20:56 +00:00
|
|
|
data, err := wac.encryptBinaryMessage(node)
|
|
|
|
if err != nil {
|
2021-02-01 20:29:04 +00:00
|
|
|
close(ch)
|
|
|
|
return ch, errors.Wrap(err, "encryptBinaryMessage(node) failed")
|
2019-05-30 10:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes := []byte(messageTag + ",")
|
|
|
|
bytes = append(bytes, byte(metric), byte(flag))
|
|
|
|
bytes = append(bytes, data...)
|
|
|
|
|
2021-02-01 20:29:04 +00:00
|
|
|
wac.addListener(ch, messageTag)
|
|
|
|
|
|
|
|
err = wac.write(websocket.BinaryMessage, bytes)
|
2019-05-30 10:20:56 +00:00
|
|
|
if err != nil {
|
2021-02-01 20:29:04 +00:00
|
|
|
close(ch)
|
|
|
|
wac.removeListener(messageTag)
|
|
|
|
return ch, errors.Wrap(err, "failed to write message")
|
2019-05-30 10:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wac.msgCount++
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wac *Conn) sendKeepAlive() error {
|
2021-02-01 20:29:04 +00:00
|
|
|
|
|
|
|
respChan := make(chan string, 1)
|
|
|
|
wac.addListener(respChan, "!")
|
|
|
|
|
2019-05-30 10:20:56 +00:00
|
|
|
bytes := []byte("?,,")
|
2021-02-01 20:29:04 +00:00
|
|
|
err := wac.write(websocket.TextMessage, bytes)
|
2019-05-30 10:20:56 +00:00
|
|
|
if err != nil {
|
2021-02-01 20:29:04 +00:00
|
|
|
close(respChan)
|
|
|
|
wac.removeListener("!")
|
2019-05-30 10:20:56 +00:00
|
|
|
return errors.Wrap(err, "error sending keepAlive")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case resp := <-respChan:
|
|
|
|
msecs, err := strconv.ParseInt(resp, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error converting time string to uint")
|
|
|
|
}
|
|
|
|
wac.ServerLastSeen = time.Unix(msecs/1000, (msecs%1000)*int64(time.Millisecond))
|
|
|
|
|
|
|
|
case <-time.After(wac.msgTimeout):
|
|
|
|
return ErrConnectionTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-26 23:45:57 +00:00
|
|
|
/*
|
2019-08-26 21:22:34 +00:00
|
|
|
When phone is unreachable, WhatsAppWeb sends ["admin","test"] time after time to try a successful contact.
|
|
|
|
Tested with Airplane mode and no connection at all.
|
|
|
|
*/
|
|
|
|
func (wac *Conn) sendAdminTest() (bool, error) {
|
|
|
|
data := []interface{}{"admin", "test"}
|
2019-10-26 23:45:57 +00:00
|
|
|
|
2019-08-26 21:22:34 +00:00
|
|
|
r, err := wac.writeJson(data)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "error sending admin test")
|
|
|
|
}
|
|
|
|
|
|
|
|
var response []interface{}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case resp := <-r:
|
|
|
|
if err := json.Unmarshal([]byte(resp), &response); err != nil {
|
|
|
|
return false, fmt.Errorf("error decoding response message: %v\n", err)
|
|
|
|
}
|
|
|
|
case <-time.After(wac.msgTimeout):
|
|
|
|
return false, ErrConnectionTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(response) == 2 && response[0].(string) == "Pong" && response[1].(bool) == true {
|
|
|
|
return true, nil
|
2019-10-26 23:45:57 +00:00
|
|
|
} else {
|
2019-08-26 21:22:34 +00:00
|
|
|
return false, nil
|
2019-10-26 23:45:57 +00:00
|
|
|
}
|
2019-08-26 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 20:29:04 +00:00
|
|
|
func (wac *Conn) write(messageType int, data []byte) error {
|
2019-05-30 10:20:56 +00:00
|
|
|
|
2020-08-24 21:35:08 +00:00
|
|
|
if wac == nil || wac.ws == nil {
|
2021-02-01 20:29:04 +00:00
|
|
|
return ErrInvalidWebsocket
|
2020-08-24 21:35:08 +00:00
|
|
|
}
|
2021-02-01 20:29:04 +00:00
|
|
|
|
2019-05-30 10:20:56 +00:00
|
|
|
wac.ws.Lock()
|
|
|
|
err := wac.ws.conn.WriteMessage(messageType, data)
|
|
|
|
wac.ws.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
2021-02-01 20:29:04 +00:00
|
|
|
return errors.Wrap(err, "error writing to websocket")
|
2019-05-30 10:20:56 +00:00
|
|
|
}
|
2021-02-01 20:29:04 +00:00
|
|
|
|
|
|
|
return nil
|
2019-05-30 10:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wac *Conn) encryptBinaryMessage(node binary.Node) (data []byte, err error) {
|
|
|
|
b, err := binary.Marshal(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "binary node marshal failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
cipher, err := cbc.Encrypt(wac.session.EncKey, nil, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "encrypt failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|