session.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package whatsapp
  2. import (
  3. "crypto/hmac"
  4. "crypto/rand"
  5. "crypto/sha256"
  6. "encoding/base64"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. "github.com/Rhymen/go-whatsapp/crypto/cbc"
  11. "github.com/Rhymen/go-whatsapp/crypto/curve25519"
  12. "github.com/Rhymen/go-whatsapp/crypto/hkdf"
  13. )
  14. /*
  15. Session contains session individual information. To be able to resume the connection without scanning the qr code
  16. every time you should save the Session returned by Login and use RestoreSession the next time you want to login.
  17. Every successful created connection returns a new Session. The Session(ClientToken, ServerToken) is altered after
  18. every re-login and should be saved every time.
  19. */
  20. type Session struct {
  21. ClientId string
  22. ClientToken string
  23. ServerToken string
  24. EncKey []byte
  25. MacKey []byte
  26. Wid string
  27. }
  28. type Info struct {
  29. Battery int
  30. Platform string
  31. Connected bool
  32. Pushname string
  33. Wid string
  34. Lc string
  35. Phone *PhoneInfo
  36. Plugged bool
  37. Tos int
  38. Lg string
  39. Is24h bool
  40. }
  41. type PhoneInfo struct {
  42. Mcc string
  43. Mnc string
  44. OsVersion string
  45. DeviceManufacturer string
  46. DeviceModel string
  47. OsBuildNumber string
  48. WaVersion string
  49. }
  50. func newInfoFromReq(info map[string]interface{}) *Info {
  51. phoneInfo := info["phone"].(map[string]interface{})
  52. ret := &Info{
  53. Battery: int(info["battery"].(float64)),
  54. Platform: info["platform"].(string),
  55. Connected: info["connected"].(bool),
  56. Pushname: info["pushname"].(string),
  57. Wid: info["wid"].(string),
  58. Lc: info["lc"].(string),
  59. Phone: &PhoneInfo{
  60. phoneInfo["mcc"].(string),
  61. phoneInfo["mnc"].(string),
  62. phoneInfo["os_version"].(string),
  63. phoneInfo["device_manufacturer"].(string),
  64. phoneInfo["device_model"].(string),
  65. phoneInfo["os_build_number"].(string),
  66. phoneInfo["wa_version"].(string),
  67. },
  68. Plugged: info["plugged"].(bool),
  69. Lg: info["lg"].(string),
  70. Tos: int(info["tos"].(float64)),
  71. }
  72. if is24h, ok := info["is24h"]; ok {
  73. ret.Is24h = is24h.(bool)
  74. }
  75. return ret
  76. }
  77. /*
  78. SetClientName sets the long and short client names that are sent to WhatsApp when logging in and displayed in the
  79. WhatsApp Web device list. As the values are only sent when logging in, changing them after logging in is not possible.
  80. */
  81. func (wac *Conn) SetClientName(long, short string) error {
  82. if wac.session != nil && (wac.session.EncKey != nil || wac.session.MacKey != nil) {
  83. return fmt.Errorf("cannot change client name after logging in")
  84. }
  85. wac.longClientName, wac.shortClientName = long, short
  86. return nil
  87. }
  88. /*
  89. Login is the function that creates a new whatsapp session and logs you in. If you do not want to scan the qr code
  90. every time, you should save the returned session and use RestoreSession the next time. Login takes a writable channel
  91. as an parameter. This channel is used to push the data represented by the qr code back to the user. The received data
  92. should be displayed as an qr code in a way you prefer. To print a qr code to console you can use:
  93. github.com/Baozisoftware/qrcode-terminal-go Example login procedure:
  94. wac, err := whatsapp.NewConn(5 * time.Second)
  95. if err != nil {
  96. panic(err)
  97. }
  98. qr := make(chan string)
  99. go func() {
  100. terminal := qrcodeTerminal.New()
  101. terminal.Get(<-qr).Print()
  102. }()
  103. session, err := wac.Login(qr)
  104. if err != nil {
  105. fmt.Fprintf(os.Stderr, "error during login: %v\n", err)
  106. }
  107. fmt.Printf("login successful, session: %v\n", session)
  108. */
  109. func (wac *Conn) Login(qrChan chan<- string) (Session, error) {
  110. session := Session{}
  111. if wac.session != nil && (wac.session.EncKey != nil || wac.session.MacKey != nil) {
  112. return session, fmt.Errorf("already logged in")
  113. }
  114. clientId := make([]byte, 16)
  115. _, err := rand.Read(clientId)
  116. if err != nil {
  117. return session, fmt.Errorf("error creating random ClientId: %v", err)
  118. }
  119. session.ClientId = base64.StdEncoding.EncodeToString(clientId)
  120. //oldVersion=8691
  121. login := []interface{}{"admin", "init", []int{0, 3, 225}, []string{wac.longClientName, wac.shortClientName}, session.ClientId, true}
  122. loginChan, err := wac.write(login)
  123. if err != nil {
  124. return session, fmt.Errorf("error writing login: %v\n", err)
  125. }
  126. var r string
  127. select {
  128. case r = <-loginChan:
  129. case <-time.After(wac.msgTimeout):
  130. return session, fmt.Errorf("login connection timed out")
  131. }
  132. var resp map[string]interface{}
  133. if err = json.Unmarshal([]byte(r), &resp); err != nil {
  134. return session, fmt.Errorf("error decoding login resp: %v\n", err)
  135. }
  136. ref := resp["ref"].(string)
  137. priv, pub, err := curve25519.GenerateKey()
  138. if err != nil {
  139. return session, fmt.Errorf("error generating keys: %v\n", err)
  140. }
  141. //listener for Login response
  142. messageTag := "s1"
  143. wac.listener[messageTag] = make(chan string, 1)
  144. qrChan <- fmt.Sprintf("%v,%v,%v", ref, base64.StdEncoding.EncodeToString(pub[:]), session.ClientId)
  145. var resp2 []interface{}
  146. select {
  147. case r1 := <-wac.listener[messageTag]:
  148. if err := json.Unmarshal([]byte(r1), &resp2); err != nil {
  149. return session, fmt.Errorf("error decoding qr code resp: %v", err)
  150. }
  151. case <-time.After(time.Duration(resp["ttl"].(float64)) * time.Millisecond):
  152. return session, fmt.Errorf("qr code scan timed out")
  153. }
  154. info := resp2[1].(map[string]interface{})
  155. wac.Info = newInfoFromReq(info)
  156. session.ClientToken = info["clientToken"].(string)
  157. session.ServerToken = info["serverToken"].(string)
  158. session.Wid = info["wid"].(string)
  159. s := info["secret"].(string)
  160. decodedSecret, err := base64.StdEncoding.DecodeString(s)
  161. if err != nil {
  162. return session, fmt.Errorf("error decoding secret: %v", err)
  163. }
  164. var pubKey [32]byte
  165. copy(pubKey[:], decodedSecret[:32])
  166. sharedSecret := curve25519.GenerateSharedSecret(*priv, pubKey)
  167. hash := sha256.New
  168. nullKey := make([]byte, 32)
  169. h := hmac.New(hash, nullKey)
  170. h.Write(sharedSecret)
  171. sharedSecretExtended, err := hkdf.Expand(h.Sum(nil), 80, "")
  172. if err != nil {
  173. return session, fmt.Errorf("hkdf error: %v", err)
  174. }
  175. //login validation
  176. checkSecret := make([]byte, 112)
  177. copy(checkSecret[:32], decodedSecret[:32])
  178. copy(checkSecret[32:], decodedSecret[64:])
  179. h2 := hmac.New(hash, sharedSecretExtended[32:64])
  180. h2.Write(checkSecret)
  181. if !hmac.Equal(h2.Sum(nil), decodedSecret[32:64]) {
  182. return session, fmt.Errorf("abort login")
  183. }
  184. keysEncrypted := make([]byte, 96)
  185. copy(keysEncrypted[:16], sharedSecretExtended[64:])
  186. copy(keysEncrypted[16:], decodedSecret[64:])
  187. keyDecrypted, err := cbc.Decrypt(sharedSecretExtended[:32], nil, keysEncrypted)
  188. if err != nil {
  189. return session, fmt.Errorf("error decryptAes: %v", err)
  190. }
  191. session.EncKey = keyDecrypted[:32]
  192. session.MacKey = keyDecrypted[32:64]
  193. wac.session = &session
  194. return session, nil
  195. }
  196. /*
  197. RestoreSession is the function that restores a given session. It will try to reestablish the connection to the
  198. WhatsAppWeb servers with the provided session. If it succeeds it will return a new session. This new session has to be
  199. saved because the Client and Server-Token will change after every login. Logging in with old tokens is possible, but not
  200. suggested. If so, a challenge has to be resolved which is just another possible point of failure.
  201. */
  202. func (wac *Conn) RestoreSession(session Session) (Session, error) {
  203. if wac.session != nil && (wac.session.EncKey != nil || wac.session.MacKey != nil) {
  204. return Session{}, fmt.Errorf("already logged in")
  205. }
  206. wac.session = &session
  207. //listener for Conn or challenge; s1 is not allowed to drop
  208. wac.listener["s1"] = make(chan string, 1)
  209. //admin init
  210. init := []interface{}{"admin", "init", []int{0, 3, 225}, []string{wac.longClientName, wac.shortClientName}, session.ClientId, true}
  211. initChan, err := wac.write(init)
  212. if err != nil {
  213. wac.session = nil
  214. return Session{}, fmt.Errorf("error writing admin init: %v\n", err)
  215. }
  216. //admin login with takeover
  217. login := []interface{}{"admin", "login", session.ClientToken, session.ServerToken, session.ClientId, "takeover"}
  218. loginChan, err := wac.write(login)
  219. if err != nil {
  220. wac.session = nil
  221. return Session{}, fmt.Errorf("error writing admin login: %v\n", err)
  222. }
  223. select {
  224. case r := <-initChan:
  225. var resp map[string]interface{}
  226. if err = json.Unmarshal([]byte(r), &resp); err != nil {
  227. wac.session = nil
  228. return Session{}, fmt.Errorf("error decoding login connResp: %v\n", err)
  229. }
  230. if int(resp["status"].(float64)) != 200 {
  231. wac.session = nil
  232. return Session{}, fmt.Errorf("init responded with %d", resp["status"])
  233. }
  234. case <-time.After(wac.msgTimeout):
  235. wac.session = nil
  236. return Session{}, fmt.Errorf("restore session init timed out")
  237. }
  238. //wait for s1
  239. var connResp []interface{}
  240. select {
  241. case r1 := <-wac.listener["s1"]:
  242. if err := json.Unmarshal([]byte(r1), &connResp); err != nil {
  243. wac.session = nil
  244. return Session{}, fmt.Errorf("error decoding s1 message: %v\n", err)
  245. }
  246. case <-time.After(wac.msgTimeout):
  247. wac.session = nil
  248. return Session{}, fmt.Errorf("restore session connection timed out")
  249. }
  250. //check if challenge is present
  251. if len(connResp) == 2 && connResp[0] == "Cmd" && connResp[1].(map[string]interface{})["type"] == "challenge" {
  252. wac.listener["s2"] = make(chan string, 1)
  253. if err := wac.resolveChallenge(connResp[1].(map[string]interface{})["challenge"].(string)); err != nil {
  254. wac.session = nil
  255. return Session{}, fmt.Errorf("error resolving challenge: %v\n", err)
  256. }
  257. select {
  258. case r := <-wac.listener["s2"]:
  259. if err := json.Unmarshal([]byte(r), &connResp); err != nil {
  260. wac.session = nil
  261. return Session{}, fmt.Errorf("error decoding s2 message: %v\n", err)
  262. }
  263. case <-time.After(wac.msgTimeout):
  264. wac.session = nil
  265. return Session{}, fmt.Errorf("restore session challenge timed out")
  266. }
  267. }
  268. //check for login 200 --> login success
  269. select {
  270. case r := <-loginChan:
  271. var resp map[string]interface{}
  272. if err = json.Unmarshal([]byte(r), &resp); err != nil {
  273. wac.session = nil
  274. return Session{}, fmt.Errorf("error decoding login connResp: %v\n", err)
  275. }
  276. if int(resp["status"].(float64)) != 200 {
  277. wac.session = nil
  278. return Session{}, fmt.Errorf("admin login responded with %d", resp["status"])
  279. }
  280. case <-time.After(wac.msgTimeout):
  281. wac.session = nil
  282. return Session{}, fmt.Errorf("restore session login timed out")
  283. }
  284. info := connResp[1].(map[string]interface{})
  285. wac.Info = newInfoFromReq(info)
  286. //set new tokens
  287. session.ClientToken = info["clientToken"].(string)
  288. session.ServerToken = info["serverToken"].(string)
  289. session.Wid = info["wid"].(string)
  290. return *wac.session, nil
  291. }
  292. func (wac *Conn) resolveChallenge(challenge string) error {
  293. decoded, err := base64.StdEncoding.DecodeString(challenge)
  294. if err != nil {
  295. return err
  296. }
  297. h2 := hmac.New(sha256.New, wac.session.MacKey)
  298. h2.Write([]byte(decoded))
  299. ch := []interface{}{"admin", "challenge", base64.StdEncoding.EncodeToString(h2.Sum(nil)), wac.session.ServerToken, wac.session.ClientId}
  300. challengeChan, err := wac.write(ch)
  301. if err != nil {
  302. return fmt.Errorf("error writing challenge: %v\n", err)
  303. }
  304. select {
  305. case r := <-challengeChan:
  306. var resp map[string]interface{}
  307. if err := json.Unmarshal([]byte(r), &resp); err != nil {
  308. return fmt.Errorf("error decoding login resp: %v\n", err)
  309. }
  310. if int(resp["status"].(float64)) != 200 {
  311. return fmt.Errorf("challenge responded with %d\n", resp["status"])
  312. }
  313. case <-time.After(wac.msgTimeout):
  314. return fmt.Errorf("connection timed out")
  315. }
  316. return nil
  317. }
  318. /*
  319. Logout is the function to logout from a WhatsApp session. Logging out means invalidating the current session.
  320. The session can not be resumed and will disappear on your phone in the WhatsAppWeb client list.
  321. */
  322. func (wac *Conn) Logout() error {
  323. login := []interface{}{"admin", "Conn", "disconnect"}
  324. _, err := wac.write(login)
  325. if err != nil {
  326. return fmt.Errorf("error writing logout: %v\n", err)
  327. }
  328. return nil
  329. }