session.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. Login is the function that creates a new whatsapp session and logs you in. If you do not want to scan the qr code
  79. every time, you should save the returned session and use RestoreSession the next time. Login takes a writable channel
  80. as an parameter. This channel is used to push the data represented by the qr code back to the user. The received data
  81. should be displayed as an qr code in a way you prefer. To print a qr code to console you can use:
  82. github.com/Baozisoftware/qrcode-terminal-go Example login procedure:
  83. wac, err := whatsapp.NewConn(5 * time.Second)
  84. if err != nil {
  85. panic(err)
  86. }
  87. qr := make(chan string)
  88. go func() {
  89. terminal := qrcodeTerminal.New()
  90. terminal.Get(<-qr).Print()
  91. }()
  92. session, err := wac.Login(qr)
  93. if err != nil {
  94. fmt.Fprintf(os.Stderr, "error during login: %v\n", err)
  95. }
  96. fmt.Printf("login successful, session: %v\n", session)
  97. */
  98. func (wac *Conn) Login(qrChan chan<- string) (Session, error) {
  99. session := Session{}
  100. if wac.session != nil && (wac.session.EncKey != nil || wac.session.MacKey != nil) {
  101. return session, fmt.Errorf("already logged in")
  102. }
  103. clientId := make([]byte, 16)
  104. _, err := rand.Read(clientId)
  105. if err != nil {
  106. return session, fmt.Errorf("error creating random ClientId: %v", err)
  107. }
  108. session.ClientId = base64.StdEncoding.EncodeToString(clientId)
  109. //oldVersion=8691
  110. login := []interface{}{"admin", "init", []int{0, 3, 225}, []string{"github.com/rhymen/go-whatsapp", "go-whatsapp"}, session.ClientId, true}
  111. loginChan, err := wac.write(login)
  112. if err != nil {
  113. return session, fmt.Errorf("error writing login: %v\n", err)
  114. }
  115. var r string
  116. select {
  117. case r = <-loginChan:
  118. case <-time.After(wac.msgTimeout):
  119. return session, fmt.Errorf("login connection timed out")
  120. }
  121. var resp map[string]interface{}
  122. if err = json.Unmarshal([]byte(r), &resp); err != nil {
  123. return session, fmt.Errorf("error decoding login resp: %v\n", err)
  124. }
  125. ref := resp["ref"].(string)
  126. priv, pub, err := curve25519.GenerateKey()
  127. if err != nil {
  128. return session, fmt.Errorf("error generating keys: %v\n", err)
  129. }
  130. //listener for Login response
  131. messageTag := "s1"
  132. wac.listener[messageTag] = make(chan string, 1)
  133. qrChan <- fmt.Sprintf("%v,%v,%v", ref, base64.StdEncoding.EncodeToString(pub[:]), session.ClientId)
  134. var resp2 []interface{}
  135. select {
  136. case r1 := <-wac.listener[messageTag]:
  137. if err := json.Unmarshal([]byte(r1), &resp2); err != nil {
  138. return session, fmt.Errorf("error decoding qr code resp: %v", err)
  139. }
  140. case <-time.After(time.Duration(resp["ttl"].(float64)) * time.Millisecond):
  141. return session, fmt.Errorf("qr code scan timed out")
  142. }
  143. info := resp2[1].(map[string]interface{})
  144. wac.Info = newInfoFromReq(info)
  145. session.ClientToken = info["clientToken"].(string)
  146. session.ServerToken = info["serverToken"].(string)
  147. session.Wid = info["wid"].(string)
  148. s := info["secret"].(string)
  149. decodedSecret, err := base64.StdEncoding.DecodeString(s)
  150. if err != nil {
  151. return session, fmt.Errorf("error decoding secret: %v", err)
  152. }
  153. var pubKey [32]byte
  154. copy(pubKey[:], decodedSecret[:32])
  155. sharedSecret := curve25519.GenerateSharedSecret(*priv, pubKey)
  156. hash := sha256.New
  157. nullKey := make([]byte, 32)
  158. h := hmac.New(hash, nullKey)
  159. h.Write(sharedSecret)
  160. sharedSecretExtended, err := hkdf.Expand(h.Sum(nil), 80, "")
  161. if err != nil {
  162. return session, fmt.Errorf("hkdf error: %v", err)
  163. }
  164. //login validation
  165. checkSecret := make([]byte, 112)
  166. copy(checkSecret[:32], decodedSecret[:32])
  167. copy(checkSecret[32:], decodedSecret[64:])
  168. h2 := hmac.New(hash, sharedSecretExtended[32:64])
  169. h2.Write(checkSecret)
  170. if !hmac.Equal(h2.Sum(nil), decodedSecret[32:64]) {
  171. return session, fmt.Errorf("abort login")
  172. }
  173. keysEncrypted := make([]byte, 96)
  174. copy(keysEncrypted[:16], sharedSecretExtended[64:])
  175. copy(keysEncrypted[16:], decodedSecret[64:])
  176. keyDecrypted, err := cbc.Decrypt(sharedSecretExtended[:32], nil, keysEncrypted)
  177. if err != nil {
  178. return session, fmt.Errorf("error decryptAes: %v", err)
  179. }
  180. session.EncKey = keyDecrypted[:32]
  181. session.MacKey = keyDecrypted[32:64]
  182. wac.session = &session
  183. return session, nil
  184. }
  185. /*
  186. RestoreSession is the function that restores a given session. It will try to reestablish the connection to the
  187. WhatsAppWeb servers with the provided session. If it succeeds it will return a new session. This new session has to be
  188. saved because the Client and Server-Token will change after every login. Logging in with old tokens is possible, but not
  189. suggested. If so, a challenge has to be resolved which is just another possible point of failure.
  190. */
  191. func (wac *Conn) RestoreSession(session Session) (Session, error) {
  192. if wac.session != nil && (wac.session.EncKey != nil || wac.session.MacKey != nil) {
  193. return Session{}, fmt.Errorf("already logged in")
  194. }
  195. wac.session = &session
  196. //listener for Conn or challenge; s1 is not allowed to drop
  197. wac.listener["s1"] = make(chan string, 1)
  198. //admin init
  199. init := []interface{}{"admin", "init", []int{0, 3, 225}, []string{"github.com/rhymen/go-whatsapp", "go-whatsapp"}, session.ClientId, true}
  200. initChan, err := wac.write(init)
  201. if err != nil {
  202. wac.session = nil
  203. return Session{}, fmt.Errorf("error writing admin init: %v\n", err)
  204. }
  205. //admin login with takeover
  206. login := []interface{}{"admin", "login", session.ClientToken, session.ServerToken, session.ClientId, "takeover"}
  207. loginChan, err := wac.write(login)
  208. if err != nil {
  209. wac.session = nil
  210. return Session{}, fmt.Errorf("error writing admin login: %v\n", err)
  211. }
  212. select {
  213. case r := <-initChan:
  214. var resp map[string]interface{}
  215. if err = json.Unmarshal([]byte(r), &resp); err != nil {
  216. wac.session = nil
  217. return Session{}, fmt.Errorf("error decoding login connResp: %v\n", err)
  218. }
  219. if int(resp["status"].(float64)) != 200 {
  220. wac.session = nil
  221. return Session{}, fmt.Errorf("init responded with %d", resp["status"])
  222. }
  223. case <-time.After(wac.msgTimeout):
  224. wac.session = nil
  225. return Session{}, fmt.Errorf("restore session init timed out")
  226. }
  227. //wait for s1
  228. var connResp []interface{}
  229. select {
  230. case r1 := <-wac.listener["s1"]:
  231. if err := json.Unmarshal([]byte(r1), &connResp); err != nil {
  232. wac.session = nil
  233. return Session{}, fmt.Errorf("error decoding s1 message: %v\n", err)
  234. }
  235. case <-time.After(wac.msgTimeout):
  236. wac.session = nil
  237. return Session{}, fmt.Errorf("restore session connection timed out")
  238. }
  239. //check if challenge is present
  240. if len(connResp) == 2 && connResp[0] == "Cmd" && connResp[1].(map[string]interface{})["type"] == "challenge" {
  241. wac.listener["s2"] = make(chan string, 1)
  242. if err := wac.resolveChallenge(connResp[1].(map[string]interface{})["challenge"].(string)); err != nil {
  243. wac.session = nil
  244. return Session{}, fmt.Errorf("error resolving challenge: %v\n", err)
  245. }
  246. select {
  247. case r := <-wac.listener["s2"]:
  248. if err := json.Unmarshal([]byte(r), &connResp); err != nil {
  249. wac.session = nil
  250. return Session{}, fmt.Errorf("error decoding s2 message: %v\n", err)
  251. }
  252. case <-time.After(wac.msgTimeout):
  253. wac.session = nil
  254. return Session{}, fmt.Errorf("restore session challenge timed out")
  255. }
  256. }
  257. //check for login 200 --> login success
  258. select {
  259. case r := <-loginChan:
  260. var resp map[string]interface{}
  261. if err = json.Unmarshal([]byte(r), &resp); err != nil {
  262. wac.session = nil
  263. return Session{}, fmt.Errorf("error decoding login connResp: %v\n", err)
  264. }
  265. if int(resp["status"].(float64)) != 200 {
  266. wac.session = nil
  267. return Session{}, fmt.Errorf("admin login responded with %d", resp["status"])
  268. }
  269. case <-time.After(wac.msgTimeout):
  270. wac.session = nil
  271. return Session{}, fmt.Errorf("restore session login timed out")
  272. }
  273. info := connResp[1].(map[string]interface{})
  274. wac.Info = newInfoFromReq(info)
  275. //set new tokens
  276. session.ClientToken = info["clientToken"].(string)
  277. session.ServerToken = info["serverToken"].(string)
  278. session.Wid = info["wid"].(string)
  279. return *wac.session, nil
  280. }
  281. func (wac *Conn) resolveChallenge(challenge string) error {
  282. decoded, err := base64.StdEncoding.DecodeString(challenge)
  283. if err != nil {
  284. return err
  285. }
  286. h2 := hmac.New(sha256.New, wac.session.MacKey)
  287. h2.Write([]byte(decoded))
  288. ch := []interface{}{"admin", "challenge", base64.StdEncoding.EncodeToString(h2.Sum(nil)), wac.session.ServerToken, wac.session.ClientId}
  289. challengeChan, err := wac.write(ch)
  290. if err != nil {
  291. return fmt.Errorf("error writing challenge: %v\n", err)
  292. }
  293. select {
  294. case r := <-challengeChan:
  295. var resp map[string]interface{}
  296. if err := json.Unmarshal([]byte(r), &resp); err != nil {
  297. return fmt.Errorf("error decoding login resp: %v\n", err)
  298. }
  299. if int(resp["status"].(float64)) != 200 {
  300. return fmt.Errorf("challenge responded with %d\n", resp["status"])
  301. }
  302. case <-time.After(wac.msgTimeout):
  303. return fmt.Errorf("connection timed out")
  304. }
  305. return nil
  306. }
  307. /*
  308. Logout is the function to logout from a WhatsApp session. Logging out means invalidating the current session.
  309. The session can not be resumed and will disappear on your phone in the WhatsAppWeb client list.
  310. */
  311. func (wac *Conn) Logout() error {
  312. login := []interface{}{"admin", "Conn", "disconnect"}
  313. _, err := wac.write(login)
  314. if err != nil {
  315. return fmt.Errorf("error writing logout: %v\n", err)
  316. }
  317. return nil
  318. }