This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
213 lines (171 loc) · 3.61 KB
/
connect.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
package messenger
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"time"
)
func generateClientID() string {
data := make([]byte, 4)
_, err := io.ReadFull(rand.Reader, data)
if err != nil {
panic(err)
}
return hex.EncodeToString(data)
}
type accessibilityStruct struct {
Sr int64 `json:"sr"`
SrTs int64 `json:"sr-ts"`
Jk int64 `json:"jk"`
JkTs int64 `json:"jk-ts"`
Kb int64 `json:"kb"`
KbTs int64 `json:"kb-ts"`
Hcm int64 `json:"hcm"`
HcmTs int64 `json:"hcm-ts"`
}
func generateAccessibilityCookie() string {
now := time.Now().UnixNano() / 1e6
access := accessibilityStruct{
Sr: 0,
SrTs: now,
Jk: 0,
JkTs: now,
Kb: 0,
KbTs: now,
Hcm: 0,
HcmTs: now,
}
res, err := json.Marshal(access)
if err != nil {
panic(err)
}
return url.QueryEscape(string(res))
}
// ConnectToChat connects the session to chat after you've successfully
// logged in.
func (s *Session) ConnectToChat() error {
err := s.populateMeta()
if err != nil {
return err
}
s.l.form = s.newPullForm()
err = s.requestReconnect()
if err != nil {
return err
}
err = s.connectToStage1()
if err != nil {
return err
}
err = s.connectToStage2()
if err != nil {
return err
}
// err = s.connectToStage3()
// if err != nil {
// return err
// }
return nil
}
func (s *Session) requestReconnect() error {
req, _ := http.NewRequest(http.MethodGet, reconnectURL, nil)
req.Header = defaultHeader()
resp, err := s.doRequest(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
func (s *Session) connectToStage1() error {
req, err := s.createStage1Request()
if err != nil {
return err
}
resp, err := s.doRequest(req)
if err != nil {
return err
}
defer resp.Body.Close()
respInfo, err := parseResponse(resp.Body)
if err != nil {
return err
}
if respInfo.Type != "lb" {
return ParseError{"non t: \"lb\" response from chat server"}
}
s.l.form.stickyPool = respInfo.Sticky.Pool
s.l.form.stickyToken = respInfo.Sticky.Token
return nil
}
func (s *Session) createStage1Request() (*http.Request, error) {
cookies := s.client.Jar.Cookies(fbURL)
for _, cookie := range cookies {
if cookie.Name == "c_user" {
s.userID = cookie.Value
break
}
}
if s.userID == "" {
return nil, ParseError{"missing required c_user user ID"}
}
s.clientID = generateClientID()
presence := s.generatePresence()
cookies = append(cookies, []*http.Cookie{
{
Name: "presence",
Value: presence,
Domain: ".facebook.com",
Secure: true,
},
{
Name: "locale",
Value: "en_US",
Domain: ".facebook.com",
Secure: true,
},
{
Name: "a11y",
Value: generateAccessibilityCookie(),
Domain: ".facebook.com",
Secure: true,
},
}...)
s.client.Jar.SetCookies(fbURL, cookies)
form := s.newPullForm()
req, _ := http.NewRequest(http.MethodGet,
chatURL+form.form().Encode(), nil)
req.Header = defaultHeader()
return req, nil
}
func (s *Session) connectToStage2() error {
req, _ := http.NewRequest(http.MethodGet,
chatURL+s.l.form.form().Encode(), nil)
req.Header = defaultHeader()
resp, err := s.doRequest(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
func (s *Session) connectToStage3() error {
form := make(url.Values)
form.Set("client", "mercury")
form.Set("folders[0]", "inbox")
form.Set("last_action_timestamp", "0")
form = s.addFormMeta(form)
req, _ := http.NewRequest(http.MethodPost, threadSyncURL,
strings.NewReader(form.Encode()))
req.Header = defaultHeader()
resp, err := s.doRequest(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}