-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathauth.go
210 lines (182 loc) · 5.19 KB
/
auth.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
package fbmsgr
import (
"bytes"
"errors"
"io"
"math/rand"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strconv"
"sync"
"time"
"github.com/unixpickle/essentials"
"github.com/yhat/scrape"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
const (
BaseURL = "https://www.messenger.com"
SpoofedUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0"
)
// A Session is an authenticated session with the
// messenger backend.
type Session struct {
Client *http.Client
userID string
fbDTSGLock sync.Mutex
fbDTSGTime time.Time
fbDTSG string
defaultStreamLock sync.Mutex
defaultStream *EventStream
randLock sync.Mutex
randGen *rand.Rand
}
// Auth creates a new Session by authenticating with the
// Facebook backend.
func Auth(user, password string) (sess *Session, err error) {
defer essentials.AddCtxTo("fbmsgr: authenticate", &err)
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
loginPage, err := client.Get(BaseURL + "/")
if loginPage != nil {
defer loginPage.Body.Close()
}
if err != nil {
return nil, errors.New("request login page: " + err.Error())
}
root, err := html.Parse(loginPage.Body)
if err != nil {
return nil, errors.New("parse login page: " + err.Error())
}
formValues, action, err := loginFormValues(root)
if err != nil {
return nil, errors.New("read login form: " + err.Error())
}
if err := requestLoginCookies(client, root); err != nil {
return nil, errors.New("gather cookies: " + err.Error())
}
formValues.Set("email", user)
formValues.Set("pass", password)
formValues.Set("persistent", "1")
formValues.Set("login", "1")
body := []byte(formValues.Encode())
req, err := http.NewRequest("POST", BaseURL+action, bytes.NewBuffer(body))
if err != nil {
return nil, errors.New("create login request: " + err.Error())
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
req.Header.Set("User-Agent", SpoofedUserAgent)
req.Header.Set("Referer", BaseURL+"/")
postRes, err := client.Do(req)
if postRes != nil {
defer postRes.Body.Close()
}
if err != nil {
return nil, errors.New("failed to login: " + err.Error())
}
if postRes.Request.URL.Path == "/" {
return sessionForHomepage(client, postRes.Body)
}
return nil, errors.New("login failed")
}
// FBID returns the authenticated user's FBID.
func (s *Session) FBID() string {
return s.userID
}
func sessionForHomepage(c *http.Client, body io.Reader) (*Session, error) {
root, err := html.Parse(body)
if err != nil {
return nil, errors.New("parse homepage: " + err.Error())
}
userID, err := findJSField(root, "USER_ID")
if err != nil {
return nil, errors.New("find USER_ID: " + err.Error())
}
return &Session{
Client: c,
userID: userID,
randGen: rand.New(rand.NewSource(time.Now().UnixNano())),
}, nil
}
func requestLoginCookies(c *http.Client, body *html.Node) error {
reqID, err := findJSField(body, "initialRequestID")
if err != nil {
return errors.New("find initialRequestID: " + err.Error())
}
identifier, err := findJSField(body, "identifier")
if err != nil {
return errors.New("find identifier: " + err.Error())
}
dAtr, err := findJSField(body, "_js_datr")
if err != nil {
return errors.New("find _js_datr: " + err.Error())
}
cookieGetter := "https://www.facebook.com/login/messenger_dot_com_iframe/" +
"?redirect_uri=https%3A%2F%2Fwww.messenger.com%2Flogin%2Ffb_iframe_target%2F" +
"%3Finitial_request_id%3D" + reqID + "&identifier=" + identifier +
"&initial_request_id=" + reqID
req, err := http.NewRequest("GET", cookieGetter, nil)
if err != nil {
return err
}
req.Header.Set("Referer", "https://www.messenger.com")
req.Header.Set("User-Agent", SpoofedUserAgent)
resp, err := c.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
u, err := url.Parse(BaseURL)
if err != nil {
panic(err)
}
c.Jar.SetCookies(u, []*http.Cookie{&http.Cookie{
Name: "_js_datr",
Value: dAtr,
}})
getURL := BaseURL + "/login/fb_iframe_target/?userid=0&initial_request_id=" +
reqID
req, err = http.NewRequest("GET", getURL, nil)
req.Header.Set("Referer", "https://www.messenger.com")
req.Header.Set("User-Agent", SpoofedUserAgent)
resp, err = c.Do(req)
if resp != nil {
defer resp.Body.Close()
}
return err
}
func loginFormValues(body *html.Node) (vals url.Values, action string, err error) {
form, ok := scrape.Find(body, scrape.ById("login_form"))
if !ok {
return nil, "", errors.New("form not found")
}
action = scrape.Attr(form, "action")
if action == "" {
return nil, "", errors.New("no action attribute")
}
inputs := scrape.FindAll(form, scrape.ByTag(atom.Input))
vals = url.Values{}
for _, input := range inputs {
if scrape.Attr(input, "type") == "hidden" {
vals.Set(scrape.Attr(input, "name"), scrape.Attr(input, "value"))
}
}
return
}
func findJSField(body *html.Node, field string) (string, error) {
var out bytes.Buffer
html.Render(&out, body)
expr := regexp.MustCompile("\"" + field + "\"(,|:)\"(.*?)\"")
match := expr.FindSubmatch(out.Bytes())
if match == nil {
return "", errors.New("could not locate JS field")
}
return string(match[2]), nil
}