-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
309 lines (251 loc) · 7.26 KB
/
client.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package rumblelivestreamlib
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"sync"
"github.com/robertkrimen/otto"
)
const (
domain = "rumble.com"
urlWeb = "https://" + domain
urlAccount = urlWeb + "/account/"
urlGetSalts = urlWeb + "/service.php?name=user.get_salts"
urlUserLogin = urlWeb + "/service.php?name=user.login"
urlUserLogout = urlWeb + "/service.php?name=user.logout"
)
type Client struct {
httpClient *http.Client
chatInfo *ChatInfo
chatStream *ChatStream
chatStreamMu sync.Mutex
ApiKey string
LiveStreamUrl string
}
func (c *Client) cookies() ([]*http.Cookie, error) {
u, err := url.Parse(urlWeb)
if err != nil {
return nil, fmt.Errorf("error parsing domain: %v", err)
}
return c.httpClient.Jar.Cookies(u), nil
}
func (c *Client) PrintCookies() error {
cookies, err := c.cookies()
if err != nil {
return pkgErr("error getting cookies", err)
}
fmt.Println("Cookies:", len(cookies))
for _, cookie := range cookies {
fmt.Println(cookie.String())
}
return nil
}
type NewClientOptions struct {
Cookies []*http.Cookie `json:"cookies"`
ApiKey string `json:"stream_key"`
LiveStreamUrl string `json:"stream_url"`
}
func NewClient(opts NewClientOptions) (*Client, error) {
cl, err := newHttpClient(opts.Cookies)
if err != nil {
return nil, pkgErr("error creating http client", err)
}
return &Client{httpClient: cl, ApiKey: opts.ApiKey, LiveStreamUrl: opts.LiveStreamUrl}, nil
}
func newHttpClient(cookies []*http.Cookie) (*http.Client, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, fmt.Errorf("error creating cookiejar: %v", err)
}
url, err := url.Parse(urlWeb)
if err != nil {
return nil, fmt.Errorf("error parsing domain: %v", err)
}
jar.SetCookies(url, cookies)
return &http.Client{Jar: jar}, nil
}
type GetSaltsData struct {
Salts []string `json:"salts"`
}
type GetSaltsResponse struct {
Data GetSaltsData `json:"data"`
}
func (c *Client) Login(username string, password string) ([]*http.Cookie, error) {
if c.httpClient == nil {
return nil, pkgErr("", fmt.Errorf("http client is nil"))
}
salts, err := c.getSalts(username)
if err != nil {
return nil, pkgErr("error getting salts", err)
}
cookies, err := c.userLogin(username, password, salts)
if err != nil {
return nil, pkgErr("error logging in", err)
}
return cookies, nil
}
func (c *Client) getWebpage(url string) (*http.Response, error) {
resp, err := c.httpClient.Get(url)
if err != nil {
return nil, fmt.Errorf("http Get request returned error: %v", err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("http Get response status not %s: %s", http.StatusText(http.StatusOK), resp.Status)
}
return resp, nil
}
func (c *Client) getSalts(username string) ([]string, error) {
u := url.URL{}
q := u.Query()
q.Add("username", username)
body := q.Encode()
resp, err := c.httpClient.Post(urlGetSalts, "application/x-www-form-urlencoded", strings.NewReader(body))
if err != nil {
return nil, fmt.Errorf("http Post request returned error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http Post response status not %s: %s", http.StatusText(http.StatusOK), resp.Status)
}
bodyB, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading body bytes: %v", err)
}
var gsr GetSaltsResponse
err = json.NewDecoder(strings.NewReader(string(bodyB))).Decode(&gsr)
if err != nil {
return nil, fmt.Errorf("error decoding response body from server: %v", err)
}
return gsr.Data.Salts, nil
}
type DataBool struct {
Session bool `json:"session"`
}
type LoginResponseBool struct {
Data DataBool `json:"data"`
}
type DataString struct {
Session string `json:"session"`
}
type LoginResponseString struct {
Data DataString `json:"data"`
}
func loginResponseSession(body []byte) (string, error) {
bodyS := string(body)
var lrs LoginResponseString
err := json.NewDecoder(strings.NewReader(bodyS)).Decode(&lrs)
if err == nil {
return lrs.Data.Session, nil
}
var lrb LoginResponseBool
err = json.NewDecoder(strings.NewReader(bodyS)).Decode(&lrb)
if err == nil {
return "false", nil
}
return "", fmt.Errorf("error decoding login response")
}
func (c *Client) userLogin(username string, password string, salts []string) ([]*http.Cookie, error) {
hashes, err := generateHashes(password, salts)
if err != nil {
return nil, fmt.Errorf("error generating password hashes: %v", err)
}
u := url.URL{}
q := u.Query()
q.Add("username", username)
q.Add("password_hashes", hashes)
body := q.Encode()
resp, err := c.httpClient.Post(urlUserLogin, "application/x-www-form-urlencoded", strings.NewReader(body))
if err != nil {
return nil, fmt.Errorf("http Post request returned error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http Post response status not %s: %s", http.StatusText(http.StatusOK), resp.Status)
}
bodyB, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading body bytes: %v", err)
}
session, err := loginResponseSession(bodyB)
if err != nil {
return nil, fmt.Errorf("error getting login response session: %v", err)
}
if session == "false" {
return nil, fmt.Errorf("failed to log in")
}
return resp.Cookies(), nil
}
func generateHashes(password string, salts []string) (string, error) {
vm := otto.New()
vm.Set("password", password)
vm.Set("salt0", salts[0])
vm.Set("salt1", salts[1])
vm.Set("salt2", salts[2])
_, err := vm.Run(md5)
if err != nil {
return "", fmt.Errorf("error running md5 javascript: %v", err)
}
value, err := vm.Get("hashes")
if err != nil {
return "", fmt.Errorf("error getting hashes value: %v", err)
}
hashes, err := value.ToString()
if err != nil {
return "", fmt.Errorf("error converting hashes value to string: %v", err)
}
return hashes, nil
}
func (c *Client) Logout() error {
if c.httpClient == nil {
return pkgErr("", fmt.Errorf("http client is nil"))
}
err := c.userLogout()
if err != nil {
return pkgErr("error logging out", err)
}
return nil
}
func (c *Client) userLogout() error {
resp, err := c.httpClient.Get(urlUserLogout)
if err != nil {
return fmt.Errorf("http Get request returned error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http Get response status not %s: %s", http.StatusText(http.StatusOK), resp.Status)
}
return nil
}
type LoggedInResponseData struct {
Username string `json:"username"`
}
type LoggedInResponseUser struct {
ID string `json:"id"`
LoggedIn bool `json:"logged_in"`
}
type LoggedInResponse struct {
Data LoggedInResponseData `json:"data"`
User LoggedInResponseUser `json:"user"`
}
func (c *Client) LoggedIn() (*LoggedInResponse, error) {
resp, err := c.httpClient.Get(urlUserLogin)
if err != nil {
return nil, pkgErr("error getting login service", err)
}
defer resp.Body.Close()
bodyB, err := io.ReadAll(resp.Body)
if err != nil {
return nil, pkgErr("error reading body bytes", err)
}
var lir LoggedInResponse
err = json.NewDecoder(strings.NewReader(string(bodyB))).Decode(&lir)
if err != nil {
return nil, pkgErr("error un-marshaling response body", err)
}
return &lir, nil
}